|
3 | 3 | # Pico Four Step Switch Keypad Demo
|
4 | 4 | import time
|
5 | 5 | import board
|
6 |
| -from digitalio import Direction, DigitalInOut, Pull |
7 |
| -from adafruit_debouncer import Debouncer |
| 6 | +import keypad |
| 7 | +from digitalio import Direction, DigitalInOut |
8 | 8 |
|
9 | 9 | board_led = DigitalInOut(board.LED)
|
10 | 10 | board_led.direction = Direction.OUTPUT
|
11 | 11 | board_led.value = True
|
12 | 12 |
|
13 | 13 | switch_pins = (board.GP6, board.GP7, board.GP8, board.GP9)
|
14 |
| -step_switches = [] |
15 |
| -for switch_pin in switch_pins: |
16 |
| - tmp_switch_pin = DigitalInOut(switch_pin) |
17 |
| - tmp_switch_pin.pull = Pull.UP |
18 |
| - step_switches.append(Debouncer(tmp_switch_pin)) |
| 14 | +keys = keypad.Keys(switch_pins, value_when_pressed=False, pull=True) |
19 | 15 |
|
20 | 16 | led_pins = (board.GP2, board.GP3, board.GP4, board.GP5)
|
21 | 17 | leds = []
|
@@ -44,15 +40,16 @@ def blink_all_leds(pause, repeat):
|
44 | 40 | modes = (0, 1, 2, 3)
|
45 | 41 | mode_names = ("MIDI", "DESK", "SELECTOR", "COPY-PASTE")
|
46 | 42 |
|
47 |
| -print("Select the mode by pressing a button...") |
| 43 | +print("Select the mode by pressing a button: MIDI, DESK, SELECTOR, or COPY-PASTE") |
48 | 44 | while not mode_picked: # program waits for a mode to be picked
|
49 |
| - for i in range(len(step_switches)): |
50 |
| - step_switches[i].update() |
51 |
| - if step_switches[i].fell: |
52 |
| - mode_choice = i |
| 45 | + key = keys.events.get() |
| 46 | + if key: |
| 47 | + if key.pressed: |
| 48 | + mode_choice = key.key_number |
53 | 49 | print(mode_names[mode_choice], "mode")
|
54 | 50 | mode_picked = True
|
55 | 51 |
|
| 52 | + |
56 | 53 | if mode_choice == 0: # MIDI mode
|
57 | 54 | import usb_midi
|
58 | 55 | import adafruit_midi
|
@@ -97,19 +94,19 @@ def blink_all_leds(pause, repeat):
|
97 | 94 | # MODIFIER = Keycode.CONTROL # For Windows
|
98 | 95 | MODIFIER = Keycode.COMMAND
|
99 | 96 | KEYMAP = (
|
100 |
| - ("wire 1", [MODIFIER, Keycode.A]), # select all |
101 |
| - ("wire 2", [MODIFIER, Keycode.X]), # cut |
102 |
| - ("wire 3", [MODIFIER, Keycode.C]), # copy |
103 |
| - ("wire 4", [MODIFIER, Keycode.V]), # paste |
| 97 | + ("Copy/Paste 1", [MODIFIER, Keycode.A]), # select all |
| 98 | + ("Copy/Paste 2", [MODIFIER, Keycode.X]), # cut |
| 99 | + ("Copy/Paste 3", [MODIFIER, Keycode.C]), # copy |
| 100 | + ("Copy/Paste 4", [MODIFIER, Keycode.V]), # paste |
104 | 101 | )
|
105 | 102 |
|
106 | 103 | blink_led(mode_choice, 0.1, 3)
|
107 | 104 |
|
108 |
| - |
109 | 105 | while True:
|
110 |
| - for i in range(len(step_switches)): |
111 |
| - step_switches[i].update() |
112 |
| - if step_switches[i].fell: |
| 106 | + key = keys.events.get() |
| 107 | + if key: |
| 108 | + if key.pressed: |
| 109 | + i = key.key_number |
113 | 110 | print(i, "pressed")
|
114 | 111 | if mode_choice == 0:
|
115 | 112 | leds[i].value = not leds[i].value
|
|
0 commit comments