kopia lustrzana https://github.com/blaz-r/pi_pico_neopixel
Added get_pixelRGB method
Added method to read back the RGB colour of any pixelpull/10/head
rodzic
3f50b85c5f
commit
a1f6e8987f
253
neopixel.py
253
neopixel.py
|
@ -2,7 +2,6 @@ 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():
|
||||
|
@ -18,7 +17,6 @@ def ws2812():
|
|||
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():
|
||||
|
@ -35,17 +33,6 @@ def sk6812():
|
|||
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)
|
||||
|
@ -60,57 +47,27 @@ slice_maker = slice_maker_class()
|
|||
# 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.0001):
|
||||
"""
|
||||
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:
|
||||
self.pixels = array.array("I", [0 for _ in range(num_leds)])
|
||||
self.mode = set(mode) # set for better performance
|
||||
if 'W' in self.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)
|
||||
# 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}
|
||||
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.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.sm.active(1)
|
||||
self.num_leds = num_leds
|
||||
self.delay = delay
|
||||
self.brightnessvalue = 255
|
||||
|
||||
# Set the overal value to adjust brightness when updating leds
|
||||
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:
|
||||
if brightness == None:
|
||||
return self.brightnessvalue
|
||||
else:
|
||||
if brightness < 1:
|
||||
|
@ -119,114 +76,66 @@ class Neopixel:
|
|||
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
|
||||
"""
|
||||
# Create a gradient with two RGB colors between "pixel1" and "pixel2" (inclusive)
|
||||
# Function accepts two (r, g, b) / (r, g, b, w) tuples
|
||||
def set_pixel_line_gradient(self, pixel1, pixel2, left_rgb_w, right_rgb_w):
|
||||
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])
|
||||
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])
|
||||
# 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)
|
||||
if len(left_rgb_w) == 4 and 'W' in self.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))
|
||||
else:
|
||||
self.set_pixel(left_pixel + i, (red, green, blue), how_bright)
|
||||
self.set_pixel(left_pixel + i, (red, green, blue))
|
||||
|
||||
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.
|
||||
# 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):
|
||||
for i in range(pixel1, pixel2 + 1):
|
||||
self.set_pixel(i, rgb_w)
|
||||
|
||||
: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)
|
||||
# Set red, green and blue value of pixel on position <pixel_num>
|
||||
# Function accepts (r, g, b) / (r, g, b, w) tuple
|
||||
def set_pixel(self, pixel_num, rgb_w):
|
||||
pos = self.shift
|
||||
|
||||
def set_pixel(self, pixel_num, rgb_w, how_bright=None):
|
||||
"""
|
||||
Set red, green and blue (+ white) value of pixel on position <pixel_num>
|
||||
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)
|
||||
red = round(rgb_w[0] * (self.brightness() / 255))
|
||||
green = round(rgb_w[1] * (self.brightness() / 255))
|
||||
blue = round(rgb_w[2] * (self.brightness() / 255))
|
||||
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)
|
||||
if len(rgb_w) == 4 and 'W' in self.mode:
|
||||
white = round(rgb_w[3] * (self.brightness() / 255))
|
||||
|
||||
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
|
||||
self.pixels[pixel_num] = white << pos['W'] | blue << pos['B'] | red << pos['R'] | green << pos['G']
|
||||
|
||||
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'
|
||||
(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 of form (r, g, b) or (r, g, b, w) representing color to be used
|
||||
:return:
|
||||
"""
|
||||
self.set_pixel(idx, rgb_w)
|
||||
# Get red, green and blue value of pixel on position <pixel_num>
|
||||
# Function hard coded for GRB data order only
|
||||
def get_pixelRGB(self, pixel_num):
|
||||
balance = self.pixels[pixel_num]
|
||||
b = balance & 255
|
||||
balance = (balance-b) >> 8
|
||||
r = balance & 255
|
||||
g = ((balance-r) >> 8) & 255
|
||||
red = int(r * 255 / self.brightness() )
|
||||
green = int(g * 255 / self.brightness() )
|
||||
blue = int(b * 255 / self.brightness() )
|
||||
return (red,green,blue)
|
||||
|
||||
# Converts HSV color to rgb tuple and returns it
|
||||
# Function accepts integer values for <hue>, <saturation> and <value>
|
||||
# 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)
|
||||
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
|
||||
|
||||
|
@ -270,59 +179,33 @@ class Neopixel:
|
|||
|
||||
return r, g, b
|
||||
|
||||
def rotate_left(self, num_of_pixels=None):
|
||||
"""
|
||||
Rotate <num_of_pixels> 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:
|
||||
# Rotate <num_of_pixels> pixels to the left
|
||||
def rotate_left(self, num_of_pixels):
|
||||
if num_of_pixels == 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 <num_of_pixels> 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:
|
||||
# Rotate <num_of_pixels> pixels to the right
|
||||
def rotate_right(self, num_of_pixels):
|
||||
if num_of_pixels == None:
|
||||
num_of_pixels = 1
|
||||
num_of_pixels = -1 * num_of_pixels
|
||||
self.pixels = self.pixels[num_of_pixels:] + self.pixels[:num_of_pixels]
|
||||
|
||||
# Update 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:
|
||||
if 'W' in self.mode:
|
||||
cut = 0
|
||||
sm_put = self.sm.put
|
||||
for pixval in self.pixels:
|
||||
sm_put(pixval, cut)
|
||||
for i in range(self.num_leds):
|
||||
self.sm.put(self.pixels[i], 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)
|
||||
# Set all pixels to given rgb values
|
||||
# Function accepts (r, g, b) / (r, g, b, w)
|
||||
def fill(self, rgb_w):
|
||||
for i in range(self.num_leds):
|
||||
self.set_pixel(i, rgb_w)
|
||||
time.sleep(self.delay)
|
||||
|
|
Ładowanie…
Reference in New Issue