Repository navigation
Expand file tree
/
Copy pathMultiPicoBoxV2.py
More file actions
432 lines (378 loc) · 18.2 KB
/
Copy pathMultiPicoBoxV2.py
File metadata and controls
432 lines (378 loc) · 18.2 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# MultiPicoBoxV2 main class in CircuitPython (V9)
# GitHub project : https://github.com/Mick3DIY/MultiPicoBox
# Raspberry Pi Pico : https://www.raspberrypi.com/products/raspberry-pi-pico/
# Documentation, tutorials : https://projects.raspberrypi.org
# CircuitPython : https://learn.adafruit.com/welcome-to-circuitpython
# Thonny IDE : https://thonny.org
# https://docs.circuitpython.org/en/latest/shared-bindings/board/
import board
# https://docs.circuitpython.org/en/latest/shared-bindings/busio/
import busio
# https://docs.circuitpython.org/en/latest/shared-bindings/digitalio/index.html
from digitalio import DigitalInOut, Direction, Pull
# https://docs.circuitpython.org/en/latest/shared-bindings/rotaryio/index.html
from rotaryio import IncrementalEncoder
# https://docs.circuitpython.org/projects/mcp230xx/en/latest/
from adafruit_mcp230xx.mcp23017 import MCP23017
from time import sleep
# --------------------------------------------------------------
class EncoderManager:
"""Sub-class for managing encoders"""
def __init__(self, pin_a, pin_b, pin_sw, name) -> None:
"""Encoder pin_a, pin_b, pin_switch, name"""
self._encoder = IncrementalEncoder(pin_a, pin_b)
self._encoder.position = 0
self._button = DigitalInOut(pin_sw)
self._button.direction = Direction.INPUT
self._button.pull = Pull.UP
self._last_position = self._encoder.position
self._name = str.upper(name)
def get_position(self) -> int:
"""Encoder position"""
return self._encoder.position
def set_last_position(self, value: int):
"""Set encoder last position"""
self._last_position = value
def get_last_position(self) -> int:
"""Encoder last position"""
return self._last_position
def get_button(self) -> DigitalInOut:
"""Encoder button switch"""
return self._button
def get_name(self) -> str:
"""Encoder name"""
return self._name
def __str__(self):
"""Encoder user-friendly detail"""
return f"Encoder: {self._name}"
# --------------------------------------------------------------
class ButtonManager:
"""Sub-class for managing push buttons or momentary switches"""
def __init__(self, pin_p, name) -> None:
"""Push or momentary switch pin, name"""
self._button = DigitalInOut(pin_p)
self._button.direction = Direction.INPUT
self._button.pull = Pull.DOWN
self._name = str.upper(name)
def get_button(self) -> DigitalInOut:
"""Push button, momentary switch themselves"""
return self._button
def get_name(self) -> str:
"""Push button, momentary switch name"""
return self._name
def __str__(self):
"""Push button, momentary switch user-friendly detail"""
return f"Push button/switch: {self._name}"
# --------------------------------------------------------------
class MCPManager:
"""Sub-class for managing the MCP23017 with toggle switches and LEDs"""
def __init__(self, pin_scl, pin_sda, address=0x20) -> None:
"""MCP23017 serial_clock_pin, serial_data_pin, I2C address"""
try:
# Initialize the I2C bus
_i2c = busio.I2C(pin_scl, pin_sda)
# Initialize the MCP23017 communication
self._address = hex(address)
self._mcp = MCP23017(_i2c, address)
except Exception:
self._address = None
print(f"No MCP23017 found at {hex(address)}")
finally:
_i2c.unlock()
def get_address(self) -> str:
"""MCP23017 actual I2C address"""
return f"{self._address}"
def get_toggle(self, pin_1, pin_3, name) -> list[DigitalInOut, DigitalInOut, str]:
"""Toggle switch pin_1, pin_3, name"""
_toggleSwitch = [self._mcp.get_pin(pin_1), self._mcp.get_pin(pin_3)]
for switch in _toggleSwitch:
switch.direction = Direction.INPUT
switch.pull = Pull.UP
_toggleSwitch.append(str.upper(name))
return _toggleSwitch
def get_led(self, pin_2, name) -> list[DigitalInOut, str]:
"""LEDs themselves or MCP outputs pin_2, name"""
_led = [self._mcp.get_pin(pin_2)]
_led[0].direction = Direction.OUTPUT
_led[0].value = False
_led.append(str.upper(name))
return _led
def __str__(self):
"""MCP23017 user-friendly detail"""
return f"MCP23017 address: {self._address}"
# -----------------------------------------------------------------------------
class MultiPicoBoxV2:
"""Main class for the MultiPicoBoxV2 PCB"""
def __init__(self) -> None:
"""Create all objects in the board"""
# Showing messages for debug
self._debug_ = False
# --------------------------------------------------------------
# Rotary encoders with push button (Pico)
# --------------------------------------------------------------
# Rotary encoder SW5 (J5)
self._sw5 = EncoderManager(board.GP0, board.GP1, board.GP2, "SW5")
# Rotary encoder SW6 (J6)
self._sw6 = EncoderManager(board.GP3, board.GP4, board.GP5, "SW6")
# Rotary encoder SW7 (J7)
self._sw7 = EncoderManager(board.GP6, board.GP7, board.GP8, "SW7")
# Rotary encoder SW8 (J8)
self._sw8 = EncoderManager(board.GP9, board.GP10, board.GP11, "SW8")
# Constants for dictionnary, for actions
self.rotary_encoders = {}
for encoder in self._sw5, self._sw6, self._sw7, self._sw8:
# self.C_SWX = "SWX" -> set constants with "SWX" for each encoder
_const = f"C_{encoder.get_name()}"
setattr(self, _const, encoder.get_name())
# self.C_SWX = self.swx -> set index for actions for each encoder
self.rotary_encoders[encoder.get_name()] = encoder
# --------------------------------------------------------------
# Push buttons (Pico)
# --------------------------------------------------------------
# Push button SW9 (J15)
self._sw9 = ButtonManager(board.GP12, "SW9")
# Push button SW10 (J16)
self._sw10 = ButtonManager(board.GP13, "SW10")
# Push button SW11 (J17)
self._sw11 = ButtonManager(board.GP14, "SW11")
# Push button SW12 (J18)
self._sw12 = ButtonManager(board.GP15, "SW12")
# Constants for dictionnary, for actions
self.push_buttons = {}
for pbutton in self._sw9, self._sw10, self._sw11, self._sw12:
_const = f"C_{pbutton.get_name()}"
setattr(self, _const, pbutton.get_name())
self.push_buttons[pbutton.get_name()] = pbutton
# --------------------------------------------------------------
# Momentary switches (Pico)
# --------------------------------------------------------------
# Momentary switch SW13 (J19)
self._sw13 = ButtonManager(board.GP16, "SW13")
# Momentary switch SW14 (J20)
self._sw14 = ButtonManager(board.GP17, "SW14")
# Momentary switch SW15 (J21)
self._sw15 = ButtonManager(board.GP18, "SW15")
# Momentary switch SW16 (J22)
self._sw16 = ButtonManager(board.GP19, "SW16")
# Constants for dictionnary, for actions
self.moment_switches = {}
for mswitch in self._sw13, self._sw14, self._sw15, self._sw16:
_const = f"C_{mswitch.get_name()}"
setattr(self, _const, mswitch.get_name())
self.moment_switches[mswitch.get_name()] = mswitch
# --------------------------------------------------------------
# Pico on-board LED (GPIO25)
# --------------------------------------------------------------
self._ledOnboard = [DigitalInOut(board.GP25)]
self._ledOnboard[0].direction = Direction.OUTPUT
self._ledOnboard[0].value = False
self._ledOnboard.append("ONBOARD")
# --------------------------------------------------------------
# Bus I2C Raspberry Pi Pico <-> MCP23017 (Default address '0x20')
# --------------------------------------------------------------
self._mcp = MCPManager(board.GP21, board.GP20)
# Toggle switch SW1 (J1)
self._tssw1 = self._mcp.get_toggle(8, 9, "TSSW1")
# Toggle switch SW2 (J2)
self._tssw2 = self._mcp.get_toggle(10, 11, "TSSW2")
# Toggle switch SW3 (J3)
self._tssw3 = self._mcp.get_toggle(12, 13, "TSSW3")
# Toggle switch SW4 (J4)
self._tssw4 = self._mcp.get_toggle(14, 0, "TSSW4")
# Constants for dictionnary, for actions
self.toggle_switches = {}
for tswitch in self._tssw1, self._tssw2, self._tssw3, self._tssw4:
_pin1, _pin3, _name = f"{tswitch[2]}_1", f"{tswitch[2]}_3", tswitch[2]
# Set constants with "TSSWX" for pin 1
setattr(self, f"C_{_pin1}", _pin1)
# Set constants with "TSSWX" for pin 3
setattr(self, f"C_{_pin3}", _pin3)
self.toggle_switches[_pin1] = tswitch[0]
self.toggle_switches[_pin3] = tswitch[1]
# --------------------------------------------------------------
# LEDs (MCP)
# --------------------------------------------------------------
# LED D1 (J9)
self._d1 = self._mcp.get_led(1, "D1")
# LED D2 (J10)
self._d2 = self._mcp.get_led(2, "D2")
# LED D3 (J11)
self._d3 = self._mcp.get_led(3, "D3")
# LED D4 (J12)
self._d4 = self._mcp.get_led(4, "D4")
# LED D5 (J13)
self._d5 = self._mcp.get_led(5, "D5")
# LED D6 (J14)
self._d6 = self._mcp.get_led(6, "D6")
# --------------------------------------------------------------
# Free connectors : GPA7, GPB7 are OUTPUT only like LEDs (MCP)
# --------------------------------------------------------------
# GPA7, GPB7 (J24)
self._gpa7 = self._mcp.get_led(7, "GPA7")
self._gpb7 = self._mcp.get_led(15, "GPB7")
# --------------------------------------------------------------
# Free connector : GP22 as a input (Pico)
# --------------------------------------------------------------
# GP22 (J25)
self._sw17 = ButtonManager(board.GP22, "SW17")
# --------------------------------------------------------------
# Internal methods for actions
# --------------------------------------------------------------
def _action_encoders(self, name: str) -> None:
"""Actions for rotary encoder push buttons"""
if name == self.C_SW5:
self._action_debug(f"Rotary encoder pressed n°1 : {name}")
elif name == self.C_SW6:
self._action_debug(f"Rotary encoder pressed n°2 : {name}")
elif name == self.C_SW7:
self._action_debug(f"Rotary encoder pressed n°3 : {name}")
elif name == self.C_SW8:
self._action_debug(f"Rotary encoder pressed n°4 : {name}")
else:
raise ValueError("_action_encoders wrong value")
def _action_pushbuttons(self, name: str) -> None:
"""Actions for push buttons"""
if name == self.C_SW9:
self._action_debug(f"Push button pressed n°1 : {name}")
elif name == self.C_SW10:
self._action_debug(f"Push button pressed n°2 : {name}")
elif name == self.C_SW11:
self._action_debug(f"Push button pressed n°3 : {name}")
elif name == self.C_SW12:
self._action_debug(f"Push button pressed n°4 : {name}")
else:
raise ValueError("_action_pushbuttons wrong value")
def _action_momswitches(self, name: str) -> None:
"""Actions for momentary switches"""
if name == self.C_SW13:
self._action_debug(f"Momentary switch pressed n°1 : {name}")
elif name == self.C_SW14:
self._action_debug(f"Momentary switch pressed n°2 : {name}")
elif name == self.C_SW15:
self._action_debug(f"Momentary switch pressed n°3 : {name}")
elif name == self.C_SW16:
self._action_debug(f"Momentary switch pressed n°4 : {name}")
else:
raise ValueError("_action_momswitches wrong value")
def _action_togswitches(self, name: str) -> None:
"""Actions for toggle switches"""
if name == self.C_TSSW1_1:
self._action_debug(f"Toggle switch pressed n°1 : {name}")
elif name == self.C_TSSW1_3:
self._action_debug(f"Toggle switch pressed n°2 : {name}")
elif name == self.C_TSSW2_1:
self._action_debug(f"Toggle switch pressed n°3 : {name}")
elif name == self.C_TSSW2_3:
self._action_debug(f"Toggle switch pressed n°4 : {name}")
elif name == self.C_TSSW3_1:
self._action_debug(f"Toggle switch pressed n°5 : {name}")
elif name == self.C_TSSW3_3:
self._action_debug(f"Toggle switch pressed n°6 : {name}")
elif name == self.C_TSSW4_1:
self._action_debug(f"Toggle switch pressed n°7 : {name}")
elif name == self.C_TSSW4_3:
self._action_debug(f"Toggle switch pressed n°8 : {name}")
else:
raise ValueError("_action_togswitches wrong value")
def _action_debug(self, message: str) -> None:
"""Show action messages from all inputs"""
if self._debug_:
print(message)
# --------------------------------------------------------------
# Other internal methods
# --------------------------------------------------------------
def _get_all_leds(self) -> list[[DigitalInOut, str]]:
"""Return all LEDs in the board"""
return (self._d1, self._d2, self._d3, self._d4, self._d5, self._d6, self._ledOnboard)
def _get_all_mcp_leds(self) -> list[[DigitalInOut, str]]:
"""Return all MCP LEDs and outputs in the board"""
return (self._d1, self._d2, self._d3, self._d4, self._d5, self._d6, self._gpa7, self._gpb7)
# --------------------------------------------------------------
# Public methods for external updates, actions
# --------------------------------------------------------------
def update(self) -> None:
"""Physical inputs from Pico and MCP"""
# Rotary encoders push button (Pico), PULL.UP
for name, button in self.rotary_encoders.items():
if not button.get_button().value:
self._action_encoders(name)
# Push buttons (Pico), PULL.DOWN
for name, button in self.push_buttons.items():
if button.get_button().value:
self._action_pushbuttons(name)
# Momentary switches (Pico), PULL.DOWN
for name, button in self.moment_switches.items():
if button.get_button().value:
self._action_momswitches(name)
# Toggle switches (MCP), PULL.UP
for name, button in self.toggle_switches.items():
if not button.value:
self._action_togswitches(name)
def update_vi(self, encoders_vi) -> None:
"""Encoder virtual inputs for JoystickXL"""
# Rotary encoders update (Pico) with two virtual inputs :
# <EncoderManager>, <Button>, <Button>
for encoder, vi_pin_a, vi_pin_b in encoders_vi:
_current_pos = encoder.get_position()
_pos_change = _current_pos - encoder.get_last_position()
if _pos_change != 0:
vi_pin_a.source_value = _pos_change > 0 # True or False ?
vi_pin_b.source_value = _pos_change < 0
encoder.set_last_position(_current_pos)
else:
vi_pin_a.source_value = vi_pin_b.source_value = False
def show_encoder_pins(self, encoder: EncoderManager, encoder_last_pos: int, encoder_name: str) -> int:
"""Show encoders pins values WITHOUT JoystickXL"""
_enc_position = encoder.get_position()
_position_change = _enc_position - encoder_last_pos
if _position_change > 0:
print(f"Rotary encoder ({_enc_position}) pin_a ++ : {encoder_name}")
elif _position_change < 0:
print(f"Rotary encoder ({_enc_position}) pin_b -- : {encoder_name}")
return _enc_position
def get_all_rot_encoders(self) -> list[EncoderManager]:
"""Return all rotary encoders"""
return (self._sw5, self._sw6, self._sw7, self._sw8)
def get_all_push_buttons(self) -> list[ButtonManager]:
"""Return all push buttons"""
return (self._sw9, self._sw10, self._sw11, self._sw12)
def get_all_mom_switches(self) -> list[ButtonManager]:
"""Return all momentary switches"""
return (self._sw13, self._sw14, self._sw15, self._sw16)
def get_all_tog_switches(self) -> list[DigitalInOut, DigitalInOut, str]:
"""Return all toggle switches"""
return (self._tssw1, self._tssw2, self._tssw3, self._tssw4)
def blink_leds(self, duration=0.1) -> None:
"""Blink all LEDs with minimal duration"""
for led, state in self._get_all_leds():
led.value = True
sleep(duration)
led.value = False
def switch_mcp_leds(self, leds: list[[str, int]]) -> None:
"""Switch ON/OFF the MCP LEDs and outputs : D1 -> D6, GPA7, GPB7
list[["D1", 1],["D2", 0]] where 1 = ON, 0 = OFF
"""
_all_leds_ouputs = self._get_all_mcp_leds()
for led_name, state in leds:
for index, mcp_leds in enumerate(_all_leds_ouputs):
if mcp_leds[1] == led_name.upper():
mcp_leds[0].value = bool(state)
self._action_debug(f"Switch MCP LED/output {led_name} : {state}")
break
# No LED/output name found in the list
if index == len(_all_leds_ouputs) - 1:
raise ValueError(f"switch_mcp_leds wrong LED/output value name : {led_name}")
def switch_on_ledOnboard(self) -> None:
"""Switch on the onboard LED"""
self._ledOnboard[0].value = True
def switch_on_leds(self) -> None:
"""Switch ON all LEDs onboard"""
for led in self._get_all_leds():
led[0].value = True
def switch_off_leds(self) -> None:
"""Switch OFF all LEDs onboard"""
for led in self._get_all_leds():
led[0].value = False
def show_debug(self) -> None:
"""Show debug messages"""
self._debug_ = True