slotUI/SlotCli.py

changeset 54
68eca8057d68
parent 50
84b8ab4cd79e
child 55
9293f3efcc06
--- a/slotUI/SlotCli.py	Fri Dec 09 09:40:15 2011 +0100
+++ b/slotUI/SlotCli.py	Fri Dec 09 11:54:19 2011 +0100
@@ -7,22 +7,116 @@
 from freeslot import Blackbox
 from optparse import OptionParser
 import time, sys
+from copy import copy
+import curses
 
-VERSION = "1.0"
+VERSION = "1.1"
+MAXSLOTS = 6
+TERM = {
+    "caption": "\033[1;37m\033[1;44m",
+    "text": "\033[1;30m",
+    }
 
 class SlotCli():
     def __init__(self):
         self.box = Blackbox()
         self.box.connect()
+        self.slot_dummy = {
+            "name": "Unnamed",
+            "laps": 0,
+            "last": 0,
+            "best": 0,
+            "fuel": 0,
+            "position": 0,
+            }
 
-    def run(self):
+        self.slot = [
+            copy(self.slot_dummy), copy(self.slot_dummy),
+            copy(self.slot_dummy), copy(self.slot_dummy),
+            copy(self.slot_dummy), copy(self.slot_dummy),
+            ]
+
+    def update_positions(self):
+        for idx in range(MAXSLOTS):
+            self.slot[idx]["position"] = idx + 1
+        # TODO
+
+    def render_slots(self):
+        self.update_positions()
+        self.scr.addstr(3,0,
+            "Pos | #/Name          | Laps | Best   | Last   | Fuel |",
+            curses.color_pair(2))
+        for idx in range(MAXSLOTS):
+            self.scr.addstr((3 + self.slot[idx]["position"]), 0,
+                "%3i | %15s | %4i | %5.2fs | %5.2fs | %3i%% |" % (
+                self.slot[idx]["position"],
+                self.slot[idx]["name"],
+                self.slot[idx]["laps"],
+                self.slot[idx]["best"],
+                self.slot[idx]["last"],
+                self.slot[idx]["fuel"],
+                ) )
+
+    def cleartop(self):
+        self.scr.addstr(0,0, "%80s" % "Live monitor running, press keys to control or (q)uit")
+        self.scr.addstr(1,0, "%80s" % " ")
+
+    def readName(self, slot):
+        curses.echo()
+        self.scr.addstr(0,0, "Enter Name for Controller %i [%s]:" % (
+            slot + 1,
+            self.slot[slot]["name"]),
+            curses.color_pair(1))
+        self.scr.refresh()
+        name = self.scr.getstr(1,0, 15)
+        if name != "":
+            self.slot[slot]["name"] = name
+        self.cleartop()
+        curses.noecho()
+
+    def monitor(self):
         """
+        Live Monitor on the console
         Keyboard loop to control it???
         """
+        self.scr = curses.initscr()
+        curses.start_color()
+        curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK) # standard text
+        curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLUE) # label
+        curses.noecho() # disable key echo
+        curses.cbreak() # do not buffer keypresses
+        self.scr.keypad(1) # enable special keys
+        self.scr.nodelay(1) # disable delay on readkey
+
+        while 1:
+            key = self.scr.getch()
+            if key == ord('q'): break
+            elif key == ord(' '): self.cyclemode()
+            elif key == ord('1'): self.readName(0)
+            elif key == ord('2'): self.readName(1)
+            elif key == ord('3'): self.readName(2)
+            elif key == ord('4'): self.readName(3)
+            elif key == ord('5'): self.readName(4)
+            elif key == ord('6'): self.readName(5)
+
+            self.render_slots()
+            self.scr.refresh()
+
+        # terminate
+        curses.nocbreak()
+        self.scr.keypad(0)
+        curses.echo()
+        curses.endwin()
+        
+        
+        print repr(self.slot)
         return None
 
 if __name__ == "__main__":
     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("--carid", dest="carid",
         help="Required for programming a car directly", metavar="[0-5]")
     parser.add_option("--fuel", dest="fuel",
@@ -42,6 +136,10 @@
     cli = SlotCli()
     # should a CLI function be started?
 
+    if options.live == True:
+        # start the live monitor
+        cli.monitor()
+        sys.exit(0)
 
     # check commandline if we have to program something
     if options.carid == None:

mercurial