--- 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;