2020-11-07 04:00:23 +00:00
|
|
|
from time import sleep
|
|
|
|
import neopixel
|
|
|
|
import board
|
2020-11-07 04:12:10 +00:00
|
|
|
import logging
|
2020-11-07 04:00:23 +00:00
|
|
|
|
|
|
|
def defaultPattern(n, t):
|
2020-11-07 21:15:22 +00:00
|
|
|
return (n % 255, 0, (t * 50) % 255)
|
2020-11-07 04:00:23 +00:00
|
|
|
|
|
|
|
class LightStrip:
|
2020-11-07 20:58:17 +00:00
|
|
|
def __init__(self, data_pin = board.D18, string_length = 300, brightness = 1, pixel_order = neopixel.GRB, max_changes = 5):
|
2020-11-07 04:00:23 +00:00
|
|
|
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
|
2020-11-07 20:58:17 +00:00
|
|
|
self.max_changes = max_changes
|
2020-11-07 21:07:28 +00:00
|
|
|
self.cur_pattern = []
|
2020-11-07 04:00:23 +00:00
|
|
|
|
|
|
|
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
|
2020-11-07 04:00:23 +00:00
|
|
|
|
|
|
|
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()
|
2020-11-07 20:58:17 +00:00
|
|
|
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:
|
2020-11-07 20:58:17 +00:00
|
|
|
changes = changes + 1
|
2020-11-07 04:09:59 +00:00
|
|
|
np[i] = col
|
2020-11-07 04:12:10 +00:00
|
|
|
logging.debug("CHANGE COLOR OF PIXEL {} TO {} ON TICK {}".format(i, col, t))
|
2020-11-07 20:58:17 +00:00
|
|
|
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
|