refactor state
This commit is contained in:
parent
58774264f6
commit
d2a4cf3bc2
5 changed files with 83 additions and 63 deletions
|
@ -1,5 +1,6 @@
|
|||
from time import sleep
|
||||
|
||||
|
||||
class Adafruit_CharLCD:
|
||||
|
||||
# commands
|
||||
|
|
54
leds.py
54
leds.py
|
@ -11,6 +11,11 @@ import lcd_manager
|
|||
import light_manager
|
||||
import logging
|
||||
|
||||
level = 0
|
||||
level_max = 14
|
||||
level_state = 0
|
||||
color_state = 0
|
||||
|
||||
|
||||
def lightlevel(lcd, level):
|
||||
logging.debug("display level")
|
||||
|
@ -18,9 +23,8 @@ def lightlevel(lcd, level):
|
|||
lcd.message("Light Level:\n]" + "-"*level + "[")
|
||||
|
||||
|
||||
def querylightlevel():
|
||||
logging.debug("NYI")
|
||||
return 7
|
||||
def query():
|
||||
level_state = 2
|
||||
|
||||
|
||||
def color(lcd):
|
||||
|
@ -29,33 +33,39 @@ def color(lcd):
|
|||
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():
|
||||
lcd = lcd_manager.Adafruit_CharLCD()
|
||||
lights = light_manager.LightStrip()
|
||||
level = 0
|
||||
level_max = 14
|
||||
idle = 0
|
||||
idle_max = 15
|
||||
cur_color = (255, 255, 255)
|
||||
while True:
|
||||
logging.debug("loop")
|
||||
query_level = querylightlevel()
|
||||
idle = idle + 1
|
||||
logging.debug("idle value: {}".format(idle))
|
||||
lights.tick()
|
||||
if query_level != level:
|
||||
level = query_level
|
||||
lights.set_light_level(level / level_max)
|
||||
idle = 0
|
||||
if lcd.displaycontrol & lcd.LCD_DISPLAYON != lcd.displaycontrol:
|
||||
lcd.display()
|
||||
query()
|
||||
state = get_state()
|
||||
|
||||
if state == "level":
|
||||
if lights.get_light_level() != (level / level_max):
|
||||
lights.set_light_level(level / level_max)
|
||||
lightlevel(lcd, level)
|
||||
elif idle >= idle_max:
|
||||
logging.debug("hit idle threshold")
|
||||
idle = idle_max
|
||||
lcd.noDisplay()
|
||||
elif state == "color":
|
||||
color(lcd)
|
||||
else:
|
||||
lightlevel(lcd, level)
|
||||
lcd.noDisplay()
|
||||
sleep(0.1)
|
||||
|
||||
|
||||
|
|
|
@ -3,54 +3,63 @@ import neopixel
|
|||
import board
|
||||
import logging
|
||||
|
||||
|
||||
def defaultPattern(n, t):
|
||||
return (((n + t) * 5) % 255, (t * 42) % 255, (t * 50) % 255)
|
||||
|
||||
|
||||
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.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.cur_tick = 0
|
||||
self.cur_index = 0
|
||||
self.max_changes = max_changes
|
||||
self.cur_pattern = []
|
||||
|
||||
def set_light_level(self, level):
|
||||
self.np.brightness = level
|
||||
def pattern(self, pattern_callback):
|
||||
self.pattern = pattern_callback
|
||||
def get_light_level(self):
|
||||
return self.np.brightness
|
||||
|
||||
def get_next_pattern_tick(self):
|
||||
n = self.np.n
|
||||
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 set_light_level(self, level):
|
||||
self.np.brightness = level
|
||||
|
||||
def tick(self):
|
||||
np = self.np
|
||||
t = self.cur_tick
|
||||
n = np.n
|
||||
def pattern(self, pattern_callback):
|
||||
self.pattern = pattern_callback
|
||||
|
||||
if not len(self.cur_pattern) >= n - 1:
|
||||
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
|
||||
def get_next_pattern_tick(self):
|
||||
n = self.np.n
|
||||
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):
|
||||
np = self.np
|
||||
t = self.cur_tick
|
||||
n = np.n
|
||||
|
||||
if not len(self.cur_pattern) >= n - 1:
|
||||
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
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
debug_statements = True
|
||||
debug_statements = False
|
||||
|
||||
def debug(msg):
|
||||
if debug_statements:
|
||||
|
|
0
receive.py
Normal file
0
receive.py
Normal file
Loading…
Reference in a new issue