-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
207 lines (178 loc) · 5.85 KB
/
code.py
File metadata and controls
207 lines (178 loc) · 5.85 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
import time
import board
import busio
import display
import config
from deej import Deej
from macropad import (
HIDType as hid,
ButtonInputType as BiT,
Button,
SplitRotaryEncoder,
ButtonMatrix,
)
from adafruit_pcf8574 import PCF8574
from adafruit_hid.keycode import Keycode as key
from rp2pio_dualincrementalencoder import DualIncrementalEncoder
from adafruit_hid.consumer_control_code import ConsumerControlCode as cc_code
DEEJ = Deej(config.DEEJ_PROGRAMS)
SCREEN = display.DisplayScreen(
pin_clock=config.DISPLAY_PINS["CLOCK"],
pin_mosi=config.DISPLAY_PINS["MOSI"],
pin_cs=config.DISPLAY_PINS["CS"],
pin_dc=config.DISPLAY_PINS["DC"],
pin_reset=config.DISPLAY_PINS["RESET"],
pin_bl=config.DISPLAY_PINS["BL"],
rotation=config.DISPLAY_PINS["ROTATION"],
)
ENCODERS = [
{ # Encoder 1
"actions": (
lambda: SCREEN.change_brightness(-1),
lambda: SCREEN.change_brightness(+1),
),
"button": {"pin": 1, "actions": (BiT.KEY, [key.ONE])},
},
{ # Encoder 2
"actions": (
lambda: DEEJ.change_volume(-5),
lambda: DEEJ.change_volume(+5),
),
"button": {"pin": 0, "actions": (BiT.KEY, [key.TWO])},
},
{ # Encoder 3
"actions": (lambda: hid.MOUSE.move(wheel=-1), lambda: hid.MOUSE.move(wheel=1)),
"button": {"pin": 3, "actions": (BiT.KEY, [key.THREE])},
},
{ # Encoder 4
"actions": (
lambda: hid.KBD.send(key.CONTROL, key.LEFT_BRACKET),
lambda: hid.KBD.send(key.CONTROL, key.RIGHT_BRACKET),
),
"button": {"pin": 2, "actions": (BiT.KEY, [key.FOUR])},
},
{ # Encoder 5
"actions": (
lambda: hid.KBD.send(key.CONTROL, key.Z),
lambda: hid.KBD.send(key.CONTROL, key.SHIFT, key.Z),
),
"button": {"pin": 4, "actions": (BiT.KEY, [key.FIVE])},
},
{ # Encoder 6
"actions": (
lambda: hid.KBD.send(key.I),
lambda: hid.KBD.send(key.J),
),
"button": {"pin": 5, "actions": (BiT.KEY, [key.SIX])},
},
]
KEYPADS = [
[ # Row 1
(BiT.CUSTOM, lambda: DEEJ.cycle_programs(-1)),
(BiT.MEDIA, cc_code.SCAN_PREVIOUS_TRACK),
(BiT.MEDIA, cc_code.PLAY_PAUSE),
(BiT.MEDIA, cc_code.SCAN_NEXT_TRACK),
(BiT.CUSTOM, lambda: DEEJ.cycle_programs(+1)),
],
[ # Row 2
(BiT.KEY, [key.F]),
(BiT.KEY, [key.G]),
(BiT.KEY, [key.H]),
(BiT.KEY, [key.I]),
(BiT.KEY, [key.J]),
],
[ # Row 3
(BiT.KEY, [key.K]),
(BiT.KEY, [key.L]),
(BiT.KEY, [key.M]),
(BiT.KEY, [key.N]),
(BiT.KEY, [key.O]),
],
[ # Row 4
(BiT.KEY, [key.P]),
(BiT.KEY, [key.Q]),
(BiT.KEY, [key.R]),
(BiT.KEY, [key.S]),
(BiT.KEY, [key.T]),
],
]
def init_expander(scl, sda):
i2c = busio.I2C(scl, sda)
# Wait for I2C to be ready
while not i2c.try_lock():
pass
i2c.unlock()
return i2c
def init_encoders(encoders_num):
"""This loop is for looping through encoders pin and .
So if there's 6 rotary encoders, encoders_num = 6
(0, 6 * 2, 4) mean we start from 0, and since each encoder have 2 pins, multiplied by 2.
DualIncrementalEncoder require 4 pins so we split it by 4
and it will append to dual_encoders as 3 object of DualIncrementalEncoder
"""
dual_encoders = []
for i in range(0, encoders_num * 2, 4):
pins = [getattr(board, f"GP{j}") for j in range(i, i + 4)]
init_dual_encoder = DualIncrementalEncoder(*pins)
dual_encoders.append(init_dual_encoder)
""" After we get DualIncrementalEncoders we need to get value of each encoder and
apply action of the encoders
"""
encoders = []
for i in range(encoders_num):
group = i // 2 # integer division groups every two items
local_index = i % 2 # 0 or 1
split_encoder = SplitRotaryEncoder(
name=f"Encoder {i}",
encoder=dual_encoders[group],
index=local_index,
actions=ENCODERS[config.ROTARY_ENCODERS_PHYSICAL_ORDER[i] - 1]["actions"],
)
encoders.append(split_encoder)
return encoders
def init_button_encoders(i2c):
expander = PCF8574(i2c, address=config.BUTTON_IO_EXPANDER_ADDRESS)
buttons = []
for encoder in ENCODERS:
button_pin = expander.get_pin(encoder["button"]["pin"])
button = Button(pin_button=button_pin, actions=encoder["button"]["actions"])
buttons.append(button)
return buttons
def init_volumes_label():
labels = []
for i in range(DEEJ.num_programs):
y = 50 + (i - 1) * 25
text = f"{DEEJ.volumes[i]} - {DEEJ.programs[i]}"
label = SCREEN.label(text, x=15, y=y)
labels.append(label)
return labels
def main():
i2c = init_expander(
scl=config.IO_EXPANDER_PINS["SCL"], sda=config.IO_EXPANDER_PINS["SDA"]
)
encoders = init_encoders(config.ROTARY_ENCODERS_NUM)
encoder_buttons = init_button_encoders(i2c)
keypad = ButtonMatrix(
actions=KEYPADS,
expander=PCF8574(i2c, address=config.MATRIX_IO_EXPANDER_ADDRESS),
rows=config.MATRIX_ROW_PINS,
columns=config.MATRIX_COL_PINS,
)
if config.USE_DEEJ:
labels = init_volumes_label()
slideshow = display.Slideshow(SCREEN.images_group)
# gif = display.PlayGif("media/mon.gif", SCREEN.gif_group, SCREEN.display)
SCREEN.show_screen()
while True:
for encoder in encoders:
turned = encoder.encoder_action()
if turned and config.USE_DEEJ:
labels[DEEJ.current].text = DEEJ.display
for button in encoder_buttons:
button.button_action()
keypad.matrix_scanning()
slideshow.update()
# gif.update_gif()
time.sleep(0.01)
if __name__ == "__main__":
main()