Skip to content

Commit 1f6908d

Browse files
committed
code for three button foot switch
code for three button foot switch
1 parent 9662399 commit 1f6908d

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# SPDX-FileCopyrightText: 2022 Ruiz Bros for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import time
5+
import digitalio
6+
import board
7+
import usb_hid
8+
from adafruit_hid.consumer_control import ConsumerControl
9+
from adafruit_hid.consumer_control_code import ConsumerControlCode
10+
11+
# The button pins we'll use, each will have an internal pullup
12+
buttonpins = [board.A1, board.A2, board.A3]
13+
14+
# The keycode sent for each button, will be paired with a control key
15+
buttonkeys = [
16+
ConsumerControlCode.PLAY_PAUSE, # Center Foot Pad
17+
ConsumerControlCode.VOLUME_DECREMENT, #Left Foot Pad
18+
ConsumerControlCode.VOLUME_INCREMENT, # Right Foot Pad
19+
]
20+
21+
# the keyboard object!
22+
cc = ConsumerControl(usb_hid.devices) # List of available controls https://circuitpython.readthedocs.io/projects/hid/en/latest/api.html#adafruit-hid-keycode-keycode
23+
# our array of button objects
24+
buttons = []
25+
26+
# make all pin objects, make them inputs w/pullups
27+
for pin in buttonpins:
28+
button = digitalio.DigitalInOut(pin)
29+
button.direction = digitalio.Direction.INPUT
30+
button.pull = digitalio.Pull.UP
31+
buttons.append(button)
32+
33+
print("Waiting for button presses")
34+
35+
while True:
36+
# check each button
37+
for button in buttons:
38+
if not button.value: # pressed?
39+
i = buttons.index(button)
40+
41+
print("Button #%d Pressed" % i)
42+
43+
while not button.value:
44+
pass # wait for it to be released!
45+
# type the keycode!
46+
k = buttonkeys[i] # get the corresp. keycode
47+
cc.send(k)
48+
time.sleep(0.01)

0 commit comments

Comments
 (0)