-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrgbkeypad.py
427 lines (330 loc) · 11.1 KB
/
rgbkeypad.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
MICROPYTHON = True
try:
import machine
PIN_SDA = 4
PIN_SCL = 5
PIN_CS = 17
PIN_SCK = 18
PIN_MOSI = 19
except ImportError:
MICROPYTHON = False
CIRCUITPYTHON = True
try:
import board
import busio
import digitalio
PIN_SDA = board.GP4
PIN_SCL = board.GP5
PIN_CS = board.GP17
PIN_SCK = board.GP18
PIN_MOSI = board.GP19
except ImportError:
CIRCUITPYTHON = False
if not MICROPYTHON and not CIRCUITPYTHON:
raise Exception("Could not find MicroPython or CircuitPython.")
class RGBKeypad():
KEYPAD_ADDRESS = 32
class MPDevice():
def __init__(self):
"""
Internal class used to communicate with the keypad device when
using MicroPython.
"""
# setup i2c
self._i2c = machine.I2C(
0,
scl=machine.Pin(PIN_SCL),
sda=machine.Pin(PIN_SDA),
freq=400000
)
# setup spi
self._spi = machine.SPI(
0,
baudrate=4*1024*1024,
sck=machine.Pin(PIN_SCK),
mosi=machine.Pin(PIN_MOSI)
)
# setup cs
self._cs = machine.Pin(
PIN_CS,
machine.Pin.OUT
)
self._cs.high()
def read_keys(self):
self._i2c.writeto(RGBKeypad.KEYPAD_ADDRESS, bytearray(1), True)
data = self._i2c.readfrom(RGBKeypad.KEYPAD_ADDRESS, 2, False)
return data
def write_leds(self, led_data):
self._cs.low()
self._spi.write(led_data)
self._cs.high()
class CPDevice():
def __init__(self):
"""
Internal class used to communicate with the keypad device when
using CircuitPython.
"""
PIN_SDA = board.GP4
PIN_SCL = board.GP5
PIN_CS = board.GP17
PIN_SCK = board.GP18
PIN_MOSI = board.GP19
# setup i2c
self._i2c = busio.I2C(
scl=PIN_SCL,
sda=PIN_SDA,
frequency=400000
)
self._i2c.try_lock()
# setup spi
self._spi = busio.SPI(
clock=PIN_SCK,
MOSI=PIN_MOSI
)
self._spi.try_lock()
self._spi.configure(baudrate=4*1024*1024)
# setup cs
self._cs = digitalio.DigitalInOut(PIN_CS)
self._cs.direction = digitalio.Direction.OUTPUT
self._cs.value = True
def read_keys(self):
self._i2c.writeto(RGBKeypad.KEYPAD_ADDRESS, bytearray(1))
data = bytearray(2)
self._i2c.readfrom_into(RGBKeypad.KEYPAD_ADDRESS, data)
return data
def write_leds(self, led_data):
self._cs.value = False
self._spi.write(led_data)
self._cs.value = True
class RGBKey():
def __init__(self, keypad, x, y, red, green, blue, brightness):
"""
Represents a single key of the RGB Keypad.
Returned when using RGBKeypad[x,y] or RGBKeypad.get_key(x,y)::
keypad = RGBKeypad()
key = keypad[0, 0]
"""
self._keypad = keypad
self._x = x
self._y = y
self._red = red
self._green = green
self._blue = blue
self._brightness = brightness
@property
def x(self):
"""
Returns the x position of the key.
"""
return self._x
@property
def y(self):
"""
Returns the y position of the key.
"""
return self._y
@property
def brightness(self):
"""
Sets or returns the brightness of the key.
A value between 0 and 1 where 0 is off and 1 is full brightness.
"""
return self._brightness
@brightness.setter
def brightness(self, value):
value = max(min(1, value), 0)
self._brightness = value
self._update()
@property
def red(self):
"""
Sets or returns the red color of the key.
A value between 0 and 255.
"""
return self._red
@red.setter
def red(self, value):
value = max(min(255, value), 0)
self._red = value
self._update()
@property
def green(self):
"""
Sets or returns the green color of the key.
A value between 0 and 255.
"""
return self._green
@green.setter
def green(self, value):
value = max(min(255, value), 0)
self._green = value
self._update()
@property
def blue(self):
"""
Sets or returns the blue color of the key.
A value between 0 and 255.
"""
return self._blue
@blue.setter
def blue(self, value):
value = max(min(255, value), 0)
self._blue = value
self._update()
@property
def color(self):
"""
Sets or returns the color of the key.
A tuple of (red, green, blue) values between 0 and 255.
"""
return (self.red, self.green, self.blue)
@color.setter
def color(self, value):
auto_update_value = self._keypad.auto_update
self.red = value[0]
self.green = value[1]
self.blue = value[2]
self.auto_update = self._keypad.auto_update
self._update()
def is_pressed(self):
"""
Returns True if the key is pressed when called.
"""
return self._keypad.get_keys_pressed()[
(4 * self._y) + (self._x % 4)
]
def clear(self):
"""
Clears the key color.
Clear is the same as setting the color to black (0, 0, 0).
"""
self.color = (0, 0, 0)
def _update(self):
if self._keypad.auto_update:
self._keypad.update()
def __init__(self, color=(0,0,0), brightness=0.5, auto_update=True):
"""
Represents a pimoroni RGB Keypad device connected to a Raspberry Pi
pico running either MicroPython or CircuitPython.
::
rgbkeypad = RGBKeyPad()
A single key can be obtained using its x, y coordinate ::
key = rgbkeypad[0, 0]
:param tuple color:
The initial color for all the keys.
A tuple of (red, green, blue) values between 0 and 255.
The default is (0, 0, 0), black or off.
:param int brightness:
The initial brightness of the keys.
A value between 0 and 1 where 0 is off and 1 is full brightness.
The default is 0.5.
:param bool auto_update:
When True, the key pad will be automatically updated
when the color or brightness of a key is changed.
If False, the update() method will need to be called
before any changes are reflected on the keypad.
The default is True
"""
# create the device
if MICROPYTHON:
self._device = RGBKeypad.MPDevice()
else:
self._device = RGBKeypad.CPDevice()
# setup all the keys before setting aut o update
self.auto_update = False
# setup keys
self._keys = []
for y in range(4):
for x in range(4):
self._keys.append(
RGBKeypad.RGBKey(self, x, y, color[0], color[1], color[2], brightness)
)
self.update()
self.auto_update = auto_update
self._color = color
self._brightness = brightness
def clear(self):
"""
Clears all the keys color.
Clear is the same as setting the color to black (0, 0, 0).
"""
self.color = (0, 0, 0)
@property
def color(self):
"""
Sets the color of all the keys as a tuple of (red, green, blue) values
between 0 and 255.
Note - color will return the "default" or "initial" color of the keys.
If an individual key's color has been change this wont be represented.
"""
return self._color
@color.setter
def color(self, value):
auto_update_value = self.auto_update
self.auto_update = False
for key in self._keys:
key.color = (value[0], value[1], value[2])
self.update()
self.auto_update = auto_update_value
@property
def brightness(self):
"""
Sets the brightness of all the keys as a value between 0 and 1 where
0 is off and 1 is full brightness.
Note - brightness will return the "default" or "initial" brightness
of the keys. If an individual key's brightness has been change this
wont be represented.
"""
return self._brightness
@brightness.setter
def brightness(self, value):
auto_update_value = self.auto_update
self.auto_update = False
for key in self._keys:
key.brightness = value
self.update()
self.auto_update = auto_update_value
@property
def keys(self):
"""
Returns a list of all the keys as RGBKey objects.
"""
return self._keys
def get_keys_pressed(self):
"""
Returns a list of 16 booleans represent the current pressed
state of all the keys.
"""
data = self._device.read_keys()
button_data = int.from_bytes(data, "little")
# button states
button_states = []
for button in range(16):
button_states.append(0 == (button_data & (1<<button)))
return button_states
def get_key(self, x, y):
"""
Returns a key as an RGBKey object.
get_key(x, y) is the equivalent of RGBKeypad[x, y].
:param int x:
The x position of the key.
:param int y:
The y position of the key.
"""
return self._keys[(4 * y) + (x % 4)]
def update(self):
"""
Updates the RGB Keypad with the current color and brightness.
Is only required to be called when auto_update = False.
"""
led_data = bytearray((16*4) + 8)
data_pos = 4
for key in self._keys:
led_data[data_pos] = int(255 - 31 + (31 * key.brightness))
led_data[data_pos + 1] = key.blue
led_data[data_pos + 2] = key.green
led_data[data_pos + 3] = key.red
data_pos += 4
self._device.write_leds(led_data)
def __getitem__(self, index):
return self.get_key(index[0], index[1])