-
Notifications
You must be signed in to change notification settings - Fork 0
/
led.py
101 lines (73 loc) · 3.32 KB
/
led.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Constants
I2C_ADDRESS = 0x10
SET_SINGLE_COLOR = 0x15
COLOR_WIPE = 0x20
RAINBOW = 0x50
RAINBOW_CYCLE = 0x51
THEATER_CHASE = 0x52
THEATER_CHASE_RAINBOW = 0x53
FLASHER = 0x60
FLASHINF = 0x71
FLASHALT = 0x72
SPARKLE = 0x73
SPARKLE_FADE = 0x74
FLAME = 0x75
GLOW = 0x76
BRIGHT = 0x80
RESTORE_STATE = 0xFD
SET_ALL = 0xFE
CLEAR_ALL = 0xFF
EMPTY_BYTES = [0, 0, 0, 0, 0, 0]
NUM_LEDS = 29
class ledStrip():
"""Arduino slave WS2812 interaction"""
def __init__(self, i2c, address=I2C_ADDRESS):
"""Initialize"""
self.i2c = i2c
self.address = address
self.ledState = [0] * 3 * NUM_LEDS
def setLed(self, led, r, g, b, wait, blink):
self.i2c.write_i2c_block_data(self.address, SET_SINGLE_COLOR, [led, r, g, b, wait, blink])
self.ledState[led * 3] = r
self.ledState[led * 3 + 1] = g
self.ledState[led * 3 + 2] = b
def setLedRange(self, ledStart, ledEnd, r, g, b, wait, blink):
for x in range(ledStart, ledEnd):
self.i2c.write_i2c_block_data(self.address, SET_SINGLE_COLOR, [x, r, g, b, wait, blink])
self.ledState[x * 3] = r
self.ledState[x * 3 + 1] = g
self.ledState[x * 3 + 2] = b
def setBright(self, brightness):
self.i2c.write_i2c_block_data(self.address, BRIGHT, [brightness, 0, 0, 0, 0, 0])
def restoreState(self):
self.i2c.write_i2c_block_data(self.address, RESTORE_STATE, EMPTY_BYTES)
def colorWipe(self, wait, r, g, b):
self.i2c.write_i2c_block_data(self.address, COLOR_WIPE, [0, r, g, b, wait, 0])
def rainbow(self, wait):
self.i2c.write_i2c_block_data(self.address, RAINBOW, [0, 0, 0, 0, wait, 0])
def rainbowCycle(self, wait):
self.i2c.write_i2c_block_data(self.address, RAINBOW_CYCLE, [0, 0, 0, 0, wait, 0])
def theaterChase(self, wait, r, g, b):
self.i2c.write_i2c_block_data(self.address, THEATER_CHASE, [0, r, g, b, wait, 0])
def theaterChaseRainbow(self, wait):
self.i2c.write_i2c_block_data(self.address, THEATER_CHASE_RAINBOW, [0, 0, 0, 0, wait, 0])
def flash(self, wait, repetitions, r, g, b):
self.i2c.write_i2c_block_data(self.address, FLASHER, [0, r, g, b, wait, repetitions])
def flashInf(self, wait, r, g, b):
self.i2c.write_i2c_block_data(self.address, FLASHINF, [0, r, g, b, wait, 0])
def flashAlt(self, wait, r, g, b):
self.i2c.write_i2c_block_data(self.address, FLASHALT, [0, r, g, b, wait, 0])
def sparkle(self, wait, r, g, b):
self.i2c.write_i2c_block_data(self.address, SPARKLE, [0, r, g, b, wait, 0])
def sparkleFade(self, wait, chance, r, g, b):
self.i2c.write_i2c_block_data(self.address, SPARKLE_FADE, [0, r, g, b, wait, chance])
def flame(self, wait):
self.i2c.write_i2c_block_data(self.address, FLAME, [0, 0, 0, 0, wait, 0])
def glow(self, wait):
self.i2c.write_i2c_block_data(self.address, GLOW, [0, 0, 0, 0, wait, 0])
def setAll(self, r, g, b):
self.i2c.write_i2c_block_data(self.address, SET_ALL, [0, r, g, b, 0, 0])
def clear(self):
self.i2c.write_i2c_block_data(self.address, CLEAR_ALL, EMPTY_BYTES)
for x in range(0, len(self.ledState)):
self.ledState[x] = 0