leds/light_manager.py

56 lines
1.7 KiB
Python
Raw Normal View History

from time import sleep
import neopixel
import board
2020-11-07 04:12:10 +00:00
import logging
def defaultPattern(n, t):
2020-11-08 00:19:51 +00:00
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):
self.data_pin = data_pin
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
2020-11-07 21:07:28 +00:00
self.cur_index = 0
self.max_changes = max_changes
2020-11-07 21:07:28 +00:00
self.cur_pattern = []
def set_light_level(self, level):
self.np.brightness = level
def pattern(self, pattern_callback):
self.pattern = pattern_callback
2020-11-07 21:07:28 +00:00
def get_next_pattern_tick(self):
n = self.np.n
t = self.cur_tick
2020-11-07 21:14:25 +00:00
if not len(self.cur_pattern) >= n - 1:
li = [(255, 255, 255)] * (n - len(self.cur_pattern))
self.cur_pattern.extend(li)
2020-11-07 21:07:28 +00:00
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
2020-11-07 21:10:01 +00:00
if not len(self.cur_pattern) >= n - 1:
self.get_next_pattern_tick()
changes = 0
2020-11-07 21:07:28 +00:00
ind = self.cur_index
for i in range(ind, n):
col = self.cur_pattern[i]
self.cur_index = i
2020-11-07 04:15:40 +00:00
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
2020-11-07 04:12:10 +00:00
logging.debug("CHANGE COLOR OF PIXEL {} TO {} ON TICK {}".format(i, col, t))
if changes >= self.max_changes:
break;
2020-11-07 21:07:28 +00:00
if self.cur_index >= (n-1):
self.get_next_pattern_tick()
self.cur_index = 0