picote/code.py

73 lines
2.8 KiB
Python

# Raspberry Pi Pico Stopmo pad
#
# Using CircuitPython : https://circuitpython.org/board/raspberry_pi_pico/
# Using Adafruit USB_HID Library : https://github.com/adafruit/Adafruit_CircuitPython_HID/releases
#
# Buttons default mapping
# Red = F
# Green = G
# Blue = H
# Black = I
#
# Yellow on : multimedia mode
#
# Forked from
# DroneBot Workshop 2021
# https://dronebotworkshop.com
import time
import board
import digitalio
import usb_hid
# Regulare keys
from adafruit_hid.keyboard import Keyboard
from keyboard_layout_win_fr import KeyboardLayout
from keycode_win_fr import Keycode
# multimedia keys
# ~ from adafruit_hid.consumer_control import ConsumerControl
# ~ from adafruit_hid.consumer_control_code import ConsumerControlCode
# ~ cc = ConsumerControl(usb_hid.devices)
# KB setup
keyboard = Keyboard(usb_hid.devices)
keyboard_layout = KeyboardLayout(keyboard)
# Buttons setup
# Change GPIOs according to HW
buttons_gpio = {
# name GPIO Keycode State DigitalIO (switch)
"red" : dict(gpio=board.GP18, keycode=Keycode.D, state=False, DIO=None),
"green" : dict(gpio=board.GP19, keycode=Keycode.N, state=False, DIO=None),
"blue" : dict(gpio=board.GP16, keycode=Keycode.J, state=False, DIO=None),
"black" : dict(gpio=board.GP20, keycode=Keycode.E, state=False, DIO=None),
"yellow": dict(gpio=board.GP17, keycode=Keycode.B, state=False, DIO=None),
"switch": dict(gpio=board.GP21, keycode=Keycode.LEFT_SHIFT, state=False, DIO=None, switch_setup=dict(gpio=board.GP7, DIO=None))
}
# GPIO setup
for btn in buttons_gpio:
buttons_gpio[btn]['DIO'] = digitalio.DigitalInOut(buttons_gpio[btn]['gpio'])
buttons_gpio[btn]['DIO'].direction = digitalio.Direction.INPUT
buttons_gpio[btn]['DIO'].pull = digitalio.Pull.DOWN
# Switch setup
if 'switch_setup' in buttons_gpio[btn]:
buttons_gpio[btn]['switch_setup']['DIO'] = digitalio.DigitalInOut(buttons_gpio[btn]['switch_setup']['gpio'])
buttons_gpio[btn]['switch_setup']['DIO'].direction = digitalio.Direction.OUTPUT
btn_scan_delay = 1/200
while True:
for btn in buttons_gpio:
if buttons_gpio[btn]['DIO'].value and not buttons_gpio[btn]['state']:
keyboard.press(buttons_gpio[btn]['keycode'])
buttons_gpio[btn]['state'] = not buttons_gpio[btn]['state']
if 'switch_setup' in buttons_gpio[btn]:
buttons_gpio[btn]['switch_setup']['DIO'].value = True
if not buttons_gpio[btn]['DIO'].value and buttons_gpio[btn]['state']:
keyboard.release(buttons_gpio[btn]['keycode'])
buttons_gpio[btn]['state'] = not buttons_gpio[btn]['state']
if 'switch_setup' in buttons_gpio[btn]:
buttons_gpio[btn]['switch_setup']['DIO'].value = False
time.sleep(btn_scan_delay)