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,5 +1,6 @@
from time import sleep from time import sleep
class Adafruit_CharLCD: class Adafruit_CharLCD:
# commands # commands

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:
li = [(255, 255, 255)] * (n - len(self.cur_pattern))
self.cur_pattern.extend(li)
for i in range(n):
self.cur_pattern[i] = self.pattern(i, t)
self.cur_tick = t + 1
def tick(self): def pattern(self, pattern_callback):
np = self.np self.pattern = pattern_callback
t = self.cur_tick
n = np.n
if not len(self.cur_pattern) >= n - 1: def get_next_pattern_tick(self):
self.get_next_pattern_tick() n = self.np.n
changes = 0 t = self.cur_tick
ind = self.cur_index if not len(self.cur_pattern) >= n - 1:
for i in range(ind, n): li = [(255, 255, 255)] * (n - len(self.cur_pattern))
col = self.cur_pattern[i] self.cur_pattern.extend(li)
self.cur_index = i for i in range(n):
logging.debug("TEST VALUES: np {} col {} same {}".format(np[i], col, tuple(np[i]) != col)) self.cur_pattern[i] = self.pattern(i, t)
if tuple(np[i]) != col: self.cur_tick = t + 1
changes = changes + 1
np[i] = col def tick(self):
logging.debug("CHANGE COLOR OF PIXEL {} TO {} ON TICK {}".format(i, col, t)) np = self.np
if changes >= self.max_changes: t = self.cur_tick
break; n = np.n
if self.cur_index >= (n-1):
self.get_next_pattern_tick() if not len(self.cur_pattern) >= n - 1:
self.cur_index = 0 self.get_next_pattern_tick()
changes = 0
ind = self.cur_index
for i in range(ind, n):
col = self.cur_pattern[i]
self.cur_index = i
logging.debug("TEST VALUES: np {} col {} same {}".format(
np[i], col, tuple(np[i]) != col))
if tuple(np[i]) != col:
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