memsave and implemented minimum slot speed for virtual cars

Thu, 08 Dec 2011 17:14:02 +0100

author
Malte Bayer <mbayer@neo-soft.org>
date
Thu, 08 Dec 2011 17:14:02 +0100
changeset 48
5bba01aad0a6
parent 47
34ac9f92bc1e
child 49
3957ff8e86c6

memsave and implemented minimum slot speed for virtual cars

blackbox/driver/rs232.h file | annotate | diff | comparison | revisions
blackbox/main.c file | annotate | diff | comparison | revisions
--- a/blackbox/driver/rs232.h	Thu Dec 08 15:56:20 2011 +0100
+++ b/blackbox/driver/rs232.h	Thu Dec 08 17:14:02 2011 +0100
@@ -1,7 +1,7 @@
 #ifndef __RS232_H__
 #define __RS232_H__
 
-#define RS232_BUFSIZE   15
+#define RS232_BUFSIZE   10
 
 extern void RS232_init(void);
 extern void RS232_putc(char c);
--- a/blackbox/main.c	Thu Dec 08 15:56:20 2011 +0100
+++ b/blackbox/main.c	Thu Dec 08 17:14:02 2011 +0100
@@ -27,12 +27,15 @@
 
 #define MAX_SLOTS       6
 typedef struct {
-    uint8_t speedlimit, fuel;
+    uint8_t speedlimit; // 4bits speedlimit
+    uint8_t speedminimum; // 4bits speedminimum
+    uint8_t fuel;
     uint16_t jumpstart_time, laps;
     u32 lap_time_start, lap_time;
+    uint8_t trackswitch; // 1bit bool
 } cardata;
 
-static unsigned char s[10];
+static unsigned char s[8];
 static uint8_t  countdown, countdown_loops;
 uint8_t  mode = 0;
 // valid race modes:
@@ -45,7 +48,7 @@
 volatile uint8_t sysclk_packettimer = 0;
 volatile cardata slot[MAX_SLOTS];
 
-volatile uint16_t car0, car1;
+volatile uint8_t car0, car1;
 volatile uint16_t car0_new, car0_old;
 volatile uint16_t car1_new, car1_old;
 uint8_t car0_state, car1_state;
@@ -120,11 +123,21 @@
                     RS232_puts_p(ok);
                     break;
 
+                case 'S': // set minimum speed for a car
+                    tmp = buffer[2]-'0';
+                    if (tmp > 9)
+                        tmp = buffer[2]-'A'+10;
+                    slot[buffer[1]-'0'].speedminimum = tmp;
+                    RS232_puts_p(ok);
+                    break;
+
                 case 'I': // get Information data (incl. important global parameter dump)
                     RS232_puts(VERSION);
                     RS232_putc(':');
                     for (tmp=0;tmp<MAX_SLOTS;tmp++) RS232_putc(slot[tmp].speedlimit); // output speed limits
                     RS232_putc(':');
+                    for (tmp=0;tmp<MAX_SLOTS;tmp++) RS232_putc(slot[tmp].speedminimum); // output minimum speed
+                    RS232_putc(':');
                     for (tmp=0;tmp<MAX_SLOTS;tmp++) {
                         itoa(slot[tmp].fuel, s, 16);
                         RS232_putc(s); // output fuel levels (0=empty, 100=full, 0xff=no fuel option)
@@ -168,9 +181,14 @@
 int do_controller(uint8_t controller) {
     // read controller X speed & encode controller data packet
     uint16_t tmp = 0;
+    if ( (PIN(SW_PACECAR_PORT) & _BV(SW_PACECAR)) == 0 ) {
+        // map controller 1+2 to 5+6
+    }
+
     switch (controller) {
         case 0:
             if (mode!=1) tmp = ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
+            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;
             tmp = tmp << 1;
@@ -181,6 +199,7 @@
             break;
         case 1:
             if (mode!=1) tmp = ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
+            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;
             tmp = tmp << 1;
@@ -191,6 +210,7 @@
             break;
         case 2:
             if (mode!=1) tmp = ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
+            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;
             tmp = tmp << 1;
@@ -201,6 +221,7 @@
             break;
         case 3:
             if (mode!=1) tmp = ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
+            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;
             tmp = tmp << 1;
@@ -209,12 +230,22 @@
                 if (mode == 0) LED(5,0);
             } else if (mode == 0) LED(5,1);
             break;
-        case 4: tmp = (1<<5); break; // todo regler 5
-        case 5: tmp = (1<<5); break; // todo regler 6
+        case 4: // virtual car #1
+            if ((mode == 0) && (tmp < slot[controller].speedminimum)) tmp = slot[controller].speedminimum;
+            if (tmp > slot[controller].speedlimit) tmp = slot[controller].speedlimit;
+            tmp = tmp << 1;
+            if (slot[controller].trackswitch) tmp |= (1<<5);
+            break;
+        case 5: // virtual car #2
+            if ((mode == 0) && (tmp < slot[controller].speedminimum)) tmp = slot[controller].speedminimum;
+            if (tmp > slot[controller].speedlimit) tmp = slot[controller].speedlimit;
+            tmp = tmp << 1;
+            if (slot[controller].trackswitch) tmp |= (1<<5);
+            break;
+
     }
     tmp |=  (0b1000000000 | (controller << 6));
     if ( (PIN(SW_FUEL_PORT) & _BV(SW_FUEL)) != 0) tmp |= 1; // benzinstand aktiv - tankmodusschalter
-
     return insert_queue(tmp, 9);
 }
 
@@ -238,11 +269,12 @@
 int do_active(void) {
     // send controller active data packet
     uint16_t tmp = 0b10000000;
-    if ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b11000001;
-    if ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b10100001;
-    if ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b10010001;
-    if ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b10001001;
-    // todo: regler 5 und 6
+    if ((slot[0].speedminimum != 0) || ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) > 0)) tmp |= 0b11000001;
+    if ((slot[1].speedminimum != 0) || ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) > 0)) tmp |= 0b10100001;
+    if ((slot[2].speedminimum != 0) || ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) > 0)) tmp |= 0b10010001;
+    if ((slot[3].speedminimum != 0) || ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) > 0)) tmp |= 0b10001001;
+    if (slot[4].speedminimum != 0) tmp |= 0b10000101;
+    if (slot[5].speedminimum != 0) tmp |= 0b10000011;
     // todo: wenn Daten enpfangen wurden hier eine Quittierung senden anstatt dem Active Word
 
     return insert_queue(tmp, 7);
@@ -387,7 +419,9 @@
 void reset_vars(void) {
     uint8_t i;
     for (i=0; i<MAX_SLOTS; i++) {
-        if (i<4) slot[i].speedlimit = 15; else slot[i].speedlimit = 0;
+        slot[i].speedlimit = 15;
+        slot[i].speedminimum = 0;
+        slot[i].trackswitch = 0;
         slot[i].fuel = 100;
         slot[i].jumpstart_time = 0;
         slot[i].laps = 0;

mercurial