Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCommandHandler.h
More file actions
Latest commit
149 lines (123 loc) · 6.39 KB
/
Copy pathCommandHandler.h
File metadata and controls
149 lines (123 loc) · 6.39 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* CommandHandler - A Wiring/Arduino library to tokenize and parse commands
* received in different forms, serial, string, or char.
*
* Copyright (C) 2015 Cronin Group http://www.chem.gla.ac.uk/cronin/
* Copyright (C) 2012 Stefan Rado
* Copyright (C) 2011 Steven Cogswell <steven.cogswell@gmail.com>
* http://husks.wordpress.com
*
* Version 20151029
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CommandHandler_h
#defineCommandHandler_h
#if defined(WIRING) && WIRING >= 100
#include<Wiring.h>
#elif defined(ARDUINO) && ARDUINO >= 100
#include<Arduino.h>
#else
#include<WProgram.h>
#endif
#include<string.h>
// Size of the input buffer in bytes (maximum length of one command plus arguments)
#defineCOMMANDHANDLER_BUFFER64
// Maximum length of a command excluding the terminating null
#defineCOMMANDHANDLER_MAXCOMMANDLENGTH8
// Default delimitor and terminator
#defineCOMMANDHANDLER_DEFAULT_DELIM","
#defineCOMMANDHANDLER_DEFAULT_TERM';'
// The null term for string
#defineSTRING_NULL_TERM'\0'
// Uncomment the next line to run the library in debug mode (verbose messages)
// #define COMMANDHANDLER_DEBUG
classCommandHandler {
public:
CommandHandler(constchar *newdelim = COMMANDHANDLER_DEFAULT_DELIM, constchar newterm = COMMANDHANDLER_DEFAULT_TERM); // Constructor
voidaddCommand(constchar *command, void(*function)()); // Add a command to the processing dictionary.
voidaddRelay(constchar *command, void (*function)(constchar *, void*), void* pt2Object = NULL); // Add a command to the relay dictionary. Such relay are given the remaining of the command. pt2Object is the reference to the instance associated with the callback, it will be given as the second argument of the callback function, default is NULL
voidsetDefaultHandler(void (*function)(constchar *)); // A handler to call when no valid command received.
voidsetDefaultHandler(void (*function)(constchar *, void*), void* pt2Object); // A handler to call when no valid command received.
voidsetInCmdSerial(Stream &inStream); // define to which serial to send the read commands
voidprocessSerial(); // Process what on the in stream
voidprocessSerial(Stream &inStream); // Process what on the designated stream
voidprocessString(constchar *inString); // Process a String
voidprocessChar(char inChar); //Process a char
voidclearBuffer(); // Clears the input buffer.
char *remaining(); // Returns pointer to remaining of the command buffer (for getting arguments to commands).
char *next(); // Returns pointer to next token found in command buffer (for getting arguments to commands).
// helpers to cast next into different types
bool argOk; // this variable is set after the below function are run, it tell you if thing went well
boolreadBoolArg();
intreadIntArg();
longreadLongArg();
floatreadFloatArg();
doublereadDoubleArg();
char *readStringArg();
boolcompareStringArg(constchar *stringToCompare);
//helpers to create a message
voidsetCmdHeader(constchar *cmdHeader, bool addDelim = true); // setting a char to be added at the start of each out message (default "")
voidinitCmd(); // initialize the command buffer to build next message to be sent
voidclearCmd(); // clear the output command
voidaddCmdDelim();
voidaddCmdTerm();
voidaddCmdBool(bool value);
voidaddCmdInt(int value);
voidaddCmdLong(long value);
voidsetCmdDecimal(byte decimal);
voidaddCmdFloat(double value);
voidaddCmdFloat(float value, byte decimal);
voidaddCmdDouble(double value);
voidaddCmdDouble(double value, byte decimal);
voidaddCmdString(constchar *value);
char* getOutCmd(); // get pointer to command buffer
voidsetOutCmdSerial(Stream &outStream); // define to which serial to send the out commands
voidsendCmdSerial(); //send current command thought the Stream
voidsendCmdSerial(Stream &outStream); //send current command thought the Stream
private:
// Command/handler dictionary
structCommandHandlerCallback {
char command[COMMANDHANDLER_MAXCOMMANDLENGTH + 1];
void (*function)();
}; // Data structure to hold Command/Handler function key-value pairs
CommandHandlerCallback *commandList; // Actual definition for command/handler array
byte commandCount;
// Relay/handler dictionary
structRelayHandlerCallback {
char command[COMMANDHANDLER_MAXCOMMANDLENGTH + 1];
void* pt2Object;
void (*function)(constchar *, void*);
}; // Data structure to hold Relay/Handler function key-value pairs
RelayHandlerCallback *relayList; // Actual definition for Relay/handler array
byte relayCount;
// Pointer to the default handler function
void (*defaultHandler)(constchar *);
void* pt2defaultHandlerObject;
void (*wrapper_defaultHandler)(constchar *, void*);
constchar *delim; // null-terminated list of character to be used as delimeters for tokenizing (default " ")
char term; // Character that signals end of command (default '\n')
char buffer[COMMANDHANDLER_BUFFER + 1]; // Buffer of stored characters while waiting for terminator character
byte bufPos; // Current position in the buffer
char *last; // State variable used by strtok_r during processing
char remains[COMMANDHANDLER_BUFFER + 1]; // Buffer of stored characters to pass to a relay function
char command[COMMANDHANDLER_BUFFER + 1];
String commandString; // Out Command
String commandHeader; // header for out command
byte commandDecimal;
// in and out default strem
Stream *inCmdStream;
Stream *outCmdStream;
};
#endif//CommandHandler_h