This is cleaner and more flexible than the global buffer, and isn't any
slower.
uint8_t recv_tail = 0;
volatile uint8_t recv_fill = 0;
volatile uint8_t recv_overrun = 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);
void process_commands(void);
-uint8_t process_command(void);
+uint8_t dispatch_command(const uint8_t *, uint8_t);
void interface_init(void)
{
void interface_init(void)
{
- 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)
- cmd_length = ~c;
- if(recv_fill<=cmd_length)
- 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;
{
if(j>=sizeof(recv_buf))
j = 0;
- cmd_buf[i] = recv_buf[j];
-
- consumed = 1+cmd_length;
serial_write(0xFE);
serial_write(FRAMING_ERROR);
serial_write(0xFE);
serial_write(FRAMING_ERROR);
if(recv_tail>=sizeof(recv_buf))
recv_tail -= sizeof(recv_buf);
if(recv_tail>=sizeof(recv_buf))
recv_tail -= sizeof(recv_buf);
- uint8_t result = process_command();
+ uint8_t result = dispatch_command(cmd, length);
serial_write(0xFE);
serial_write(result);
}
}
}
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;
- uint8_t subtype = (cmd_buf[0]>>3)&1;
+ uint8_t subtype = (cmd[0]>>3)&1;
- return output_command();
+ return output_command(cmd, length);
- return monitor_command();
+ return monitor_command(cmd, length);
- return motorola_command();
+ return motorola_command(cmd, length);
else
return INVALID_COMMAND;
}
else
return INVALID_COMMAND;
}
#include <stdint.h>
#include "commands.h"
#include <stdint.h>
#include "commands.h"
-extern uint8_t cmd_buf[];
-extern uint8_t cmd_length;
-
void interface_init(void);
void interface_check(void);
void interface_init(void);
void interface_check(void);
#include "monitor.h"
#include "output.h"
#include "serial.h"
#include "monitor.h"
#include "output.h"
#include "serial.h"
-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)
{
{
if(cmd_buf[0]==READ_TRACK_CURRENT)
{
void monitor_init(void);
void monitor_check(void);
void monitor_init(void);
void monitor_check(void);
-uint8_t monitor_command(void);
+uint8_t monitor_command(const uint8_t *, uint8_t);
#include "motorola.h"
#include "output.h"
#include "motorola.h"
#include "output.h"
-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)
{
{
if(cmd_buf[0]==MOTOROLA_SPEED || cmd_buf[0]==MOTOROLA_SPEED_DIRECTION || cmd_buf[0]==MOTOROLA_SPEED_FUNCTION)
{
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);
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);
#include "output.h"
#include "timer.h"
#include "output.h"
#include "timer.h"
-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)
{
{
if(cmd_buf[0]==POWER_ON || cmd_buf[0]==POWER_OFF)
{
void output_init(void);
void clear_packet(void);
void output_set_power(uint8_t);
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);