merge

Fri, 09 Dec 2011 19:41:28 +0100

author
Malte Bayer <mbayer@neo-soft.org>
date
Fri, 09 Dec 2011 19:41:28 +0100
changeset 61
040b6b2094fb
parent 60
bfc2f2d6913c (diff)
parent 58
042c490fdfaa (current diff)
child 62
66dd5763dfb3

merge

slotUI/SlotCli.py file | annotate | diff | comparison | revisions
--- a/blackbox/main.c	Fri Dec 09 18:20:21 2011 +0100
+++ b/blackbox/main.c	Fri Dec 09 19:41:28 2011 +0100
@@ -26,16 +26,21 @@
 } u32;
 
 #define MAX_SLOTS       6
+#define FUEL_FULL       10000
+//#define FUEL_DIVISOR    25
+#define FUEL_DIVISOR    10
 typedef struct {
     unsigned speedlimit:4; // 4bits speedlimit
     unsigned speedminimum:4; // 4bits speedminimum
+    unsigned seccnt:4; // 4 bits tenth seconds counter
+    unsigned accel:4; // 4 bits last configured acceleration
     unsigned trackswitch:1; // 1bit bool
-    uint8_t fuel;
-    uint16_t jumpstart_time, laps;
+    unsigned canrefuel:1; // 1bit bool
+    uint16_t jumpstart_time, laps, fuel;
     u32 lap_time_start, lap_time;
 } cardata;
 
-static unsigned char s[8];
+static char s[8];
 static uint8_t  countdown, countdown_loops;
 uint8_t  mode = 0;
 // valid race modes:
@@ -177,9 +182,10 @@
 
 }
 
-int do_controller(uint8_t controller) {
+uint8_t do_controller(uint8_t controller) {
     // read controller X speed & encode controller data packet
     uint16_t tmp = 0;
+    uint8_t speed;
     uint8_t trackchange = 0xff;
 
     if ( (PIN(SW_PACECAR_PORT) & _BV(SW_PACECAR)) == 0 ) {
@@ -188,6 +194,7 @@
         if (controller == 5) tmp = ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
         if (controller == 4) trackchange = (PIN(CONTROLLER_PORT) & _BV(CONTROLLER1_SW));
         if (controller == 5) trackchange = (PIN(CONTROLLER_PORT) & _BV(CONTROLLER2_SW));
+        speed = tmp;
     } else {
         // read speeds
         if ((controller == 0) && (mode!=1)) tmp = ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
@@ -203,13 +210,16 @@
             if (tmp < slot[controller].speedminimum) tmp = slot[controller].speedminimum;
             if ((mode == 2) && (tmp != 0)) { jumpstart(controller); tmp = 0; }
             if (tmp > slot[controller].speedlimit) tmp = slot[controller].speedlimit;
+            speed = tmp;
             tmp = tmp << 1;
         } else {
             if ((mode == 0) && (tmp < slot[controller].speedminimum)) tmp = slot[controller].speedminimum;
             if (tmp > slot[controller].speedlimit) tmp = slot[controller].speedlimit;
+            speed = tmp;
             tmp = tmp << 1;
             if (trackchange || slot[controller].trackswitch) tmp |= (1<<5);
         }
+
     }
 
     switch (controller) {
@@ -240,8 +250,27 @@
     }
 
     tmp |=  (0b1000000000 | (controller << 6));
-    if ( (PIN(SW_FUEL_PORT) & _BV(SW_FUEL)) != 0) tmp |= 1; // benzinstand aktiv - tankmodusschalter
-    return insert_queue(tmp, 9);
+    // FUEL BIT GETS SET WHEN FUEL == 0,
+    // THIS REQUIRES PHYSICAL CAR FUEL LEVEL SET TO ZERO BEFORE!
+    if ( ((PIN(SW_FUEL_PORT) & _BV(SW_FUEL)) != 0) | (slot[controller].fuel == 0)) tmp |= 1; // benzinstand aktiv - tankmodusschalter
+    if (insert_queue(tmp, 9)) {
+        if (speed != 0) {
+            // do the fuel calculation, regardless if fuel logic active or not
+            tmp = (uint8_t)(((slot[controller].accel * speed) + 1) / FUEL_DIVISOR);
+            if (tmp == 0) tmp = 1;
+            if (slot[controller].fuel > 0) {
+                // enough fuel left to decrement?
+                if (slot[controller].fuel > tmp) {
+                    slot[controller].fuel -= tmp; // decrement fuel level
+                } else slot[controller].fuel = 0;
+            }
+        } else if (slot[controller].canrefuel) {
+            // increase fuel by 5%/sec, this equals by adding 50 to the counter
+            slot[controller].fuel += 50;
+            if (slot[controller].fuel > FUEL_FULL) slot[controller].fuel = FUEL_FULL;
+        }
+        return 1;
+    } else return 0;
 }
 
 uint8_t mirror( uint8_t n ) {
@@ -299,9 +328,12 @@
         slot[i].speedlimit = 15;
         slot[i].speedminimum = 0;
         slot[i].trackswitch = 0;
-        slot[i].fuel = 100;
+        slot[i].fuel = FUEL_FULL;
         slot[i].jumpstart_time = 0;
         slot[i].laps = 0;
+        slot[i].seccnt = 0;
+        slot[i].accel = 15; // full acceleration per default - TODO
+        slot[i].canrefuel = 1; // TODO: only set to 1 when on a pitlane
     }
     sysclk.value = 0;
 }
