Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # SPDX-FileCopyrightText: 2021 John Park for Adafruit Industries
- # SPDX-License-Identifier: MIT
- # RaspberryPi Pico RP2040 Mechanical Keyboard
- import time
- import board
- from digitalio import DigitalInOut, Direction, Pull
- import usb_hid
- from adafruit_hid.keyboard import Keyboard
- from adafruit_hid.keycode import Keycode
- from adafruit_hid.consumer_control import ConsumerControl
- # from adafruit_hid.consumer_control_code import ConsumerControlCode
- print("---Pico Pad Keyboard---")
- led = DigitalInOut(board.LED)
- led.direction = Direction.OUTPUT
- led.value = True
- kbd = Keyboard(usb_hid.devices)
- cc = ConsumerControl(usb_hid.devices)
- # list of pins to use (skipping GP15 on Pico because it's funky)
- pins = [
- 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.GP16,
- board.GP17,
- board.GP18,
- board.GP19,
- board.GP20,
- board.GP21,
- ]
- MEDIA = 1
- KEY = 2
- keymap = {
- (0): (KEY, [Keycode.F]), # Fillet
- (1): (KEY, (Keycode.SHIFT, Keycode.C)), # Chamfer
- (2): (KEY, (Keycode.SHIFT, Keycode.O)),
- (3): (KEY, (Keycode.SHIFT, Keycode.A)),
- (4): (KEY, (Keycode.SHIFT, Keycode.THREE)), # three point arc
- (5): (KEY, [Keycode.O]), # Offset
- (6): (KEY, [Keycode.H]), # Hole
- (7): (KEY, [Keycode.C]), # Center point circle
- (8): (KEY, [Keycode.R]), # 2 point rectangle
- (9): (KEY, (Keycode.SHIFT, Keycode.R)), # center rectangle
- (10): (KEY, (Keycode.SHIFT, Keycode.M)), # Mirror
- (11): (KEY, [Keycode.P]), # Project
- (12): (KEY, [Keycode.Z]), # create sketch
- (13): (KEY, [Keycode.L]), # Line
- (14): (KEY, (Keycode.SHIFT, Keycode.P)), # Point
- (15): (KEY, (Keycode.SHIFT, Keycode.T)), # Text
- (16): (KEY, (Keycode.SHIFT, Keycode.L)), # Line
- (17): (KEY, [Keycode.DOWN_ARROW]),
- (18): (KEY, [Keycode.RIGHT_ARROW]),
- (19): (KEY, [Keycode.ALT]),
- (20): (KEY, [Keycode.U]),
- }
- switches = [
- 0,
- 1,
- 2,
- 3,
- 4,
- 5,
- 6,
- 7,
- 8,
- 9,
- 10,
- 11,
- 12,
- 13,
- 14,
- 15,
- 16,
- 17,
- 18,
- 19,
- 20,
- 21,
- ]
- for i in range(21):
- switches[i] = DigitalInOut(pins[i])
- switches[i].direction = Direction.INPUT
- switches[i].pull = Pull.UP
- switch_state = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
- while True:
- for button in range(21):
- if switch_state[button] == 0:
- if not switches[button].value:
- try:
- if keymap[button][0] == KEY:
- kbd.press(*keymap[button][1])
- else:
- cc.send(keymap[button][1])
- except ValueError: # deals w six key limit
- pass
- switch_state[button] = 1
- if switch_state[button] == 1:
- if switches[button].value:
- try:
- if keymap[button][0] == KEY:
- kbd.release(*keymap[button][1])
- except ValueError:
- pass
- switch_state[button] = 0
- time.sleep(0.01) # debounce
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement