slotUI/freeslot.py

Sat, 03 Dec 2011 09:26:20 +0100

author
Malte Bayer <mbayer@neo-soft.org>
date
Sat, 03 Dec 2011 09:26:20 +0100
changeset 27
3e617fcf999a
parent 26
4af697fa5ea9
child 35
00166228a419
permissions
-rw-r--r--

added exception, started SlotCli class

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

mercurial