blackbox/main.c

changeset 4
58529e14c45c
parent 3
1a0ef40ef458
child 5
7319de27d44a
--- a/blackbox/main.c	Mon Nov 14 21:38:00 2011 +0100
+++ b/blackbox/main.c	Tue Nov 15 10:22:13 2011 +0100
@@ -13,13 +13,17 @@
 #include "main.h"
 #include "lowlevel.h"
 
+volatile uint8_t program_count = 0;
+volatile uint8_t program_id;
+volatile uint8_t program_command;
+volatile uint8_t program_parameter;
+
 volatile uint8_t datalen = 0;
 char data[10]; // 8 bytes data buffer + string termination
 
 static char buffer[RS232_BUFSIZE+1];
 static uint8_t buffer_len;
 
-
 // USART0 RX interrupt
 ISR ( USART_RXC_vect ) {
     char c = UDR;
@@ -27,20 +31,33 @@
     // check for buffer overflow
     if (buffer_len==sizeof(buffer)) {
         buffer_len=0;
-        if (c == 27) {
-            // escape sequence, store to empty buffer
-            buffer[buffer_len++] = c;
-        }
     }  else {
         // collect characters until end of line
-        if (c == 27) {
-            // escape sequence, clear buffer
-            buffer_len = 0;
-            buffer[buffer_len++] = c;
-        } else if ( (c==0xff) && (buffer_len > 3) ) {
+        if ( (c==0x0A) ) {
             buffer[buffer_len]=0;
 
             // packet end received, parse the received packet
+            switch (buffer[0]) {
+                case 'P': // inject a program data word to the rails
+                    // TODO: at the moment only parameters 0..9 supported
+                    // needs to build a "human" parser
+                    program_id = buffer[3]-'0';
+                    program_command = buffer[1]-'0';
+                    program_parameter = buffer[2]-'0';
+                    program_count = 0x02; // send commands twice
+                    RS232_puts_p(PSTR("Executing command: "));
+/*
+                    RS232_putc(buffer[2]);
+                    RS232_putc(',');
+                    RS232_putc(buffer[3]);
+                    RS232_puts_p(PSTR(" for slot "));
+                    RS232_putc(buffer[1]);
+*/
+                    RS232_putc('\n');
+
+                    break;
+            
+            }
 
             // wait for the next packet
             buffer_len=0;
@@ -126,8 +143,7 @@
     return insert_queue(tmp, 9);
 }
 
-unsigned char mirror( unsigned char n ) {
-    // mirror all 8 bits
+uint8_t mirror( uint8_t n ) {
     n = ((n >> 1) & 0x55) | ((n << 1) & 0xaa);
     n = ((n >> 2) & 0x33) | ((n << 2) & 0xcc);
     n = ((n >> 4) & 0x0f) | ((n << 4) & 0xf0);
@@ -135,11 +151,14 @@
 }
 
 
+
 int do_program(uint8_t controller, uint8_t command, uint8_t parameter) {
     // send program data packet
     uint16_t tmp;
-    tmp = 0b1000000000000 | (mirror(parameter) << 4) | mirror(command)| (mirror(controller) >> 5);
-
+    parameter = mirror(parameter);
+    controller = mirror(controller);
+    command = mirror(command);
+    tmp = 0b1000000000000 | (parameter << 4) | command | (controller >> 5);
     return insert_queue(tmp, 12);
 }
 
@@ -151,6 +170,7 @@
     if ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b10010001;
     if ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b10001001;
     // todo: regler 5 und 6
+    // todo: wenn Daten enpfangen wurden hier eine Quittierung senden anstatt dem Active Word
 
     return insert_queue(tmp, 7);
 }
@@ -185,9 +205,6 @@
 
     init_hardware();
 
-    itoa(TIMER2_50US, s, 10);
-    RS232_puts(s);
-
     // switch on rails power
     RAIL_POWER_PORT |= _BV(RAIL_POWER);
 
@@ -197,9 +214,19 @@
 
         switch (packet_index) {
             case 1:
-                if (do_program(7, 19, 0)) packet_index++; // reset
-                //do_program(7, 16, 3); // led an startampel
-                //do_program(0, 4, 0); //
+                if (program_count > 0) {
+                    // command in queue
+                    if (do_program(program_id, program_command, program_parameter)) {
+                        packet_index++;
+                        program_count--;
+itoa(transmit_buffer_queue, s, 2);
+RS232_puts(s);
+RS232_putc('\n');
+                    }
+                } else {
+                    // output idle command
+                    if (do_program(7, 19, 0)) packet_index++; // reset
+                }
                 break;
             case 2:
                 if (do_pace_ghost()) packet_index++;

mercurial