Skip to content

Instantly share code, notes, and snippets.

@chardane

chardane/novalight.py Secret

Created Aug 1, 2020
Embed
What would you like to do?
Code for the tutorial on charlyn.codes
import board
import neopixel
import busio
import adafruit_pcf8523
import time
import random
# COLORS
OFF = (0, 0, 0, 0)
WHITE = (0, 0, 0, 255)
WHITE_RGB = (100, 100, 100)
RED = (100, 5, 5, 0)
BLUE = (0, 0, 100)
GREEN = (50, 150, 50)
PURPLE = (180,50,180)
ORANGE = (155, 50, 0)
YELLOW = (200, 150, 0)
CYAN = (0, 100, 100)
PINK = (231,84,128)
color_array = [GREEN, PURPLE, ORANGE, YELLOW, CYAN, PINK, OFF, WHITE]
# Pixel numbers
STAR_FRAG_LEN = 8
TOP_NOVA_LEN = 16
BOTTOM_NOVA_LEN = 16
brightness_val = 0.7
star_frag = neopixel.NeoPixel(board.D9, STAR_FRAG_LEN, pixel_order=neopixel.GRBW, auto_write=False, brightness=brightness_val)
top_nova = neopixel.NeoPixel(board.D6, TOP_NOVA_LEN, pixel_order=neopixel.GRB, auto_write=False, brightness=brightness_val)
bottom_nova = neopixel.NeoPixel(board.D5, BOTTOM_NOVA_LEN, pixel_order=neopixel.GRB, auto_write=False, brightness=brightness_val)
# Start with eveything turned off
star_frag.fill(OFF)
star_frag.write()
top_nova.fill(OFF)
top_nova.write()
bottom_nova.fill(OFF)
bottom_nova.write()
myI2C = busio.I2C(board.SCL, board.SDA)
rtc = adafruit_pcf8523.PCF8523(myI2C)
is_it_friday = True
days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
if False: # change to True if you want to write the time!
# year, mon, date, hour, min, sec, wday, yday, isdst
t = time.struct_time((2020, 07, 31, 0, 10, 0, 6, -1, -1))
# you must set year, mon, date, hour, min, sec and weekday
# yearday is not supported, isdst can be set but we don't do anything with it at this time
print("Setting time to:", t) # uncomment for debugging
rtc.datetime = t
print()
def breathe(light_unit, length, color):
for i in range(0, length):
light_unit[i] = color
time.sleep(.07)
light_unit.write()
time.sleep(1)
for i in range(0, length):
light_unit[i] = OFF
time.sleep(.07)
light_unit.write()
def color_chase(light_unit, light_len, color, wait):
for i in range(light_len):
light_unit[i] = color
time.sleep(wait)
light_unit.show()
time.sleep(0.5)
def rainbow_chase(light_unit, len, wait):
color_chase(light_unit, len, RED, wait) # Increase the number to slow down the color chase
color_chase(light_unit, len, YELLOW, wait)
color_chase(light_unit, len, GREEN, wait)
color_chase(light_unit, len, CYAN, wait)
color_chase(light_unit, len, BLUE, wait)
color_chase(light_unit, len, PURPLE, wait)
def pick_rando_color():
pick = random.randint(0, (len(color_array)-1))
rando_color = color_array[pick]
return rando_color
def what_even_is_time(light_one, light_two, light_len, wait):
color1 = pick_rando_color()
color2 = pick_rando_color()
color3 = pick_rando_color()
if color1 == color2 == color3:
color1 = pick_rando_color()
color_chase(light_one, light_len, color1, wait)
color_chase(light_two, light_len, color2, wait)
color_chase(star_frag, STAR_FRAG_LEN, color3, wait)
def the_time_is_now(color, friday):
if not friday:
breathe(star_frag, STAR_FRAG_LEN, WHITE)
top_nova.fill(color)
top_nova.write()
def it_feels_like(color):
bottom_nova.fill(color)
bottom_nova.write()
def friday_feels():
wait = 0.05
what_even_is_time(bottom_nova, top_nova, BOTTOM_NOVA_LEN, wait)
while True:
t = rtc.datetime
#print(t) # uncomment for debugging
print("The date is %s %d/%d/%d" % (days[t.tm_wday], t.tm_mday, t.tm_mon, t.tm_year))
print("The time is %d:%02d:%02d" % (t.tm_hour, t.tm_min, t.tm_sec))
if t.tm_hour >= 0 and t.tm_hour < 7: # 12a to 7a, you should be sleeping
star_frag.fill(OFF)
star_frag.write()
top_nova.fill(OFF)
top_nova.write()
bottom_nova.fill(OFF)
bottom_nova.write()
continue
# Top nova light indicates time span
if t.tm_min == 0 and (t.tm_sec >=0 and t.tm_sec < 10): # top of the hour party cuckoo rainbow
rainbow_chase(top_nova, TOP_NOVA_LEN, 0.01)
rainbow_chase(top_nova, TOP_NOVA_LEN, 0.01)
if t.tm_hour >= 0 and t.tm_hour < 7: # 12a to 7a, you should be sleeping
the_time_is_now(OFF, is_it_friday)
elif t.tm_hour >= 7 and t.tm_hour < 12: # 7a to 12p rise and shine! it's morning!
the_time_is_now(WHITE_RGB, is_it_friday) # newborn nova
elif t.tm_hour >= 12 and t.tm_hour < 15: # 12p to 3p, middle of the day burns bright
the_time_is_now(CYAN, is_it_friday)
elif t.tm_hour >= 15 and t.tm_hour < 18: # 3p to 6p, back to whatever you were doing
the_time_is_now(YELLOW, is_it_friday)
elif t.tm_hour >= 18 and t.tm_hour < 23: # 6p to 11p, night time calm
the_time_is_now(ORANGE, is_it_friday)
elif t.tm_hour >= 23 and t.tm_hour < 24: # 11p - 12a, time for bed!
the_time_is_now(RED, is_it_friday) # super novaaaa
# Bottom nova light indicates day of week
if days[t.tm_wday] == "Sunday" or days[t.tm_wday] == "Saturday":
it_feels_like(WHITE_RGB)
is_it_friday = False
elif days[t.tm_wday] == "Monday":
it_feels_like(BLUE)
elif days[t.tm_wday] == "Tuesday":
it_feels_like(CYAN)
elif days[t.tm_wday] == "Wednesday" or days[t.tm_wday] == "Thursday":
it_feels_like(PINK)
elif days[t.tm_wday] == "Friday":
is_it_friday = True
friday_feels()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.