Skip to content

Commit 30e4c7e

Browse files
authored
Add files via upload
1 parent 7417076 commit 30e4c7e

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

NeoPixel Rain Effect/WS2812.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from micropython import const
2+
from utime import sleep_us
3+
4+
5+
one = const(0xF8)
6+
zero = const(0xC0)
7+
8+
number_of_colour_channels = const(3)
9+
number_of_bits_per_channel = const(8)
10+
number_of_bits = (number_of_colour_channels * number_of_bits_per_channel)
11+
12+
13+
class WS2812():
14+
15+
def __init__(self, _no_of_LEDs, _spi):
16+
self.no_of_LEDs = _no_of_LEDs
17+
self.spi = _spi
18+
19+
self.buffer_size = (self.no_of_LEDs * number_of_bits)
20+
self.buffer = bytearray(self.buffer_size)
21+
22+
self.reset()
23+
24+
25+
def reset(self):
26+
for i in range(0, self.buffer_size):
27+
self.buffer[i] = 0x00
28+
29+
self.set_all_channel(0, 0, 0)
30+
31+
32+
def send(self, channel, r, g, b):
33+
s = 0
34+
n = (number_of_bits * channel)
35+
temp = 0
36+
value = 0x00000000
37+
value = ((g << 16) | (r << 8) | b)
38+
39+
while(s < number_of_bits):
40+
if(value & 0x800000):
41+
temp = one
42+
43+
else:
44+
temp = zero
45+
46+
self.buffer[s + n] = temp
47+
value <<= 1
48+
s += 1
49+
50+
51+
def set_all_channel(self, r, g, b):
52+
for i in range(0, self.no_of_LEDs):
53+
self.send(i, r, g, b)
54+
self.show()
55+
56+
57+
def show(self):
58+
self.spi.send(self.buffer)
59+
sleep_us(60)
60+
61+
62+

NeoPixel Rain Effect/main.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from pyb import Pin, SPI, ADC
2+
from WS2812 import WS2812
3+
from utime import sleep_ms
4+
5+
6+
dly = 0
7+
8+
9+
colour_matrix = [
10+
(50, 100, 200), (35, 70, 140), (30, 45, 60), (5, 7, 10),
11+
(200, 100, 50), (140, 70, 35), (60, 45, 30), (10, 7, 5),
12+
(240, 200, 120), (170, 140, 60), (90, 80, 45), (40, 30, 15),
13+
(200, 0, 0), (140, 0, 0), (60, 0, 0), (10, 0, 0),
14+
(0, 200, 0), (0, 140, 0), (0, 60, 0), (0, 10, 0),
15+
(0, 0, 200), (0, 0, 140), (0, 0, 60), (0, 0, 10),
16+
(200, 200, 200), (140, 140, 140), (60, 60, 60), (10, 10, 10),
17+
(120, 0, 0), (0, 120, 0), (0, 0, 120), (90, 90, 90),
18+
]
19+
20+
21+
adc = ADC(Pin("PA0"))
22+
23+
spi = SPI(2, mode = SPI.MASTER, baudrate = 10_000_000, polarity = 0, phase = 1, bits = 8, firstbit = SPI.MSB)
24+
25+
np = WS2812(8, spi)
26+
27+
28+
while (True):
29+
for j in range (0, 32, 4):
30+
for i in range (4, -1, -1):
31+
np.send(i, colour_matrix[j][0], colour_matrix[j][1], colour_matrix[j][2])
32+
np.send((i + 1), colour_matrix[j + 1][0], colour_matrix[j + 1][1], colour_matrix[j + 1][2])
33+
np.send((i + 2), colour_matrix[j + 2][0], colour_matrix[j + 2][1], colour_matrix[j + 2][2])
34+
np.send((i + 3), colour_matrix[j + 3][0], colour_matrix[j + 3][1], colour_matrix[j + 3][2])
35+
np.show()
36+
sleep_ms(dly)
37+
38+
for i in range (4, -1, -1):
39+
np.send(i, 0, 0, 0)
40+
np.send((i + 1), 0, 0, 0)
41+
np.send((i + 2), 0, 0, 0)
42+
np.send((i + 3), 0, 0, 0)
43+
np.show()
44+
sleep_ms(dly)
45+
46+
dly = ((adc.read() + 96) >> 3)
47+
48+

0 commit comments

Comments
 (0)