From 69614217bc4420fc21b362bbe5c82d3e2d2e660b Mon Sep 17 00:00:00 2001 From: Konstantin Koslowski Date: Sun, 18 Apr 2021 13:41:23 +0200 Subject: [PATCH] add mode, set brightness to 10% --- src/main.py | 93 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 53 insertions(+), 40 deletions(-) diff --git a/src/main.py b/src/main.py index eccaf8e..47b493e 100644 --- a/src/main.py +++ b/src/main.py @@ -9,19 +9,20 @@ PIN_MODE= 0 PIN_COLOR = 4 # NeoPixel NUM_LED = 16 -# y - Mode -MODE_MAX = 4 -MODES = [ "on", "color_chase", "rainbow", "strobe" ] +# Mode +MODE_MAX = 5 +MODES = [ "flow", "color_chase", "strobe", "on", "rainbow" ] MODE_STEP = 1 -# g - Color +# Color COLOR_MAX = 255 -COLOR_STEP = 5 -# b - Brightness -BRIGHTNESS_MAX = 250 -BRIGHTNESS_STEP = 50 +COLOR_STEP = 3 +# Brightness +BRIGHTNESS = 0.1 # Delay DELAY = 0.1 DELAY_CHASE = 0.05 +DELAY_STROBE = 0.4 +DELAY_FLOW = 0.2 DELAY_MODE = 0.5 light = None @@ -38,10 +39,10 @@ def handle_color(pin): light.set_fn("color") class Light: - def __init__(self): + def __init__(self, brightness): self.mode = 0 self.color = 0 - self.brightness = 0 + self.brightness = brightness self.np = neopixel.NeoPixel(machine.Pin(PIN_NP), NUM_LED) self.sw_mode = machine.Pin(PIN_MODE, machine.Pin.IN, machine.Pin.PULL_UP) self.sw_color = machine.Pin(PIN_COLOR, machine.Pin.IN, machine.Pin.PULL_UP) @@ -55,20 +56,27 @@ class Light: elif fn == "color": self.color = (self.color + COLOR_STEP) % COLOR_MAX print("set fn {} to {}".format(fn, self.color)) - elif fn == "brightness": - self.brightness = (self.brightness + BRIGHTNESS_STEP) % BRIGHTNESS_MAX - print("set fn {} to {}".format(fn, self.brightness)) else: print("invalid fn {}".format(fn)) def set_color(self, color): for i in range(NUM_LED): - self.np[i] = self.wheel(color) + self.set_color_at(color, i) + self.np.write() + + def set_color_at(self, color, index): + c_tmp = list(self.wheel(color)) + for i in range(len(c_tmp)): + c_tmp[i] = round(self.brightness * c_tmp[i]) + c = tuple(c_tmp) + self.np[index] = c + + def apply(self): self.np.write() def wheel(self, pos): if pos < 0 or pos > 255: - return (0, 0, 0) + return (0, 0, 0) if pos < 85: return (255 - pos * 3, pos * 3, 0) if pos < 170: @@ -77,22 +85,19 @@ class Light: pos -= 170 return (pos * 3, 0, 255 - pos * 3) + def dim(self, color, brightness): + pass + + def start(self): global state old_mode = None old_color = self.color - old_brightness = self.brightness i = 0 j = 0 while True: if not self.mode == old_mode: print("mode: {} ({}), old_mode: {}".format(self.mode, MODES[self.mode], old_mode)) - # # Off - # if MODES[self.mode] == "off": - # if not self.mode == old_mode: - # old_mode = self.mode - # self.set_color(-1) - # time.sleep(DELAY) # On if MODES[self.mode] == "on": if not self.mode == old_mode: @@ -112,26 +117,21 @@ class Light: k = 0 if not self.color == old_color: old_color = self.color - self.np[i] = self.wheel(self.color) - self.np[(i+1) % NUM_LED] = self.wheel(self.color) - self.np[(i+2) % NUM_LED] = self.wheel(self.color) - self.np.write() - self.np[i] = self.wheel(-1) + self.set_color_at(self.color, i) + self.set_color_at(self.color, (i+1) % NUM_LED) + self.set_color_at(self.color, (i+2) % NUM_LED) + self.apply() + self.set_color_at(-1, i) i = (i + 1) % NUM_LED time.sleep(DELAY_CHASE) - # Rainbow - elif MODES[self.mode] == "rainbow": + # Flow + elif MODES[self.mode] == "flow": if not self.mode == old_mode: old_mode = self.mode - i = 0 - j = 0 - rc_index = (i * 256 // NUM_LED) + j - self.np[i] = self.wheel(rc_index & 255) - i = (i + 1) - if i == NUM_LED: - i = 0 - self.np.write() - j = (j + 1) % 255 + self.color = (self.color + 1 ) % 255 + self.set_color(self.color) + self.apply() + time.sleep(DELAY_FLOW) # Strobe elif MODES[self.mode] == "strobe": if not self.mode == old_mode: @@ -142,7 +142,20 @@ class Light: else: self.set_color(-1) j = (j + 1) % 2 - time.sleep(DELAY) + time.sleep(DELAY_STROBE) + # Rainbow + elif MODES[self.mode] == "rainbow": + if not self.mode == old_mode: + old_mode = self.mode + i = 0 + j = 0 + rc_index = (i * 256 // NUM_LED) + j + self.set_color_at(rc_index & 255, i) + i = i + 1 + if i == NUM_LED: + i = 0 + self.apply() + j = (j + 1) % 255 else: print("else") @@ -150,7 +163,7 @@ class Light: def main(): print("") global light - light = Light() + light = Light(BRIGHTNESS) light.start()