-
Notifications
You must be signed in to change notification settings - Fork 776
/
code.py
executable file
·65 lines (58 loc) · 1.65 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# SPDX-FileCopyrightText: 2020 Collin Cunningham for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import board
from digitalio import DigitalInOut, Direction, Pull
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
kbd = Keyboard(usb_hid.devices)
kbdLayout = KeyboardLayoutUS(kbd)
state = []
pins = {}
buttonMap = [
dict(row="D4", col="D0", id=1),
dict(row="D4", col="D1", id=2),
dict(row="D4", col="D2", id=3),
dict(row="D3", col="D2", id=4),
dict(row="D3", col="D0", id=5),
dict(row="D3", col="D1", id=6)]
# Set up row pins
for pin in ["D4", "D3"]:
p = DigitalInOut(getattr(board, pin))
p.direction = Direction.OUTPUT
pins[pin] = p
# Set up column pins
for pin in ["D0", "D1", "D2"]:
p = DigitalInOut(getattr(board, pin))
p.direction = Direction.INPUT
p.pull = Pull.DOWN
pins[pin] = p
buttonIDtoKeycode = {
1: Keycode.V,
2: Keycode.O,
3: Keycode.T,
4: Keycode.E,
5: Keycode.SPACE,
6: Keycode.ENTER}
while True:
# Compare old and new state
oldState = state
newState = []
newBtn = None
for button in buttonMap:
r = pins[button["row"]]
r.value = True
if pins[button["col"]].value:
newState += [button["id"]]
if not button["id"] in oldState:
newBtn = button["id"]
r.value = False
# Press & release keys
for oldID in oldState:
if not oldID in newState:
kbd.release(buttonIDtoKeycode[oldID])
if newBtn:
kbd.press(buttonIDtoKeycode[newBtn])
state = newState