Skip to content

Commit 360b202

Browse files
authored
Merge pull request #1189 from adafruit/Vote_keyboard
initial commit
2 parents 2a92546 + 61dcf8d commit 360b202

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

VOTE_Keyboard/code.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import board
2+
from digitalio import DigitalInOut, Direction, Pull
3+
import adafruit_dotstar as dotstar
4+
from adafruit_hid.keyboard import Keyboard
5+
from adafruit_hid.keycode import Keycode
6+
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
7+
8+
dot = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
9+
dot[0] = (0, 0, 0)
10+
11+
kbd = Keyboard()
12+
kbdLayout = KeyboardLayoutUS(kbd)
13+
state = []
14+
pins = {}
15+
buttonMap = [
16+
dict(row="D4", col="D0", id=1),
17+
dict(row="D4", col="D1", id=2),
18+
dict(row="D4", col="D2", id=3),
19+
dict(row="D3", col="D2", id=4),
20+
dict(row="D3", col="D0", id=5),
21+
dict(row="D3", col="D1", id=6)]
22+
23+
# Set up row pins
24+
for pin in ["D4", "D3"]:
25+
p = DigitalInOut(getattr(board, pin))
26+
p.direction = Direction.OUTPUT
27+
pins[pin] = p
28+
29+
# Set up column pins
30+
for pin in ["D0", "D1", "D2"]:
31+
p = DigitalInOut(getattr(board, pin))
32+
p.direction = Direction.INPUT
33+
p.pull = Pull.DOWN
34+
pins[pin] = p
35+
36+
buttonIDtoKeycode = {
37+
1: Keycode.V,
38+
2: Keycode.O,
39+
3: Keycode.T,
40+
4: Keycode.E,
41+
5: Keycode.SPACE,
42+
6: Keycode.ENTER}
43+
44+
while True:
45+
# Compare old and new state
46+
oldState = state
47+
newState = []
48+
newBtn = None
49+
for button in buttonMap:
50+
r = pins[button["row"]]
51+
r.value = True
52+
if pins[button["col"]].value:
53+
newState += [button["id"]]
54+
if not button["id"] in oldState:
55+
newBtn = button["id"]
56+
r.value = False
57+
# Press & release keys
58+
for oldID in oldState:
59+
if not oldID in newState:
60+
kbd.release(buttonIDtoKeycode[oldID])
61+
dot[0] = (0, 0, 0)
62+
if newBtn:
63+
kbd.press(buttonIDtoKeycode[newBtn])
64+
dot[0] = (255, 0, 0)
65+
state = newState

0 commit comments

Comments
 (0)