-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytx.py
55 lines (42 loc) · 1.23 KB
/
pytx.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
import termios, sys, os
import serial
import time
TERMIOS = termios
KEYS = ['a','A','w','W','s','S','d','D']
SERIALPORT = '/dev/ttyUSB1'
SERIALBR = 9600
MSG_PREFIX = 'cbots2k10-'
def getkey():
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
new[6][TERMIOS.VMIN] = 1
new[6][TERMIOS.VTIME] = 0
termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
c = None
try:
c = os.read(fd, 1)
finally:
termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
return c
#def initSerial():
# try:
# arduino = serial.Serial(SERIALPORT, SERIALBR)
# print "Successfuly connected to",SERIALPORT,"at",SERIALBR
# except Exception:
# print "Failed to aquire serial connetion to",SERIALPORT,". Moving on..."
if __name__ == '__main__':
print 'Codebits 2010 Robot Control Console'
#initSerial()
#arduino = serial.Serial(SERIALPORT, SERIALBR)
arduino = serial.Serial('/dev/ttyUSB1', 9600)
while True:
c = getkey()
if c in KEYS:
print "captured key", c
arduino.write(c)
time.sleep(0.255)
elif c == 'q':
break
print "Laters."