code.py
 1  # SPDX-FileCopyrightText: 2022 Jeff Epler for Adafruit Industries
 2  # SPDX-License-Identifier: MIT
 3  
 4  # Commodore 16 to USB HID adapter with Adafruit KB2040
 5  #
 6  # Note that:
 7  #  * This matrix is different than the (more common) Commodore 64 matrix
 8  #  * There are no diodes, not even on modifiers, so there's only 2-key rollover.
 9  #  * This is a "physical" keymap, so that the functions of the keys are similar to the
10  #    function of a standard PC keyboard key in the same location.
11  #
12  # See the guide or the advanced code for more information about the key matrix
13  
14  import board
15  import keypad
16  from adafruit_hid.keycode import Keycode as K
17  from adafruit_hid.keyboard import Keyboard
18  import usb_hid
19  
20  rows = [board.A3, board.D6, board.D10, board.D9, board.MOSI, board.D2, board.A0, board.D4]
21  cols = [board.A2, board.SCK, board.MISO, board.A1, board.D5, board.D7, board.D8, board.D3]
22  
23  keycodes = [
24      K.BACKSPACE, K.ENTER, K.LEFT_ARROW, K.F8, K.F1, K.F2, K.F3, K.LEFT_BRACKET,
25      K.THREE, K.W, K.A, K.FOUR, K.Z, K.S, K.E, K.LEFT_SHIFT,
26      K.FIVE, K.R, K.D, K.SIX, K.C, K.F, K.T, K.X,
27      K.SEVEN, K.Y, K.G, K.EIGHT, K.B, K.H, K.U, K.V,
28      K.NINE, K.I, K.J, K.ZERO, K.M, K.K, K.O, K.N,
29      K.DOWN_ARROW, K.P, K.L, K.UP_ARROW, K.PERIOD, K.SEMICOLON, K.BACKSLASH, K.COMMA,
30      K.MINUS, K.WINDOWS, K.QUOTE, K.EQUALS, K.ESCAPE, K.RIGHT_ARROW, K.RIGHT_BRACKET,
31      K.FORWARD_SLASH, K.ONE, K.HOME, K.LEFT_CONTROL, K.TWO, K.SPACE, K.ALT, K.Q, K.GRAVE_ACCENT,
32  ]
33  
34  kbd = Keyboard(usb_hid.devices)
35  
36  with keypad.KeyMatrix(rows, cols) as keys:
37      while True:
38          if ev := keys.events.get():
39              keycode = keycodes[ev.key_number]
40              if ev.pressed:
41                  kbd.press(keycode)
42              else:
43                  kbd.release(keycode)