slotUI/freeslot.py

changeset 26
4af697fa5ea9
parent 25
646ee4dc3a6b
child 27
3e617fcf999a
equal deleted inserted replaced
25:646ee4dc3a6b 26:4af697fa5ea9
1 """ 1 """
2 FreeSlot project 2 FreeSlot project
3 Blackbox communication library 3 Blackbox communication library
4 """ 4 """
5 5
6 from serial import Serial
7
6 class SerialCommunicator(): 8 class SerialCommunicator():
7 def __init__(self, device, speed): 9 def __init__(self, device, speed):
8 self.device = device 10 self.device = device
9 self.speed = speed 11 self.speed = speed
10 self.port = None 12 self.com = None
11 self.connected = False 13 self.connected = False
12 14
13 def connect(self): 15 def connect(self):
14 if self.connected: 16 if self.connected:
15 return True 17 return True
18 self.com = Serial(self.device, baudrate=self.speed, xonxoff=0, timeout=1)
19 self.connected = True
20 return True
16 21
17 def disconnect(self): 22 def disconnect(self):
23 self.com = None
18 return True 24 return True
19 25
20 def write(self): 26 def write(self, msg, getanswer=False):
21 return True 27 self.com.write(msg + "\n")
28 if getanswer:
29 return self.readline()
30 return None
22 31
23 def read(self): 32 def readline(self):
24 return True 33 answer = self.com.readline()
34 return string.strip(answer, "\n")
25 35
26 class Blackbox(): 36 class Blackbox():
27 def __init__(self): 37 def __init__(self):
28 self.com = None 38 self.com = None
29 self.info = None 39 self.info = None
30 40
31 def connect(self, device="/dev/ttyUSB0", speed=115200): 41 def connect(self, device="/dev/ttyUSB0", speed=115200):
32 if self.com == None: 42 if self.com == None:
33 self.com = SerialCommunicator(device, speed) 43 self.com = SerialCommunicator(device, speed)
44 if self.com.connected:
45 self.com.disconnect()
34 self.com.connect() 46 self.com.connect()
35 self.info = self.readinfo() 47 self.info = self.readinfo()
36 48
37 def disconnect(): 49 def disconnect():
38 self.com.disconnect() 50 self.com.disconnect()
39 51
40 def readinfo(): 52 def readinfo():
41 """ 53 """
42 Read complete Information from connected box 54 Read complete Information from connected box
55 This does not include race+car status!
43 """ 56 """
44 return None 57 return None
58
59 def progcar(self, carid, command, value):
60 """
61 Send program packets to specified car id
62 valid command: speed, brake
63 valid value: 4 bit integer (0..15)
64 """
65 return True
66
67 def setmode(self, mode):
68 """
69 Switch the Blackbox mode
70 Valid modes are: idle, prepare, race
71 note: box will permanently send status info in race mode, so no
72 polling is required
73 """
74 return True
75
76 def getmode(self):
77 self.readinfo()
78 return self.info["mode"]

mercurial