blackbox/main.c

changeset 36
aea84f4f5a12
parent 35
00166228a419
child 37
136a79772098
--- a/blackbox/main.c	Sat Dec 03 13:53:44 2011 +0100
+++ b/blackbox/main.c	Sat Dec 03 14:25:06 2011 +0100
@@ -13,6 +13,9 @@
 #include "main.h"
 #include "lowlevel.h"
 
+#define MAX_SLOTS       5
+uint8_t speedlimit[MAX_SLOTS];
+
 volatile uint8_t program_count = 0;
 volatile uint8_t program_id;
 volatile uint8_t program_command;
@@ -45,8 +48,12 @@
     return 0;
 }
 
+char ok[]       PROGMEM="OK\n";
+char busy[]     PROGMEM="BUSY\n";
+
 
 ISR ( USART_RXC_vect ) {
+    uint8_t tmp;
     char c = UDR;
 
     // check for buffer overflow
@@ -72,8 +79,16 @@
                             program_count = 0x02; // send commands twice (fuel, speed, brake)
                             else
                             program_count = 0x01;
-                        RS232_puts_p(PSTR("OK\n"));
-                    } else RS232_puts_p(PSTR("BUSY\n"));
+                        RS232_puts_p(ok);
+                    } else RS232_puts_p(busy);
+                    break;
+
+                case 'L': // Limit maximum speed for a car
+                    tmp = buffer[2]-'0';
+                    if (tmp > 9)
+                        tmp = buffer[2]-'A'+10;
+                    speedlimit[buffer[1]-'0'] = tmp;
+                    RS232_puts_p(ok);
                     break;
 
             }
@@ -95,28 +110,36 @@
     uint16_t tmp;
     switch (controller) {
         case 0:
-            tmp = ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) & 0x0F) << 1;
+            tmp = ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
+            if (tmp > speedlimit[controller]) tmp = speedlimit[controller];
+            tmp = tmp << 1;
             if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER1_SW)) != 0) {
                 tmp |= (1<<5);
                 LED(1,0);
             } else LED(1,1);
             break;
         case 1:
-            tmp = ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) & 0x0F) << 1;
+            tmp = ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
+            if (tmp > speedlimit[controller]) tmp = speedlimit[controller];
+            tmp = tmp << 1;
             if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER2_SW)) != 0) {
                 tmp |= (1<<5);
                 LED(2,0);
             } else LED(2,1);
             break;
         case 2:
-            tmp = ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) & 0x0F) << 1;
+            tmp = ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
+            if (tmp > speedlimit[controller]) tmp = speedlimit[controller];
+            tmp = tmp << 1;
             if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER3_SW)) != 0) {
                 tmp |= (1<<5);
                 LED(3,0);
             } else LED(3,1);
             break;
         case 3:
-            tmp = ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) & 0x0F) << 1;
+            tmp = ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
+            if (tmp > speedlimit[controller]) tmp = speedlimit[controller];
+            tmp = tmp << 1;
             if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER4_SW)) != 0) {
                 tmp |= (1<<5);
                 LED(4,0);
@@ -260,6 +283,13 @@
     LED(5,2);
 }
 
+void reset_vars(void) {
+    uint8_t i;
+    for (i=0; i<MAX_SLOTS; i++) {
+        speedlimit[i] = 15;
+    }
+}
+
 
 int main(void)
 {
@@ -284,6 +314,7 @@
 
 
     init_hardware();
+    reset_vars();
 
     // switch on rails power
     RAIL_POWER_PORT |= _BV(RAIL_POWER);

mercurial