-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
182 lines (158 loc) · 5.93 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import usb_hid
from adafruit_hid.keyboard import Keyboard
from keycode_win_gr import Keycode
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
import board
import digitalio
from busio import I2C
import time
import microcontroller
# Function to set a pin as an input (external pullup is soldered to the board) so it's high unless grounded by a key press
def go_z(p):
pins[p].direction = digitalio.Direction.INPUT #set as input
pins[p].pull = None
# Function to set a pin as an output and drive it to a logic low (0 volts)
def go_0(p):
pins[p].pull = None #disable pullup
pins[p].direction = digitalio.Direction.OUTPUT #set as output
pins[p].value = 0 #drive low
# definition of matrix keymap (which pin connection belongs to which key)
keymap=[
[ 0, 15, Keycode.PRINT_SCREEN ],
[ 0, 14, Keycode.ONE ],
[ 0, 10, Keycode.CAPS_LOCK ],
[ 0, 3, Keycode.X ],
[ 0, 12, Keycode.C ],
[ 0, 4, Keycode.B ],
[ 0, 13, Keycode.COMMA ],
[ 0, 2, Keycode.ALTGR ],
[ 0, 9, Keycode.DOWN_ARROW ],
[ 1, 7, Keycode.W ],
[ 1, 11, Keycode.A ],
[ 1, 6, Keycode.S ],
[ 1, 21, Keycode.Y ],
[ 1, 22, 0 ], #this is the FN key - it isn't reported via USB, but handeled internally
[ 2, 22, Keycode.LEFT_ALT ],
[ 3, 17, Keycode.FOUR ],
[ 3, 8, Keycode.FIVE ],
[ 3, 6, Keycode.E ],
[ 3, 7, Keycode.R ],
[ 3, 21, Keycode.D ],
[ 3, 11, Keycode.F ],
[ 3, 22, Keycode.OEM_102 ],
[ 4, 17, Keycode.EIGHT ],
[ 4, 8, Keycode.NINE ],
[ 4, 6, Keycode.Z ],
[ 4, 7, Keycode.U ],
[ 4, 11, Keycode.J ],
[ 4, 21, Keycode.N ],
[ 5, 17, Keycode.AKUT ],
[ 5, 7, Keycode.EQUALS ],
[ 5, 6, Keycode.ENTER ],
[ 5, 11, Keycode.FORWARD_SLASH ],
[ 5, 22, Keycode.APPLICATION ],
[ 6, 15, Keycode.THREE ],
[ 6, 10, Keycode.TAB ],
[ 6, 14, Keycode.Q ],
[ 6, 13, Keycode.I ],
[ 6, 12, Keycode.H ],
[ 6, 16, Keycode.GRAVE_ACCENT ],
[ 6, 9, Keycode.PAGE_UP ],
[ 7, 10, Keycode.F5 ],
[ 7, 14, Keycode.F8 ],
[ 7, 15, Keycode.BACKSPACE ],
[ 7, 12, Keycode.T ],
[ 7, 13, Keycode.O ],
[ 7, 16, Keycode.QUOTE ],
[ 8, 10, Keycode.ESCAPE ],
[ 8, 15, Keycode.F10 ],
[ 8, 14, Keycode.F11 ],
[ 8, 9, Keycode.INSERT ],
[ 8, 12, Keycode.SEVEN ],
[ 8, 13, Keycode.LEFT_BRACKET ],
[ 8, 16, Keycode.SEMICOLON ],
[ 9, 17, Keycode.DELETE ],
[ 9, 11, Keycode.PAGE_DOWN ],
[ 9, 22, Keycode.LEFT_ARROW ],
[ 9, 21, Keycode.RIGHT_ARROW ],
[ 10, 11, Keycode.F1 ],
[ 10, 21, Keycode.F2 ],
[ 10, 22, Keycode.F6 ],
[ 10, 17, Keycode.ZIRKUMFLEX ],
[ 11, 14, Keycode.F4 ],
[ 11, 15, Keycode.TWO ],
[ 11, 12, Keycode.G ],
[ 11, 13, Keycode.L ],
[ 11, 16, Keycode.MINUS ],
[ 11, 19, Keycode.RIGHT_SHIFT ],
[ 12, 17, Keycode.SIX ],
[ 12, 21, Keycode.V ],
[ 12, 22, Keycode.SPACE ],
[ 13, 17, Keycode.ZERO ],
[ 13, 21, Keycode.K ],
[ 13, 22, Keycode.M ],
[ 14, 21, Keycode.F3 ],
[ 14, 22, Keycode.F7 ],
[ 14, 17, Keycode.F12 ],
[ 15, 17, Keycode.F9 ],
[ 15, 22, Keycode.PAUSE ],
[ 15, 21, Keycode.UP_ARROW ],
[ 16, 17, Keycode.P ],
[ 16, 21, Keycode.PERIOD ],
[ 18, 22, Keycode.LEFT_CONTROL ],
[ 18, 21, Keycode.RIGHT_CONTROL ],
[ 19, 21, Keycode.LEFT_SHIFT ],
[ 20, 22, Keycode.WINDOWS ]
]
#list that saves the current state of each key, 1 = released, 0 = pressed
keystatus = [1] * len(keymap)
#try connecting to USB HID, if it fails, reset and try again after 15 seconds.
#(this is needed in my environment because USB devices aren't accepted for the first few seconds)
try:
kbd = Keyboard(usb_hid.devices)
cc = ConsumerControl(usb_hid.devices)
except:
print("USB error! Restarting in 15 seconds")
time.sleep(15)
microcontroller.reset()
#setup the mcp23017 port expander
from adafruit_mcp230xx.mcp23017 import MCP23017
i2c = I2C(board.GP27, board.GP26, frequency=1000000)
mcp = MCP23017(i2c)
#which pins is the keyboard ribbon connector connected to?
KBD_pinnumbers = [board.GP2, board.GP3, board.GP4, board.GP5, board.GP6, board.GP7, board.GP8, board.GP9, board.GP10,
board.GP11, board.GP12, board.GP13, board.GP14, board.GP15, board.GP16, board.GP17, board.GP18, board.GP19, board.GP20,
board.GP21,
mcp.get_pin(0),mcp.get_pin(1),mcp.get_pin(2)]
pins = []
for x,p in enumerate(KBD_pinnumbers):
if x<20: pins.append(digitalio.DigitalInOut(p))
else: pins.append(p) # mcp pins are already DigitalInOut'ed and can just be copied
#save some RAM by deleting this list we won't use again
del KBD_pinnumbers
#set each pin as input
for p in range(23):
go_z(p)
while True:
for idx, line in enumerate(keymap): #check each possible combination
go_0(line[0])
reading = pins[line[1]].value #pull down, read, pull up
go_z(line[0])
if not keystatus[idx] == reading: #key status changed, report it to USB HID keyboard
keystatus[idx] = reading
report = (not line[2] == 0) #FN key isn't reported via USB but handled directly on the pico
if keystatus[13] == 0 and reading == 0: #If FN button is pressed too, handle it as FN-Combination (only on key down)
report = False
# customize these FN-combination responses to your liking
if line[2] == Keycode.DOWN_ARROW: #volume down
cc.send(ConsumerControlCode.VOLUME_DECREMENT)
if line[2] == Keycode.UP_ARROW: #volume up
cc.send(ConsumerControlCode.VOLUME_INCREMENT)
if line[2] == Keycode.F8: #mute / unmute
cc.send(ConsumerControlCode.MUTE)
if report:
if reading == 0: #New key pressed
kbd.press(line[2])
else: #Key released
kbd.release(line[2])