Skip to content

Commit 3786581

Browse files
authored
Merge pull request #2252 from jedgarpark/step-switch-party-keypad
changed step switch party demo to use keypad library
2 parents 450cf40 + de67530 commit 3786581

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

Step_Switch_Pico_Demo/code.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,15 @@
33
# Pico Four Step Switch Keypad Demo
44
import time
55
import board
6-
from digitalio import Direction, DigitalInOut, Pull
7-
from adafruit_debouncer import Debouncer
6+
import keypad
7+
from digitalio import Direction, DigitalInOut
88

99
board_led = DigitalInOut(board.LED)
1010
board_led.direction = Direction.OUTPUT
1111
board_led.value = True
1212

1313
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)
1915

2016
led_pins = (board.GP2, board.GP3, board.GP4, board.GP5)
2117
leds = []
@@ -44,15 +40,16 @@ def blink_all_leds(pause, repeat):
4440
modes = (0, 1, 2, 3)
4541
mode_names = ("MIDI", "DESK", "SELECTOR", "COPY-PASTE")
4642

47-
print("Select the mode by pressing a button...")
43+
print("Select the mode by pressing a button: MIDI, DESK, SELECTOR, or COPY-PASTE")
4844
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
5349
print(mode_names[mode_choice], "mode")
5450
mode_picked = True
5551

52+
5653
if mode_choice == 0: # MIDI mode
5754
import usb_midi
5855
import adafruit_midi
@@ -97,19 +94,19 @@ def blink_all_leds(pause, repeat):
9794
# MODIFIER = Keycode.CONTROL # For Windows
9895
MODIFIER = Keycode.COMMAND
9996
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
104101
)
105102

106103
blink_led(mode_choice, 0.1, 3)
107104

108-
109105
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
113110
print(i, "pressed")
114111
if mode_choice == 0:
115112
leds[i].value = not leds[i].value

0 commit comments

Comments
 (0)