add mode, set brightness to 10%

This commit is contained in:
Konstantin Koslowski 2021-04-18 13:41:23 +02:00
parent bfbd8316fc
commit 69614217bc

View file

@ -9,19 +9,20 @@ PIN_MODE= 0
PIN_COLOR = 4 PIN_COLOR = 4
# NeoPixel # NeoPixel
NUM_LED = 16 NUM_LED = 16
# y - Mode # Mode
MODE_MAX = 4 MODE_MAX = 5
MODES = [ "on", "color_chase", "rainbow", "strobe" ] MODES = [ "flow", "color_chase", "strobe", "on", "rainbow" ]
MODE_STEP = 1 MODE_STEP = 1
# g - Color # Color
COLOR_MAX = 255 COLOR_MAX = 255
COLOR_STEP = 5 COLOR_STEP = 3
# b - Brightness # Brightness
BRIGHTNESS_MAX = 250 BRIGHTNESS = 0.1
BRIGHTNESS_STEP = 50
# Delay # Delay
DELAY = 0.1 DELAY = 0.1
DELAY_CHASE = 0.05 DELAY_CHASE = 0.05
DELAY_STROBE = 0.4
DELAY_FLOW = 0.2
DELAY_MODE = 0.5 DELAY_MODE = 0.5
light = None light = None
@ -38,10 +39,10 @@ def handle_color(pin):
light.set_fn("color") light.set_fn("color")
class Light: class Light:
def __init__(self): def __init__(self, brightness):
self.mode = 0 self.mode = 0
self.color = 0 self.color = 0
self.brightness = 0 self.brightness = brightness
self.np = neopixel.NeoPixel(machine.Pin(PIN_NP), NUM_LED) 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_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) self.sw_color = machine.Pin(PIN_COLOR, machine.Pin.IN, machine.Pin.PULL_UP)
@ -55,20 +56,27 @@ class Light:
elif fn == "color": elif fn == "color":
self.color = (self.color + COLOR_STEP) % COLOR_MAX self.color = (self.color + COLOR_STEP) % COLOR_MAX
print("set fn {} to {}".format(fn, self.color)) 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: else:
print("invalid fn {}".format(fn)) print("invalid fn {}".format(fn))
def set_color(self, color): def set_color(self, color):
for i in range(NUM_LED): 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() self.np.write()
def wheel(self, pos): def wheel(self, pos):
if pos < 0 or pos > 255: if pos < 0 or pos > 255:
return (0, 0, 0) return (0, 0, 0)
if pos < 85: if pos < 85:
return (255 - pos * 3, pos * 3, 0) return (255 - pos * 3, pos * 3, 0)
if pos < 170: if pos < 170:
@ -77,22 +85,19 @@ class Light:
pos -= 170 pos -= 170
return (pos * 3, 0, 255 - pos * 3) return (pos * 3, 0, 255 - pos * 3)
def dim(self, color, brightness):
pass
def start(self): def start(self):
global state global state
old_mode = None old_mode = None
old_color = self.color old_color = self.color
old_brightness = self.brightness
i = 0 i = 0
j = 0 j = 0
while True: while True:
if not self.mode == old_mode: if not self.mode == old_mode:
print("mode: {} ({}), old_mode: {}".format(self.mode, MODES[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 # On
if MODES[self.mode] == "on": if MODES[self.mode] == "on":
if not self.mode == old_mode: if not self.mode == old_mode:
@ -112,26 +117,21 @@ class Light:
k = 0 k = 0
if not self.color == old_color: if not self.color == old_color:
old_color = self.color old_color = self.color
self.np[i] = self.wheel(self.color) self.set_color_at(self.color, i)
self.np[(i+1) % NUM_LED] = self.wheel(self.color) self.set_color_at(self.color, (i+1) % NUM_LED)
self.np[(i+2) % NUM_LED] = self.wheel(self.color) self.set_color_at(self.color, (i+2) % NUM_LED)
self.np.write() self.apply()
self.np[i] = self.wheel(-1) self.set_color_at(-1, i)
i = (i + 1) % NUM_LED i = (i + 1) % NUM_LED
time.sleep(DELAY_CHASE) time.sleep(DELAY_CHASE)
# Rainbow # Flow
elif MODES[self.mode] == "rainbow": elif MODES[self.mode] == "flow":
if not self.mode == old_mode: if not self.mode == old_mode:
old_mode = self.mode old_mode = self.mode
i = 0 self.color = (self.color + 1 ) % 255
j = 0 self.set_color(self.color)
rc_index = (i * 256 // NUM_LED) + j self.apply()
self.np[i] = self.wheel(rc_index & 255) time.sleep(DELAY_FLOW)
i = (i + 1)
if i == NUM_LED:
i = 0
self.np.write()
j = (j + 1) % 255
# Strobe # Strobe
elif MODES[self.mode] == "strobe": elif MODES[self.mode] == "strobe":
if not self.mode == old_mode: if not self.mode == old_mode:
@ -142,7 +142,20 @@ class Light:
else: else:
self.set_color(-1) self.set_color(-1)
j = (j + 1) % 2 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: else:
print("else") print("else")
@ -150,7 +163,7 @@ class Light:
def main(): def main():
print("") print("")
global light global light
light = Light() light = Light(BRIGHTNESS)
light.start() light.start()