commit d616963256ed3f336dad5df57c9eef38a10070f4 Author: JonOfUs Date: Tue Aug 6 23:04:18 2024 +0200 first commit diff --git a/diagram.json b/diagram.json new file mode 100644 index 0000000..4f7729d --- /dev/null +++ b/diagram.json @@ -0,0 +1,110 @@ +{ + "version": 1, + "author": "Uri Shaked", + "editor": "wokwi", + "parts": [ + { + "type": "wokwi-pi-pico", + "id": "pico", + "top": 102.45, + "left": -678, + "attrs": { "env": "micropython-20210902-v1.17" } + }, + { "type": "wokwi-neopixel", "id": "rgb1", "top": -137.9, "left": -404.2, "attrs": {} }, + { "type": "wokwi-neopixel", "id": "rgb2", "top": -137.9, "left": -375.4, "attrs": {} }, + { "type": "wokwi-neopixel", "id": "rgb3", "top": -137.9, "left": -346.6, "attrs": {} }, + { "type": "wokwi-neopixel", "id": "rgb4", "top": -137.9, "left": -317.8, "attrs": {} }, + { "type": "wokwi-neopixel", "id": "rgb5", "top": -51.5, "left": -269.8, "attrs": {} }, + { "type": "wokwi-neopixel", "id": "rgb6", "top": -51.5, "left": 47, "attrs": {} }, + { "type": "wokwi-neopixel", "id": "rgb7", "top": -128.3, "left": 75.8, "attrs": {} }, + { "type": "wokwi-neopixel", "id": "rgb8", "top": -128.3, "left": 104.6, "attrs": {} }, + { "type": "wokwi-neopixel", "id": "rgb9", "top": -128.3, "left": 133.4, "attrs": {} }, + { "type": "wokwi-neopixel", "id": "rgb10", "top": -128.3, "left": 162.2, "attrs": {} }, + { "type": "wokwi-neopixel", "id": "rgb11", "top": -50, "left": -241, "attrs": {} }, + { "type": "wokwi-neopixel", "id": "rgb12", "top": -50, "left": -212.2, "attrs": {} }, + { "type": "wokwi-neopixel", "id": "rgb13", "top": -50, "left": -183.4, "attrs": {} }, + { "type": "wokwi-neopixel", "id": "rgb14", "top": -50, "left": -154.6, "attrs": {} }, + { "type": "wokwi-neopixel", "id": "rgb15", "top": -50, "left": -125.8, "attrs": {} }, + { "type": "wokwi-neopixel", "id": "rgb16", "top": -50, "left": -97, "attrs": {} }, + { "type": "wokwi-neopixel", "id": "rgb17", "top": -50, "left": -68.2, "attrs": {} }, + { "type": "wokwi-neopixel", "id": "rgb18", "top": -50, "left": -39.4, "attrs": {} }, + { "type": "wokwi-neopixel", "id": "rgb19", "top": -50, "left": -10.6, "attrs": {} }, + { "type": "wokwi-neopixel", "id": "rgb20", "top": -50, "left": 18.2, "attrs": {} }, + { "type": "wokwi-slide-switch", "id": "sw1", "top": 206, "left": -457.7, "attrs": {} }, + { "type": "wokwi-slide-switch", "id": "sw2", "top": 225.2, "left": -371.3, "attrs": {} }, + { + "type": "wokwi-pushbutton", + "id": "btn1", + "top": 371, + "left": -480, + "attrs": { "color": "green" } + } + ], + "connections": [ + [ "pico:GND.1", "rgb20:VSS", "black", [ "h-19.2", "v-96", "h730.4" ] ], + [ "rgb19:VSS", "rgb20:VSS", "black", [ "v36", "h-0.8" ] ], + [ "rgb19:VSS", "rgb18:VSS", "black", [ "v36", "h-20" ] ], + [ "rgb17:VSS", "rgb18:VSS", "black", [ "v36", "h28" ] ], + [ "rgb16:VSS", "rgb17:VSS", "black", [ "v36", "h28" ] ], + [ "rgb15:VSS", "rgb16:VSS", "black", [ "v36", "h28" ] ], + [ "rgb14:VSS", "rgb15:VSS", "black", [ "v36", "h28" ] ], + [ "rgb13:VSS", "rgb14:VSS", "black", [ "v36", "h28" ] ], + [ "rgb12:VSS", "rgb13:VSS", "black", [ "v36", "h28" ] ], + [ "rgb11:VSS", "rgb12:VSS", "black", [ "v36", "h28" ] ], + [ "rgb10:VSS", "rgb11:VSS", "black", [ "v36", "h28" ] ], + [ "rgb9:VSS", "rgb10:VSS", "black", [ "v36", "h28" ] ], + [ "rgb20:VSS", "rgb1:VSS", "black", [ "v7.2", "h37.6", "v-48", "h-354.4" ] ], + [ "rgb1:VSS", "rgb2:VSS", "black", [ "v39.2", "h28.8" ] ], + [ "rgb2:VSS", "rgb3:VSS", "black", [ "v39.2", "h28.8" ] ], + [ "rgb3:VSS", "rgb4:VSS", "black", [ "v39.2", "h28.8" ] ], + [ "rgb4:VSS", "rgb5:VSS", "black", [ "v39.2", "h86.4" ] ], + [ "rgb5:VSS", "rgb6:VSS", "black", [ "v39.2", "h28.8" ] ], + [ "rgb6:VSS", "rgb7:VSS", "black", [ "v39.2", "h28.8" ] ], + [ "pico:VBUS", "rgb1:VDD", "red", [ "h1.2", "v-278.4", "h201.6" ] ], + [ "rgb2:VDD", "rgb1:VDD", "red", [ "h0", "v-27.1", "h-28.8" ] ], + [ "rgb2:VDD", "rgb3:VDD", "red", [ "h0", "v-27.1", "h28.8" ] ], + [ "rgb3:VDD", "rgb4:VDD", "red", [ "h0", "v-27.1", "h19.2" ] ], + [ "rgb4:VDD", "rgb5:VDD", "red", [ "h0", "v-27.1", "h86.4" ] ], + [ "rgb6:VDD", "rgb7:VDD", "red", [ "v-28.8", "h28.8" ] ], + [ "rgb7:VDD", "rgb8:VDD", "red", [ "v-28.8", "h28.8" ] ], + [ "rgb8:VDD", "rgb20:VDD", "red", [ "v-28.8", "h-86.4" ] ], + [ "rgb20:VDD", "rgb19:VDD", "red", [ "v-20.7", "h-28.8" ] ], + [ "rgb19:VDD", "rgb18:VDD", "red", [ "v-20.7", "h-28.8" ] ], + [ "rgb18:VDD", "rgb17:VDD", "red", [ "v-20.7", "h-28.8" ] ], + [ "rgb17:VDD", "rgb16:VDD", "red", [ "v-20.7", "h-28.8" ] ], + [ "rgb16:VDD", "rgb15:VDD", "red", [ "v-20.7", "h-28.8" ] ], + [ "rgb15:VDD", "rgb14:VDD", "red", [ "v-20.7", "h-28.8" ] ], + [ "rgb14:VDD", "rgb13:VDD", "red", [ "v-20.7", "h-28.8" ] ], + [ "rgb13:VDD", "rgb12:VDD", "red", [ "v-20.7", "h-28.8" ] ], + [ "rgb12:VDD", "rgb11:VDD", "red", [ "v-20.7", "h-28.8" ] ], + [ "rgb1:DOUT", "rgb2:DIN", "green", [ "v10.4", "h48.8" ] ], + [ "rgb2:DOUT", "rgb3:DIN", "green", [ "v-18.4", "h48.8" ] ], + [ "rgb3:DOUT", "rgb4:DIN", "green", [ "v10.4", "h48.8" ] ], + [ "rgb4:DOUT", "rgb5:DIN", "green", [ "v-18.4", "h106.4" ] ], + [ "rgb6:DOUT", "rgb7:DIN", "green", [ "v-18.4", "h48.8" ] ], + [ "rgb7:DOUT", "rgb8:DIN", "green", [ "v18.3", "h48.8" ] ], + [ "rgb8:DOUT", "rgb9:DIN", "green", [ "v-20.1", "h48.8" ] ], + [ "rgb9:DOUT", "rgb10:DIN", "green", [ "v18.3", "h57.6", "v-28.8" ] ], + [ "rgb11:DOUT", "rgb12:DIN", "green", [ "v7.2", "h48.8" ] ], + [ "rgb12:DOUT", "rgb13:DIN", "green", [ "v-21.6", "h48.8" ] ], + [ "rgb13:DOUT", "rgb14:DIN", "green", [ "v7.2", "h48.8" ] ], + [ "rgb14:DOUT", "rgb15:DIN", "green", [ "v-21.6", "h48.8" ] ], + [ "rgb15:DOUT", "rgb16:DIN", "green", [ "v7.2", "h48.8" ] ], + [ "rgb16:DOUT", "rgb17:DIN", "green", [ "v-21.6", "h48.8" ] ], + [ "rgb17:DOUT", "rgb18:DIN", "green", [ "v7.2", "h48.8" ] ], + [ "rgb18:DOUT", "rgb19:DIN", "green", [ "v-21.6", "h48.8" ] ], + [ "rgb19:DOUT", "rgb20:DIN", "green", [ "v7.2", "h48.8" ] ], + [ "rgb5:DOUT", "rgb11:DIN", "green", [ "v8.7", "h144.8" ] ], + [ "rgb8:VDD", "rgb9:VDD", "red", [ "v-28.8", "h28.8" ] ], + [ "rgb9:VDD", "rgb10:VDD", "red", [ "h0", "v-28.8", "h28.8" ] ], + [ "rgb20:DOUT", "rgb6:DIN", "green", [ "v-60", "h48.8" ] ], + [ "sw1:2", "pico:GP15", "green", [ "v86.4", "h-191.9" ] ], + [ "pico:GP14", "sw2:2", "green", [ "h-19.2", "v57.6", "h345.6" ] ], + [ "sw1:1", "sw2:1", "red", [ "v48" ] ], + [ "rgb1:DIN", "pico:GP28", "green", [ "h-0.8", "v-19.2", "h-163.2", "v278.4" ] ], + [ "sw1:1", "pico:3V3", "red", [ "h-67.2", "v-86.4" ] ], + [ "pico:GND.5", "btn1:2.l", "black", [ "h30", "v105.6" ] ], + [ "btn1:1.l", "pico:GP18", "green", [ "h-86.4", "v-105.6" ] ] + ], + "dependencies": {} +} \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..b04994e --- /dev/null +++ b/main.py @@ -0,0 +1,353 @@ +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() diff --git a/neopixel.py b/neopixel.py new file mode 100644 index 0000000..76983b3 --- /dev/null +++ b/neopixel.py @@ -0,0 +1,366 @@ +import array, time +from machine import Pin +import rp2 + + +# PIO state machine for RGB. Pulls 24 bits (rgb -> 3 * 8bit) automatically +@rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True, pull_thresh=24) +def ws2812(): + T1 = 2 + T2 = 5 + T3 = 3 + wrap_target() + label("bitloop") + out(x, 1) .side(0) [T3 - 1] + jmp(not_x, "do_zero") .side(1) [T1 - 1] + jmp("bitloop") .side(1) [T2 - 1] + label("do_zero") + nop() .side(0) [T2 - 1] + wrap() + + +# PIO state machine for RGBW. Pulls 32 bits (rgbw -> 4 * 8bit) automatically +@rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True, pull_thresh=32) +def sk6812(): + T1 = 2 + T2 = 5 + T3 = 3 + wrap_target() + label("bitloop") + out(x, 1) .side(0) [T3 - 1] + jmp(not_x, "do_zero") .side(1) [T1 - 1] + jmp("bitloop") .side(1) [T2 - 1] + label("do_zero") + nop() .side(0) [T2 - 1] + wrap() + + +# we need this because Micropython can't construct slice objects directly, only by +# way of supporting slice notation. +# So, e.g. slice_maker[1::4] gives a slice(1,None,4) object. +class slice_maker_class: + def __getitem__(self, slc): + return slc + + +slice_maker = slice_maker_class() + + +# Delay here is the reset time. You need a pause to reset the LED strip back to the initial LED +# however, if you have quite a bit of processing to do before the next time you update the strip +# you could put in delay=0 (or a lower delay) +# +# Class supports different order of individual colors (GRB, RGB, WRGB, GWRB ...). In order to achieve +# this, we need to flip the indexes: in 'RGBW', 'R' is on index 0, but we need to shift it left by 3 * 8bits, +# so in it's inverse, 'WBGR', it has exactly right index. Since micropython doesn't have [::-1] and recursive rev() +# isn't too efficient we simply do that by XORing (operator ^) each index with 3 (0b11) to make this flip. +# When dealing with just 'RGB' (3 letter string), this means same but reduced by 1 after XOR!. +# Example: in 'GRBW' we want final form of 0bGGRRBBWW, meaning G with index 0 needs to be shifted 3 * 8bit -> +# 'G' on index 0: 0b00 ^ 0b11 -> 0b11 (3), just as we wanted. +# Same hold for every other index (and - 1 at the end for 3 letter strings). + +class Neopixel: + # Micropython doesn't implement __slots__, but it's good to have a place + # to describe the data members... + # __slots__ = [ + # 'num_leds', # number of LEDs + # 'pixels', # array.array('I') of raw data for LEDs + # 'mode', # mode 'RGB' etc + # 'W_in_mode', # bool: is 'W' in mode + # 'sm', # state machine + # 'shift', # shift amount for each component, in a tuple for (R,B,G,W) + # 'delay', # delay amount + # 'brightnessvalue', # brightness scale factor 1..255 + # ] + + def __init__(self, num_leds, state_machine, pin, mode="RGB", delay=0.0003): + """ + Constructor for library class + + :param num_leds: number of leds on your led-strip + :param state_machine: id of PIO state machine used + :param pin: pin on which data line to led-strip is connected + :param mode: [default: "RGB"] mode and order of bits representing the color value. + This can be any order of RGB or RGBW (neopixels are usually GRB) + :param delay: [default: 0.0001] delay used for latching of leds when sending data + """ + self.pixels = array.array("I", [0] * num_leds) + self.mode = mode + self.W_in_mode = 'W' in mode + if self.W_in_mode: + # RGBW uses different PIO state machine configuration + self.sm = rp2.StateMachine(state_machine, sk6812, freq=8000000, sideset_base=Pin(pin)) + # tuple of values required to shift bit into position (check class desc.) + self.shift = ((mode.index('R') ^ 3) * 8, (mode.index('G') ^ 3) * 8, + (mode.index('B') ^ 3) * 8, (mode.index('W') ^ 3) * 8) + else: + self.sm = rp2.StateMachine(state_machine, ws2812, freq=8000000, sideset_base=Pin(pin)) + self.shift = (((mode.index('R') ^ 3) - 1) * 8, ((mode.index('G') ^ 3) - 1) * 8, + ((mode.index('B') ^ 3) - 1) * 8, 0) + self.sm.active(1) + self.num_leds = num_leds + self.delay = delay + self.brightnessvalue = 255 + + def brightness(self, brightness=None): + """ + Set the overall value to adjust brightness when updating leds + or return class brightnessvalue if brightness is None + + :param brightness: [default: None] Value of brightness on interval 1..255 + :return: class brightnessvalue member or None + """ + if brightness is None: + return self.brightnessvalue + else: + if brightness < 1: + brightness = 1 + if brightness > 255: + brightness = 255 + self.brightnessvalue = brightness + + def set_pixel_line_gradient(self, pixel1, pixel2, left_rgb_w, right_rgb_w, how_bright=None): + """ + Create a gradient with two RGB colors between "pixel1" and "pixel2" (inclusive) + + :param pixel1: Index of starting pixel (inclusive) + :param pixel2: Index of ending pixel (inclusive) + :param left_rgb_w: Tuple of form (r, g, b) or (r, g, b, w) representing starting color + :param right_rgb_w: Tuple of form (r, g, b) or (r, g, b, w) representing ending color + :param how_bright: [default: None] Brightness of current interval. If None, use global brightness value + :return: None + """ + if pixel2 - pixel1 == 0: + return + right_pixel = max(pixel1, pixel2) + left_pixel = min(pixel1, pixel2) + + with_W = len(left_rgb_w) == 4 and self.W_in_mode + r_diff = right_rgb_w[0] - left_rgb_w[0] + g_diff = right_rgb_w[1] - left_rgb_w[1] + b_diff = right_rgb_w[2] - left_rgb_w[2] + if with_W: + w_diff = (right_rgb_w[3] - left_rgb_w[3]) + + for i in range(right_pixel - left_pixel + 1): + fraction = i / (right_pixel - left_pixel) + red = round(r_diff * fraction + left_rgb_w[0]) + green = round(g_diff * fraction + left_rgb_w[1]) + blue = round(b_diff * fraction + left_rgb_w[2]) + # if it's (r, g, b, w) + if with_W: + white = round(w_diff * fraction + left_rgb_w[3]) + self.set_pixel(left_pixel + i, (red, green, blue, white), how_bright) + else: + self.set_pixel(left_pixel + i, (red, green, blue), how_bright) + + def set_pixel_line(self, pixel1, pixel2, rgb_w, how_bright=None): + """ + Set an array of pixels starting from "pixel1" to "pixel2" (inclusive) to the desired color. + + :param pixel1: Index of starting pixel (inclusive) + :param pixel2: Index of ending pixel (inclusive) + :param rgb_w: Tuple of form (r, g, b) or (r, g, b, w) representing color to be used + :param how_bright: [default: None] Brightness of current interval. If None, use global brightness value + :return: None + """ + if pixel2 >= pixel1: + self.set_pixel(slice_maker[pixel1:pixel2 + 1], rgb_w, how_bright) + + def set_pixel(self, pixel_num, rgb_w, how_bright=None): + """ + Set red, green and blue (+ white) value of pixel on position + pixel_num may be a 'slice' object, and then the operation is applied + to all pixels implied by the slice (most useful when called via __setitem__) + + :param pixel_num: Index of pixel to be set or slice object representing multiple leds + :param rgb_w: Tuple of form (r, g, b) or (r, g, b, w) representing color to be used + :param how_bright: [default: None] Brightness of current interval. If None, use global brightness value + :return: None + """ + if how_bright is None: + how_bright = self.brightness() + sh_R, sh_G, sh_B, sh_W = self.shift + bratio = how_bright / 255.0 + + red = round(rgb_w[0] * bratio) + green = round(rgb_w[1] * bratio) + blue = round(rgb_w[2] * bratio) + white = 0 + # if it's (r, g, b, w) + if len(rgb_w) == 4 and self.W_in_mode: + white = round(rgb_w[3] * bratio) + + pix_value = white << sh_W | blue << sh_B | red << sh_R | green << sh_G + # set some subset, if pixel_num is a slice: + if type(pixel_num) is slice: + for i in range(*pixel_num.indices(self.num_leds)): + self.pixels[i] = pix_value + else: + self.pixels[pixel_num] = pix_value + + def get_pixel(self, pixel_num): + """ + Get red, green, blue and white (if applicable) values of pixel on position + + :param pixel_num: Index of pixel to be set + :return rgb_w: Tuple of form (r, g, b) or (r, g, b, w) representing color to be used + """ + balance = self.pixels[pixel_num] + sh_R, sh_G, sh_B, sh_W = self.shift + if self.W_in_mode: + w = (balance >> sh_W) & 255 + b = (balance >> sh_B) & 255 + r = (balance >> sh_R) & 255 + g = (balance >> sh_G) & 255 + red = int(r * 255 / self.brightness() ) + green = int(g * 255 / self.brightness() ) + blue = int(b * 255 / self.brightness() ) + if self.W_in_mode: + white = int(w * 255 / self.brightness() ) + return (red,green,blue,white) + else: + return (red,green,blue) + + def __setitem__(self, idx, rgb_w): + """ + if npix is a Neopixel object, + npix[10] = (0,255,0) # <- sets #10 to green + npix[15:21] = (255,0,0) # <- sets 16,17 .. 20 to red + npix[21:29:2] = (0,0,255) # <- sets 21,23,25,27 to blue + npix[1::2] = (0,0,0) # <- sets all odd pixels to 'off' + npix[:] = [(0,5,0),(0,5,0)] # <- replaces all pixels with those from the array + (the 'slice' cases pass idx as a 'slice' object, and + set_pixel processes the slice) + + :param idx: Index can either be indexing number or slice + :param rgb_w: Tuple (or list of tuples) of form (r, g, b) or (r, g, b, w) representing color to be used + :return: None + """ + if type(rgb_w) is list: + # set some subset, if idx is a slice: + if type(idx) is slice: + for rgb_i, pixel_i in enumerate(range(*idx.indices(self.num_leds))): + self.set_pixel(pixel_i, rgb_w[rgb_i]) + else: + raise ValueError("Index must be a slice when setting multiple pixels as list") + else: + self.set_pixel(idx, rgb_w) + + def __len__(self): + return self.num_leds + + def __getitem__(self, idx): + return self.get_pixel(idx) + + def colorHSV(self, hue, sat, val): + """ + Converts HSV color to rgb tuple and returns it. + The logic is almost the same as in Adafruit NeoPixel library: + https://github.com/adafruit/Adafruit_NeoPixel so all the credits for that + go directly to them (license: https://github.com/adafruit/Adafruit_NeoPixel/blob/master/COPYING) + + :param hue: Hue component. Should be on interval 0..65535 + :param sat: Saturation component. Should be on interval 0..255 + :param val: Value component. Should be on interval 0..255 + :return: (r, g, b) tuple + """ + if hue >= 65536: + hue %= 65536 + + hue = (hue * 1530 + 32768) // 65536 + if hue < 510: + b = 0 + if hue < 255: + r = 255 + g = hue + else: + r = 510 - hue + g = 255 + elif hue < 1020: + r = 0 + if hue < 765: + g = 255 + b = hue - 510 + else: + g = 1020 - hue + b = 255 + elif hue < 1530: + g = 0 + if hue < 1275: + r = hue - 1020 + b = 255 + else: + r = 255 + b = 1530 - hue + else: + r = 255 + g = 0 + b = 0 + + v1 = 1 + val + s1 = 1 + sat + s2 = 255 - sat + + r = ((((r * s1) >> 8) + s2) * v1) >> 8 + g = ((((g * s1) >> 8) + s2) * v1) >> 8 + b = ((((b * s1) >> 8) + s2) * v1) >> 8 + + return r, g, b + + def rotate_left(self, num_of_pixels=None): + """ + Rotate pixels to the left + + :param num_of_pixels: Number of pixels to be shifted to the left. If None, it shifts for 1. + :return: None + """ + if num_of_pixels is None: + num_of_pixels = 1 + self.pixels = self.pixels[num_of_pixels:] + self.pixels[:num_of_pixels] + + def rotate_right(self, num_of_pixels=None): + """ + Rotate pixels to the right + + :param num_of_pixels: Number of pixels to be shifted to the right. If None, it shifts for 1. + :return: None + """ + if num_of_pixels is None: + num_of_pixels = 1 + num_of_pixels = -1 * num_of_pixels + self.pixels = self.pixels[num_of_pixels:] + self.pixels[:num_of_pixels] + + def show(self): + """ + Send data to led-strip, making all changes on leds have an effect. + This method should be used after every method that changes the state of leds or after a chain of changes. + :return: None + """ + # If mode is RGB, we cut 8 bits of, otherwise we keep all 32 + cut = 8 + if self.W_in_mode: + cut = 0 + + self.sm.put(self.pixels, cut) + + time.sleep(self.delay) + + def fill(self, rgb_w, how_bright=None): + """ + Fill the entire strip with color rgb_w + + :param rgb_w: Tuple of form (r, g, b) or (r, g, b, w) representing color to be used + :param how_bright: [default: None] Brightness of current interval. If None, use global brightness value + :return: None + """ + # set_pixel over all leds. + self.set_pixel(slice_maker[:], rgb_w, how_bright) + + def clear(self): + """ + Clear the entire strip, i.e. set every led color to 0. + + :return: None + """ + self.pixels = array.array("I", [0] * self.num_leds) \ No newline at end of file diff --git a/wokwi-project.txt b/wokwi-project.txt new file mode 100644 index 0000000..a006cfe --- /dev/null +++ b/wokwi-project.txt @@ -0,0 +1,3 @@ +Downloaded from https://wokwi.com/projects/402298670663127041 + +Simulate this project on https://wokwi.com