test getting pattern all at once

This commit is contained in:
janeptrv 2020-11-07 16:07:28 -05:00
parent 9da07501ba
commit 3d017e1ec7

View file

@ -4,7 +4,7 @@ import board
import logging
def defaultPattern(n, t):
return (0, 0, 255)
return (n % 255, 0, t % 255)
class LightStrip:
def __init__(self, data_pin = board.D18, string_length = 300, brightness = 1, pixel_order = neopixel.GRB, max_changes = 5):
@ -13,20 +13,31 @@ class LightStrip:
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_next_pattern_tick(self):
n = self.np.n
t = self.cur_tick
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
changes = 0
for i in range(n):
col = self.pattern(i, t)
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
@ -34,4 +45,6 @@ class LightStrip:
logging.debug("CHANGE COLOR OF PIXEL {} TO {} ON TICK {}".format(i, col, t))
if changes >= self.max_changes:
break;
self.cur_tick = t + 1
if self.cur_index >= (n-1):
self.get_next_pattern_tick()
self.cur_index = 0