From 7dc24c256a06d2d482a4c09ab9718549c72e961f Mon Sep 17 00:00:00 2001 From: Greg Smith Date: Sun, 5 Jun 2022 18:59:34 -0400 Subject: [PATCH 01/10] default parm values for rotate_left/right; fix some "== None" --- neopixel.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/neopixel.py b/neopixel.py index 53c37d4..d31e249 100644 --- a/neopixel.py +++ b/neopixel.py @@ -67,7 +67,7 @@ class Neopixel: # Set the overal value to adjust brightness when updating leds def brightness(self, brightness=None): - if brightness == None: + if brightness is None: return self.brightnessvalue else: if brightness < 1: @@ -105,7 +105,7 @@ class Neopixel: # Set red, green and blue value of pixel on position # Function accepts (r, g, b) / (r, g, b, w) tuple def set_pixel(self, pixel_num, rgb_w, how_bright = None): - if how_bright == None: + if how_bright is None: how_bright = self.brightness() pos = self.shift @@ -170,14 +170,14 @@ class Neopixel: # Rotate pixels to the left - def rotate_left(self, num_of_pixels): - if num_of_pixels == None: + def rotate_left(self, num_of_pixels = None): + if num_of_pixels is None: num_of_pixels = 1 self.pixels = self.pixels[num_of_pixels:] + self.pixels[:num_of_pixels] # Rotate pixels to the right - def rotate_right(self, num_of_pixels): - if num_of_pixels == None: + def rotate_right(self, num_of_pixels = 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] From a93b872f11f908deab59692de84e8fe65cc67401 Mon Sep 17 00:00:00 2001 From: Greg Smith Date: Sun, 5 Jun 2022 19:03:34 -0400 Subject: [PATCH 02/10] factor how_bright/255 --- neopixel.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/neopixel.py b/neopixel.py index d31e249..cafa13a 100644 --- a/neopixel.py +++ b/neopixel.py @@ -108,14 +108,15 @@ class Neopixel: if how_bright is None: how_bright = self.brightness() pos = self.shift + bratio = how_bright / 255.0 - red = round(rgb_w[0] * (how_bright / 255)) - green = round(rgb_w[1] * (how_bright / 255)) - blue = round(rgb_w[2] * (how_bright / 255)) + 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 'W' in self.mode: - white = round(rgb_w[3] * (how_bright / 255)) + white = round(rgb_w[3] * bratio) self.pixels[pixel_num] = white << pos['W'] | blue << pos['B'] | red << pos['R'] | green << pos['G'] From 04d6a2e49c8246b2b1889fbd1709857966a84a78 Mon Sep 17 00:00:00 2001 From: Greg Smith Date: Sun, 5 Jun 2022 19:06:09 -0400 Subject: [PATCH 03/10] create data member W_in_mode --- neopixel.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/neopixel.py b/neopixel.py index cafa13a..71436b5 100644 --- a/neopixel.py +++ b/neopixel.py @@ -49,8 +49,9 @@ def sk6812(): class Neopixel: def __init__(self, num_leds, state_machine, pin, mode="RGB", delay=0.0001): self.pixels = array.array("I", [0 for _ in range(num_leds)]) - self.mode = set(mode) # set for better performance - if 'W' in self.mode: + 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)) # dictionary of values required to shift bit into position (check class desc.) @@ -90,7 +91,7 @@ class Neopixel: green = round((right_rgb_w[1] - left_rgb_w[1]) * fraction + left_rgb_w[1]) blue = round((right_rgb_w[2] - left_rgb_w[2]) * fraction + left_rgb_w[2]) # if it's (r, g, b, w) - if len(left_rgb_w) == 4 and 'W' in self.mode: + if len(left_rgb_w) == 4 and self.W_in_mode: white = round((right_rgb_w[3] - left_rgb_w[3]) * fraction + left_rgb_w[3]) self.set_pixel(left_pixel + i, (red, green, blue, white), how_bright) else: @@ -115,7 +116,7 @@ class Neopixel: blue = round(rgb_w[2] * bratio) white = 0 # if it's (r, g, b, w) - if len(rgb_w) == 4 and 'W' in self.mode: + if len(rgb_w) == 4 and self.W_in_mode: white = round(rgb_w[3] * bratio) self.pixels[pixel_num] = white << pos['W'] | blue << pos['B'] | red << pos['R'] | green << pos['G'] @@ -187,7 +188,7 @@ class Neopixel: def show(self): # If mode is RGB, we cut 8 bits of, otherwise we keep all 32 cut = 8 - if 'W' in self.mode: + if self.W_in_mode: cut = 0 for i in range(self.num_leds): self.sm.put(self.pixels[i], cut) From 5a59a8f50bdfad899ca415ad224733fc974cffd2 Mon Sep 17 00:00:00 2001 From: Greg Smith Date: Sun, 5 Jun 2022 19:11:34 -0400 Subject: [PATCH 04/10] add __slots__ to Neopixel class (as comment!) --- neopixel.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/neopixel.py b/neopixel.py index 71436b5..fe50e4f 100644 --- a/neopixel.py +++ b/neopixel.py @@ -47,6 +47,19 @@ def sk6812(): # 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 : { 'R': shift_R, ... } + # 'delay', # delay amount + # 'brightnessvalue', # brightness scale factor 1..255 + #] + def __init__(self, num_leds, state_machine, pin, mode="RGB", delay=0.0001): self.pixels = array.array("I", [0 for _ in range(num_leds)]) self.mode = mode From 2293525434babb59e461830db4cebf38fa4dce70 Mon Sep 17 00:00:00 2001 From: Greg Smith Date: Sun, 5 Jun 2022 19:17:00 -0400 Subject: [PATCH 05/10] make "shift" a tuple --- neopixel.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/neopixel.py b/neopixel.py index fe50e4f..e3a4c0b 100644 --- a/neopixel.py +++ b/neopixel.py @@ -55,7 +55,7 @@ class Neopixel: # 'mode', # mode 'RGB' etc # 'W_in_mode', # bool: is 'W' in mode # 'sm', # state machine - # 'shift', # shift amount for each component : { 'R': shift_R, ... } + # 'shift', # shift amount for each component, in a tuple for (R,B,G,W) # 'delay', # delay amount # 'brightnessvalue', # brightness scale factor 1..255 #] @@ -67,13 +67,13 @@ class Neopixel: 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)) - # dictionary of values required to shift bit into position (check class desc.) - self.shift = {'R': (mode.index('R') ^ 3) * 8, 'G': (mode.index('G') ^ 3) * 8, - 'B': (mode.index('B') ^ 3) * 8, 'W': (mode.index('W') ^ 3) * 8} + # 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 = {'R': ((mode.index('R') ^ 3) - 1) * 8, 'G': ((mode.index('G') ^ 3) - 1) * 8, - 'B': ((mode.index('B') ^ 3) - 1) * 8, 'W': 0} + 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 @@ -121,7 +121,7 @@ class Neopixel: def set_pixel(self, pixel_num, rgb_w, how_bright = None): if how_bright is None: how_bright = self.brightness() - pos = self.shift + sh_R, sh_G, sh_B, sh_W = self.shift bratio = how_bright / 255.0 red = round(rgb_w[0] * bratio) @@ -132,7 +132,7 @@ class Neopixel: if len(rgb_w) == 4 and self.W_in_mode: white = round(rgb_w[3] * bratio) - self.pixels[pixel_num] = white << pos['W'] | blue << pos['B'] | red << pos['R'] | green << pos['G'] + self.pixels[pixel_num] = white << sh_W | blue << sh_B | red << sh_R | green << sh_G # Converts HSV color to rgb tuple and returns it # Function accepts integer values for , and From e4b2465eafdab6d874e93a95d089e5c043b1dca7 Mon Sep 17 00:00:00 2001 From: Greg Smith Date: Sun, 5 Jun 2022 21:18:54 -0400 Subject: [PATCH 06/10] make set_pixel handle a slice; add __setitem__ method --- neopixel.py | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/neopixel.py b/neopixel.py index e3a4c0b..cf54fe2 100644 --- a/neopixel.py +++ b/neopixel.py @@ -32,6 +32,13 @@ def sk6812(): 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 @@ -113,11 +120,14 @@ class Neopixel: # Set an array of pixels starting from "pixel1" to "pixel2" (inclusive) to the desired color. # Function accepts (r, g, b) / (r, g, b, w) tuple def set_pixel_line(self, pixel1, pixel2, rgb_w, how_bright = None): - for i in range(pixel1, pixel2 + 1): - self.set_pixel(i, rgb_w, how_bright) + if pixel2 >= pixel1: + self.set_pixel(slice_maker[pixel1:pixel2 + 1], rgb_w, how_bright) # Set red, green and blue value of pixel on position # Function accepts (r, g, b) / (r, g, b, w) tuple + # 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__) def set_pixel(self, pixel_num, rgb_w, how_bright = None): if how_bright is None: how_bright = self.brightness() @@ -132,7 +142,23 @@ class Neopixel: if len(rgb_w) == 4 and self.W_in_mode: white = round(rgb_w[3] * bratio) - self.pixels[pixel_num] = white << sh_W | blue << sh_B | red << sh_R | green << sh_G + 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 + + # 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' + # (the 'slice' cases pass idx as a 'slice' object, and + # set_pixel processes the slice) + def __setitem__(self, idx, rgb_w): + self.set_pixel(idx,rgb_w) # Converts HSV color to rgb tuple and returns it # Function accepts integer values for , and @@ -210,8 +236,8 @@ class Neopixel: # Set all pixels to given rgb values # Function accepts (r, g, b) / (r, g, b, w) def fill(self, rgb_w, how_bright = None): - for i in range(self.num_leds): - self.set_pixel(i, rgb_w, how_bright) + # set_pixel over all leds. + self.set_pixel(slice_maker[:], rgb_w, how_bright) # Clear the strip def clear(self): From 4932d91202a74d3e2a96de59ac2f04cc042cfdb6 Mon Sep 17 00:00:00 2001 From: Greg Smith Date: Sun, 5 Jun 2022 19:41:30 -0400 Subject: [PATCH 07/10] hoist some invariants in set_pixel_line_gradient --- neopixel.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/neopixel.py b/neopixel.py index cf54fe2..0a89df6 100644 --- a/neopixel.py +++ b/neopixel.py @@ -105,14 +105,21 @@ class Neopixel: 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((right_rgb_w[0] - left_rgb_w[0]) * fraction + left_rgb_w[0]) - green = round((right_rgb_w[1] - left_rgb_w[1]) * fraction + left_rgb_w[1]) - blue = round((right_rgb_w[2] - left_rgb_w[2]) * fraction + left_rgb_w[2]) + 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 len(left_rgb_w) == 4 and self.W_in_mode: - white = round((right_rgb_w[3] - left_rgb_w[3]) * fraction + left_rgb_w[3]) + 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) From 7bc29398d29f73cf40976dbc21245e5905e84a37 Mon Sep 17 00:00:00 2001 From: Greg Smith Date: Sun, 5 Jun 2022 19:45:20 -0400 Subject: [PATCH 08/10] faster generation of zero-arrays --- neopixel.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/neopixel.py b/neopixel.py index 0a89df6..5bb4a1c 100644 --- a/neopixel.py +++ b/neopixel.py @@ -68,7 +68,7 @@ class Neopixel: #] def __init__(self, num_leds, state_machine, pin, mode="RGB", delay=0.0001): - self.pixels = array.array("I", [0 for _ in range(num_leds)]) + self.pixels = array.array("I", [0] * num_leds) self.mode = mode self.W_in_mode = 'W' in mode if self.W_in_mode: @@ -248,4 +248,5 @@ class Neopixel: # Clear the strip def clear(self): - self.pixels = array.array("I", [0 for _ in range(self.num_leds)]) + self.pixels = array.array("I", [0] * self.num_leds) + From 3e817f90c47ab438d7a840d231a136aa5f50df64 Mon Sep 17 00:00:00 2001 From: Greg Smith Date: Sun, 5 Jun 2022 19:50:08 -0400 Subject: [PATCH 09/10] faster loop in "show" --- neopixel.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/neopixel.py b/neopixel.py index 5bb4a1c..b84326f 100644 --- a/neopixel.py +++ b/neopixel.py @@ -236,8 +236,9 @@ class Neopixel: cut = 8 if self.W_in_mode: cut = 0 - for i in range(self.num_leds): - self.sm.put(self.pixels[i], cut) + sm_put = self.sm.put + for pixval in self.pixels: + sm_put(pixval, cut) time.sleep(self.delay) # Set all pixels to given rgb values From 80ee99748fd48636bc965f4823b734388bd5b963 Mon Sep 17 00:00:00 2001 From: Greg Smith Date: Tue, 7 Jun 2022 19:56:08 -0400 Subject: [PATCH 10/10] add examples/set_range.py --- examples/set_range.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 examples/set_range.py diff --git a/examples/set_range.py b/examples/set_range.py new file mode 100644 index 0000000..bf71141 --- /dev/null +++ b/examples/set_range.py @@ -0,0 +1,40 @@ +# Example showing use of 'slice setting' +import time +from neopixel import Neopixel + + +numpix = 60 +K = 3 + +strip = Neopixel(numpix, 0, 0, "GRB") +red = (255, 0, 0) +green = (0, 255, 0) +blue = (0, 0, 255) + +# set the first K to red, next K to green, next K to blue; +# and the rest to R,G,B,R,B ... and then spin it. + +# reduce K, if numpix is < K*3+1 +K = min(K,(numpix-1)//3) + +strip.brightness(80) + +strip[:] = blue # all to blue first... +# now fill in the red & green... +strip[:K] = red +strip[K:2*K] = green +strip[3*K::3] = red +strip[3*K+1::3] = green + +strip.show() + +# show it for 5 seconds... +time.sleep(5.0) + +# spin it... + +while(True): + strip.rotate_right() + strip.show() + time.sleep(0.5) +