-
Notifications
You must be signed in to change notification settings - Fork 4
/
drums.py
164 lines (139 loc) · 5.27 KB
/
drums.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
import displayio
import terminalio
from settings import Settings
from adafruit_display_text import label
from adafruit_display_shapes.rect import Rect
from rainbowio import colorwheel
class Drums:
def __init__(self, macropad):
self.settings = Settings()
self.display = Display(macropad, self.settings.display['brightness'])
self.pixels = Pixels(macropad, self.settings.display['brightness'])
self.macropad = macropad
self.kits = DrumKits(self.settings.drums)
self.active_notes = [None for i in range(12)]
self.channel = self.settings.drums['channel']
def refresh(self):
self.display.refresh()
self.pixels.refresh()
name, kit = self.kits.get()
self.display.set_kit(name, kit)
self.pixels.set_kit(kit)
def keypad_events(self, events):
note_velocity = self.settings.midi['Velocity']
_, kit = self.kits.get()
for event in events:
if event.pressed:
row = event.key_number // 3
column = event.key_number % 3
if row < len(kit) and column < len(kit[row]):
color, _, note = kit[row][column]
self.macropad.midi.send(self.macropad.NoteOn(note, note_velocity, channel=self.channel))
self.active_notes[event.key_number] = note
else: # event.released
note = self.active_notes[event.key_number]
self.macropad.midi.send(self.macropad.NoteOff(note, note_velocity, channel=self.channel))
self.active_notes[event.key_number] = None
self.pixels.set_playing(self.active_notes)
def rotate_event(self, encoder_position, encoder_last_position, encoder_switch):
change = encoder_position - encoder_last_position
name, kit = self.kits.prev() if change < 0 else self.kits.next()
self.display.set_kit(name, kit)
self.pixels.set_kit(kit)
def sleep_event(self):
self.pixels.sleep()
self.display.sleep()
class DrumKits:
def __init__(self, config):
self.index = 0
self.kits = config['kits']
def get(self):
return self.kits[self.index]
def next(self):
self.index = (self.index + 1) % len(self.kits)
return self.get()
def prev(self):
self.index = self.index - 1 if self.index > 0 else len(self.kits) - 1
return self.get()
class Display:
def __init__(self, macropad, brightness):
self.display = macropad.display
self.scaled_brightness = 0.5 + (brightness * 0.5)
self.group = displayio.Group()
for key_index in range(12):
x = key_index % 3
y = key_index // 3
self.group.append(
label.Label(terminalio.FONT,
text='',
color=0xFFFFFF,
anchored_position=((self.display.width - 1) * x / 2,
self.display.height - 1 - (3 - y) * 12),
anchor_point=(x / 2, 1.0)
)
)
self.group.append(Rect(0, 0, self.display.width, 12, fill=0xFFFFFF))
self.group.append(
label.Label(
terminalio.FONT,
text='',
color=0x000000,
anchored_position=(self.display.width//2, -1),
anchor_point=(0.5, 0.0)
)
)
def refresh(self):
self.display.auto_refresh = False
self.display.brightness = self.scaled_brightness
self.display.show(self.group)
self.display.refresh()
def wake(self):
if self.display.brightness <= 0:
self.display.brightness = self.scaled_brightness
self.display.refresh()
def sleep(self):
self.display.brightness = 0
self.display.refresh()
def set_kit(self, name, kit):
self.group[13].text = name
for i in range(12):
row = i // 3
column = i % 3
if row < len(kit) and column < len(kit[row]):
_, self.group[i].text, _ = kit[row][column]
self.refresh()
SEGMENT_SIZE = 255 // 7
SUBSEGMENT_SIZE = SEGMENT_SIZE // 3
class Pixels:
def __init__(self, macropad, brightness):
self.pixels = macropad.pixels
self.brightness = brightness
self.palette = [0x0F0F0F for i in range(12)]
def refresh(self):
self.pixels.auto_write = False
self.pixels.brightness = self.brightness
self.reset()
def wake(self):
if self.pixels.brightness <= 0:
self.pixels.brightness = self.brightness
self.pixels.show()
def sleep(self):
self.pixels.brightness = 0
self.pixels.show()
def reset(self):
self.wake()
for index in range(12):
self.pixels[index] = self.palette[index]
self.pixels.show()
def set_kit(self, kit):
for i in range(12):
row = i // 3
column = i % 3
if row < len(kit) and column < len(kit[row]):
self.palette[i], _, _ = kit[row][column]
self.reset()
def set_playing(self, active_notes):
self.wake()
for index in range(12):
self.pixels[index] = 0xFFFFFF if active_notes[index] else self.palette[index]
self.pixels.show()