forked from kevinmcaleer/pideck
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkey.py
More file actions
411 lines (374 loc) · 13.8 KB
/
key.py
File metadata and controls
411 lines (374 loc) · 13.8 KB
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
from adafruit_hid.keycode import Keycode
from time import sleep
pulse_timing = 10 # Default timing.
def convert_hex_to_rgb(value):
""" Converts a hex value to RGB values """
value = value.lstrip('#')
lv = len(value)
return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
def convert_rgb_to_hex(r:int,g:int,b:int):
""" Converts RGB values into a Hex string """
hex = "{:02x}{:02x}{:02x}".format(r,g,b).upper()
return hex
class Key():
""" Models a single Key on the keypad """
_off = ""
_on = ""
_effect = "" # pulse
_command = ""
_button_type = "" # press, toggle
_pulse_count = pulse_timing # initialize to the pulse timing
_pulse_timing = pulse_timing
_pulse_up = False
_toggle = False
_repeatable = False
def __init__(self):
self._type = "press"
@property
def command(self):
""" Gets the current keystrokes to be sent on keypress """
return self._command
@command.setter
def command(self, value):
""" Sets the current keystrokes to be sent on keypress """
self._command = value
@property
def on(self):
""" Gets the current colour value for the button when its pressed """
return self._on
@on.setter
def on(self, value):
""" Sets the current colour value for the button when its pressed """
self._on = value
@property
def off(self):
""" Gets the current colour value for the button when its not pressed """
return self._off
@off.setter
def off(self, value):
""" Sets the current colour value for the button when its not pressed """
self._off = value
@property
def effect(self):
""" Gets the button effect (pulse) """
return self._effect
@effect.setter
def effect(self, value):
""" Sets the button effect (pulse, flash, none) """
if value in ["pulse","flash","none"]:
self._effect = value
else:
print(f"{value} is not a valid effect type")
@property
def pulse_timing(self):
""" Gets the button color change timing """
return self._pulse_timing
@pulse_timing.setter
def pulse_timing(self, value):
""" Sets the button color change timing """
if value is not None and value.isdigit() and int(value) > 0:
self._pulse_timing = int(value)
self._pulse_count = self._pulse_timing
@property
def button_type(self):
""" Gets the button type (toggle or press) """
return self._button_type
@button_type.setter
def button_type(self, value:str):
""" Sets the button type (toggle or press) """
if value in ["press","toggle"]:
self._button_type = value
else:
print("not a valid button_type")
@property
def repeatable(self):
""" Gets the setting if the command can be repeated on the same press """
return self._repeatable
@repeatable.setter
def repeatable(self, value:bool):
""" Sets if the command can be repeated on the same press """
if value.lower() == "true":
self._repeatable = True
@property
def toggle(self)->bool:
""" Gets the current toggle state (True or False) and flips the state """
if self._toggle:
self._toggle = False
return True
else:
self._toggle = True
return False
@property
def toggle_value(self):
""" Gets the current toggle state """
return self._toggle
def fade_colour(self, percent:float):
'''assumes color is rgb between (0, 0, 0) and (255, 255, 255)'''
# print (f"on: {self._on}, off: {self.off}, percent: {percent}")
color_from_r, color_from_g, color_from_b = convert_hex_to_rgb(self._off)
color_to_r, color_to_g, color_to_b = convert_hex_to_rgb(self._on)
r_vector = color_from_r - color_to_r
g_vector = color_from_g - color_to_g
b_vector = color_from_b - color_to_b
r = int(color_to_r + r_vector * percent)
g = int(color_to_g + g_vector * percent)
b = int(color_to_b + b_vector * percent)
# return the colours
return convert_rgb_to_hex(r,g,b)
def pulse_tick(self):
""" cycles the pulse animation through one step """
if self._pulse_up:
if self._pulse_count < self._pulse_timing:
self._pulse_count += 1
else:
self._pulse_up = False
else:
if self._pulse_count > 0:
self._pulse_count -= 1
else:
self._pulse_up = True
# print(f"pulse_count: {self._pulse_count}")
return self.fade_colour(self._pulse_count/self._pulse_timing)
def flash_tick(self):
if self._pulse_count < self._pulse_timing:
self._pulse_count += 1
else:
self._pulse_count = 0
if self._pulse_up:
self._pulse_up = False
else:
self._pulse_up = True
if self._pulse_up:
return self._on
else:
return self._off
def send(self, keyb):
""" Sends the current command to the attached computer """
# split the string into separate strings
keys = self._command.upper().replace("+"," ").split()
for command in keys:
if command in ["CTRL", "CONTROL"]:
keyb.press(Keycode.CONTROL)
elif command == "SHIFT":
keyb.press(Keycode.SHIFT)
elif command in ["OPT", "OPTION", "ALT"]:
keyb.press(Keycode.OPTION)
elif command in ["CMD", "COMMAND"]:
keyb.press(Keycode.COMMAND)
elif command == "PRINT_SCREEN":
keyb.press(Keycode.PRINT_SCREEN)
keyb.release_all()
elif command == "A":
keyb.press(Keycode.A)
keyb.release_all()
elif command == "B":
keyb.press(Keycode.B)
keyb.release_all()
elif command == "C":
keyb.press(Keycode.C)
keyb.release_all()
elif command == "D":
keyb.press(Keycode.D)
keyb.release_all()
elif command == "E":
keyb.press(Keycode.E)
keyb.release_all()
elif command == "F":
keyb.press(Keycode.F)
keyb.release_all()
elif command == "G":
keyb.press(Keycode.G)
keyb.release_all()
elif command == "H":
keyb.press(Keycode.H)
keyb.release_all()
elif command == "I":
keyb.press(Keycode.I)
keyb.release_all()
elif command == "J":
keyb.press(Keycode.J)
keyb.release_all()
elif command == "K":
keyb.press(Keycode.K)
keyb.release_all()
elif command == "L":
keyb.press(Keycode.L)
keyb.release_all()
elif command == "M":
keyb.press(Keycode.M)
keyb.release_all()
elif command == "N":
keyb.press(Keycode.N)
keyb.release_all()
elif command == "O":
keyb.press(Keycode.O)
keyb.release_all()
elif command == "P":
keyb.press(Keycode.P)
keyb.release_all()
elif command == "Q":
keyb.press(Keycode.Q)
keyb.release_all()
elif command == "R":
keyb.press(Keycode.R)
keyb.release_all()
elif command == "S":
keyb.press(Keycode.S)
keyb.release_all()
elif command == "T":
keyb.press(Keycode.T)
keyb.release_all()
elif command == "U":
keyb.press(Keycode.U)
keyb.release_all()
elif command == "V":
keyb.press(Keycode.V)
keyb.release_all()
elif command == "W":
keyb.press(Keycode.W)
keyb.release_all()
elif command == "X":
keyb.press(Keycode.X)
keyb.release_all()
elif command == "Y":
keyb.press(Keycode.Y)
keyb.release_all()
elif command == "Z":
keyb.press(Keycode.Z)
keyb.release_all()
elif command == "1":
keyb.press(Keycode.ONE)
keyb.release_all()
elif command == "2":
keyb.press(Keycode.TWO)
keyb.release_all()
elif command == "3":
keyb.press(Keycode.THREE)
keyb.release_all()
elif command == "4":
keyb.press(Keycode.FOUR)
keyb.release_all()
elif command == "5":
keyb.press(Keycode.FIVE)
keyb.release_all()
elif command == "6":
keyb.press(Keycode.SIX)
keyb.release_all()
elif command == "7":
keyb.press(Keycode.SEVEN)
keyb.release_all()
elif command == "8":
keyb.press(Keycode.EIGHT)
keyb.release_all()
elif command == "9":
keyb.press(Keycode.NINE)
keyb.release_all()
elif command == "0":
keyb.press(Keycode.ZERO)
keyb.release_all()
elif command in ["[","{"]:
keyb.press(Keycode.LEFT_BRACKET)
keyb.release_all()
elif command in ["]","}"]:
keyb.press(Keycode.RIGHT_BRACKET)
keyb.release_all()
elif command == "TAB":
keyb.press(Keycode.TAB)
keyb.release_all()
elif command == "MINUS":
keyb.press(Keycode.KEYPAD_MINUS)
keyb.release_all()
elif command == "PLUS":
keyb.press(Keycode.KEYPAD_PLUS)
keyb.release_all()
elif command == "EQUALS":
keyb.press(Keycode.EQUALS)
keyb.release_all()
elif command in ["ESC", "ECSCAPE"]:
keyb.press(Keycode.ESCAPE)
keyb.release_all()
elif command == "SPACE":
keyb.press(Keycode.SPACE)
keyb.release_all()
elif command in [".", "PERIOD"]:
keyb.press(Keycode.PERIOD)
keyb.release_all()
elif command in [",", "COMMA"]:
keyb.press(Keycode.COMMA)
keyb.release_all()
elif command in [";", "SEMICOLON"]:
keyb.press(Keycode.SEMICOLON)
keyb.release_all()
elif command == "COLON":
keyb.press(Keycode.SHIFT)
keyb.press(Keycode.SEMICOLON)
keyb.release_all()
elif command in ["\\", "BACKSLASH"]:
keyb.press(Keycode.BACKSLASH)
keyb.release_all()
elif command in ["/", "FORWARD_SLASH"]:
keyb.press(Keycode.FORWARD_SLASH)
keyb.release_all()
elif command in ["LEFT", "LEFT_ARROW"]:
keyb.press(Keycode.LEFT_ARROW)
keyb.release_all()
elif command in ["RIGHT", "RIGHT_ARROW"]:
keyb.press(Keycode.RIGHT_ARROW)
keyb.release_all()
elif command in ["UP", "UP_ARROW"]:
keyb.press(Keycode.UP_ARROW)
keyb.release_all()
elif command in ["DOWN", "DOWN_ARROW"]:
keyb.press(Keycode.DOWN_ARROW)
keyb.release_all()
elif command == "F1":
keyb.press(Keycode.F1)
keyb.release_all()
elif command == "F2":
keyb.press(Keycode.F2)
keyb.release_all()
elif command == "F3":
keyb.press(Keycode.F3)
keyb.release_all()
elif command == "F4":
keyb.press(Keycode.F4)
keyb.release_all()
elif command == "F5":
keyb.press(Keycode.F5)
keyb.release_all()
elif command == "F6":
keyb.press(Keycode.F6)
keyb.release_all()
elif command == "F7":
keyb.press(Keycode.F7)
keyb.release_all()
elif command == "F8":
keyb.press(Keycode.F8)
keyb.release_all()
elif command == "F9":
keyb.press(Keycode.F9)
keyb.release_all()
elif command == "F10":
keyb.press(Keycode.F10)
keyb.release_all()
elif command == "F11":
keyb.press(Keycode.F11)
keyb.release_all()
elif command == "F12":
keyb.press(Keycode.F12)
keyb.release_all()
elif command in ["RETURN", "ENTER"]:
keyb.press(Keycode.ENTER)
keyb.release_all()
elif command == "THUMBS-UP":
keyb.press(Keycode.SHIFT)
keyb.press(Keycode.SEMICOLON)
keyb.release_all()
keyb.press(Keycode.KEYPAD_PLUS)
keyb.release_all()
keyb.press(Keycode.ONE)
keyb.release_all()
keyb.press(Keycode.SHIFT)
keyb.press(Keycode.SEMICOLON)
keyb.release_all()
keyb.release_all()