Skip to content

Commit 4844a0c

Browse files
committed
Changed that EEPROM functionality gets excluded in case the board doesn't support it. Updated in-code documentation and Readme
1 parent 7ef1493 commit 4844a0c

File tree

3 files changed

+201
-71
lines changed

3 files changed

+201
-71
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Features:
1111
* Send different kind of messages with a simple and lightweight message format.
1212
* Send status updates of the device (e.g. for pushing new sensor values via Serial Interface).
1313
* Send arbitrary types of messages.
14-
* Get/Set an ID from and to the EEPROM.
14+
* Get/Set an ID from and to the EEPROM (If the target board has one available).
1515
# Folder structure
1616
* `src` contains the source code.
1717
* `examples` contains an example sketch.
@@ -44,7 +44,7 @@ The StreamCommander will be initialised with the standard `Serial`-Object by def
4444
7. Send status updates with `updateStatus`-function.
4545
1. If the status has changed since the last update, a new status message will automatically be sent.
4646
2. If the device is not activated, possible status updates won't be sent out. They can still be queried manually with the `status`-command.
47-
8. In case you need the device to have an ID (for example if you need to adress multiple devices separately), set an id with the `setid`-command the first time you boot the device.
47+
8. In case you need the device to have an ID (for example if you need to adress multiple devices separately), set an id with the `setid`-command the first time you boot the device. If an EEPROM is available on the board:
4848
1. The ID will be persisted in the EEPROM of the device, and will automatically be loaded on every initialisation of the StreamCommander. No need to hardcode this.
4949
2. The ID will only be updated in the EEPROM if it really changes, which extends the lifespan of the EEPROM.
5050
## Arguments

src/StreamCommander.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ StreamCommander::~StreamCommander()
3939

4040
void StreamCommander::init( bool active, char commandDelimiter, char messageDelimiter, bool echoCommands, bool addStandardCommands, long streamBufferTimeout )
4141
{
42+
#if __has_include("<EEPROM.h>")
4243
loadIdFromEeprom();
44+
#endif
45+
4346
setCommandDelimiter( commandDelimiter );
4447
setMessageDelimiter( messageDelimiter );
4548
setStreamBufferTimeout( streamBufferTimeout );
@@ -156,6 +159,7 @@ long StreamCommander::getStreamBufferTimeout()
156159
return this->streamBufferTimeout;
157160
}
158161

162+
#if __has_include("<EEPROM.h>")
159163
void StreamCommander::saveIdToEeprom( String id )
160164
{
161165
// Since EEPROM.put can't handle strings, we have to convert it to a c_str
@@ -172,6 +176,7 @@ void StreamCommander::loadIdFromEeprom()
172176

173177
setId( String( id ) );
174178
}
179+
#endif
175180

176181
void StreamCommander::setId( String id )
177182
{
@@ -192,7 +197,10 @@ void StreamCommander::setId( String id )
192197
return;
193198
}
194199

200+
#if __has_include("<EEPROM.h>")
195201
saveIdToEeprom( id );
202+
#endif
203+
196204
this->id = id;
197205
sendId();
198206
}

src/StreamCommander.hpp

Lines changed: 191 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919

2020
// Arduino Standard Libraries
2121
#include <Arduino.h>
22-
#include <EEPROM.h>
2322
#include <MessageTypes.hpp>
2423

