from neopixel import Neopixel import utime from machine import Pin, Timer import _thread ####################TO DO´s########################## # 1. Multithread für trennung von blinker und animation # 2. buttoninterrupt für längere animationen (rainbow/police) # - https://www.elektronik-kompendium.de/sites/raspberry-pi/2612181.htm # 3. animation Police optimieren # # # # # ######################################################## numpix = 20 segment = 4 delay = 0.15 brightness = 100 anzahl_animationen = 5 global animation_NR #setup strip_blinker = Neopixel(numpix, 0, 28, "RGB") strip_animation = Neopixel(numpix, 0, 28, "RGB") strip_animation.brightness(brightness) strip_blinker.brightness(brightness) button = Pin(18, Pin.IN, Pin.PULL_UP) toggle_switch_left = Pin(15, Pin.IN, Pin.PULL_DOWN) toggle_switch_right = Pin(14, Pin.IN, Pin.PULL_DOWN) #Farben definnieren blank = (0, 0, 0) white = (255, 255, 255) red = (0, 255, 0) orange = (127, 255, 0) yellow = (255, 255, 0) lime = (255, 127, 0) green = (255, 0, 0) teal = (255, 0, 127) cyan = (255, 0, 255) sky_blue = (127, 0, 255) blue = (0, 0, 255) indigo = (0, 127, 255) magenta = (0, 255, 255) pink = (0, 255, 127) colors = [red, orange, yellow, lime, green, teal, cyan, sky_blue, blue, indigo, magenta, pink] ############ Blinker Links ################### def blink_links(): while True: strip_blinker.show() for x in reversed(range(segment)): strip_blinker.set_pixel(x, yellow) strip_blinker.show() utime.sleep(delay) utime.sleep(2 * delay) for y in range(segment): strip_blinker.set_pixel(y, blank) strip_blinker.show() return ################################################## ############ Blinker Rechts ################## def blink_rechts(): startpoint = numpix - segment while True: strip_blinker.show() for x in range(segment): strip_blinker.set_pixel(startpoint + x, yellow) strip_blinker.show() utime.sleep(delay) utime.sleep(2 * delay) for y in range(segment): strip_blinker.set_pixel(startpoint + y, blank) strip_blinker.show() return ################################################## # ----------------Animationen------------------# ############ 0.Animation Licht aus ################ def licht_aus(): for i in range(numpix - segment): if i >= segment: strip_animation.set_pixel(i, blank) strip_animation.show() ################################################## ############### 1.Animation Licht an ############### def fernlicht_an(): for i in range(numpix - segment): if i >= segment: strip_animation.set_pixel(i, white) strip_animation.show() ############### 2.Animation Rainbow ############### def rainbow(): for i in range(len(colors)): for j in range(numpix - segment): if j >= segment: strip_animation.set_pixel(j, colors[i]) strip_animation.show() utime.sleep(delay) ################################################## ############### 3.Animation Police ############### def police(): while True: strip_animation.set_pixel(4, blue) strip_animation.set_pixel(5, blue) strip_animation.set_pixel(6, blue) strip_animation.set_pixel(7, red) strip_animation.set_pixel(8, red) strip_animation.set_pixel(9, red) strip_animation.set_pixel(10, blue) strip_animation.set_pixel(11, blue) strip_animation.set_pixel(12, blue) strip_animation.set_pixel(13, red) strip_animation.set_pixel(14, red) strip_animation.set_pixel(15, red) strip_animation.show() utime.sleep(delay * 2) strip_animation.set_pixel(4, red) strip_animation.set_pixel(5, red) strip_animation.set_pixel(6, red) strip_animation.set_pixel(7, blue) strip_animation.set_pixel(8, blue) strip_animation.set_pixel(9, blue) strip_animation.set_pixel(10, red) strip_animation.set_pixel(11, red) strip_animation.set_pixel(12, red) strip_animation.set_pixel(13, blue) strip_animation.set_pixel(14, blue) strip_animation.set_pixel(15, blue) strip_animation.show() utime.sleep(delay * 2) return ################################################## ############### 4.Animation Kit ############### def kit(): delay = 0.01 while True: strip_animation.show() ############### Left to Right ############### for x in range(numpix - segment - 2): if x >= segment: strip_animation.set_pixel(x + 1, red) utime.sleep(delay) strip_animation.show() strip_animation.set_pixel(x, red) utime.sleep(delay) strip_animation.show() strip_animation.set_pixel(x + 2, red) utime.sleep(delay) strip_animation.show() strip_animation.set_pixel(x, blank) utime.sleep(delay) strip_animation.set_pixel(x + 1, blank) utime.sleep(delay) strip_animation.set_pixel(x + 2, blank) strip_animation.show() ############### Left to Right ############### for x in reversed(range(numpix - segment - 2)): if x > segment: strip_animation.set_pixel(x + 1, red) utime.sleep(delay) strip_animation.show() strip_animation.set_pixel(x, red) utime.sleep(delay) strip_animation.show() strip_animation.set_pixel(x + 2, red) utime.sleep(delay) strip_animation.show() strip_animation.set_pixel(x, blank) utime.sleep(delay) strip_animation.set_pixel(x + 1, blank) utime.sleep(delay) strip_animation.set_pixel(x + 2, blank) strip_animation.show() return ################################################## # -----------------------------------------------# button = Pin(18, Pin.IN, Pin.PULL_UP) ############### Schalterstellung ############### def get_switch_left(): global status_links if toggle_switch_left.value() == 1: status_links = 1 elif toggle_switch_left.value() == 0: status_links = 0 return status_links def get_switch_right(): global status_rechts if toggle_switch_right.value() == 1: status_rechts = 1 elif toggle_switch_right.value() == 0: status_rechts = 0 return ########################################################################### # INTERRUPT HANDLER def switch_left(pin): pass def switch_left_debouncer(pin): Timer().init(mode=Timer.ONE_SHOT, period=200, callback=switch_left) toggle_switch_left.irq(handler=switch_left_debouncer, trigger=Pin.IRQ_RISING) def switch_right(pin): pass def switch_right_debouncer(pin): Timer().init(mode=Timer.ONE_SHOT, period=200, callback=switch_right) toggle_switch_right.irq(handler=switch_right_debouncer, trigger=Pin.IRQ_RISING) def button_pressed(pin): pass def button_debouncer(pin): Timer().init(mode=Timer.ONE_SHOT, period=200, callback=button_pressed) button.irq(handler=button_debouncer, trigger=Pin.IRQ_RISING) ########################################################################### # MAIN LOOPS def blinker_loop(): while True: get_switch_left() get_switch_right() if status_rechts == 1: blink_rechts() if status_links == 1: blink_links() utime.sleep(0.1) def animation_loop(): animation_NR = 0 while True: if button.value() == 1: print(animation_NR) elif button.value() == 0: if animation_NR < anzahl_animationen - 1: animation_NR = animation_NR + 1 else: animation_NR = 0 print(animation_NR) utime.sleep(delay) if animation_NR == 0: licht_aus() elif animation_NR == 1: fernlicht_an() elif animation_NR == 2: rainbow() elif animation_NR == 3: police() elif animation_NR == 4: kit() def test_loop2(): while True: utime.sleep(0.5) print('thread2') def test_loop(): while True: #rainbow() # kit() # fernlicht_an() utime.sleep(0.5) # licht_aus() # utime.sleep(0.5) # police() print('thread1') blink_rechts() blink_links() # start blinker_loop on second core _thread.start_new_thread(test_loop2, ()) #blinker_loop() #animation_loop() test_loop()