From 0aa24fb14204992fe26d0a5b7eb2f5eb8e2cee60 Mon Sep 17 00:00:00 2001 From: blaz-r Date: Wed, 3 Mar 2021 19:15:45 +0100 Subject: [PATCH] Added example colorwave showing use of tuples when calling functions --- README.md | 2 +- examples/colorwave.py | 32 ++++++++++++++++++++++++++++++++ ws2812b.py | 4 ++-- 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 examples/colorwave.py diff --git a/README.md b/README.md index 9e8adbd..1e2a5d7 100644 --- a/README.md +++ b/README.md @@ -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. 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) diff --git a/examples/colorwave.py b/examples/colorwave.py new file mode 100644 index 0000000..d5a182b --- /dev/null +++ b/examples/colorwave.py @@ -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() diff --git a/ws2812b.py b/ws2812b.py index 98d3a10..f6d9358 100644 --- a/ws2812b.py +++ b/ws2812b.py @@ -61,7 +61,7 @@ class ws2812b: 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 def set_pixel_line(self, pixel1, pixel2, rgb_or_red, green=0, blue=0): if type(rgb_or_red) is not tuple: @@ -69,7 +69,7 @@ class ws2812b: else: rgb = rgb_or_red - for i in range(pixel1, pixel2+1): + for i in range(pixel1, pixel2 + 1): self.set_pixel(i, rgb) # Set red, green and blue value of pixel on position