Download the Project Bundle
Your project will use a specific set of CircuitPython libraries and the code.py file. To get everything you need, click on the Download Project Bundle link below, and uncompress the .zip file.
Drag the contents of the uncompressed bundle directory onto your board's CIRCUITPY drive, replacing any existing files or directories with the same names, and adding any new ones that are necessary.
Continue below the full code listing for a walkthrough of the code.
# SPDX-FileCopyrightText: 2022 Jeff Epler for Adafruit Industries # SPDX-License-Identifier: MIT # Commodore 16 to USB HID adapter with Adafruit KB2040 # # Note that: # * This matrix is different than the (more common) Commodore 64 matrix # * There are no diodes, not even on modifiers, so there's only 2-key rollover. # * This is a "physical" keymap, so that the functions of the keys are similar to the # function of a standard PC keyboard key in the same location. # # See the guide or the advanced code for more information about the key matrix import board import keypad from adafruit_hid.keycode import Keycode as K from adafruit_hid.keyboard import Keyboard import usb_hid rows = [board.A3, board.D6, board.D10, board.D9, board.MOSI, board.D2, board.A0, board.D4] cols = [board.A2, board.SCK, board.MISO, board.A1, board.D5, board.D7, board.D8, board.D3] keycodes = [ K.BACKSPACE, K.ENTER, K.LEFT_ARROW, K.F8, K.F1, K.F2, K.F3, K.LEFT_BRACKET, K.THREE, K.W, K.A, K.FOUR, K.Z, K.S, K.E, K.LEFT_SHIFT, K.FIVE, K.R, K.D, K.SIX, K.C, K.F, K.T, K.X, K.SEVEN, K.Y, K.G, K.EIGHT, K.B, K.H, K.U, K.V, K.NINE, K.I, K.J, K.ZERO, K.M, K.K, K.O, K.N, K.DOWN_ARROW, K.P, K.L, K.UP_ARROW, K.PERIOD, K.SEMICOLON, K.BACKSLASH, K.COMMA, K.MINUS, K.WINDOWS, K.QUOTE, K.EQUALS, K.ESCAPE, K.RIGHT_ARROW, K.RIGHT_BRACKET, K.FORWARD_SLASH, K.ONE, K.HOME, K.LEFT_CONTROL, K.TWO, K.SPACE, K.ALT, K.Q, K.GRAVE_ACCENT, ] kbd = Keyboard(usb_hid.devices) with keypad.KeyMatrix(rows, cols) as keys: while True: if ev := keys.events.get(): keycode = keycodes[ev.key_number] if ev.pressed: kbd.press(keycode) else: kbd.release(keycode)
The code begins with required imports, then defines the pins that make up the rows and columns of the keyboard matrix:
import board import keypad from adafruit_hid.keycode import Keycode as K from adafruit_hid.keyboard import Keyboard import usb_hid rows = [board.A3, board.D6, board.D10, board.D9, board.MOSI, board.D2, board.A0, board.D4] cols = [board.A2, board.SCK, board.MISO, board.A1, board.D5, board.D7, board.D8, board.D3]
Define a keymap. This is a "positional" keymap, in which keys are assigned according to their position, rather than their legend, so the key to the right of "0" acts as "-" rather than as an arrow key.
Because the rows and columns of the matrix have only a loose relationship to the rows of keys on the keyboard, the order of these items may seem very arbitrary.
Here, K
refers to the adafruit_hid.keycode.Keycode
object, making it easier to refer to keycodes in short-hand.
keycodes = [ K.BACKSPACE, K.ENTER, K.LEFT_ARROW, K.F8, K.F1, K.F2, K.F3, K.LEFT_BRACKET, K.THREE, K.W, K.A, K.FOUR, K.Z, K.S, K.E, K.LEFT_SHIFT, K.FIVE, K.R, K.D, K.SIX, K.C, K.F, K.T, K.X, K.SEVEN, K.Y, K.G, K.EIGHT, K.B, K.H, K.U, K.V, K.NINE, K.I, K.J, K.ZERO, K.M, K.K, K.O, K.N, K.DOWN_ARROW, K.P, K.L, K.UP_ARROW, K.PERIOD, K.SEMICOLON, K.BACKSLASH, K.COMMA, K.MINUS, K.WINDOWS, K.QUOTE, K.EQUALS, K.ESCAPE, K.RIGHT_ARROW, K.RIGHT_BRACKET, K.FORWARD_SLASH, K.ONE, K.HOME, K.LEFT_CONTROL, K.TWO, K.SPACE, K.ALT, K.Q, K.GRAVE_ACCENT, ]
All that's left to do is loop forever, translating key up/down events into their USB HID equivalents:
kbd = Keyboard(usb_hid.devices) with keypad.KeyMatrix(rows, cols) as keys: while True: if ev := keys.events.get(): keycode = keycodes[ev.key_number] if ev.pressed: kbd.press(keycode) else: kbd.release(keycode)
That's all there is to it! At this point, typing keys on the keyboard will cause corresponding keypresses on your computer.
If some keys aren't working at all, make sure all the wire connections are good, and make sure all the right pin positions are used. If some keys are sending the wrong codes to the computer, trace out the wiring and see if you made an error. If you got two wires flipped, you can correct it in software by swapping them in the rows
and/or cols
pin lists.
Continue to the next page for a more complex implementation of the translation to USB HID.
Page last edited January 22, 2025
Text editor powered by tinymce.