Skip to content

Instantly share code, notes, and snippets.

@kou1okada
Created September 14, 2024 15:48
Show Gist options
  • Save kou1okada/cf46d861ecb6e8ec69680cd7b73d5762 to your computer and use it in GitHub Desktop.
Save kou1okada/cf46d861ecb6e8ec69680cd7b73d5762 to your computer and use it in GitHub Desktop.
FootSwitch.py for Raspberry Pi Pico with CircuitPython
# FootSwitch.py for RaspBerry Pi Pico with CircuitPython
# Copyright 2024 (c) Koichi OKADA. All right reserved.
# This script is distributed under the MIT license.
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
import time
import board
import digitalio
KeyMap = [
Keycode.LEFT_CONTROL,
Keycode.LEFT_SHIFT,
Keycode.SPACE,
Keycode.B,
Keycode.E,
Keycode.F,
Keycode.G,
Keycode.ONE,
Keycode.TWO,
Keycode.THREE,
Keycode.FOUR,
Keycode.FIVE,
]
KeyStat = [False] * len(KeyMap)
IO = [
board.GP0,
board.GP1,
board.GP2,
board.GP3,
board.GP4,
board.GP5,
board.GP6,
board.GP7,
board.GP8,
board.GP9,
board.GP10,
board.GP11,
board.GP12,
board.GP13,
board.GP14,
board.GP15,
board.GP16,
board.GP17,
board.GP18,
board.GP19,
board.GP20,
board.GP21,
board.GP22,
board.GP23,
board.GP24,
board.GP25,
board.GP26,
board.GP27,
board.GP28,
]
gp = [None] * len(KeyMap)
kbd = Keyboard(usb_hid.devices)
for i in range(len(KeyMap)):
gp[i] = digitalio.DigitalInOut(IO[i])
gp[i].switch_to_input(pull=digitalio.Pull.UP)
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
while True:
for i, io in enumerate(gp):
if not io.value:
if not KeyStat[i]:
kbd.press(KeyMap[i])
KeyStat[i] = not KeyStat[i]
else:
if KeyStat[i]:
kbd.release(KeyMap[i])
KeyStat[i] = not KeyStat[i]
led.value = not led.value
time.sleep(0.001)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment