]> git.tdb.fi Git - model-railway-devices.git/commitdiff
Pass command data through a pointer
authorMikko Rasa <tdb@tdb.fi>
Wed, 23 Oct 2013 17:36:25 +0000 (20:36 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 23 Oct 2013 17:36:25 +0000 (20:36 +0300)
This is cleaner and more flexible than the global buffer, and isn't any
slower.

arducontrol/interface.c
arducontrol/interface.h
arducontrol/monitor.c
arducontrol/monitor.h
arducontrol/motorola.c
arducontrol/motorola.h
arducontrol/output.c
arducontrol/output.h

index 041dd67bafc05f07fb9ef096b9f9b8a0668f9c8b..96b1f96cd97e8cc1cf78435eb60d0a22123b9b6c 100644 (file)
@@ -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<cmd_length; ++i, ++j)
+                       for(i=0, j=recv_tail+1; i<length; ++i, ++j)
                        {
                                if(j>=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;
 }
index 7e89e2e0e91208602b4f722ab55b95bdfacaa6e8..6b5a948fbec426b49253cfc576fde129d0fc0c82 100644 (file)
@@ -4,9 +4,6 @@
 #include <stdint.h>
 #include "commands.h"
 
-extern uint8_t cmd_buf[];
-extern uint8_t cmd_length;
-
 void interface_init(void);
 void interface_check(void);
 
index 112e1ac56486c2317dce74d189e815de0f6afe83..4adc9377ee5da54a78c5aba88aa3497302f8cd94 100644 (file)
@@ -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)
        {
index b21763d097c9b6e6036cdd9a8f22e239d689c11c..031d26ec8bb246eb1845c89438482e9fe1e9d50a 100644 (file)
@@ -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
index 33cf1c0a9209054391b6c380000cdf34ce1ebd85..0ee6704ede87aaba476f2cc758223fe072d60cd6 100644 (file)
@@ -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)
        {
index 4a64d62255cb4202aec4d7da95c26926088bf43d..102e790ea03344c76738085a41ad251345ba4c89 100644 (file)
@@ -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
index 748c554d32cc6028347bbcc2d7bc9138f385fee7..f961a436b99c2d24bf9d73255fe0a951d30df37c 100644 (file)
@@ -1,5 +1,5 @@
 #include <avr/io.h>
-#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)
        {
index 05c9a21ef713f1988f9b83768c6436cfc196fb25..c94c665bad6d9c322be59636b6b720b5103fe374 100644 (file)
@@ -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