Added example colorwave showing use of tuples when calling functions

main
blaz-r 2021-03-03 19:15:45 +01:00
rodzic 869fb2418d
commit 0aa24fb142
3 zmienionych plików z 35 dodań i 3 usunięć

Wyświetl plik

@ -16,7 +16,7 @@ pixels = ws2812b.ws2812b(10,0,0)
This class has many methods, two main ones being show() which sends the data to the strip, and set_pixel which sets the colour values for a particular LED. The parameters are LED number, red, green, blue or a tuple of form (red, green blue) with the colours 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 colour values for a particular LED. The parameters are LED number, red, green, blue or a tuple of form (red, green blue) with the colours 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(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 colour 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(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 colour r,g,b.
Every method also works with tuple containing rgb values: set_pixel(num, (r,g,b)) and set_pixel_line_gradient(p1, p2, rgb1, rgb2) where rgb1 and rgb2 are of form (r,g,b). This enables writing simpler code for certain usecases. Every method also works with tuple containing rgb values: set_pixel(num, (r,g,b)) and set_pixel_line_gradient(p1, p2, rgb1, rgb2) where rgb1 and rgb2 are of form (r,g,b). This enables writing simpler code for certain usecases, one of such is in examples folder: "colorwave.py"
``` ```
pixels.set_pixel(5,10,0,0) pixels.set_pixel(5,10,0,0)

Wyświetl plik

@ -0,0 +1,32 @@
# Example showing how functions, that accept tuples of rgb values,
# simplify working with gradients
import time
import ws2812b
numpix = 60
strip = ws2812b.ws2812b(numpix, 0, 0)
red = (255, 0, 0)
orange = (255, 50, 0)
yellow = (255, 100, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
indigo = (100, 0, 90)
violet = (200, 0, 100)
colors = [red, orange, yellow, green, blue, indigo, violet]
step = round(numpix / len(colors))
current_pixel = 0
strip.brightness(20)
for color1, color2 in zip(colors, colors[1:]):
strip.set_pixel_line_gradient(current_pixel, current_pixel + step, color1, color2)
current_pixel += step
strip.set_pixel_line_gradient(current_pixel, numpix - 1, violet, red)
while True:
strip.rotate_right(1)
time.sleep(0.042)
strip.show()

Wyświetl plik

@ -61,7 +61,7 @@ class ws2812b:
self.set_pixel(left_pixel + i, (red, green, blue)) self.set_pixel(left_pixel + i, (red, green, blue))
# Set an array of pixels starting from "pixel1" to "pixel2" to the desired color. # Set an array of pixels starting from "pixel1" to "pixel2" (inclusive) to the desired color.
# Function accepts (r, g, b) tuple or individual rgb values # Function accepts (r, g, b) tuple or individual rgb values
def set_pixel_line(self, pixel1, pixel2, rgb_or_red, green=0, blue=0): def set_pixel_line(self, pixel1, pixel2, rgb_or_red, green=0, blue=0):
if type(rgb_or_red) is not tuple: if type(rgb_or_red) is not tuple:
@ -69,7 +69,7 @@ class ws2812b:
else: else:
rgb = rgb_or_red rgb = rgb_or_red
for i in range(pixel1, pixel2+1): for i in range(pixel1, pixel2 + 1):
self.set_pixel(i, rgb) self.set_pixel(i, rgb)
# Set red, green and blue value of pixel on position <pixel_num> # Set red, green and blue value of pixel on position <pixel_num>