picote/code.py

90 lines
3.8 KiB
Python
Raw Permalink Normal View History

2024-02-16 18:16:03 +01:00
# 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
2024-09-27 16:28:32 +02:00
# Red = Del/E
# Green = Spacebar/Return
# Blue = Right arrow/Up
# Yellow = Left arrow/Down
# Black = Esc/R
2024-02-16 18:16:03 +01:00
#
2024-09-27 16:28:32 +02:00
# Switch on : Alt mode
2024-02-16 18:16:03 +01:00
#
# Forked from
# DroneBot Workshop 2021
# https://dronebotworkshop.com
import time
import board
import digitalio
import usb_hid
2024-09-27 16:28:32 +02:00
# Regular keys
2024-02-16 18:16:03 +01:00
from adafruit_hid.keyboard import Keyboard
from keyboard_layout_win_fr import KeyboardLayout
from keycode_win_fr import Keycode
2024-09-27 16:28:32 +02:00
# When in ALT mode, buttons use an alternative keycode.
ALT = False
2024-02-16 18:16:03 +01:00
# KB setup
keyboard = Keyboard(usb_hid.devices)
keyboard_layout = KeyboardLayout(keyboard)
# Buttons setup
buttons_gpio = {
2024-09-27 16:28:32 +02:00
# name GPIO Keycode_1 Keycode_2(ALT mode) State DigitalIO (switch)
2024-09-29 11:32:39 +02:00
"red" : dict(gpio=board.GP18, keycode_1=Keycode.DELETE, keycode_2=Keycode.E, state=False, DIO=None),
"green" : dict(gpio=board.GP19, keycode_1=Keycode.SPACEBAR, keycode_2=Keycode.RETURN, state=False, DIO=None),
"blue" : dict(gpio=board.GP16, keycode_1=Keycode.RIGHT_ARROW, keycode_2=Keycode.DOWN_ARROW,state=False, DIO=None),
"black" : dict(gpio=board.GP20, keycode_1=Keycode.ESCAPE, keycode_2=Keycode.R, state=False, DIO=None),
"yellow": dict(gpio=board.GP17, keycode_1=Keycode.LEFT_ARROW, keycode_2=Keycode.UP_ARROW, state=False, DIO=None),
"switch": dict(gpio=board.GP21, keycode_1=None, keycode_2=None, state=False, DIO=None, switch_setup=dict(gpio=board.GP7, state=False, DIO=None))
2024-02-16 18:16:03 +01:00
}
# 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:
2024-09-27 16:28:32 +02:00
# If digital input is high and was not pressed previously
2024-02-16 18:16:03 +01:00
if buttons_gpio[btn]['DIO'].value and not buttons_gpio[btn]['state']:
2024-09-27 16:28:32 +02:00
if ALT:
if buttons_gpio[btn]['keycode_2'] is not None:
keyboard.press(buttons_gpio[btn]['keycode_2'])
else:
if buttons_gpio[btn]['keycode_1'] is not None:
keyboard.press(buttons_gpio[btn]['keycode_1'])
buttons_gpio[btn]['state'] = True
# Turn light on, turn ALT mode on
2024-02-16 18:16:03 +01:00
if 'switch_setup' in buttons_gpio[btn]:
2024-09-27 16:28:32 +02:00
if not buttons_gpio[btn]['switch_setup']['DIO'].value:
buttons_gpio[btn]['switch_setup']['DIO'].value = True
ALT = True
# If digital input is down and was pressed previously
2024-02-16 18:16:03 +01:00
if not buttons_gpio[btn]['DIO'].value and buttons_gpio[btn]['state']:
2024-09-27 16:28:32 +02:00
if ALT:
if buttons_gpio[btn]['keycode_2'] is not None:
keyboard.release(buttons_gpio[btn]['keycode_2'])
else:
if buttons_gpio[btn]['keycode_1'] is not None:
keyboard.release(buttons_gpio[btn]['keycode_1'])
buttons_gpio[btn]['state'] = False
# Turn light off, turn ALT mode off
2024-02-16 18:16:03 +01:00
if 'switch_setup' in buttons_gpio[btn]:
2024-09-27 16:28:32 +02:00
if buttons_gpio[btn]['switch_setup']['DIO'].value:
buttons_gpio[btn]['switch_setup']['DIO'].value = False
ALT = False
time.sleep(btn_scan_delay)