From de977f33ed5c55b2b45c8dcae0c0687bd0cbf1e6 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 23 Oct 2013 20:36:25 +0300 Subject: [PATCH] Pass command data through a pointer This is cleaner and more flexible than the global buffer, and isn't any slower. --- arducontrol/interface.c | 43 +++++++++++++++++------------------------ arducontrol/interface.h | 3 --- arducontrol/monitor.c | 4 ++-- arducontrol/monitor.h | 2 +- arducontrol/motorola.c | 4 ++-- arducontrol/motorola.h | 2 +- arducontrol/output.c | 4 ++-- arducontrol/output.h | 2 +- 8 files changed, 27 insertions(+), 37 deletions(-) diff --git a/arducontrol/interface.c b/arducontrol/interface.c index 041dd67..96b1f96 100644 --- a/arducontrol/interface.c +++ b/arducontrol/interface.c @@ -9,11 +9,9 @@ uint8_t recv_head = 0; uint8_t recv_tail = 0; volatile uint8_t recv_fill = 0; volatile uint8_t recv_overrun = 0; -uint8_t cmd_buf[15]; -uint8_t cmd_length; void process_commands(void); -uint8_t process_command(void); +uint8_t dispatch_command(const uint8_t *, uint8_t); void interface_init(void) { @@ -38,61 +36,56 @@ void process_commands(void) { while(recv_fill>0) { - uint8_t consumed; - uint8_t c = recv_buf[recv_tail]; + uint8_t cmd[15]; + uint8_t length = ~recv_buf[recv_tail]; - cmd_length = 0; - - if(c>=0xF0) + if(length<0x10) { - cmd_length = ~c; - if(recv_fill<=cmd_length) + if(recv_fill<=length) break; uint8_t i, j; - for(i=0, j=recv_tail+1; i=sizeof(recv_buf)) j = 0; - cmd_buf[i] = recv_buf[j]; + cmd[i] = recv_buf[j]; } - - consumed = 1+cmd_length; } else { + length = 0; serial_write(0xFE); serial_write(FRAMING_ERROR); - consumed = 1; } - recv_tail += consumed; + recv_tail += length+1; if(recv_tail>=sizeof(recv_buf)) recv_tail -= sizeof(recv_buf); - recv_fill -= consumed; + recv_fill -= length+1; - if(cmd_length>0) + if(length>0) { - uint8_t result = process_command(); + uint8_t result = dispatch_command(cmd, length); serial_write(0xFE); serial_write(result); } } } -uint8_t process_command(void) +uint8_t dispatch_command(const uint8_t *cmd, uint8_t length) { - uint8_t type = cmd_buf[0]>>4; + uint8_t type = cmd[0]>>4; if(type==0) { - uint8_t subtype = (cmd_buf[0]>>3)&1; + uint8_t subtype = (cmd[0]>>3)&1; if(subtype==0) - return output_command(); + return output_command(cmd, length); else - return monitor_command(); + return monitor_command(cmd, length); } else if(type==1) - return motorola_command(); + return motorola_command(cmd, length); else return INVALID_COMMAND; } diff --git a/arducontrol/interface.h b/arducontrol/interface.h index 7e89e2e..6b5a948 100644 --- a/arducontrol/interface.h +++ b/arducontrol/interface.h @@ -4,9 +4,6 @@ #include #include "commands.h" -extern uint8_t cmd_buf[]; -extern uint8_t cmd_length; - void interface_init(void); void interface_check(void); diff --git a/arducontrol/monitor.c b/arducontrol/monitor.c index 112e1ac..4adc937 100644 --- a/arducontrol/monitor.c +++ b/arducontrol/monitor.c @@ -1,5 +1,5 @@ #include "adc.h" -#include "interface.h" +#include "commands.h" #include "monitor.h" #include "output.h" #include "serial.h" @@ -66,7 +66,7 @@ void monitor_check(void) } } -uint8_t monitor_command(void) +uint8_t monitor_command(const uint8_t *cmd_buf, uint8_t cmd_length) { if(cmd_buf[0]==READ_TRACK_CURRENT) { diff --git a/arducontrol/monitor.h b/arducontrol/monitor.h index b21763d..031d26e 100644 --- a/arducontrol/monitor.h +++ b/arducontrol/monitor.h @@ -5,6 +5,6 @@ void monitor_init(void); void monitor_check(void); -uint8_t monitor_command(void); +uint8_t monitor_command(const uint8_t *, uint8_t); #endif diff --git a/arducontrol/motorola.c b/arducontrol/motorola.c index 33cf1c0..0ee6704 100644 --- a/arducontrol/motorola.c +++ b/arducontrol/motorola.c @@ -1,4 +1,4 @@ -#include "interface.h" +#include "commands.h" #include "motorola.h" #include "output.h" @@ -125,7 +125,7 @@ void motorola_solenoid_packet(uint8_t addr, uint8_t output, uint8_t state) packet.ready = 1; } -uint8_t motorola_command(void) +uint8_t motorola_command(const uint8_t *cmd_buf, uint8_t cmd_length) { if(cmd_buf[0]==MOTOROLA_SPEED || cmd_buf[0]==MOTOROLA_SPEED_DIRECTION || cmd_buf[0]==MOTOROLA_SPEED_FUNCTION) { diff --git a/arducontrol/motorola.h b/arducontrol/motorola.h index 4a64d62..102e790 100644 --- a/arducontrol/motorola.h +++ b/arducontrol/motorola.h @@ -9,6 +9,6 @@ void motorola_locomotive_speed_direction_packet(uint8_t, uint8_t, uint8_t, uint8 void motorola_locomotive_speed_function_packet(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t); void motorola_solenoid_packet(uint8_t, uint8_t, uint8_t); void motorola_set_repeat_count(uint8_t); -uint8_t motorola_command(void); +uint8_t motorola_command(const uint8_t *, uint8_t); #endif diff --git a/arducontrol/output.c b/arducontrol/output.c index 748c554..f961a43 100644 --- a/arducontrol/output.c +++ b/arducontrol/output.c @@ -1,5 +1,5 @@ #include -#include "interface.h" +#include "commands.h" #include "output.h" #include "timer.h" @@ -36,7 +36,7 @@ void output_set_power(uint8_t p) PORTD &= ~BIT(ENABLE); } -uint8_t output_command(void) +uint8_t output_command(const uint8_t *cmd_buf, uint8_t cmd_length) { if(cmd_buf[0]==POWER_ON || cmd_buf[0]==POWER_OFF) { diff --git a/arducontrol/output.h b/arducontrol/output.h index 05c9a21..c94c665 100644 --- a/arducontrol/output.h +++ b/arducontrol/output.h @@ -21,6 +21,6 @@ extern OutputPacket packet; void output_init(void); void clear_packet(void); void output_set_power(uint8_t); -uint8_t output_command(void); +uint8_t output_command(const uint8_t *, uint8_t); #endif -- 2.45.2