slotUI/SlotCli.py

changeset 78
d9126a55295c
parent 77
cede78304992
child 81
4f2f94a043fd
equal deleted inserted replaced
77:cede78304992 78:d9126a55295c
5 """ 5 """
6 6
7 from freeslot import Blackbox, LOGLEVEL 7 from freeslot import Blackbox, LOGLEVEL
8 from optparse import OptionParser 8 from optparse import OptionParser
9 from operator import itemgetter 9 from operator import itemgetter
10 import sys 10 from subprocess import Popen, PIPE
11 import sys, os
11 from copy import copy 12 from copy import copy
12 import curses 13 import curses
13 14
14 VERSION = "1.3" 15 VERSION = "1.3"
15 MAXSLOTS = 6 16 MAXSLOTS = 6
18 "text": "\033[1;30m", 19 "text": "\033[1;30m",
19 } 20 }
20 21
21 # disable debug log output 22 # disable debug log output
22 LOGLEVEL = 10 23 LOGLEVEL = 10
24
25 SOUNDS = {
26 "countdown_start": os.path.abspath("sound/countdown.mp3"),
27 "race_start": os.path.abspath("sound/racestart.mp3"),
28 "race_prepare": os.path.abspath("sound/siren.mp3"),
29 "lap_record": os.path.abspath("sound/laprecord.mp3"),
30 }
31
32 def trigger_sound(what):
33 if what in SOUNDS:
34 Popen(["/usr/bin/mpg123", "-q", SOUNDS[what]])
35 #os.spawnlp(os.P_NOWAIT, "/usr/bin/mpg123", "mpg123", SOUNDS[what])
36 #Popen(["/usr/bin/mpg123", SOUNDS[what]]).pid
23 37
24 class SlotCli(): 38 class SlotCli():
25 def __init__(self): 39 def __init__(self):
26 self.box = Blackbox() 40 self.box = Blackbox()
27 self.box.connect() 41 self.box.connect()
43 copy(self.slot_dummy), copy(self.slot_dummy), 57 copy(self.slot_dummy), copy(self.slot_dummy),
44 copy(self.slot_dummy), copy(self.slot_dummy), 58 copy(self.slot_dummy), copy(self.slot_dummy),
45 ] 59 ]
46 self.reset_slots() 60 self.reset_slots()
47 self.sysclk = 0.00 61 self.sysclk = 0.00
62 self.bestlap = 9999999.00
48 63
49 def reset_slots(self): 64 def reset_slots(self):
50 idx = 0 65 idx = 0
51 for slt in self.slot: 66 for slt in self.slot:
52 slt["laps"] = 0 67 slt["laps"] = 0
56 slt["position"] = idx 71 slt["position"] = idx
57 slt["car"] = idx # used for sort order calculation 72 slt["car"] = idx # used for sort order calculation
58 slt["status"] = self.slot_dummy["status"] 73 slt["status"] = self.slot_dummy["status"]
59 slt["clk"] = 0 74 slt["clk"] = 0
60 idx += 1 75 idx += 1
76 self.bestlap = 0.00
61 77
62 def update_positions(self): 78 def update_positions(self):
63 order1 = sorted(self.slot, key=itemgetter( 79 order1 = sorted(self.slot, key=itemgetter(
64 "clk")) 80 "clk"))
65 order2 = sorted(self.slot, key=itemgetter( 81 order2 = sorted(self.slot, key=itemgetter(
176 self.sysclk = int(data[5], 16) / 2000.00 192 self.sysclk = int(data[5], 16) / 2000.00
177 self.slot[slot]["laps"] = l 193 self.slot[slot]["laps"] = l
178 self.slot[slot]["last"] = t 194 self.slot[slot]["last"] = t
179 self.slot[slot]["clk"] = self.sysclk 195 self.slot[slot]["clk"] = self.sysclk
180 if (self.slot[slot]["best"] > t) or (self.slot[slot]["best"] == 0): self.slot[slot]["best"] = t 196 if (self.slot[slot]["best"] > t) or (self.slot[slot]["best"] == 0): self.slot[slot]["best"] = t
197 if self.bestlap > t:
198 trigger_sound("lap_record")
199 self.bestlap = t
200
181 self.slot[slot]["status"] = "IN-RACE" 201 self.slot[slot]["status"] = "IN-RACE"
182 self.render_slots() 202 self.render_slots()
183 203
184 if rx[:2] == "F:": 204 if rx[:2] == "F:":
185 # update fuel level 205 # update fuel level
212 self.render_slots() 232 self.render_slots()
213 233
214 if rx == "!RACE PREPARE": 234 if rx == "!RACE PREPARE":
215 # reset current race status 235 # reset current race status
216 # and display PREPARE PHASE 236 # and display PREPARE PHASE
237 self.reset_slots()
217 for slot in range(MAXSLOTS): 238 for slot in range(MAXSLOTS):
218 self.slot[slot]["status"] = "Prepare" 239 self.slot[slot]["status"] = "Prepare"
240 trigger_sound("race_prepare")
219 241
220 if rx == "!RACE START": 242 if rx == "!RACE START":
221 for slot in range(MAXSLOTS): 243 for slot in range(MAXSLOTS):
222 if self.slot[slot]["status"] == "~~~~~~~~~~": 244 if self.slot[slot]["status"] == "~~~~~~~~~~":
223 self.slot[slot]["status"] = "Idle" 245 self.slot[slot]["status"] = "Idle"
246 trigger_sound("race_start")
224 247
225 if rx == "!COUNTDOWN": 248 if rx == "!COUNTDOWN":
226 # countdown initiated 249 # countdown initiated
227 for slot in range(MAXSLOTS): 250 for slot in range(MAXSLOTS):
228 self.slot[slot]["status"] = "~~~~~~~~~~" 251 self.slot[slot]["status"] = "~~~~~~~~~~"
252 trigger_sound("countdown_start")
229 253
230 self.scr.addstr(10,31, 254 self.scr.addstr(10,31,
231 "Race Timer: %7.3f min" % (self.sysclk / 60), 255 "Race Timer: %7.3f min" % (self.sysclk / 60),
232 curses.color_pair(2)) 256 curses.color_pair(2))
233 257

mercurial