|
| 1 | +# ItsyBitsy Keypad |
| 2 | +# Uses ItsyBitsy M4/M0 plus Pimoroni Keybow |
| 3 | +# To build a customizable USB keypad |
| 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 | +import adafruit_dotstar as dotstar |
| 14 | + |
| 15 | +dots = dotstar.DotStar(board.SCK, board.MOSI, 12, brightness=0.4) |
| 16 | + |
| 17 | +RED = 0xFF0000 |
| 18 | +AMBER = 0xAA9900 |
| 19 | +BLUE = 0x0066FF |
| 20 | +MAGENTA = 0xFF00FF |
| 21 | +PURPLE = 0x3B0F85 |
| 22 | +BLACK = 0x000000 |
| 23 | + |
| 24 | +kbd = Keyboard(usb_hid.devices) |
| 25 | +cc = ConsumerControl(usb_hid.devices) |
| 26 | + |
| 27 | +orientation = 1 # 0 = portrait/vertical, 1 = landscape/horizontal |
| 28 | +if orientation == 0: |
| 29 | + key_dots = [0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11] |
| 30 | + # 0 #4 #8 |
| 31 | + # 1 #5 #9 |
| 32 | + # 2 #6 #10 |
| 33 | + # 3 #7 #11 |
| 34 | +if orientation == 1: |
| 35 | + key_dots = [3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8] |
| 36 | + # 3 #2 #1 #0 |
| 37 | + # 7 #6 #5 #4 |
| 38 | + # 11 #10 #9 #8 |
| 39 | + |
| 40 | + |
| 41 | +def dot_on(dot, color): |
| 42 | + dots[dot] = color |
| 43 | + |
| 44 | + |
| 45 | +def dot_off(dot): |
| 46 | + dots[dot] = BLACK |
| 47 | + |
| 48 | + |
| 49 | +# Pin definitions |
| 50 | +if orientation == 0: # 0 = portrait/vertical |
| 51 | + pins = [ |
| 52 | + board.D11, |
| 53 | + board.D12, |
| 54 | + board.D2, |
| 55 | + board.D10, |
| 56 | + board.D9, |
| 57 | + board.D7, |
| 58 | + board.A5, |
| 59 | + board.A4, |
| 60 | + board.A3, |
| 61 | + board.A2, |
| 62 | + board.A1, |
| 63 | + board.A0, |
| 64 | + ] |
| 65 | +if orientation == 1: # 1 = landscape/horizontal |
| 66 | + pins = [ |
| 67 | + board.A2, |
| 68 | + board.A5, |
| 69 | + board.D10, |
| 70 | + board.D11, |
| 71 | + board.A1, |
| 72 | + board.A4, |
| 73 | + board.D9, |
| 74 | + board.D12, |
| 75 | + board.A0, |
| 76 | + board.A3, |
| 77 | + board.D7, |
| 78 | + board.D2, |
| 79 | + ] |
| 80 | +# the two command types -- MEDIA for ConsumerControlCodes, KEY for Keycodes |
| 81 | +# this allows button press to send the correct HID command for the type specified |
| 82 | +MEDIA = 1 |
| 83 | +KEY = 2 |
| 84 | +keymap = { |
| 85 | + (0): (AMBER, MEDIA, ConsumerControlCode.PLAY_PAUSE), |
| 86 | + (1): (AMBER, MEDIA, ConsumerControlCode.MUTE), |
| 87 | + (2): (AMBER, MEDIA, ConsumerControlCode.VOLUME_DECREMENT), |
| 88 | + (3): (AMBER, MEDIA, ConsumerControlCode.VOLUME_INCREMENT), |
| 89 | + (4): (BLUE, KEY, (Keycode.GUI, Keycode.C)), |
| 90 | + (5): (BLUE, KEY, (Keycode.GUI, Keycode.V)), |
| 91 | + (6): (MAGENTA, KEY, [Keycode.UP_ARROW]), |
| 92 | + (7): (PURPLE, KEY, [Keycode.BACKSPACE]), |
| 93 | + (8): (BLUE, KEY, [Keycode.SPACE]), |
| 94 | + (9): (MAGENTA, KEY, [Keycode.LEFT_ARROW]), |
| 95 | + (10): (MAGENTA, KEY, [Keycode.DOWN_ARROW]), |
| 96 | + (11): (MAGENTA, KEY, [Keycode.RIGHT_ARROW]), |
| 97 | +} |
| 98 | + |
| 99 | +switches = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] |
| 100 | +for i in range(12): |
| 101 | + switches[i] = DigitalInOut(pins[i]) |
| 102 | + switches[i].direction = Direction.INPUT |
| 103 | + switches[i].pull = Pull.UP |
| 104 | + |
| 105 | +switch_state = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
| 106 | + |
| 107 | +print("ItsyBitsy Keybow") |
| 108 | + |
| 109 | +# Starup lights |
| 110 | +for k in range(12): |
| 111 | + dot_on(key_dots[k], RED) |
| 112 | + time.sleep(0.05) |
| 113 | + dot_on(key_dots[k], keymap[k][0]) # use individual key colors from set |
| 114 | + time.sleep(0.05) |
| 115 | + |
| 116 | +while True: |
| 117 | + for button in range(12): |
| 118 | + if switch_state[button] == 0: |
| 119 | + if not switches[button].value: |
| 120 | + try: |
| 121 | + if keymap[button][1] == KEY: |
| 122 | + kbd.press(*keymap[button][2]) |
| 123 | + else: |
| 124 | + cc.send(keymap[button][2]) |
| 125 | + dot_on(key_dots[button], RED) |
| 126 | + except ValueError: # deals w six key limit |
| 127 | + pass |
| 128 | + print("pressed key{}".format(button)) |
| 129 | + switch_state[button] = 1 |
| 130 | + |
| 131 | + if switch_state[button] == 1: |
| 132 | + if switches[button].value: |
| 133 | + try: |
| 134 | + if keymap[button][1] == KEY: |
| 135 | + kbd.release(*keymap[button][2]) |
| 136 | + dot_on(key_dots[button], keymap[button][0]) |
| 137 | + except ValueError: |
| 138 | + pass |
| 139 | + print("released key{}".format(button)) |
| 140 | + switch_state[button] = 0 |
| 141 | + |
| 142 | + time.sleep(0.01) # debounce |
0 commit comments