kopia lustrzana https://github.com/blaz-r/pi_pico_neopixel
commit
f5c689afb9
29
README.md
29
README.md
|
@ -5,6 +5,8 @@ a library for using ws2812b and sk6812 leds (aka neopixels) with Raspberry Pi Pi
|
||||||
|
|
||||||
You'll first need to save the neopixel.py file to your device (for example, open it in Thonny and go file > save as and select MicroPython device. Give it the same name). Once it's there, you can import it into your code.
|
You'll first need to save the neopixel.py file to your device (for example, open it in Thonny and go file > save as and select MicroPython device. Give it the same name). Once it's there, you can import it into your code.
|
||||||
|
|
||||||
|
## Initialization
|
||||||
|
|
||||||
You create an object with the parameters number of LEDs, state machine ID, GPIO number and mode (RGB or RGBW) in that order. So, to create a strip of 10 leds on state machine 0 and GPIO 0 in RGBW mode you use:
|
You create an object with the parameters number of LEDs, state machine ID, GPIO number and mode (RGB or RGBW) in that order. So, to create a strip of 10 leds on state machine 0 and GPIO 0 in RGBW mode you use:
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -15,21 +17,25 @@ pixels = Neopixel(10, 0, 0, "RGBW")
|
||||||
|
|
||||||
Mind that you can use whichever order of RGB / RGBW you want (GRB, WRGB, GRB, RGWB ...). This only represents order of data sent to led-strip, all functions still work with RGBW order. Exact order of leds should be on package of your led-strip. (My BTF-lights sk6812 has GRBW).
|
Mind that you can use whichever order of RGB / RGBW you want (GRB, WRGB, GRB, RGWB ...). This only represents order of data sent to led-strip, all functions still work with RGBW order. Exact order of leds should be on package of your led-strip. (My BTF-lights sk6812 has GRBW).
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
This class has many methods, two main ones being show() which sends the data to the strip, and set_pixel which sets the color values for a particular LED. The parameters are LED number and a tuple of form (red, green blue) or (red, green, blue, white) with the colors taking values between 0 and 255.
|
This class has many methods, two main ones being show() which sends the data to the strip, and set_pixel which sets the color values for a particular LED. The parameters are LED number and a tuple of form (red, green blue) or (red, green, blue, white) with the colors taking values between 0 and 255.
|
||||||
|
|
||||||
At the moment, this isn't working with the interpreter, so you have to run it from a file. Looks like it's running just too slow to keep up with the PIO buffer from the interpreter. The key methods are set_pixel(n (r,g,b)), set_pixel_line(p1, p2, (r, g, b)) which sets a row of pixels from pixel p1 to pixel p2 (inclusive), and fill((r,g,b)) which fills all the pixels with the color r, g, b.
|
At the moment, this isn't working with the interpreter, so you have to run it from a file. Looks like it's running just too slow to keep up with the PIO buffer from the interpreter. The key methods are set_pixel(n (r,g,b)), set_pixel_line(p1, p2, (r, g, b)) which sets a row of pixels from pixel p1 to pixel p2 (inclusive), and fill((r,g,b)) which fills all the pixels with the color r, g, b.
|
||||||
If you want to use the library for RGBW, each function works the same just with last parameter being "white": set_pixel(num, (r, g, b, w))
|
If you want to use the library for RGBW, each function works the same just with last parameter being "white": set_pixel(num, (r, g, b, w))
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
```
|
```
|
||||||
pixels.set_pixel(5, (10, 0, 0))
|
pixels.set_pixel(5, (10, 0, 0))
|
||||||
pixels.set_pixel_line(5, 7, (0, 10, 0))
|
pixels.set_pixel_line(5, 7, (0, 10, 0))
|
||||||
pixels.fill((20, 5, 0))
|
pixels.fill((20, 5, 0))
|
||||||
|
|
||||||
rgb1 = (0, 0, 50, 0)
|
rgbw1 = (0, 0, 50, 0)
|
||||||
rgb2 = (50, 0, 0, 250)
|
rgbw2 = (50, 0, 0, 250)
|
||||||
pixels.set_pixel(42, (0, 50, 0, 0))
|
pixels.set_pixel(42, (0, 50, 0, 0))
|
||||||
pixels.set_pixel_line(5, 7, rgb1)
|
pixels.set_pixel_line(5, 7, rgbw1)
|
||||||
pixels.set_pixel_line_gradient(0, 13, rgb1, rgb2)
|
pixels.set_pixel_line_gradient(0, 13, rgbw1, rgbw2)
|
||||||
```
|
```
|
||||||
|
|
||||||
For new settings to take effect you write:
|
For new settings to take effect you write:
|
||||||
|
@ -37,4 +43,19 @@ For new settings to take effect you write:
|
||||||
pixels.show()
|
pixels.show()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
For more examples, check [examples folder](https://github.com/blaz-r/pi_pico_neopixel/tree/develop/examples).
|
||||||
|
|
||||||
|
## HSV colors
|
||||||
|
|
||||||
|
Library also supports HSV colors. For example you can look at [smoothRinbow.py](https://github.com/blaz-r/pi_pico_neopixel/blob/develop/examples/smoothRainbow.py).
|
||||||
|
To use HSV colors, call colorHSV(hue, sat, val) function with hue, saturation and value as parameters. The function returns rgb tuple that you can then use in all other functions.
|
||||||
|
|
||||||
|
Hue should be between 0 and 65535. When it becomes larger it just rolls over (65536 -> 0). Saturation and value must be in range from 0 to 255. 255 saturation means just hue, and 255 value is maximum brightness. For more info about HSV colors you can check out [Adafruit NeoPixel library documentation](https://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library-use) and scroll down to HSV section.
|
||||||
|
|
||||||
|
```
|
||||||
|
color = pixels.colorHSV(32000, 255, 200)
|
||||||
|
pixels.fill(color)
|
||||||
|
pixels.show()
|
||||||
|
```
|
||||||
|
|
||||||
Library is extended verison of https://github.com/blaz-r/pico_python_ws2812b, originally forked from https://github.com/benevpi/pico_python_ws2812b.
|
Library is extended verison of https://github.com/blaz-r/pico_python_ws2812b, originally forked from https://github.com/benevpi/pico_python_ws2812b.
|
|
@ -0,0 +1,14 @@
|
||||||
|
# Example showing use of HSV colors
|
||||||
|
import time
|
||||||
|
from neopixel import Neopixel
|
||||||
|
|
||||||
|
numpix = 60
|
||||||
|
strip = Neopixel(numpix, 0, 0, "GRB")
|
||||||
|
|
||||||
|
hue = 0
|
||||||
|
while(True):
|
||||||
|
color = strip.colorHSV(hue, 255, 150)
|
||||||
|
strip.fill(color)
|
||||||
|
strip.show()
|
||||||
|
|
||||||
|
hue += 150
|
52
neopixel.py
52
neopixel.py
|
@ -40,7 +40,7 @@ def sk6812():
|
||||||
# Class supports different order of individual colors (GRB, RGB, WRGB, GWRB ...). In order to achieve
|
# 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,
|
# 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()
|
# 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 (operand ^) each index with 3 (0b11) to make this flip.
|
# 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!.
|
# 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 ->
|
# 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.
|
# 'G' on index 0: 0b00 ^ 0b11 -> 0b11 (3), just as we wanted.
|
||||||
|
@ -117,6 +117,56 @@ class Neopixel:
|
||||||
|
|
||||||
self.pixels[pixel_num] = white << pos['W'] | blue << pos['B'] | red << pos['R'] | green << pos['G']
|
self.pixels[pixel_num] = white << pos['W'] | blue << pos['B'] | red << pos['R'] | green << pos['G']
|
||||||
|
|
||||||
|
# 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):
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
# Rotate <num_of_pixels> pixels to the left
|
# Rotate <num_of_pixels> pixels to the left
|
||||||
def rotate_left(self, num_of_pixels):
|
def rotate_left(self, num_of_pixels):
|
||||||
if num_of_pixels == None:
|
if num_of_pixels == None:
|
||||||
|
|
Ładowanie…
Reference in New Issue