blackbox/main.c

changeset 59
361bc4602cf7
parent 53
f659e6faf18f
child 60
bfc2f2d6913c
--- a/blackbox/main.c	Fri Dec 09 12:32:36 2011 +0100
+++ b/blackbox/main.c	Fri Dec 09 14:08:24 2011 +0100
@@ -26,16 +26,18 @@
 } u32;
 
 #define MAX_SLOTS       6
+#define FUEL_FULL       10000
 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;
+    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 +179,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 +191,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 +207,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) {
@@ -241,7 +248,20 @@
 
     tmp |=  (0b1000000000 | (controller << 6));
     if ( (PIN(SW_FUEL_PORT) & _BV(SW_FUEL)) != 0) tmp |= 1; // benzinstand aktiv - tankmodusschalter
-    return insert_queue(tmp, 9);
+    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) / 25);
+            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;
+            }
+        }
+        return 1;
+    } else return 0;
 }
 
 uint8_t mirror( uint8_t n ) {
@@ -299,9 +319,11 @@
         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
     }
     sysclk.value = 0;
 }
@@ -386,6 +408,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 +527,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;
         }

mercurial