-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathpixels.py
More file actions
78 lines (62 loc) · 2.22 KB
/
pixels.py
File metadata and controls
78 lines (62 loc) · 2.22 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
from commands import Sleep
class PixelListener:
BRIGHTNESS = 0.3
MAX_LEDS = 12
sleeping = False
def __init__(self, macropad):
self.pixels = macropad.pixels
self.pixels.auto_write = False
self.pixels.brightness = PixelListener.BRIGHTNESS
self.keycolors = []
self.sleeping = False
def __del__(self):
self.pixels.clear()
def initialize(self):
self.keycolors = []
for i in range(PixelListener.MAX_LEDS):
if i < len(self.keycolors):
self.pixels[i] = self.keycolors[i]
else:
self.pixels[i] = 0x000000
self.pixels.show()
def register(self, keys):
self.keycolors = list(map(lambda k: k.color, keys))
for i in range(PixelListener.MAX_LEDS):
if i < len(self.keycolors):
self.pixels[i] = self.keycolors[i]
else:
self.pixels[i] = 0x000000
self.pixels.show()
def pressed(self, keys, index):
# Ignore any button presses until we wake up
if self.sleeping: return
self.highlight(index)
commands = keys[index].commands
if not commands: return
if isinstance(commands[0], Sleep):
self.sleep()
def released(self, keys, index):
self.reset(index)
commands = keys[index].commands
if not commands: return
if isinstance(commands[0], Sleep):
self.resume()
def sleep(self):
if self.sleeping: return
self.pixels.brightness = 0.0
self.pixels.show()
self.sleeping = True
def resume(self):
if not self.sleeping: return
self.pixels.brightness = PixelListener.BRIGHTNESS
self.pixels.show()
self.sleeping = False
def highlight(self, key_index, color=0xFFFFFF):
if key_index >= PixelListener.MAX_LEDS: return
self.pixels[key_index] = color
self.pixels.show()
def reset(self, key_index):
if key_index >= PixelListener.MAX_LEDS: return
self.pixels[key_index] = self.keycolors[key_index] if key_index < len(self.keycolors) else 0x000000
self.pixels.brightness = PixelListener.BRIGHTNESS
self.pixels.show()