-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine.py
52 lines (43 loc) · 1.13 KB
/
combine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from color_transform import *
from get_matrix import *
import spidev
# original code and instructions:
# https://tinkerboarding.co.uk/wiki/index.php/Waveshare-Expansions
# https://tinkerboarding.co.uk/wiki/images/5/5d/TB_sample_code.7z
# transform byte to bits sequence
def byte2bits(byte):
b0 = 0x1
b1 = 0x7
mask = 0x80
while mask != 0:
bits.append(b0 if ( byte & mask ) == 0 else b1)
mask >>= 1
return bits
# transform rgb colors to bits sequence
def SetLedColor(red, green, blue):
byte2bits(green)
byte2bits(red)
byte2bits(blue)
spi = spidev.SpiDev()
spi.open(2, 0)
spi.max_speed_hz = 3200000
spi.mode = 0b01
spi.xfer2([0])
led_cnt = 64
k = 8
matrix = get_matrix()
try:
bits = []
while True:
m = matrix.__next__()
# transform color matrix to spi bits sequence
for i in range(0, led_cnt):
(r, g, b) = m[i // k][i % k]
SetLedColor(r, g, b)
# send bits sequence to spi interface pin
spi.xfer(bits)
bits = []
except KeyboardInterrupt:
spi.xfer2([0x1 for i in range(1536)])
spi.close()
print("\n --- Done. ---")