-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrgbled.py
299 lines (265 loc) · 8.39 KB
/
rgbled.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import math, time
from machine import Pin
from neopixel import NeoPixel
from apa102 import APA102
def rgb_to_hsv(rgb_color):
"""Converts colors from the RGB color space to the HSV color space.
Parameters
----------
rgb_color : tuple (r, g, b)
Color in the RGB color space
Returns
-------
tuple (h, s, v)
Color in the HSV color space
"""
(r, g, b) = rgb_color
r = float(1 / 255 * r)
g = float(1 / 255 * g)
b = float(1 / 255 * b)
high = max(r, g, b)
low = min(r, g, b)
h, s, v = high, high, high
d = high - low
s = 0 if high == 0 else d/high
if high == low:
h = 0.0
else:
h = {
r: (g - b) / d + (6 if g < b else 0),
g: (b - r) / d + 2,
b: (r - g) / d + 4,
}[high]
h /= 6
return h, s, v
def hsv_to_rgb(hsv_color):
"""Converts colors from the HSV color space to the RGB color space.
Parameters
----------
hsv_color : tuple (h, s, v)
Color in the HSV color space
Returns
-------
tuple (r, g, b)
Color in the RGB color space
"""
(h, s, v) = hsv_color
i = math.floor(h*6)
f = h*6 - i
p = v * (1-s)
q = v * (1-f*s)
t = v * (1-(1-f)*s)
r, g, b = [
(v, t, p),
(q, v, p),
(p, v, t),
(p, q, v),
(t, p, v),
(v, p, q),
][int(i%6)]
r = int(255 * r)
g = int(255 * g)
b = int(255 * b)
return r, g, b
def hex_to_rgb(self, hex):
""" Returns the RGB value of a Hex Colour
Parameters
----------
hex : string
The hex representation of a RGB color
Example:
hex_to_rgb("123456")
"""
return tuple(int(hex[i:i+2], 16) for i in (0, 2, 4))
COLOR_BLACK = (0, 0, 0)
COLOR_RED = (255, 0, 0)
COLOR_GREEN = (0, 255, 0)
COLOR_BLUE = (0, 0, 255)
COLOR_CYAN = (0, 255, 255)
COLOR_MAGENTA = (255, 0, 255)
COLOR_YELLOW = (255, 255, 0)
class RgbLed():
'''LED agnostic RGB LED driver'''
def __init__(self, count, data, clock=None, apa102c=False):
"""Initializes the LED driver
Parameters
----------
count : int
Required
Number of LEDs in the LED strip/matrix
data : int
Required
Pin of the Data Line
clock : int
Optional
Pin for the clock Line.
Used only by the APA102
apa102c : bool
Optional
Used only by the APA102C to reorder the color sequence
"""
data_pin = Pin(data, Pin.OUT)
self.neopixel = True
if clock is None:
self.leds = NeoPixel(data_pin, count)
else:
self.neopixel = False
clock_pin = Pin(clock, Pin.OUT)
self.leds = APA102(data_pin, clock_pin, count)
if apa102c:
self.leds.ORDER = (0, 2, 1, 3)
self.count = count
def _set_led(self, color, intensity, pos):
"""Internal method to set individual LEDs to a coloir
Parameters
----------
color : tuple (r, g, b) or string
Required
Color of the LED in the RGB color space
Either as (r, g, b) tuple or as a Hexadecimal string 'AA55CC'
intensity : float
Required
Intensity of the color/LED from 0 to 1
pos : int
Required
Position of the LED in the LED strip/matrix
"""
if type(color) is str:
color = hex_to_rgb(color)
if self.neopixel:
if intensity < 1:
h, s, _ = rgb_to_hsv(color)
v = intensity
color = hsv_to_rgb((h, s, v))
else:
intensity = math.ceil(intensity * 31)
(r, g, b) = color
color = (r, g, b, intensity)
self.leds[pos] = color
def _color_wheel(self, pos, intensity=1.0):
"""Short summary.
Parameters
----------
pos : int
Required
Position of the color in the HSV color wheel
from 0 to 360 degrees
intensity : float
Intensity of the color/LED from 0 to 1
Returns
-------
tuple (r, g, b)
Color in the RGB color space
"""
if pos > 360:
pos = pos - 360
h = pos / 360
return hsv_to_rgb((h, 1, intensity))
def clear(self):
"""Clears all LEDs and sets them to Black"""
for n in range(self.count):
self._set_led(COLOR_BLACK, 0, n)
self.leds.write()
def set_led(self, color, intensity=1.0, pos=None):
"""Set one or all LEDs to a color.
Parameters
----------
color : tuple (r, g, b) or string
Required
Color of the LED in the RGB color space
Either as (r, g, b) tuple or as a Hexadecimal string 'AA55CC'
intensity : float
Intensity of the color/LED from 0 to 1
pos : int
Position of the LED in the LED strip/matrix
"""
if pos:
self._set_led(color, intensity, pos)
else:
for n in range(self.count):
self._set_led(color, intensity, n)
self.leds.write()
def color_cycle(self, wait=10, repeats=4, intensity=1.0):
"""Cycles all LEDs through the HSV color space
Parameters
----------
wait : int
How long to wait between refreshing the LEDs
repeats : int
How many times the cycle is looped
intensity : float
Intensity of the color/LED from 0 to 1
"""
for i in range(repeats):
for j in range(360):
for k in range(self.count):
pos = j + 10 * k
self._set_led(self._color_wheel(pos), intensity, k)
self.leds.write()
time.sleep_ms(wait)
def cycle(self, color, intensity=1.0, wait=10, repeats=4, invert=False):
"""Cycles a single LED down the LED strip/matrix. The LED is either
turned off (color black), while all other LEDs are set to a color
or all LEDs are turned off with only one color.add()
Parameters
----------
color : tuple (r, g, b) or string
Required
Color of the LED in the RGB color space
Either as (r, g, b) tuple or as a Hexadecimal string 'AA55CC'
intensity : float
Intensity of the color/LED from 0 to 1
active : int
Number of LEDs that cycle
wait : int
How long to wait between refreshing the LEDs
repeats : int
How many times the cycle is looped
invert : bool
Inverts the display
"""
for i in range(repeats * self.count):
for j in range(self.count):
if invert:
self._set_led(color, intensity, j)
else:
self._set_led(COLOR_BLACK, 0, j)
if invert:
self._set_led(COLOR_BLACK, 0, i % self.count)
else:
self._set_led(color, intensity, i % self.count)
self.leds.write()
time.sleep_ms(wait)
def fade(self, color, wait=10, repeats=4, pos=None, fadein=True, fadeout=True):
"""Fades one or more LEDs in and out.
Parameters
----------
color : tuple (r, g, b) or string
Required
Color of the LED in the RGB color space
Either as (r, g, b) tuple or as a Hexadecimal string 'AA55CC'
active : int
Number of LEDs that cycle
wait : int
How long to wait between refreshing the LEDs
repeats : int
How many times the cycle is looped
pos : int
Position of the LED in the LED strip/matrix
fadein : bool
Should the LED fade in
fadeout : bool
Should the LED fade out
"""
for i in range(repeats):
if fadein:
for j in range(32):
intensity = 1 / 32 * j
self.set_led(color, intensity, pos)
time.sleep_ms(wait)
if fadeout:
for j in range(32):
intensity = 1 - (1 / 32 * j)
self.set_led(color, intensity, pos)
time.sleep_ms(wait)
self.clear()