From: Mikko Rasa Date: Wed, 30 Oct 2013 21:50:00 +0000 (+0200) Subject: Make arducontrol interface asynchronous again X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=c97e84ca85810963c0c7e05d43ce5584b94cc9b3;p=model-railway-devices.git Make arducontrol interface asynchronous again Blocking the main loop for extended periods of time (as could previously happen with an incomplete packet) is bad, because it prevents the monitor from triggering overcurrent shutdown. --- diff --git a/arducontrol/interface.c b/arducontrol/interface.c index 1cc4b15..c5a7262 100644 --- a/arducontrol/interface.c +++ b/arducontrol/interface.c @@ -6,6 +6,10 @@ #include "serial.h" #include "s88.h" +static uint8_t cmd_buffer[15]; +static uint8_t cmd_length = 0; +static uint8_t cmd_read_pos = 0; + uint8_t dispatch_command(const uint8_t *, uint8_t); void interface_init(void) @@ -17,27 +21,42 @@ void interface_init(void) void interface_check(void) { + uint8_t count; if(serial_read_overrun()) interface_send1(RECEIVE_OVERRUN); - while(serial_read_available()) + count = serial_read_available(); + if(count>0) { - uint8_t length = ~serial_read(); + if(cmd_length==0) + { + uint8_t l = ~serial_read(); + if(l==0) + serial_write(0xFF); + else if(l>=0x10) + interface_send1(FRAMING_ERROR); + else + { + cmd_length = l; + --count; + cmd_read_pos = 0; + } + } - if(length==0) - serial_write(0xFF); - else if(length>=0x10) - interface_send1(FRAMING_ERROR); - else + if(cmd_read_poscmd_length) + count = cmd_length-cmd_read_pos; + for(i=0; i=cmd_length) + { + uint8_t result = dispatch_command(cmd_buffer, cmd_length); + interface_send1(result); + cmd_length = 0; + } } } }