-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.h
100 lines (74 loc) · 1.97 KB
/
commands.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#ifndef _COMMANDS_H
#define _COMMANDS_H
#include "WProgram.h"
#define CMD_BUF_LEN 64
#define MAX_MSG_FIELDS 4
enum MESSAGE_TYPE {
M_GO,
M_STOP,
M_SET,
M_GET,
M_HOME,
M_STATE,
M_ALIVE,
M_CLICK,
NOT_A_MESSAGE,
};
enum PARAMETER {
P_VERSION,
P_INDEX,
P_MAX_VEL,
P_ACCEL,
P_STOP_MODE,
P_POS,
NOT_A_PARAMETER,
};
class CommandInterpreter {
public:
struct Message {
MESSAGE_TYPE type; //< Message type (GO, SET, etc)
char typeDefIdx; //< Index into the
long fields[MAX_MSG_FIELDS];
};
typedef void (&CommandHandler)( Message *msg );
CommandInterpreter(CommandHandler handler);
void begin( unsigned int baudrate );
void checkSerialInput();
void sendACK( const char* message );
void sendERROR( const char* message );
void sendNOTICE( const char* message );
void sendDONE( const char* message );
private:
enum MESSAGE_VALUE_TYPE {
MT_INTEGER,
MT_PARAM_NAME,
NOT_A_VALUE,
};
struct MessageTypeDefinition {
const char *name;
MESSAGE_TYPE type;
enum MESSAGE_VALUE_TYPE *values;
};
struct ParameterDefinition {
const char *name;
PARAMETER param;
boolean requiresAxis; // true if this parameter applies to a single axis
};
static MESSAGE_VALUE_TYPE NO_VALUES[];
static MESSAGE_VALUE_TYPE GO_VALUES[];
static MESSAGE_VALUE_TYPE SET_VALUES[];
static MESSAGE_VALUE_TYPE GET_VALUES[];
static MESSAGE_VALUE_TYPE HOME_VALUES[];
static MessageTypeDefinition messageTypes[];
static ParameterDefinition parameterTypes[];
boolean processCommand( const char *cmd, Message& msg );
void sendReply( const char *str );
int parseCmdType( const char *cmd, Message& msg );
PARAMETER parseParamName( const char *name, boolean& requiresAxis );
boolean parseMessageValues( const char *str, Message& msg );
char commandBuf[CMD_BUF_LEN+1];
char paramBuf[CMD_BUF_LEN+1];
char commandBufIdx;
CommandHandler cmdHandler;
};
#endif