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

52
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()
if state == "level":
if lights.get_light_level() != (level / level_max):
lights.set_light_level(level / level_max) lights.set_light_level(level / level_max)
idle = 0
if lcd.displaycontrol & lcd.LCD_DISPLAYON != lcd.displaycontrol:
lcd.display()
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,22 +3,29 @@ 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 get_light_level(self):
return self.np.brightness
def set_light_level(self, level): def set_light_level(self, level):
self.np.brightness = level self.np.brightness = level
def pattern(self, pattern_callback): def pattern(self, pattern_callback):
self.pattern = pattern_callback self.pattern = pattern_callback
@ -44,13 +51,15 @@ class LightStrip:
for i in range(ind, n): for i in range(ind, n):
col = self.cur_pattern[i] col = self.cur_pattern[i]
self.cur_index = i self.cur_index = i
logging.debug("TEST VALUES: np {} col {} same {}".format(np[i], col, tuple(np[i]) != col)) logging.debug("TEST VALUES: np {} col {} same {}".format(
np[i], col, tuple(np[i]) != col))
if tuple(np[i]) != col: if tuple(np[i]) != col:
changes = changes + 1 changes = changes + 1
np[i] = col np[i] = col
logging.debug("CHANGE COLOR OF PIXEL {} TO {} ON TICK {}".format(i, col, t)) logging.debug(
"CHANGE COLOR OF PIXEL {} TO {} ON TICK {}".format(i, col, t))
if changes >= self.max_changes: if changes >= self.max_changes:
break; break
if self.cur_index >= (n-1): if self.cur_index >= (n-1):
self.get_next_pattern_tick() self.get_next_pattern_tick()
self.cur_index = 0 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