@@ -386,6 +418,21 @@
     } car1 = 0;
 }
 
+void slot_liveinfo(uint8_t idx) {
+    // increment packet counter, if == 10 output some live info
+    if (slot[idx].seccnt == 10) {
+        // output current fuel status
+        RS232_putc('F');
+        RS232_putc(':');
+        RS232_putc(idx + '0');
+        RS232_putc(':');
+        itoa(slot[idx].fuel, s, 16);
+        RS232_puts(s);
+        RS232_putc('\n');
+
+        slot[idx].seccnt = 0;
+    } else slot[idx].seccnt++;
+}
 
 int main(void)
 {
@@ -490,25 +537,37 @@
                 if (do_active()) packet_index++;
                 break;
             case 4:
-                if (do_controller(0)) packet_index++;
+                if (do_controller(0)) { packet_index++;
+                    slot_liveinfo(0);
+                }
                 break;
             case 5:
-                if (do_controller(4)) packet_index++;
+                if (do_controller(4)) { packet_index++;
+                    slot_liveinfo(4);
+                }
                 break;
             case 6:
-                if (do_controller(1)) packet_index++;
+                if (do_controller(1)) { packet_index++;
+                    slot_liveinfo(1);
+                }
                 break;
             case 7:
-                if (do_controller(5)) packet_index++;
+                if (do_controller(5)) { packet_index++;
+                    slot_liveinfo(5);
+                }
                 break;
             case 8:
-                if (do_controller(2)) packet_index++;
+                if (do_controller(2)) { packet_index++;
+                    slot_liveinfo(2);
+                }
                 break;
             case 9:
                 if (do_active()) packet_index++;
                 break;
             case 10:
-                if (do_controller(3)) packet_index = 1;
+                if (do_controller(3)) { packet_index = 1;
+                    slot_liveinfo(3);
+                }
                 // last packet, so reset packet index
                 break;
         }
--- a/slotUI/SlotCli.py	Fri Dec 09 18:20:21 2011 +0100
+++ b/slotUI/SlotCli.py	Fri Dec 09 19:41:28 2011 +0100
@@ -92,6 +92,7 @@
         self.scr.keypad(1) # enable special keys
         self.scr.nodelay(1) # disable delay on readkey
 
+        self.cleartop()
         self.render_slots()
         self.scr.refresh()
 
@@ -136,6 +137,13 @@
                     if (self.slot[slot]["best"] > t) or (self.slot[slot]["best"] == 0): self.slot[slot]["best"] = t
                     self.render_slots()
 
+                if rx[:2] == "F:":
+                    # update fuel level
+                    slot = int(data[1])
+                    f = int(data[2], 16)
+                    f = f / 100 # fuel in percent
+                    self.slot[slot]["fuel"] = f
+                    self.render_slots()
 
                 self.scr.refresh()
 

mercurial