]> git.tdb.fi Git - model-railway-devices.git/blobdiff - arducontrol/interface.c
Drop the pretense of C89, put declarations where they make sense
[model-railway-devices.git] / arducontrol / interface.c
index 4064120992bd16e4a38e20446f3d98085ff785ba..014e8e98bcaa0ac5eacbeb061f1a38754e1a2ae5 100644 (file)
@@ -1,3 +1,4 @@
+#include <avr/io.h>
 #include "interface.h"
 #include "monitor.h"
 #include "motorola.h"
@@ -5,17 +6,15 @@
 #include "serial.h"
 #include "s88.h"
 
-volatile uint8_t recv_buf[32];
-uint8_t recv_head = 0;
-uint8_t recv_tail = 0;
-volatile uint8_t recv_fill = 0;
-volatile uint8_t recv_overrun = 0;
+static uint8_t cmd_buffer[15];
+static uint8_t cmd_length = 0;
+static uint8_t cmd_read_pos = 0;
 
-void process_commands(void);
-uint8_t dispatch_command(const uint8_t *, uint8_t);
+static uint8_t dispatch_command(const uint8_t *, uint8_t);
 
 void interface_init(void)
 {
+       DDRB |= 0x01;
        DDRD = (DDRD&0xFC)|0x02;
 
        serial_init(9600);
@@ -23,55 +22,47 @@ void interface_init(void)
 
 void interface_check(void)
 {
-       if(recv_overrun)
-       {
+       if(serial_read_overrun())
                interface_send1(RECEIVE_OVERRUN);
-               recv_overrun = 0;
-       }
-       if(recv_fill>0)
-               process_commands();
-}
 
-void process_commands(void)
-{
-       while(recv_fill>0)
+       uint8_t count = serial_read_available();
+       if(count>0)
        {
-               uint8_t cmd[15];
-               uint8_t length = ~recv_buf[recv_tail];
-
-               if(length<0x10)
+               PORTB |= 0x01;
+               if(cmd_length==0)
                {
-                       if(recv_fill<=length)
-                               break;
-
-                       uint8_t i, j;
-                       for(i=0, j=recv_tail+1; i<length; ++i, ++j)
+                       uint8_t l = ~serial_read();
+                       if(l==0)
+                               serial_write(0xFF);
+                       else if(l>=0x10)
+                               interface_send1(FRAMING_ERROR);
+                       else
                        {
-                               if(j>=sizeof(recv_buf))
-                                       j = 0;
-                               cmd[i] = recv_buf[j];
+                               cmd_length = l;
+                               --count;
+                               cmd_read_pos = 0;
                        }
                }
-               else
-               {
-                       length = 0;
-                       interface_send1(FRAMING_ERROR);
-               }
 
-               recv_tail += length+1;
-               if(recv_tail>=sizeof(recv_buf))
-                       recv_tail -= sizeof(recv_buf);
-               recv_fill -= length+1;
-
-               if(length>0)
+               if(cmd_read_pos<cmd_length)
                {
-                       uint8_t result = dispatch_command(cmd, length);
-                       interface_send1(result);
+                       if(cmd_read_pos+count>cmd_length)
+                               count = cmd_length-cmd_read_pos;
+                       for(uint8_t i=0; i<count; ++i)
+                               cmd_buffer[cmd_read_pos++] = serial_read();
+
+                       if(cmd_read_pos>=cmd_length)
+                       {
+                               uint8_t result = dispatch_command(cmd_buffer, cmd_length);
+                               interface_send1(result);
+                               cmd_length = 0;
+                       }
                }
+               PORTB &= ~0x01;
        }
 }
 
-uint8_t dispatch_command(const uint8_t *cmd, uint8_t length)
+static uint8_t dispatch_command(const uint8_t *cmd, uint8_t length)
 {
        uint8_t type = cmd[0]>>4;
        if(type==0)
@@ -92,10 +83,8 @@ uint8_t dispatch_command(const uint8_t *cmd, uint8_t length)
 
 void interface_send(const uint8_t *cmd, uint8_t length)
 {
-       uint8_t i;
-
        serial_write(~length);
-       for(i=0; i<length; ++i)
+       for(uint8_t i=0; i<length; ++i)
                serial_write(cmd[i]);
 }
 
@@ -104,19 +93,3 @@ void interface_send1(uint8_t cmd)
        serial_write(0xFE);
        serial_write(cmd);
 }
-
-static inline void receive(uint8_t c)
-{
-       if(recv_fill>=sizeof(recv_buf))
-       {
-               recv_overrun = 1;
-               return;
-       }
-
-       recv_buf[recv_head++] = c;
-       if(recv_head>=sizeof(recv_buf))
-               recv_head = 0;
-       ++recv_fill;
-}
-
-SERIAL_SET_CALLBACK(receive)