]> git.tdb.fi Git - model-railway-devices.git/commitdiff
Add functions for sending replies
authorMikko Rasa <tdb@tdb.fi>
Wed, 23 Oct 2013 18:02:42 +0000 (21:02 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 23 Oct 2013 18:02:42 +0000 (21:02 +0300)
arducontrol/interface.c
arducontrol/interface.h
arducontrol/monitor.c

index 96b1f96cd97e8cc1cf78435eb60d0a22123b9b6c..96baba7f0a4d154110bcf826fd804d5f3e7d0f86 100644 (file)
@@ -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<length; ++i)
+               serial_write(cmd[i]);
+}
+
+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))
index 6b5a948fbec426b49253cfc576fde129d0fc0c82..fbd0238d1bd3122022bf136aa7b667fa8a489ffa 100644 (file)
@@ -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
index 4adc9377ee5da54a78c5aba88aa3497302f8cd94..d8f3dadded7c44b73a3f449985380e04b1f29f0f 100644 (file)
@@ -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;