pitlane/main.c

changeset 73
ec888cfa024e
parent 72
60c4b28fd773
child 104
95b2c23cb973
--- a/pitlane/main.c	Sat Dec 10 14:56:27 2011 +0100
+++ b/pitlane/main.c	Sat Dec 10 16:29:31 2011 +0100
@@ -67,36 +67,40 @@
 volatile uint16_t bitbuf = 0;
 
 volatile uint8_t response = 0;
+volatile uint8_t response_car = 0;
 uint8_t self_id = 0b1111; // ONLY ONE pitlane
 
 void solenoid_delay(void) {
     _delay_ms(2);
 }
 
-void send_response(uint16_t data) {
+void send_response(uint8_t car, uint8_t status) {
     /* frame format:
         1 startbit
-        2 car id
-        3 car id
-        4 car id
+        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 sender id
-        8 sender id
-        9 sender id
-        9 sender id
-        10 device type
-        11 device type
-        12 device type
-        13 device type
-        14 reserved
-        15 reserved
+        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
     */
+    uint16_t data;
+    // produce packet
+    data = ((car & 0b111) << 1) | ((status & 0b1111) << 4) | ((self_id & 0b1111) << 8) | (TRACKSWITCH_TYPE << 12);
+    data |= 0b100000000000001; // make sure start/stop bits are set
+
     uint8_t index = 16; // bit count maximum
     uint8_t enable = DDR(RESPONSE_PORT) | _BV(RESPONSE_PIN);
     uint8_t disable = DDR(RESPONSE_PORT) & ~_BV(RESPONSE_PIN);
-    data |= 0b100000000000001; // make sure start/stop bits are set
     while (index != 0) {
         if ((data & 1) != 0) {
             DDR(RESPONSE_PORT) = enable; // enable response output
@@ -148,9 +152,9 @@
                 slot[clock].trackswitch = (bitbuf >> 5) & 1;
                 // current response for this car?
                 if (response != 0) {
-                    if ( ((response & 0b00001110) >> 1) == clock) {
+                    if ( response_car == clock) {
                         // add our ID to response:
-                        send_response(response | (self_id << 6));
+                        send_response(clock, response);
                         response = 0;
                     }
                 }
@@ -278,18 +282,19 @@
         // main loop
 
     /*
-        0 = AA
-        1 = AB
-        2 = BB
-        3 = BA
-        4 = ZZ -> pitlane exit
+        1 = AA
+        2 = AB
+        3 = BB
+        4 = BA
         5 = BC
+        6 = ZZ -> pitlane exit
     */
         if (sens[0].car != sens[0].state) {
             sens[0].state = sens[0].car;
 #if (TRACKSWITCH_TYPE != TYPE_PITLANE)
             if ( (sens[0].state != 0) && (slot[sens[0].state-1].trackswitch == 0) && (slot[sens[0].state-1].speed>0) ) {
-                response = (1 | ((sens[0].state-1)<<1) | (1 << 4));
+                response = 2;
+                response_car = sens[0].state - 1;
 
                 // set inside status
                 slot[sens[0].state].inside = 1;
@@ -307,7 +312,8 @@
             } else
 #endif
             if (sens[0].state != 0) {
-                response = (1 | ((sens[0].state-1)<<1));
+                response = 1;
+                response_car = sens[0].state - 1;
                 RS232_putc('A');
                 RS232_putc('A');
                 RS232_putc('0'+sens[0].state);
@@ -319,7 +325,8 @@
         if (sens[1].car != sens[1].state) {
             sens[1].state = sens[1].car;
             if ( (sens[1].state != 0) && (slot[sens[1].state-1].trackswitch == 0) && (slot[sens[1].state-1].speed>0) ) {
-                response = (1 | ((sens[1].state-1)<<1) | (5 << 4));
+                response = 5;
+                response_car = sens[1].state - 1;
 
                 // set inside status
                 slot[sens[1].state-1].inside = 1;
@@ -336,7 +343,8 @@
                 solenoid_delay();
             } else
             if (sens[1].state != 0) {
-                response = (1 | ((sens[1].state)<<1) | (2 << 4));
+                response = 3;
+                response_car = sens[1].state - 1;
                 RS232_putc('B');
                 RS232_putc('B');
                 RS232_putc('0'+sens[1].state);
@@ -352,14 +360,16 @@
                     if (slot[tmp].inside) {
                     slot[tmp].inside = 0;
                 }
-                response = (1 | (0b111 <<1) | (4 << 4));
+                response = 6;
+                response_car = 0;
                 RS232_puts_p(PSTR("PIT:EXIT\n"));
         }
 
         if (sens[2].car != sens[2].state) {
             sens[2].state = sens[2].car;
             if (sens[2].state != 0) {
-                response = (1 | ((sens[2].state-1)<<1) | (4 << 4));
+                response = 6;
+                response_car = sens[2].state-1;
 
                 // set inside status
                 slot[sens[2].state-1].inside = 0;

mercurial