root / trunk / swipe / manualtron.py @ 219
History | View | Annotate | Download (4.31 KB)
1 |
#!/usr/bin/python -i
|
---|---|
2 |
|
3 |
"""
|
4 |
This file is part of Tooltron.
|
5 |
|
6 |
Tooltron is free software: you can redistribute it and/or modify
|
7 |
it under the terms of the Lesser GNU General Public License as published by
|
8 |
the Free Software Foundation, either version 3 of the License, or
|
9 |
(at your option) any later version.
|
10 |
|
11 |
Tooltron is distributed in the hope that it will be useful,
|
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
Lesser GNU General Public License for more details.
|
15 |
You should have received a copy of the Lesser GNU General Public License
|
16 |
along with Tooltron. If not, see <http://www.gnu.org/licenses/>.
|
17 |
|
18 |
Copyright 2009 Bradford Neuman <bneuman@andrew.cmu.edu>
|
19 |
|
20 |
"""
|
21 |
|
22 |
# This allows you to interactively send crap to tooltron
|
23 |
|
24 |
import re |
25 |
import sys |
26 |
import serial |
27 |
import MySQLdb |
28 |
import getpass |
29 |
from time import * |
30 |
|
31 |
keypadTimeout = 11 #in seconds |
32 |
|
33 |
TT_GET_KEY = 'k'
|
34 |
TT_SEND_KEY = 's' ############ put this in tooltron.py |
35 |
TT_ACK = 'a'
|
36 |
TT_NACK = 'n'
|
37 |
TT_TO = 'f'
|
38 |
TT_TIMEOUT = 't'
|
39 |
TT_ON = 'o'
|
40 |
TT_PING = 'g'
|
41 |
|
42 |
MAX_PAYLOAD_LEN = 32
|
43 |
|
44 |
BAUD_RATE = 9600
|
45 |
|
46 |
#fails until a warning is sent
|
47 |
MAX_TOOL_FAILS = 5
|
48 |
|
49 |
import time |
50 |
|
51 |
def printMsg(msg): |
52 |
if len(msg) < 5: |
53 |
print "ERROR: trying to print message thats too small!" |
54 |
return
|
55 |
if msg[0] == '^': |
56 |
print '^', |
57 |
else:
|
58 |
print str(ord(msg[0])) |
59 |
|
60 |
for c in msg[1:]: |
61 |
print str(ord(c)), |
62 |
|
63 |
print
|
64 |
|
65 |
|
66 |
if len(sys.argv) < 2: |
67 |
print "usage: manualtron.py /path/to/bus/device" |
68 |
|
69 |
else:
|
70 |
|
71 |
bus = serial.Serial(sys.argv[1], BAUD_RATE, timeout = 2) |
72 |
bus.flushInput() |
73 |
print bus
|
74 |
|
75 |
#tn is an actual tool number
|
76 |
# ^ <src> <dest> <cmd> <plen> <payload> <crc>
|
77 |
def sendMessage(tn, cmd, msg): |
78 |
if len(msg) > MAX_PAYLOAD_LEN: |
79 |
print "ERROR: message too long! max size is " + str(MAX_PAYLOAD_LEN) |
80 |
else:
|
81 |
body = chr(1) + chr(tn) + cmd + chr(len(msg)) + msg |
82 |
crc = 0
|
83 |
for c in body: |
84 |
crc = crc ^ ord(c)
|
85 |
msg = '^' + body + chr(crc) |
86 |
print "sending packet to tool",tn |
87 |
printMsg(msg) |
88 |
bus.write(msg) |
89 |
|
90 |
def sendTool(t): |
91 |
tn = t |
92 |
print "seding power to tool ID",tn |
93 |
sendMessage(tn, TT_ON, "")
|
94 |
|
95 |
def sendKeyRequest(): |
96 |
print "seding key request" |
97 |
sendMessage(2, TT_GET_KEY, "") |
98 |
|
99 |
def sendAck(toolNum): |
100 |
print "seding ACK to",toolNum |
101 |
sendMessage(toolNum, TT_ACK, "")
|
102 |
|
103 |
def sendNack(toolNum): |
104 |
print "seding ACK to",toolNum |
105 |
sendMessage(toolNum, TT_NACK, "")
|
106 |
|
107 |
# This function eats the rest of the packet where data is what we have so far
|
108 |
def flushPacket(data): |
109 |
while len(data) < 6: |
110 |
data += bus.read(1)
|
111 |
|
112 |
plen = data[4]
|
113 |
if plen > 0: |
114 |
bus.read(ord(plen))
|
115 |
|
116 |
|
117 |
# Returns [src, dest, cmd, data]
|
118 |
def readMessage(): |
119 |
if bus.read(1) == '^': |
120 |
src = bus.read(1)
|
121 |
if src == chr(1): #reflection |
122 |
print "(reflection)" |
123 |
flushPacket('^' + src)
|
124 |
return readMessage()
|
125 |
|
126 |
dest = bus.read(1)
|
127 |
cmd = bus.read(1)
|
128 |
plen = bus.read(1)
|
129 |
data = bus.read(ord(plen))
|
130 |
x = bus.read(1)
|
131 |
|
132 |
print "got packet" |
133 |
printMsg('^' + src + dest + cmd + plen + data + x)
|
134 |
|
135 |
crc = ord(src) ^ ord(dest) ^ ord(cmd) ^ ord(plen) |
136 |
for d in data: |
137 |
crc ^= ord(d)
|
138 |
|
139 |
if crc == ord(x): |
140 |
return [src, dest, cmd, data]
|
141 |
else:
|
142 |
print "xor fail. got", str(ord(x)), "should have been got",crc |
143 |
return 0 #TODO: error |
144 |
|
145 |
def readKey(): |
146 |
m = readMessage() |
147 |
if m == 0: |
148 |
return 0 |
149 |
[src, dest, cmd, data] = m |
150 |
if cmd == TT_SEND_KEY:
|
151 |
return data[0] |
152 |
else:
|
153 |
return 0 |
154 |
|
155 |
#returns [src, dest, command] or [] on error
|
156 |
# because of reflection, this will quietly ignore packets send by the server
|
157 |
def readTool(): |
158 |
m = readMessage() |
159 |
if m == 0: |
160 |
return []
|
161 |
[src, dest, cmd, data] = m |
162 |
return [src, dest, cmd]
|
163 |
|
164 |
def checkAck(t): |
165 |
tn = t |
166 |
m = readTool() |
167 |
|
168 |
if m== []:
|
169 |
return False |
170 |
|
171 |
|
172 |
return m[0] == chr(tn) and m[1] == chr(1) and m[2] == TT_ACK |
173 |
|
174 |
def key(): |
175 |
sendMessage(2,TT_GET_KEY,"") |
176 |
print readKey()
|
177 |
|