|
| 1 | +# SPDX-FileCopyrightText: 2021 John Park for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# RaspberryPi Pico RP2040 Mechanical Keyboard |
| 4 | + |
| 5 | +import time |
| 6 | +import board |
| 7 | +from digitalio import DigitalInOut, Direction, Pull |
| 8 | +import usb_hid |
| 9 | +from adafruit_hid.keyboard import Keyboard |
| 10 | +from adafruit_hid.keycode import Keycode |
| 11 | +from adafruit_hid.consumer_control import ConsumerControl |
| 12 | +from adafruit_hid.consumer_control_code import ConsumerControlCode |
| 13 | + |
| 14 | +print("---Pico Pad Keyboard---") |
| 15 | + |
| 16 | +led = DigitalInOut(board.LED) |
| 17 | +led.direction = Direction.OUTPUT |
| 18 | +led.value = True |
| 19 | + |
| 20 | +kbd = Keyboard(usb_hid.devices) |
| 21 | +cc = ConsumerControl(usb_hid.devices) |
| 22 | + |
| 23 | +# list of pins to use (skipping GP15 on Pico because it's funky) |
| 24 | +pins = [ |
| 25 | + board.GP0, |
| 26 | + board.GP1, |
| 27 | + board.GP2, |
| 28 | + board.GP3, |
| 29 | + board.GP4, |
| 30 | + board.GP5, |
| 31 | + board.GP6, |
| 32 | + board.GP7, |
| 33 | + board.GP8, |
| 34 | + board.GP9, |
| 35 | + board.GP10, |
| 36 | + board.GP11, |
| 37 | + board.GP12, |
| 38 | + board.GP13, |
| 39 | + board.GP14, |
| 40 | + board.GP16, |
| 41 | + board.GP17, |
| 42 | + board.GP18, |
| 43 | + board.GP19, |
| 44 | + board.GP20, |
| 45 | + board.GP21, |
| 46 | +] |
| 47 | + |
| 48 | +MEDIA = 1 |
| 49 | +KEY = 2 |
| 50 | + |
| 51 | +keymap = { |
| 52 | + (0): (KEY, (Keycode.GUI, Keycode.C)), |
| 53 | + (1): (KEY, (Keycode.GUI, Keycode.V)), |
| 54 | + (2): (KEY, [Keycode.THREE]), |
| 55 | + (3): (KEY, [Keycode.FOUR]), |
| 56 | + (4): (KEY, [Keycode.FIVE]), |
| 57 | + (5): (MEDIA, ConsumerControlCode.VOLUME_DECREMENT), |
| 58 | + (6): (MEDIA, ConsumerControlCode.VOLUME_INCREMENT), |
| 59 | + |
| 60 | + (7): (KEY, [Keycode.R]), |
| 61 | + (8): (KEY, [Keycode.G]), |
| 62 | + (9): (KEY, [Keycode.B]), |
| 63 | + (10): (KEY, [Keycode.UP_ARROW]), |
| 64 | + (11): (KEY, [Keycode.X]), # plus key |
| 65 | + (12): (KEY, [Keycode.Y]), |
| 66 | + (13): (KEY, [Keycode.Z]), |
| 67 | + |
| 68 | + (14): (KEY, [Keycode.I]), |
| 69 | + (15): (KEY, [Keycode.O]), |
| 70 | + (16): (KEY, [Keycode.LEFT_ARROW]), |
| 71 | + (17): (KEY, [Keycode.DOWN_ARROW]), |
| 72 | + (18): (KEY, [Keycode.RIGHT_ARROW]), |
| 73 | + (19): (KEY, [Keycode.ALT]), |
| 74 | + (20): (KEY, [Keycode.U]), |
| 75 | + |
| 76 | +} |
| 77 | +switches = [0, 1, 2, 3, 4, 5, 6, |
| 78 | + 7, 8, 9, 10, 11, 12, 13, |
| 79 | + 14, 15, 16, 17, 18, 19, 20, 21] |
| 80 | + |
| 81 | +for i in range(21): |
| 82 | + switches[i] = DigitalInOut(pins[i]) |
| 83 | + switches[i].direction = Direction.INPUT |
| 84 | + switches[i].pull = Pull.UP |
| 85 | + |
| 86 | +switch_state = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
| 87 | + |
| 88 | +while True: |
| 89 | + for button in range(21): |
| 90 | + if switch_state[button] == 0: |
| 91 | + if not switches[button].value: |
| 92 | + try: |
| 93 | + if keymap[button][0] == KEY: |
| 94 | + kbd.press(*keymap[button][1]) |
| 95 | + else: |
| 96 | + cc.send(keymap[button][1]) |
| 97 | + except ValueError: # deals w six key limit |
| 98 | + pass |
| 99 | + switch_state[button] = 1 |
| 100 | + |
| 101 | + if switch_state[button] == 1: |
| 102 | + if switches[button].value: |
| 103 | + try: |
| 104 | + if keymap[button][0] == KEY: |
| 105 | + kbd.release(*keymap[button][1]) |
| 106 | + |
| 107 | + except ValueError: |
| 108 | + pass |
| 109 | + switch_state[button] = 0 |
| 110 | + |
| 111 | + time.sleep(0.01) # debounce |
0 commit comments