slotUI/SlotCli.py

changeset 103
52fd4283cd2c
parent 102
72f5eb420096
child 104
95b2c23cb973
--- a/slotUI/SlotCli.py	Sun Oct 28 11:23:18 2012 +0100
+++ b/slotUI/SlotCli.py	Sun Oct 28 16:18:53 2012 +0100
@@ -11,8 +11,9 @@
 import sys, os
 from copy import copy
 import curses
+from time import sleep
 
-VERSION = "1.6"
+VERSION = "1.7"
 MAXSLOTS = 6
 TERM = {
     "caption": "\033[1;37m\033[1;44m",
@@ -212,12 +213,104 @@
         return inp
 
 
-    def monitor_init(self):
+    def monitor_init(self, live = 1):
         """
         Send initializing commands for live monitoring
         """
         self.box.query("F1\n") # set fuel logic enabled
-        self.box.query("*1\n") # set live fuel info
+        self.box.query("*%i\n" % live) # set live fuel info
+
+    def monitor_learn(self, slot):
+        # clear garbage in UART rx buffer
+        self.box.query("*0\n") # set live fuel info
+        self.box.query("*0\n") # set live fuel info
+        while self.box.readline() != "": pass
+
+        trk = False
+        spd = 0
+        trk_old = False
+        spd_old = 0
+        clock = -1
+
+        self.monitor_init(slot + 2)
+        while 1:
+            #key = self.scr.getch()
+            #if key == ord('c'): break
+
+            # is there something in the rx buffer?
+            rx = self.box.readline()
+            if (rx != ""):
+                try:
+                    data = rx.split(":")
+                    if rx[:3] == "LN:":
+                        if clock >= 0:
+                            clock += 1
+                        spd = int(data[1], 16)
+                        trk = (data[2] != 'X')
+                        if (spd != spd_old) or (trk != trk_old):
+                            if clock < 0:
+                                clock = 0
+                            print "%i,%i,%s" % (clock, spd, trk)
+                        trk_old = trk
+                        spd_old = spd * 1
+                    if rx[:2] == "L:":
+                        # update lap time info
+                        l = int(data[2], 16)
+                        s = int(data[3]) - 1
+                        t = int(data[4], 16) / 2000.00
+                        if (slot == s):
+                            print "# lap %i complete: %3.2f seconds" % (l, t)
+                            clock = 0
+                            print "%i,%i,%s" % (clock, spd, trk)
+                except:
+                    print "RX ERROR: " % rx
+
+    def monitor_playback(self, slot, filename):
+        # clear garbage in UART rx buffer
+        self.box.query("*0\n") # set live fuel info
+        self.box.query("*0\n") # set live fuel info
+        sleep(1)
+        cli.box.speedminimum(slot, 0 )
+        while self.box.readline() != "": pass
+
+        clock = -5
+        trkfile = open(filename, "r").readlines()
+        print "Loading %s..." % filename
+
+        while 1:
+            try:
+                for l in trkfile:
+                    l = l.strip()
+                    if (l != "") and (l[:1] != "#"):
+                        print "Line: %s" % repr(l)
+                        data = l.split(",")
+                        speed = int(data[1])
+                        while (clock < int(data[0]) and (int(data[0]) > 0)):
+                            clock += 1
+                            sleep(0.07)
+                        print "CLK %i/%i -> set: %i" % (clock, int(data[0]), speed)
+                        cli.box.speedminimum(slot, speed )
+                # now wait for lap sync :)
+                while self.box.readline() != "": pass
+                rx = ""
+                while rx[:2] != "L:":
+                    rx = self.box.readline()
+                data = rx.split(":")
+                l = int(data[2], 16)
+                s = int(data[3]) - 1
+                t = int(data[4], 16) / 2000.00
+                print "# lap %i complete: %3.2f seconds" % (l, t)
+                clock = -3
+            except Exception, e:
+                print repr(e)
+                sys.exit(1)
+            except KeyboardInterrupt:
+                print "resetting"
+                cli.box.speedminimum(slot, 0 )
+                cli.box.speedminimum(slot, 0 )
+                sys.exit(0)
+                
+
 
     def monitor(self):
         """
@@ -450,6 +543,10 @@
     parser = OptionParser(version="%prog " + VERSION)
     parser.add_option("--live", dest="live", action="store_true", default=False,
         help="Run Live monitor on console", metavar="[0-5]")
+    parser.add_option("--learn", dest="learn", action="store_true", default=False,
+        help="Run Learning mode for [slot]", metavar="[0-5]")
+    parser.add_option("--teach", dest="playback", 
+        help="Playback teach file", metavar="[filename]")
 
     parser.add_option("--slot", dest="carid",
         help="Required for programming a car directly", metavar="[1-6]")
@@ -487,6 +584,16 @@
             print "Error: Invalid slot selected"
             sys.exit(1)
 
+    if options.learn:
+        # start the learn monitor
+        cli.monitor_learn(options.carid)
+        sys.exit(0)
+
+    if options.playback:
+        # start the playback monitor
+        cli.monitor_playback(options.carid, options.playback)
+        sys.exit(0)
+
     if options.fuel:
         print "setFuel: " + cli.box.progcar(int(options.carid), "fuel", int(options.fuel))
 

mercurial