-
Notifications
You must be signed in to change notification settings - Fork 4
/
controls.py
160 lines (136 loc) · 5.55 KB
/
controls.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
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
from adafruit_midi import control_change_values
DETUNE = 94
controls = [
('Attack', control_change_values.ATTACK_TIME), ('Release', control_change_values.RELEASE_TIME), ('Brightness', control_change_values.CUTOFF_FREQUENCY),
('Timbre', control_change_values.FILTER_RESONANCE), ('TimePortamento', control_change_values.PORTAMENTO_TIME), ('CtlPortamento', control_change_values.PORTAMENTO),
('ChorusSend', control_change_values.CHORUS), ('PanL/R', control_change_values.PAN), ('Volume', control_change_values.VOLUME),
('Arpeggio', None), ('Celeste', DETUNE), ('Velocity', None)
]
class Controls:
def __init__(self, macropad):
self.settings = Settings()
self.macropad = macropad
self.display = Display(macropad, self.settings.display['brightness'])
self.pixels = Pixels(macropad, self.settings.display['brightness'])
self.pressed = None
def send_controls(self, channel=None):
for control in controls:
self.send_control(control, channel=channel)
def send_control(self, control, channel=None):
name, control_number = control
if control_number is not None:
value = self.settings.midi[name]
self.macropad.midi.send(self.macropad.ControlChange(control_number, value, channel=channel))
def refresh(self):
self.display.reload()
self.pixels.refresh()
def keypad_events(self, events):
for event in events:
if event.pressed:
self.pressed = event.key_number
name, _ = controls[event.key_number]
self.display.adjust(event.key_number, self.settings.midi[name])
self.pixels.press(event.key_number)
else: # event.released
if event.key_number == self.pressed: self.pressed = None
if not self.pressed: self.display.reload()
self.pixels.release(event.key_number)
def rotate_event(self, encoder_position, encoder_last_position, encoder_switch):
if self.pressed is not None:
name, number = controls[self.pressed]
delta = encoder_position - encoder_last_position
value = (self.settings.midi[name] + delta) % 128
self.settings.midi[name] = value
self.display.adjust(self.pressed, value)
def sleep_event(self):
self.pixels.sleep()
self.display.sleep()
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, -2),
anchor_point=(0.5, 0.0)
)
)
def adjust(self, key, control_value):
control_name, _ = controls[key]
self.group[13].anchored_position=(5, -2)
self.group[13].anchor_point=(0, 0)
self.group[13].text = "%s: %d" % (control_name, control_value)
self.display.show(self.group)
self.display.refresh()
def reload(self):
self.display.auto_refresh = False
self.display.brightness = self.scaled_brightness
self.group[13].anchored_position=(self.display.width//2, -1)
self.group[13].anchor_point=(0.5, 0.0)
self.group[13].text = 'MIDI Controls'
for i in range(12):
control_name, _ = controls[i]
self.group[i].text = control_name[:6]
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()
SEGMENT_SIZE = 255 // 7
SUBSEGMENT_SIZE = SEGMENT_SIZE // 3
class Pixels:
def __init__(self, macropad, brightness):
self.pixels = macropad.pixels
self.brightness = brightness
self.default_color = 0x0F0F0F
self.highlight_color = 0xFFFFFF
def refresh(self):
self.pixels.auto_write = False
self.pixels.brightness = self.brightness
self.palette = [self.default_color for i in range(12)]
self.reset()
def press(self, key):
self.palette[key] = self.highlight_color
self.reset()
def release(self, key):
self.palette[key] = self.default_color
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()