From: Mikko Rasa Date: Wed, 23 Oct 2013 18:02:42 +0000 (+0300) Subject: Add functions for sending replies X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=b5f6cc69e07c60ca67e8e736ca2fb90a3ce175ae;p=model-railway-devices.git Add functions for sending replies --- diff --git a/arducontrol/interface.c b/arducontrol/interface.c index 96b1f96..96baba7 100644 --- a/arducontrol/interface.c +++ b/arducontrol/interface.c @@ -24,8 +24,7 @@ void interface_check(void) { if(recv_overrun) { - serial_write(0xFE); - serial_write(RECEIVE_OVERRUN); + interface_send1(RECEIVE_OVERRUN); recv_overrun = 0; } if(recv_fill>0) @@ -55,8 +54,7 @@ void process_commands(void) else { length = 0; - serial_write(0xFE); - serial_write(FRAMING_ERROR); + interface_send1(FRAMING_ERROR); } recv_tail += length+1; @@ -67,8 +65,7 @@ void process_commands(void) if(length>0) { uint8_t result = dispatch_command(cmd, length); - serial_write(0xFE); - serial_write(result); + interface_send1(result); } } } @@ -90,6 +87,21 @@ uint8_t dispatch_command(const uint8_t *cmd, uint8_t length) return INVALID_COMMAND; } +void interface_send(const uint8_t *cmd, uint8_t length) +{ + uint8_t i; + + serial_write(~length); + for(i=0; i=sizeof(recv_buf)) diff --git a/arducontrol/interface.h b/arducontrol/interface.h index 6b5a948..fbd0238 100644 --- a/arducontrol/interface.h +++ b/arducontrol/interface.h @@ -6,5 +6,7 @@ void interface_init(void); void interface_check(void); +void interface_send(const uint8_t *, uint8_t); +void interface_send1(uint8_t); #endif diff --git a/arducontrol/monitor.c b/arducontrol/monitor.c index 4adc937..d8f3dad 100644 --- a/arducontrol/monitor.c +++ b/arducontrol/monitor.c @@ -1,5 +1,5 @@ #include "adc.h" -#include "commands.h" +#include "interface.h" #include "monitor.h" #include "output.h" #include "serial.h" @@ -45,8 +45,7 @@ void monitor_check(void) if(!overcurrent_sent) { overcurrent_sent = 1; - serial_write(0xFE); - serial_write(OVERCURRENT); + interface_send1(OVERCURRENT); } } else @@ -70,14 +69,16 @@ uint8_t monitor_command(const uint8_t *cmd_buf, uint8_t cmd_length) { if(cmd_buf[0]==READ_TRACK_CURRENT) { + uint8_t reply[3]; + if(cmd_length!=1) return LENGTH_ERROR; - serial_write(0xFC); - serial_write(TRACK_CURRENT); uint16_t value = track_current_milliamps(); - serial_write(value>>8); - serial_write(value); + reply[0] = TRACK_CURRENT; + reply[1] = value>>8; + reply[2] = value; + interface_send(reply, sizeof(reply)); } else if(cmd_buf[0]==SET_OVERCURRENT_LIMIT) { @@ -95,14 +96,16 @@ uint8_t monitor_command(const uint8_t *cmd_buf, uint8_t cmd_length) } else if(cmd_buf[0]==READ_INPUT_VOLTAGE) { + uint8_t reply[3]; + if(cmd_length!=1) return LENGTH_ERROR; - serial_write(0xFC); - serial_write(INPUT_VOLTAGE); uint16_t value = input_voltage_millivolts(); - serial_write(value>>8); - serial_write(value); + reply[0] = INPUT_VOLTAGE; + reply[1] = value>>8; + reply[2] = value; + interface_send(reply, sizeof(reply)); } else return INVALID_COMMAND;