Advertisement
Guest User

Untitled

a guest
Jun 6th, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.28 KB | None | 0 0
  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.F]),  # Fillet
  53.     (1): (KEY, (Keycode.SHIFT, Keycode.C)),  # Chamfer
  54.     (2): (KEY, (Keycode.SHIFT, Keycode.O)),
  55.     (3): (KEY, (Keycode.SHIFT, Keycode.A)),
  56.     (4): (KEY, (Keycode.SHIFT, Keycode.THREE)),  # three point arc
  57.     (5): (KEY, [Keycode.O]),  # Offset
  58.     (6): (KEY, [Keycode.H]),  # Hole
  59.     (7): (KEY, [Keycode.C]),  # Center point circle
  60.     (8): (KEY, [Keycode.R]),  # 2 point rectangle
  61.     (9): (KEY, (Keycode.SHIFT, Keycode.R)),  # center rectangle
  62.     (10): (KEY, (Keycode.SHIFT, Keycode.M)),  # Mirror
  63.     (11): (KEY, [Keycode.P]),  # Project
  64.     (12): (KEY, [Keycode.Z]),  # create sketch
  65.     (13): (KEY, [Keycode.L]),  # Line
  66.     (14): (KEY, (Keycode.SHIFT, Keycode.P)),  # Point
  67.     (15): (KEY, (Keycode.SHIFT, Keycode.T)),  # Text
  68.     (16): (KEY, (Keycode.SHIFT, Keycode.L)),  # Line
  69.     (17): (KEY, [Keycode.DOWN_ARROW]),
  70.     (18): (KEY, [Keycode.RIGHT_ARROW]),
  71.     (19): (KEY, [Keycode.ALT]),
  72.     (20): (KEY, [Keycode.U]),
  73. }
  74. switches = [
  75.     0,
  76.     1,
  77.     2,
  78.     3,
  79.     4,
  80.     5,
  81.     6,
  82.     7,
  83.     8,
  84.     9,
  85.     10,
  86.     11,
  87.     12,
  88.     13,
  89.     14,
  90.     15,
  91.     16,
  92.     17,
  93.     18,
  94.     19,
  95.     20,
  96.     21,
  97. ]
  98.  
  99. for i in range(21):
  100.     switches[i] = DigitalInOut(pins[i])
  101.     switches[i].direction = Direction.INPUT
  102.     switches[i].pull = Pull.UP
  103.  
  104. switch_state = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  105.  
  106. while True:
  107.     for button in range(21):
  108.         if switch_state[button] == 0:
  109.             if not switches[button].value:
  110.                 try:
  111.                     if keymap[button][0] == KEY:
  112.                         kbd.press(*keymap[button][1])
  113.                     else:
  114.                         cc.send(keymap[button][1])
  115.                 except ValueError:  # deals w six key limit
  116.                     pass
  117.                 switch_state[button] = 1
  118.  
  119.         if switch_state[button] == 1:
  120.             if switches[button].value:
  121.                 try:
  122.                     if keymap[button][0] == KEY:
  123.                         kbd.release(*keymap[button][1])
  124.  
  125.                 except ValueError:
  126.                     pass
  127.                 switch_state[button] = 0
  128.  
  129.     time.sleep(0.01)  # debounce
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement