From d039dde5d4f35063dbf405afbcb2a67c6d65e1ab Mon Sep 17 00:00:00 2001 From: benevpi <31006592+benevpi@users.noreply.github.com> Date: Wed, 20 Jan 2021 10:16:17 +0000 Subject: [PATCH] adding fill() method and the fireflies example --- examples/fireflies.py | 54 +++++++++++++++++++++++++++++++++++++++++++ examples/flash.py | 14 +++++++++++ ws2812b.py | 5 ++++ 3 files changed, 73 insertions(+) create mode 100644 examples/fireflies.py create mode 100644 examples/flash.py diff --git a/examples/fireflies.py b/examples/fireflies.py new file mode 100644 index 0000000..23d238a --- /dev/null +++ b/examples/fireflies.py @@ -0,0 +1,54 @@ +import time +import ws2812b +import random + +bright_div = 20 +numpix = 30 # Number of NeoPixels +# Pin where NeoPixels are connected +strip = ws2812b.ws2812b(numpix, 0,0) + +colors = [ + [232, 100, 255], # Purple + [200, 200, 20], # Yellow + [30, 200, 200], # Blue + [150,50,10], + [50,200,0], +] + +max_len=20 +min_len = 5 +#pixelnum, posn in flash, flash_len, direction +flashing = [] + +num_flashes = 5 + +for i in range(num_flashes): + pix = random.randint(0, numpix - 1) + col = random.randint(1, len(colors) - 1) + flash_len = random.randint(min_len, max_len) + flashing.append([pix, colors[col], flash_len, 0, 1]) + +strip.fill(0,0,0) + +while True: + strip.show() + for i in range(num_flashes): + + pix = flashing[i][0] + brightness = (flashing[i][3]/flashing[i][2]) + colr = (int(flashing[i][1][0]*brightness), + int(flashing[i][1][1]*brightness), + int(flashing[i][1][2]*brightness)) + strip.set_pixel(pix, colr[0], colr[1], colr[2]) + + if flashing[i][2] == flashing[i][3]: + flashing[i][4] = -1 + if flashing[i][3] == 0 and flashing[i][4] == -1: + pix = random.randint(0, numpix - 1) + col = random.randint(0, len(colors) - 1) + flash_len = random.randint(min_len, max_len) + flashing[i] = [pix, colors[col], flash_len, 0, 1] + flashing[i][3] = flashing[i][3] + flashing[i][4] + time.sleep(0.007) + + \ No newline at end of file diff --git a/examples/flash.py b/examples/flash.py new file mode 100644 index 0000000..71a161c --- /dev/null +++ b/examples/flash.py @@ -0,0 +1,14 @@ +import time +from ws2812b import ws2812b + +num_leds = 30 +pixels = ws2812b(num_leds, 0,0) + +while True: + for i in range(30): + for j in range(30): + pixels.set_pixel(j,abs(i+j)%10,abs(i-(j+3))%10,abs(i-(j+6))%10) + pixels.show() + time.sleep(0.05) + + diff --git a/ws2812b.py b/ws2812b.py index 3b32a06..8c23264 100644 --- a/ws2812b.py +++ b/ws2812b.py @@ -29,4 +29,9 @@ class ws2812b: def show(self): for i in range(self.num_leds): self.sm.put(self.pixels[i],8) + + def fill(self, red, green, blue): + for i in range(self.num_leds): + self.set_pixel(i, red, green, blue) + \ No newline at end of file