refactor state

This commit is contained in:
jane 2020-11-07 22:28:58 -05:00
parent 58774264f6
commit d2a4cf3bc2
5 changed files with 83 additions and 63 deletions

View File

@ -1,7 +1,8 @@
from time import sleep from time import sleep
class Adafruit_CharLCD: class Adafruit_CharLCD:
# commands # commands
LCD_CLEARDISPLAY = 0x01 LCD_CLEARDISPLAY = 0x01
LCD_RETURNHOME = 0x02 LCD_RETURNHOME = 0x02
@ -231,4 +232,4 @@ class Adafruit_CharLCD:
if char == '\n': if char == '\n':
self.write4bits(0xC0) # next line self.write4bits(0xC0) # next line
else: else:
self.write4bits(ord(char), True) self.write4bits(ord(char), True)

54
leds.py
View File

@ -11,6 +11,11 @@ import lcd_manager
import light_manager import light_manager
import logging import logging
level = 0
level_max = 14
level_state = 0
color_state = 0
def lightlevel(lcd, level): def lightlevel(lcd, level):
logging.debug("display level") logging.debug("display level")
@ -18,9 +23,8 @@ def lightlevel(lcd, level):
lcd.message("Light Level:\n]" + "-"*level + "[") lcd.message("Light Level:\n]" + "-"*level + "[")
def querylightlevel(): def query():
logging.debug("NYI") level_state = 2
return 7
def color(lcd): def color(lcd):
@ -29,33 +33,39 @@ def color(lcd):
logging.debug("NYI") logging.debug("NYI")
def get_state():
if level_state > 0:
level_state -= 1
return "level"
elif color_state > 0:
color_state -= 1
return "color"
else:
return "idle"
def displayon(lcd):
if lcd.displaycontrol & lcd.LCD_DISPLAYON != lcd.displaycontrol:
lcd.display()
def loop(): def loop():
lcd = lcd_manager.Adafruit_CharLCD() lcd = lcd_manager.Adafruit_CharLCD()
lights = light_manager.LightStrip() lights = light_manager.LightStrip()
level = 0
level_max = 14
idle = 0
idle_max = 15
cur_color = (255, 255, 255)
while True: while True:
logging.debug("loop") logging.debug("loop")
query_level = querylightlevel()
idle = idle + 1
logging.debug("idle value: {}".format(idle))
lights.tick() lights.tick()
if query_level != level: query()
level = query_level state = get_state()
lights.set_light_level(level / level_max)
idle = 0 if state == "level":
if lcd.displaycontrol & lcd.LCD_DISPLAYON != lcd.displaycontrol: if lights.get_light_level() != (level / level_max):
lcd.display() lights.set_light_level(level / level_max)
lightlevel(lcd, level) lightlevel(lcd, level)
elif idle >= idle_max: elif state == "color":
logging.debug("hit idle threshold") color(lcd)
idle = idle_max
lcd.noDisplay()
else: else:
lightlevel(lcd, level) lcd.noDisplay()
sleep(0.1) sleep(0.1)

View File

@ -3,54 +3,63 @@ import neopixel
import board import board
import logging import logging
def defaultPattern(n, t): def defaultPattern(n, t):
return (((n + t) * 5) % 255, (t * 42) % 255, (t * 50) % 255) return (((n + t) * 5) % 255, (t * 42) % 255, (t * 50) % 255)
class LightStrip: class LightStrip:
def __init__(self, data_pin = board.D18, string_length = 300, brightness = 1, pixel_order = neopixel.GRB, max_changes = 5): def __init__(self, data_pin=board.D18, string_length=300, brightness=1, pixel_order=neopixel.GRB, max_changes=5):
self.data_pin = data_pin self.data_pin = data_pin
self.np = neopixel.NeoPixel(self.data_pin, string_length, brightness = brightness, auto_write=True, pixel_order = pixel_order) self.np = neopixel.NeoPixel(
self.data_pin, string_length, brightness=brightness, auto_write=True, pixel_order=pixel_order)
self.pattern = defaultPattern self.pattern = defaultPattern
self.cur_tick = 0 self.cur_tick = 0
self.cur_index = 0 self.cur_index = 0
self.max_changes = max_changes self.max_changes = max_changes
self.cur_pattern = [] self.cur_pattern = []
def set_light_level(self, level): def get_light_level(self):
self.np.brightness = level return self.np.brightness
def pattern(self, pattern_callback):
self.pattern = pattern_callback
def get_next_pattern_tick(self): def set_light_level(self, level):
n = self.np.n self.np.brightness = level
t = self.cur_tick
if not len(self.cur_pattern) >= n - 1: def pattern(self, pattern_callback):
li = [(255, 255, 255)] * (n - len(self.cur_pattern)) self.pattern = pattern_callback
self.cur_pattern.extend(li)
for i in range(n): def get_next_pattern_tick(self):
self.cur_pattern[i] = self.pattern(i, t) n = self.np.n
self.cur_tick = t + 1 t = self.cur_tick
if not len(self.cur_pattern) >= n - 1:
def tick(self): li = [(255, 255, 255)] * (n - len(self.cur_pattern))
np = self.np self.cur_pattern.extend(li)
t = self.cur_tick for i in range(n):
n = np.n self.cur_pattern[i] = self.pattern(i, t)
self.cur_tick = t + 1
if not len(self.cur_pattern) >= n - 1:
self.get_next_pattern_tick() def tick(self):
changes = 0 np = self.np
ind = self.cur_index t = self.cur_tick
for i in range(ind, n): n = np.n
col = self.cur_pattern[i]
self.cur_index = i if not len(self.cur_pattern) >= n - 1:
logging.debug("TEST VALUES: np {} col {} same {}".format(np[i], col, tuple(np[i]) != col)) self.get_next_pattern_tick()
if tuple(np[i]) != col: changes = 0
changes = changes + 1 ind = self.cur_index
np[i] = col for i in range(ind, n):
logging.debug("CHANGE COLOR OF PIXEL {} TO {} ON TICK {}".format(i, col, t)) col = self.cur_pattern[i]
if changes >= self.max_changes: self.cur_index = i
break; logging.debug("TEST VALUES: np {} col {} same {}".format(
if self.cur_index >= (n-1): np[i], col, tuple(np[i]) != col))
self.get_next_pattern_tick() if tuple(np[i]) != col:
self.cur_index = 0 changes = changes + 1
np[i] = col
logging.debug(
"CHANGE COLOR OF PIXEL {} TO {} ON TICK {}".format(i, col, t))
if changes >= self.max_changes:
break
if self.cur_index >= (n-1):
self.get_next_pattern_tick()
self.cur_index = 0

View File

@ -1,4 +1,4 @@
debug_statements = True debug_statements = False
def debug(msg): def debug(msg):
if debug_statements: if debug_statements:

0
receive.py Normal file
View File