-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-uinput.py
61 lines (48 loc) · 1.15 KB
/
example-uinput.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
import serial
import uinput
import sys
import time
uinput.BTN_UP = (1, 264)
uinput.BTN_DOWN = (1, 265)
uinput.BTN_LEFT = (1, 266)
uinput.BTN_RIGHT = (1, 267)
events = (
uinput.BTN_0,
uinput.BTN_1,
uinput.BTN_2,
uinput.BTN_3,
uinput.BTN_4,
uinput.BTN_5,
uinput.BTN_6,
uinput.BTN_7,
uinput.BTN_UP,
uinput.BTN_DOWN,
uinput.BTN_LEFT,
uinput.BTN_RIGHT,
uinput.ABS_X + (0, 255, 0, 0),
uinput.ABS_Y + (0, 255, 0, 0),
)
device = uinput.Device(events)
buttons = {1:uinput.BTN_0, 2:uinput.BTN_1, 3:uinput.BTN_6, 4:uinput.BTN_7, 5:uinput.BTN_UP, 6:uinput.BTN_DOWN, 7:uinput.BTN_LEFT, 8:uinput.BTN_RIGHT, 9:uinput.BTN_2, 10:uinput.BTN_3, 11:uinput.BTN_4, 12:uinput.BTN_5}
ser = serial.Serial('/dev/ttyACM0', 9600)
sc = []
c = ''
while 1:
c = ser.read()
if c != '#':
try:
sc.append(int(c))
except:
pass
else:
while len(sc) < 12:
sc.insert(0,0)
for k, i in enumerate(reversed(sc)):
print k+1,i
device.emit(buttons[k+1], i)
print "\n"*3
sc = []
time.sleep(0.0001)
if(ser.isOpen()):
print "Serial connection is still open."
ser.close()