36 |
46 |
37 def readline(self): |
47 def readline(self): |
38 answer = self.com.readline() |
48 answer = self.com.readline() |
39 return string.strip(answer, "\n") |
49 return string.strip(answer, "\n") |
40 |
50 |
|
51 def query(self, msg): |
|
52 retry = 0 |
|
53 response = self.write(msg, True) |
|
54 while (retry < RETRIES) and (response == "BUSY"): |
|
55 time.sleep(0.1) |
|
56 response = self.write(msg, True) |
|
57 retry += 1 |
|
58 log( DEBUG, "%i> %s\n< %s" % (retry, msg, response) ) |
|
59 return response |
|
60 |
|
61 |
41 class Blackbox(): |
62 class Blackbox(): |
42 def __init__(self): |
63 def __init__(self): |
43 self.com = None |
64 self.com = None |
44 self.info = None |
65 self.info = None |
45 |
66 |
46 def connect(self, device="/dev/ttyUSB0", speed=115200): |
67 def connect(self, device="/dev/ttyUSB0", speed=57600): |
47 if self.com == None: |
68 if self.com == None: |
48 self.com = SerialCommunicator(device, speed) |
69 self.com = SerialCommunicator(device, speed) |
49 if self.com.connected: |
70 if self.com.connected: |
50 self.com.disconnect() |
71 self.com.disconnect() |
51 self.com.connect() |
72 self.com.connect() |
52 self.info = self.readinfo() |
73 self.info = self.readinfo() |
53 |
74 |
54 def disconnect(): |
75 def disconnect(self): |
55 self.com.disconnect() |
76 self.com.disconnect() |
56 |
77 |
57 def readinfo(): |
78 def readinfo(self): |
58 """ |
79 """ |
59 Read complete Information from connected box |
80 Read complete Information from connected box |
60 This does not include race+car status! |
81 This does not include race+car status! |
61 """ |
82 """ |
62 return None |
83 return None |
63 |
84 |
64 def progcar(self, carid, command, value): |
85 def progcar(self, carid, command, value): |
65 """ |
86 """ |
66 Send program packets to specified car id |
87 Send program packets to specified car id |
67 valid command: speed, brake |
88 valid command: speed, brake, fuel |
68 valid value: 4 bit integer (0..15) |
89 valid value: 4 bit integer (0..15) |
|
90 valid carid: 0..5 |
69 """ |
91 """ |
70 return True |
92 if (carid < 0) or (carid > 5): |
|
93 return "ERR - invalid carid" |
|
94 cmd = -1 |
|
95 if command == "speed": |
|
96 cmd = 0 |
|
97 if command == "brake": |
|
98 cmd = 1 |
|
99 if command == "fuel": |
|
100 cmd = 2 |
|
101 if (cmd == -1): |
|
102 return "ERR - invalid command" |
|
103 if (value<0) or (value>15): |
|
104 return "ERR - invalid value" |
|
105 # transform value 10..15 to A..F |
|
106 if (value>9): |
|
107 value = chr(ord("A") + (value-10)) |
|
108 command = "P%i%s%i" % (cmd, value, carid) |
|
109 response = self.com.query( command ) |
|
110 return response |
|
111 |
|
112 def blinkcar(self, carid, blink): |
|
113 """ |
|
114 Set car blinking state |
|
115 """ |
|
116 if (carid < 0) or (carid > 5): |
|
117 return "ERR - invalid carid" |
|
118 if blink: |
|
119 return self.com.query( "P48%i" % carid ) |
|
120 else: |
|
121 return self.com.query( "P40%i" % carid ) |
71 |
122 |
72 def setmode(self, mode): |
123 def setmode(self, mode): |
73 """ |
124 """ |
74 Switch the Blackbox mode |
125 Switch the Blackbox mode |
75 Valid modes are: idle, prepare, race |
126 Valid modes are: idle, prepare, race |