Skip to content

Instantly share code, notes, and snippets.

@foxt
Created June 26, 2022 20:45
Show Gist options
  • Save foxt/52d284355d46e2eb5fb481f8fa1a5f26 to your computer and use it in GitHub Desktop.
Save foxt/52d284355d46e2eb5fb481f8fa1a5f26 to your computer and use it in GitHub Desktop.
CircuitPython code for converting TCA8418 i2c keyboard matrix to USB
import time
import board
import busio
import usb_hid
import digitalio
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
KEY_MAP = {
1: Keycode.EQUALS,
2: Keycode.ONE,
3: Keycode.TWO,
4: Keycode.THREE,
5: Keycode.FOUR,
6: Keycode.FIVE,
7: Keycode.SIX,
8: Keycode.SEVEN,
9: Keycode.EIGHT,
10: Keycode.NINE,
11: Keycode.Q,
12: Keycode.W,
13: Keycode.E,
14: Keycode.R,
15: Keycode.T,
16: Keycode.Y,
17: Keycode.U,
18: Keycode.I,
19: Keycode.O,
20: Keycode.P,
21: Keycode.A,
22: Keycode.S,
23: Keycode.D,
24: Keycode.F,
25: Keycode.G,
26: Keycode.H,
27: Keycode.J,
28: Keycode.K,
29: Keycode.L,
30: Keycode.ENTER,
31: Keycode.TAB,
32: Keycode.Z,
33: Keycode.X,
34: Keycode.C,
35: Keycode.V,
36: Keycode.B,
37: Keycode.N,
38: Keycode.M,
39: Keycode.UP_ARROW,
40: Keycode.DOWN_ARROW,
41: Keycode.ESCAPE,
43: Keycode.ALT,
44: Keycode.SPACE,
45: Keycode.CONTROL,
46: Keycode.FORWARD_SLASH,
47: Keycode.RIGHT_SHIFT,
49: Keycode.LEFT_ARROW,
50: Keycode.RIGHT_ARROW,
51: Keycode.LEFT_SHIFT,
52: Keycode.ZERO,
53: Keycode.MINUS,
54: Keycode.BACKSPACE,
55: Keycode.PERIOD
}
KEY_MAP_FN = {
1: Keycode.F12,
2: Keycode.F1,
3: Keycode.F2,
4: Keycode.F3,
5: Keycode.F4,
6: Keycode.F5,
7: Keycode.F6,
8: Keycode.F7,
9: Keycode.F8,
10: Keycode.F9,
18: Keycode.LEFT_BRACKET,
19: Keycode.RIGHT_BRACKET,
28: Keycode.QUOTE,
36: Keycode.GRAVE_ACCENT,
37: Keycode.POUND,
38: Keycode.SEMICOLON,
39: Keycode.PAGE_UP,
40: Keycode.PAGE_DOWN,
46: Keycode.BACKSLASH,
49: Keycode.HOME,
50: Keycode.END,
51: Keycode.GUI,
52: Keycode.F10,
53: Keycode.F11,
54: Keycode.BACKSPACE,
55: Keycode.COMMA,
}
i2c = busio.I2C(board.GP5, board.GP4, timeout=1000) # Pi Pico RP2040
while not i2c.try_lock():
pass
keyboard = Keyboard(usb_hid.devices)
def i2c_read(register):
result = bytearray(1)
i2c.writeto(0x34,bytes([register]))
i2c.readfrom_into(0x34,result)
return result[0]
def write(register,value):
i2c.writeto(0x34,bytes([register,value]))
write(0x1D,0x3F)
write(0x1E,255)
write(0x1F,3)
write(0x01,0x99)
fn_down = False
led_b = digitalio.DigitalInOut(board.LED_B)
led_b.direction = digitalio.Direction.OUTPUT
led_r = digitalio.DigitalInOut(board.LED_R)
led_r.direction = digitalio.Direction.OUTPUT
while True:
key_event = i2c_read(0x04)
if key_event > 0:
key_code = key_event & 0x7F
key_down = (key_event & 0x80) >> 7 == 1
if key_code == 42: fn_down = key_down
print(key_code,fn_down)
led_b.value = not fn_down
led_r.value = not fn_down
if key_down:
if fn_down and key_code in KEY_MAP_FN:
keyboard.press(KEY_MAP_FN[key_code])
elif key_code in KEY_MAP:
keyboard.press (KEY_MAP[key_code])
else:
if key_code in KEY_MAP_FN:
keyboard.release(KEY_MAP_FN[key_code])
if key_code in KEY_MAP:
keyboard.release(KEY_MAP[key_code])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment