blackbox/main.c

changeset 73
ec888cfa024e
parent 69
27c0c0095e26
child 75
0b38de31ad5d
--- a/blackbox/main.c	Sat Dec 10 14:56:27 2011 +0100
+++ b/blackbox/main.c	Sat Dec 10 16:29:31 2011 +0100
@@ -47,6 +47,8 @@
 // 1: waiting for countdown start
 // 2: race countdown initiated
 // 3: Race start condition
+uint8_t  liveinfo = 0;
+uint8_t  fuel_enabled = 1;
 
 volatile u32 sysclk;
 volatile uint8_t sysclk_packettimer = 0;
@@ -80,6 +82,70 @@
 volatile uint8_t timer0_delay;
 
 
+    /* RESPONSEWIRE frame format:
+        1 startbit
+        2 car id bit 1
+        3 car id bit 2
+        4 car id bit 3
+        5 track change status bit 1
+        6 track change status bit 2
+        7 track change status bit 3
+        8 track change status bit 4
+        9 sender id bit 1
+        10 sender id bit 2
+        11 sender id bit 3
+        12 sender id bit 4
+        13 device type bit 1
+        14 device type bit 2
+        15 device type bit 3
+        16 stopbit
+    */
+
+    /* PITLANE STATUS DEFINITION
+        1 = AA
+        2 = AB
+        3 = BB
+        4 = BA
+        5 = BC
+        6 = ZZ -> pitlane exit
+    */
+
+#define STARTSTOP 0b1000000000000001
+void decode_responsewire(void) {
+    uint16_t data = responsewire_data;
+    // first check if start + stopbit are set
+    // todo future: unsure but last bit doesnt get set?!
+/*
+    if ((data & STARTSTOP) != STARTSTOP) {
+        RS232_puts("RW BAD\n");
+        return; // incomplete response
+    }
+*/
+    // now extract the car id, track change status
+    uint8_t car = ((data >> 1) & 0b111);
+    uint8_t status = ((data >> 4) & 0b1111);
+    uint8_t sender = ((data >> 8) & 0b1111);
+    uint8_t type = ((data >> 12) & 0b111);
+    if (type == 4) {
+        // pitlane response
+        if (status == 5) slot[car].canrefuel = 1;
+        if (status == 6) for (data=0; data<MAX_SLOTS; data++) slot[data].canrefuel = 0;
+    }
+    RS232_puts("RW:");
+    itoa(car, s, 16);
+    RS232_puts(s);
+    RS232_putc(':');
+    itoa(type, s, 16);
+    RS232_puts(s);
+    RS232_putc(':');
+    itoa(sender, s, 16);
+    RS232_puts(s);
+    RS232_putc(':');
+    itoa(status, s, 16);
+    RS232_puts(s);
+    RS232_putc('\n');
+}
+
 int insert_queue(uint16_t tmp, uint8_t len) {
     if (transmit_buffer_queue == 0) {
         transmit_buffer_queue = tmp;
@@ -136,6 +202,17 @@
                     RS232_puts_p(ok);
                     break;
 
+                case 'F': // set fuel enabled
+                    fuel_enabled = buffer[1]-'0';
+                    RS232_puts_p(ok);
+                    break;
+
+                case '*': // set live information
+                    liveinfo = buffer[1]-'0';
+                    RS232_puts_p(ok);
+                    break;
+
+
                 case 'I': // get Information data (incl. important global parameter dump)
                     RS232_puts(VERSION);
                     RS232_putc(':');
@@ -254,22 +331,24 @@
     // 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;
+        if (fuel_enabled) {
+            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;
             }
-        } 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;
         }
-        return 1;
     } else return 0;
 }
 
@@ -333,7 +412,7 @@
         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
+        slot[i].canrefuel = 0;
     }
     sysclk.value = 0;
 }
@@ -375,7 +454,7 @@
                     slot[car0-1].laps++;
                     RS232_putc('L');
                     RS232_putc(':');
-                    RS232_putc('A');
+                    RS232_putc('B');
                     RS232_putc(':');
                     itoa(slot[car0-1].laps, s, 16);
                     RS232_puts(s);
@@ -401,7 +480,7 @@
                     slot[car1-1].laps++;
                     RS232_putc('L');
                     RS232_putc(':');
-                    RS232_putc('B');
+                    RS232_putc('A');
                     RS232_putc(':');
                     itoa(slot[car1-1].laps, s, 16);
                     RS232_puts(s);
@@ -419,6 +498,7 @@
 }
 
 void slot_liveinfo(uint8_t idx) {
+    if (liveinfo == 0) return;
     // increment packet counter, if == 10 output some live info
     if (slot[idx].seccnt == 10) {
         // output current fuel status

mercurial