-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard.py
executable file
·71 lines (55 loc) · 2.17 KB
/
keyboard.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
#!/usr/bin/env python3
import sys
import time
import RPi.GPIO as GPIO
COLUMNS = [25, 0, 1, 5, 6, 12, 13, 19, 21]
ROWS = [22, 23, 24, 10, 9, 11, 8, 7, 16, 26, 20]
state = [None, None, None, None, None, None, None, None, None, None, None]
shift = False
def character(column, row):
return [[None, "z", "h", "\t", "1", "u", "q", "7", None, None, None],
[" ", "x", "j", "a", "2", "i", "w", "8", None, None, None],
[None, "c", "k", "s", "3", "o", "e", "9", None, None, None],
[",", "v", "m", "d", "4", "p", "r", "0", None, None, None],
[None, "b", ".", "f", "5", "l", "t", "\b \b", None, None, None],
[None, "n", None, "g", "6", "\n", "y", "~", None, None, None],
[0x01, None, None, None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None, None, None, None]][column][row]
def event(character, down):
global shift
if character is None:
return
if character is 0x01:
shift = down
return
if down:
if shift:
character = character.upper()
sys.stdout.write(character)
sys.stdout.flush()
GPIO.setmode(GPIO.BCM)
for column in COLUMNS:
print("Configuring BCM %d..." % column)
GPIO.setup(column, GPIO.OUT)
for row in ROWS:
print("Configuring BCM %d..." % row)
GPIO.setup(row, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
while True:
for column_index, column in enumerate(COLUMNS):
output = []
GPIO.output(column, 1)
for row in ROWS:
output.append(GPIO.input(row))
GPIO.output(column, 0)
row_index = None
try:
row_index = output.index(1)
except ValueError:
pass
if state[column_index] != row_index:
if state[column_index] is not None:
event(character(column_index, state[column_index]), False)
if row_index is not None:
event(character(column_index, row_index), True)
state[column_index] = row_index