CommandCatcher
Helps obtain commands from attached host.
Loading...
Searching...
No Matches
CommandCatcher.h
Go to the documentation of this file.
1#include "Arduino.h"
2#include <stdint.h>
3#include <avr/pgmspace.h>
4#ifndef CommandCatcher_h
5#define CommandCatcher_h
6
19
24public:
25 virtual ~CommandListener() = default;
26
33 virtual void notify(char* cmd, char* param);
34};
35
36
38public:
39
43 typedef void (*CCListener)(char*, char*);
44
50
61 void init(Stream& serial=Serial, uint8_t bufsize=16);
62
70 void update(bool close=true);
71
82 void addListener(CCListener myListener);
83
92
98 void setTerminator(char term);
99
105 void setSeparator(char sep);
106
111 void close();
112
118 bool ready();
119
125 char* getCommand();
126
132 char* getParameter();
133
134
135private:
136 Stream& ser = Serial;
137 bool initialized = false;
138
139 char* buffer = NULL;
140 uint8_t buflen = 0;
141 uint8_t reclen = 0;
142 uint8_t cmdlen = 0;
143 bool cmdrdy = false;
144 bool overrun = false;
145
146 char terminator = '\n';
147 char separator = ' ';
148
149 class FListener : public CommandListener {
150 public:
151 FListener(void method(char*, char*)) : method(method) {}
152
153 void notify(char* cmd, char* param) override {
154 method(cmd, param);
155 }
156 private:
157 void (*method)(char*, char*);
158 };
159
160 static const uint8_t maxListeners = 4;
161 CommandListener* listeners[maxListeners];
162 uint8_t numListeners = 0;
163
164 // returns true if command is available
165 bool updateBuffer();
166 void notifyListeners();
167};
168
169// global command catcher object for convenience
171
172// **********************
173#endif
CommandCatcher CCatcher
Definition CommandCatcher.cpp:17
Project: CommandCatcher for Arduino.
Definition CommandCatcher.h:37
void(* CCListener)(char *, char *)
Definition CommandCatcher.h:43
CommandCatcher()
default constructor
Definition CommandCatcher.cpp:19
bool ready()
Returns true if a command is ready to be processed.
Definition CommandCatcher.cpp:85
char * getParameter()
Returns the parameter string.
Definition CommandCatcher.cpp:93
void addListener(CCListener myListener)
Adds a listener to be called when a command is available.
Definition CommandCatcher.cpp:66
void setTerminator(char term)
Sets the terminator character.
Definition CommandCatcher.cpp:47
char * getCommand()
Returns the command.
Definition CommandCatcher.cpp:89
void update(bool close=true)
checks the Serial input stream and processes any command info available.
Definition CommandCatcher.cpp:55
void init(Stream &serial=Serial, uint8_t bufsize=16)
Initializes the serial interface for communication.
Definition CommandCatcher.cpp:22
void close()
Closes out the current command.
Definition CommandCatcher.cpp:78
void setSeparator(char sep)
Sets the separator character.
Definition CommandCatcher.cpp:51
abstract class to be implemented by listening class.
Definition CommandCatcher.h:23
virtual ~CommandListener()=default
virtual void notify(char *cmd, char *param)
called when a command s ready.