24+
#if __has_include("<EEPROM.h>")
25+
#include <EEPROM.h>
26+
#endif
27+
2528
class StreamCommander
2629
{
2730
private:
@@ -76,82 +79,201 @@ class StreamCommander
7679
int numCommands;
7780

7881
// Private Methods
79-
void setStreamInstance( Stream * streamInstance ); // Sets the streamInstance of the StreamCommander.
80-
Stream * getStreamInstance(); // Gets the current streamInstance of the StreamCommander.
81-
void setAddStandardCommands( bool addStandardCommands ); // Sets whether the standard commands should be added or not (true/false).
82-
bool shouldAddStandardCommands(); // Returns whether the standard commadns should be added or not.
83-
void saveIdToEeprom( String id ); // Saves and ID to the EEPROM if it differs from the old one.
84-
void loadIdFromEeprom(); // Loads the ID from the EEPROM.
85-
CommandContainer * getCommandContainer( String command ); // Gets the container containing all commands.
86-
int getCommandContainerIndex( String command ); // Returns the index (position) of a specific command in the command container by name.
87-
void deleteCommands(); // Deletes all registered commands.
88-
void setNumCommands( int numCommands ); // Sets the number of the currently registered commands.
89-
void incrementNumCommands(); // Increments the number of the currently registered commands.
90-
void executeCommand( String command, String arguments ); // Tries to execute a command with given arguments. Arguments can be empty.
91-
92-
static void commandActivate( String arguments, StreamCommander * instance );// Definition of the command COMMAND_ACTIVATE.
93-
static void commandDeactivate( String arguments, StreamCommander * instance ); // Definition of the command COMMAND_DEACTIVATE.
94-
static void commandIsActive( String arguments, StreamCommander * instance ); // Definition of the command COMMAND_ISACTIVE.
95-
static void commandSetEcho( String arguments, StreamCommander * instance ); // Definition of the command COMMAND_SETECHO.
96-
static void commandSetId( String id, StreamCommander * instance ); // Definition of the command COMMAND_SETID.
97-
static void commandGetId( String arguments, StreamCommander * instance ); // Definition of the command COMMAND_GETID.
98-
static void commandPing( String arguments, StreamCommander * instance ); // Definition of the command COMMAND_PING.
99-
static void commandGetStatus( String arguments, StreamCommander * instance ); // Definition of the command COMMAND_GETSTATUS.
100-
static void commandListCommands( String arguments, StreamCommander * instance ); // Definition of the command COMMAND_LISTCOMMANDS.
101-
void addAllStandardCommands(); // Registers all the above commands.
102-
static void defaultCommand( String command, String arguments, StreamCommander * instance ); // Definition of the default callback.
82+
// Sets the streamInstance of the StreamCommander.
83+
void setStreamInstance( Stream * streamInstance );
84+
85+
// Gets the current streamInstance of the StreamCommander.
86+
Stream * getStreamInstance();
87+
88+
// Sets whether the standard commands should be added or not (true/false).
89+
void setAddStandardCommands( bool addStandardCommands );
90+
91+
// Returns whether the standard commadns should be added or not.
92+
bool shouldAddStandardCommands();
93+
94+
// This functions do only get implemented in case an EEPROM is available for the Board.
95+
#if __has_include("<EEPROM.h>")
96+
// Saves an ID to the EEPROM if it differs from the old one.
97+
void saveIdToEeprom( String id );
98+
99+
// Loads the ID from the EEPROM.
100+
void loadIdFromEeprom();
101+
#endif
102+
103+
// Gets the container containing all commands.
104+
CommandContainer * getCommandContainer( String command );
105+
106+
// Returns the index (position) of a specific command in the command container by name.
107+
int getCommandContainerIndex( String command );
108+
109+
// Deletes all registered commands.
110+
void deleteCommands();
111+
112+
// Sets the number of the currently registered commands.
113+
void setNumCommands( int numCommands );
114+
115+
// Increments the number of the currently registered commands.
116+
void incrementNumCommands();
117+
118+
// Tries to execute a command with given arguments. Arguments can be empty.
119+
void executeCommand( String command, String arguments );
120+
121+
// Definition of the command COMMAND_ACTIVATE.
122+
static void commandActivate( String arguments, StreamCommander * instance );
123+
124+
// Definition of the command COMMAND_DEACTIVATE.
125+
static void commandDeactivate( String arguments, StreamCommander * instance );
126+
127+
// Definition of the command COMMAND_ISACTIVE.
128+
static void commandIsActive( String arguments, StreamCommander * instance );
129+
130+
// Definition of the command COMMAND_SETECHO.
131+
static void commandSetEcho( String arguments, StreamCommander * instance );
132+
133+
// Definition of the command COMMAND_SETID.
134+
static void commandSetId( String id, StreamCommander * instance );
135+
136+
// Definition of the command COMMAND_GETID.
137+
static void commandGetId( String arguments, StreamCommander * instance );
138+
139+
// Definition of the command COMMAND_PING.
140+
static void commandPing( String arguments, StreamCommander * instance );
141+
142+
// Definition of the command COMMAND_GETSTATUS.
143+
static void commandGetStatus( String arguments, StreamCommander * instance );
144+
145+
// Definition of the command COMMAND_LISTCOMMANDS.
146+
static void commandListCommands( String arguments, StreamCommander * instance );
147+
148+
// Registers all the above commands.
149+
void addAllStandardCommands();
150+
151+
// Definition of the default callback.
152+
static void defaultCommand( String command, String arguments, StreamCommander * instance );
103153

104154
public:
105155
// Constructor
106-
StreamCommander( Stream * streamInstance = &Serial ); // Constructor, instance of a Stream object as argument.
156+
// Constructor, instance of a Stream object as argument.
157+
StreamCommander( Stream * streamInstance = &Serial );
107158

108159
// Destructor
109160
~StreamCommander();
110161

111162
// Public Methods
112-
void init( // Init function for setting up the StreamCommander correctly, after it has been successfuly constructed.
113-
bool active = true, // Whether the StreamCommander is set to active or not. This only influences the automatic status updates.
114-
char commandDelimiter = COMMAND_DELIMITER, // Character which delimits a command from its' arguments.
115-
char messageDelimiter = MESSAGE_DELIMITER, // Character which delimits a message from its' contents.
116-
bool echoCommands = false, // Should commands be echoed at arrival or not?
117-
bool addStandardCommands = true, // Should the standard commands be added or not?
118-
long streamBufferTimeout = STREAM_BUFFER_TIMEOUT // Sets the timeout of the specific streams' buffer.
163+
// Init function for setting up the StreamCommander correctly, after it has been successfuly constructed.
164+
void init(
165+
// Whether the StreamCommander is set to active or not. This only influences the automatic status updates.
166+
bool active = true,
167+
168+
// Character which delimits a command from its' arguments.
169+
char commandDelimiter = COMMAND_DELIMITER,
170+
171+
// Character which delimits a message from its' contents.
172+
char messageDelimiter = MESSAGE_DELIMITER,
173+
174+
// Should commands be echoed at arrival or not?
175+
bool echoCommands = false,
176+
177+
// Should the standard commands be added or not?
178+
bool addStandardCommands = true,
179+
180+
// Sets the timeout of the specific streams' buffer.
181+
long streamBufferTimeout = STREAM_BUFFER_TIMEOUT
119182
);
120-
void setActive( bool active ); // Sets whether the automatic status updates are activated or not (true/false).
121-
bool isActive(); // Returns if the automatic status updates are activated.
122-
void setCommandDelimiter( char commandDelimiter ); // Sets the command delimiter, separating the command from a potential argument.
123-
char getCommandDelimiter(); // Gets the command delimiter.
124-
void setMessageDelimiter( char messageDelimiter ); // Sets the message delimiter, separating the type from the content.
125-
char getMessageDelimiter(); // Gets the message delimiter.
126-
void setEchoCommands( bool echoCommands ); // Sets whether all incoming commands should be echoed or not (true/false).
127-
bool shouldEchoCommands(); // Returns if all incomming commands should be echoed.
128-
void setStreamBufferTimeout( long streamBufferTimeout ); // Sets the timeout of the specific streams' buffer.
129-
long getStreamBufferTimeout(); // Returns the timeout of the specific streams' buffer.
130-
void setId( String id ); // Sets the ID of the StreamCommander/Device.
131-
String getId(); // Gets the ID of the StreamCommander/Device.
132-
133-
void updateStatus( String status ); // Update the status of the StreamCommander/Device; updates the status and sends an automatic status message only if the status changed.
134-
void setStatus( String status ); // Sets the current status StreamCommander/Device.
135-
String getStatus(); // Gets the current status StreamCommander/Device.
136-
137-
void addCommand( String command, CommandCallbackFunction commandCallback ); // Registers a new command; a command name tied to a command callback.
138-
int getNumCommands(); // Gets the number of the registered commands.
139-
String getCommandList(); // Gets a list of all registered commands.
140-
void setDefaultCallback( DefaultCallbackFunction defaultCallbackFunction ); // Sets the default callback which gets called in case a sent command is not registered.
141-
DefaultCallbackFunction getDefaultCallback(); // Gets the default callback.
142-
143-
void fetchCommand(); // Fetches and interprets incoming commands, and invokes the corresponding callbacks. This should be called in the loop or after an interrupt/event.
144-
145-
void sendMessage( String type, String content ); // Sends a message with a specific type and content separated by our delimiter.
146-
void sendResponse( String response ); // Sends a message of type MessageType::RESPONSE.
147-
void sendInfo( String info ); // Sends a message of type MessageType::INFO.
148-
void sendError( String error ); // Sends a message of type MessageType::ERROR.
149-
void sendPing(); // Sends a message of type MessageType::PING, contains a "reply".
150-
void sendStatus(); // Sends a message of type MessageType::STATUS, contains the current status.
151-
void sendId(); // Sends a message of type MessageType::ID, contains the current ID.
152-
void sendIsActive(); // Sends a message of type MessageType::ACTIVE, contains the current active status.
153-
void sendEcho( String echo ); // Sends a message of type MessageType::ECHO.
154-
void sendCommands(); // Sends a message of type MessageType::COMMANDS, contains a list of currently registered commands.
183+
184+
// Sets whether the automatic status updates are activated or not (true/false).
185+
void setActive( bool active );
186+
187+
// Returns if the automatic status updates are activated.
188+
bool isActive();
189+
190+
// Sets the command delimiter, separating the command from a potential argument.
191+
void setCommandDelimiter( char commandDelimiter );
192+
193+
// Gets the command delimiter.
194+
char getCommandDelimiter();
195+
196+
// Sets the message delimiter, separating the type from the content.
197+
void setMessageDelimiter( char messageDelimiter );
198+
199+
// Gets the message delimiter.
200+
char getMessageDelimiter();
201+
202+
// Sets whether all incoming commands should be echoed or not (true/false).
203+
void setEchoCommands( bool echoCommands );
204+
205+
// Returns if all incomming commands should be echoed.
206+
bool shouldEchoCommands();
207+
208+
// Sets the timeout of the specific streams' buffer.
209+
void setStreamBufferTimeout( long streamBufferTimeout );
210+
211+
// Returns the timeout of the specific streams' buffer.
212+
long getStreamBufferTimeout();
213+
214+
// Sets the ID of the StreamCommander/Device.
215+
// The ID gets only saved to an EEPROM if one is available.
216+
void setId( String id );
217+
218+
// Gets the ID of the StreamCommander/Device.
219+
String getId();
220+
221+
// Update the status of the StreamCommander/Device; updates the status and sends an automatic status message only if the status changed.
222+
void updateStatus( String status );
223+
224+
// Sets the current status StreamCommander/Device.
225+
void setStatus( String status );
226+
227+
// Gets the current status StreamCommander/Device.
228+
String getStatus();
229+
230+
// Registers a new command; a command name tied to a command callback.
231+
void addCommand( String command, CommandCallbackFunction commandCallback );
232+
233+
// Gets the number of the registered commands.
234+
int getNumCommands();
235+
236+
// Gets a list of all registered commands.
237+
String getCommandList();
238+
239+
// Sets the default callback which gets called in case a sent command is not registered.
240+
void setDefaultCallback( DefaultCallbackFunction defaultCallbackFunction );
241+
242+
// Gets the default callback.
243+
DefaultCallbackFunction getDefaultCallback();
244+
245+
// Fetches and interprets incoming commands, and invokes the corresponding callbacks. This should be called in the loop or after an interrupt/event.
246+
void fetchCommand();
247+
248+
// Sends a message with a specific type and content separated by our delimiter.
249+
void sendMessage( String type, String content );
250+
251+
// Sends a message of type MessageType::RESPONSE.
252+
void sendResponse( String response );
253+
254+
// Sends a message of type MessageType::INFO.
255+
void sendInfo( String info );
256+
257+
// Sends a message of type MessageType::ERROR.
258+
void sendError( String error );
259+
260+
// Sends a message of type MessageType::PING, contains a "reply".
261+
void sendPing();
262+
263+
// Sends a message of type MessageType::STATUS, contains the current status.
264+
void sendStatus();
265+
266+
// Sends a message of type MessageType::ID, contains the current ID.
267+
void sendId();
268+
269+
// Sends a message of type MessageType::ACTIVE, contains the current active status.
270+
void sendIsActive();
271+
272+
// Sends a message of type MessageType::ECHO.
273+
void sendEcho( String echo );
274+
275+
// Sends a message of type MessageType::COMMANDS, contains a list of currently registered commands.
276+
void sendCommands();
155277
};
156278

157-
#endif // STREAMCOMMANDER_HPP
279+
#endif // STREAMCOMMANDER_HPP

0 commit comments

Comments
 (0)