Skip to content

Commit 1f3a3d2

Browse files
committed
Tiny refactoring
1 parent cc9593c commit 1f3a3d2

File tree

3 files changed

+64
-63
lines changed

3 files changed

+64
-63
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,11 @@ The StreamCommander has several standard commands which implement basic function
106106
| activate | Activates the automatic publishing of new status-messages | |
107107
| deactivate | Deactivates the automatic publishing of new status-messages | |
108108
| isactive | Returns whether the device is set to active or not | |
109-
| echo | Sets the echoing of incoming commands on or off | on / off |
109+
| setecho | Sets the echoing of incoming commands on or off | on / off |
110110
| setid | Sets the ID of the device | <id> |
111111
| getid | Returns the ID of the device | |
112-
| ping | Returns a "ping:response"-message | |
113-
| status | Returns the current status of the device | |
112+
| ping | Returns a ping response message (usually a "ping:reply" message) | |
113+
| getstatus | Returns the current status of the device | |
114114
| commands | Returns all registered commands of the device | |
115115
# Message format
116116
To make the communication more easy and consistent, a simple message format has been defined, which is used by the ArduinoStreamCommander.\
@@ -123,7 +123,7 @@ The StreamCommander uses several standard message types, which are defined in [A
123123
| response | Return responses after a command has been executed |
124124
| info | Inform about something like an internal event (e.g. for logging-purposes) |
125125
| error | Inform about an error/problem which occured during the runtime |
126-
| ping | Containts an ping response message |
126+
| ping | Contains a ping response message (usually containts "reply") |
127127
| status | Send the current/most recent status of the device (Especially used for status updates) |
128128
| id | Contains the id of the device |
129129
| active | Contains whether the device is set to active or not |

src/StreamCommander.cpp

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616

1717
#include "StreamCommander.hpp"
1818

19+
const String StreamCommander::PING_REPLY = "reply";
1920
const String StreamCommander::COMMAND_ACTIVATE = "activate";
2021
const String StreamCommander::COMMAND_DEACTIVATE = "deactivate";
2122
const String StreamCommander::COMMAND_ISACTIVE = "isactive";
22-
const String StreamCommander::COMMAND_SETECHO = "echo";
23+
const String StreamCommander::COMMAND_SETECHO = "setecho";
2324
const String StreamCommander::COMMAND_SETID = "setid";
2425
const String StreamCommander::COMMAND_GETID = "getid";
2526
const String StreamCommander::COMMAND_PING = "ping";
26-
const String StreamCommander::COMMAND_GETSTATUS = "status";
27+
const String StreamCommander::COMMAND_GETSTATUS = "getstatus";
2728
const String StreamCommander::COMMAND_LISTCOMMANDS = "commands";
2829

2930
StreamCommander::StreamCommander( Stream * streamInstance )
@@ -38,7 +39,7 @@ StreamCommander::~StreamCommander()
3839

3940
void StreamCommander::init( bool active, char commandDelimiter, char messageDelimiter, bool echoCommands, bool addStandardCommands, long streamBufferTimeout )
4041
{
41-
loadIdFromEEPROM();
42+
loadIdFromEeprom();
4243
setCommandDelimiter( commandDelimiter );
4344
setMessageDelimiter( messageDelimiter );
4445
setStreamBufferTimeout( streamBufferTimeout );
@@ -82,7 +83,8 @@ Stream * StreamCommander::getStreamInstance()
8283

8384
void StreamCommander::setActive( bool active )
8485
{
85-
if ( isActive() != active ) // Only set our active-status if it's differing
86+
// Only set & send our active-status if it's differing
87+
if ( isActive() != active )
8688
{
8789
this->active = active;
8890

@@ -137,10 +139,10 @@ bool StreamCommander::shouldAddStandardCommands()
137139

138140
void StreamCommander::setStreamBufferTimeout( long streamBufferTimeout )
139141
{
140-
// Check for Input-Errors
142+
// Check if the timeout is over or equal to 0
141143
if ( streamBufferTimeout < 0 )
142144
{
143-
sendError( F("Timeout has to be >= 0.") );
145+
sendError( F( "Timeout has to be >= 0." ) );
144146

145147
return;
146148
}
@@ -154,7 +156,7 @@ long StreamCommander::getStreamBufferTimeout()
154156
return this->streamBufferTimeout;
155157
}
156158

157-
void StreamCommander::saveIdToEEPROM( String id )
159+
void StreamCommander::saveIdToEeprom( String id )
158160
{
159161
// Since EEPROM.put can't handle strings, we have to convert it to a c_str
160162
char idBuffer[ID_MAX_LENGTH];
@@ -163,7 +165,7 @@ void StreamCommander::saveIdToEEPROM( String id )
163165
EEPROM.put( 0, idBuffer );
164166
}
165167

166-
void StreamCommander::loadIdFromEEPROM()
168+
void StreamCommander::loadIdFromEeprom()
167169
{
168170
char id[ID_MAX_LENGTH];
169171
EEPROM.get( 0, id );
@@ -173,22 +175,24 @@ void StreamCommander::loadIdFromEEPROM()
173175

174176
void StreamCommander::setId( String id )
175177
{
176-
// Check for Errors
178+
// Check if the ID is too long
177179
if ( id.length() > ID_MAX_LENGTH )
178180
{
179181
sendError( "ID '" + id + "' too long (ID_MAX_LENGTH = " + String( ID_MAX_LENGTH ) + ")." );
180182

181183
return;
182184
}
183185

186+
// Check whether the ID differs or not
187+
// Only proceed with the saving-process when it differs
184188
if ( id.equals( getId() ) )
185189
{
186190
sendResponse( "ID is already '" + id + "'." );
187191

188192
return;
189193
}
190194

191-
saveIdToEEPROM( id );
195+
saveIdToEeprom( id );
192196
this->id = id;
193197
sendId();
194198
}
@@ -210,7 +214,7 @@ void StreamCommander::updateStatus( String status )
210214
{
211215
setStatus( status );
212216

213-
// Only send a Status-Update if our device is set active
217+
// Only send a status update if our device is set active
214218
if ( isActive() )
215219
{
216220
sendStatus();
@@ -225,50 +229,52 @@ String StreamCommander::getStatus()
225229

226230
void StreamCommander::addCommand( String commandName, CommandCallbackFunction commandCallback )
227231
{
228-
// Check for Input-Errors
232+
// Check that the command name is not empty
229233
if ( commandName.length() == 0 )
230234
{
231-
sendError( F("Command-Name must not be empty.") );
235+
sendError( F( "Command name must not be empty." ) );
232236

233237
return;
234238
}
235239

240+
// Check that the command callback function is not empty
236241
if ( commandCallback == nullptr )
237242
{
238-
sendError( F("Command-Callbackfunction must not be empty.") );
243+
sendError( F( "Command callback function must not be empty." ) );
239244

240245
return;
241246
}
242247

243-
int currentCommandNum = getNumCommands();
244-
bool commandFound = false;
245-
CommandContainer * container = getCommandContainer( commandName );
248+
// Sets the currentCommandIndex to -1 if this commandName has not been added yet, or to the array-index where it has been found
249+
int currentCommandIndex = getCommandContainerIndex( commandName );
250+
bool commandFound = true;
246251

247-
// Check if the command has already been added
248-
if ( container != nullptr )
252+
// Check if the command has already been added or not
253+
// If not: sets the index to the next index where the new command will be added
254+
if ( currentCommandIndex < 0 )
249255
{
250-
commandFound = true;
251-
currentCommandNum = getCommandContainerNum( commandName ); // Set the currentCommandNum to the array-position where it has been found
256+
currentCommandIndex = getNumCommands();
257+
commandFound = false;
252258
}
253259

254260
// If the command has not been added yet, incease the array size and create a pointer to our commandName. Set the Callback in the next step.
255261
// If it has already been added, just replace the old callback with the new one in the next step.
256-
if ( !commandFound )
262+
if ( !commandFound )
257263
{
258-
commands = (CommandContainer*) realloc( commands, ( currentCommandNum + 1 ) * sizeof( CommandContainer ) );
264+
commands = (CommandContainer*) realloc( commands, ( currentCommandIndex + 1 ) * sizeof( CommandContainer ) );
259265
incrementNumCommands();
260266

261267
// Create a pointer to our command-name. On destruction of the corresponding CommandContainer, it will get deleted.
262268
String * commandNamePointer = new String( commandName );
263-
commands[currentCommandNum].command = commandNamePointer;
269+
commands[currentCommandIndex].command = commandNamePointer;
264270
}
265271
else
266272
{
267-
sendInfo( "Command '" + commandName + "' already found. Replacing with new command." );
273+
sendInfo( "Command '" + commandName + "' already found. Replacing with new callback function." );
268274
}
269275

270276
// Set the Callback-Function
271-
commands[currentCommandNum].callbackFunction = commandCallback;
277+
commands[currentCommandIndex].callbackFunction = commandCallback;
272278
}
273279

274280
StreamCommander::CommandContainer * StreamCommander::getCommandContainer( String command )
@@ -284,7 +290,7 @@ StreamCommander::CommandContainer * StreamCommander::getCommandContainer( String
284290
return nullptr;
285291
}
286292

287-
int StreamCommander::getCommandContainerNum( String command )
293+
int StreamCommander::getCommandContainerIndex( String command )
288294
{
289295
for ( int i = 0; i < getNumCommands(); i++ )
290296
{
@@ -328,7 +334,7 @@ String StreamCommander::getCommandList()
328334
commandList = commandList + *(commands[i].command) + commandSeparator;
329335
}
330336

331-
// Remove the last command separator
337+
// Remove the last commandSeparator occurence
332338
unsigned int listLength = commandList.length();
333339
unsigned int separatorLength = commandSeparator.length();
334340

@@ -342,10 +348,10 @@ String StreamCommander::getCommandList()
342348

343349
void StreamCommander::setDefaultCallback( DefaultCallbackFunction defaultCallbackFunction )
344350
{
345-
// Check for Input-Errors
351+
// Check that the default callback function is not empty
346352
if ( defaultCallbackFunction == nullptr )
347353
{
348-
sendError( F("Default-Callbackfunction must not be empty.") );
354+
sendError( F( "Default callback function must not be empty." ) );
349355

350356
return;
351357
}
@@ -390,18 +396,17 @@ void StreamCommander::fetchCommand()
390396
{
391397
stringEnd = nl;
392398
}
393-
else // If there's no CR/NL at all or if it's the first character, return -> no valid command found
399+
else // If there's no CR/NL at all, or if it's the first character, return -> no valid command found
394400
{
395401
return;
396402
}
397403

398404
// Parse command from buffer
399-
400405
int commandEnd = commandBuffer.indexOf( getCommandDelimiter() );
401406
String command = "";
402407
String arguments = "";
403408

404-
// If there is no command-delimiter, we can't parse any arguments
409+
// If there is no command-delimiter, we can't parse any arguments (cause there probably are none)
405410
if ( commandEnd == -1 )
406411
{
407412
// Set commandEnd to stringEnd, since our command ends there if no arguments are given
@@ -427,28 +432,23 @@ void StreamCommander::fetchCommand()
427432
}
428433
}
429434

430-
bool commandFound = false;
431-
432-
// Try to find our Input-Command and execute it
435+
// Try to find our input-command and execute it
433436
CommandContainer * container = getCommandContainer( command );
434437

435438
// If a container for this command has been found, try to call the callback
436439
if ( container != nullptr )
437440
{
438-
commandFound = true;
439-
440441
if ( container->callbackFunction != nullptr )
441442
{
442-
// Call our Callback-Function with the arguments and our object-instace
443+
// Call our Callback-Function with the arguments and our object-instance
443444
container->callbackFunction( arguments, this );
444445
}
445446
else
446447
{
447-
sendError( F("Command-Callbackfunction is empty.") );
448+
sendError( "Command callback function for command '" + command + "' is empty." );
448449
}
449450
}
450-
451-
if ( !commandFound )
451+
else
452452
{
453453
getDefaultCallback()( command, arguments, this );
454454
}
@@ -476,7 +476,7 @@ void StreamCommander::sendError( String error )
476476

477477
void StreamCommander::sendPing()
478478
{
479-
sendMessage( MessageType::PING, "reply" );
479+
sendMessage( MessageType::PING, PING_REPLY );
480480
}
481481

482482
void StreamCommander::sendStatus()
@@ -523,11 +523,11 @@ void StreamCommander::commandSetEcho( String arguments, StreamCommander * instan
523523
{
524524
arguments.trim();
525525

526-
if ( arguments.equals( F("on") ) )
526+
if ( arguments.equals( "on" ) )
527527
{
528528
instance->setEchoCommands( true );
529529
}
530-
else if ( arguments.equals( F("off") ) )
530+
else if ( arguments.equals( "off" ) )
531531
{
532532
instance->setEchoCommands( false );
533533
}

src/StreamCommander.hpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
Copyright 2019 Jan-Eric Schober
33
44
Licensed under the Apache License, Version 2.0 (the "License");
@@ -49,6 +49,7 @@ class StreamCommander
4949
static const char COMMAND_DELIMITER = ' ';
5050
static const char MESSAGE_DELIMITER = ':';
5151
static const int ID_MAX_LENGTH = 32;
52+
static const String PING_REPLY;
5253

5354
static const String COMMAND_ACTIVATE;
5455
static const String COMMAND_DEACTIVATE;
@@ -79,13 +80,13 @@ class StreamCommander
7980
Stream * getStreamInstance(); // Gets the current streamInstance of the StreamCommander.
8081
void setAddStandardCommands( bool addStandardCommands ); // Sets whether the standard commands should be added or not (true/false).
8182
bool shouldAddStandardCommands(); // Returns whether the standard commadns should be added or not.
82-
void saveIdToEEPROM( String id ); // Saves and ID to the EEPROM if it differs from the old one.
83-
void loadIdFromEEPROM(); // Loads the ID from the EEPROM.
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.
8485
void deleteCommands(); // Deletes all registered commands.
8586
void setNumCommands( int numCommands ); // Sets the number of the currently registered commands.
8687
void incrementNumCommands(); // Increments the number of the currently registered commands.
8788
CommandContainer * getCommandContainer( String command ); // Gets the container containing all commands.
88-
int getCommandContainerNum( String command ); // Returns the number (position) of a specific command in the command container.
89+
int getCommandContainerIndex( String command ); // Returns the index (position) of a specific command in the command container by name.
8990

9091
static void commandActivate( String arguments, StreamCommander * instance );// Definition of the command COMMAND_ACTIVATE.
9192
static void commandDeactivate( String arguments, StreamCommander * instance ); // Definition of the command COMMAND_DEACTIVATE.
@@ -102,7 +103,7 @@ class StreamCommander
102103
public:
103104
// Constructor
104105
StreamCommander( Stream * streamInstance = &Serial ); // Constructor, instance of a Stream object as argument.
105-
106+
106107
// Destructor
107108
~StreamCommander();
108109

@@ -141,15 +142,15 @@ class StreamCommander
141142
void fetchCommand(); // Fetches and interprets incoming commands, and invokes the corresponding callbacks. This should be called in the loop or after an interrupt/event.
142143

143144
void sendMessage( String type, String content ); // Sends a message with a specific type and content separated by our delimiter.
144-
void sendResponse( String response ); // Sends a message of type MessageType::RESPONSE
145-
void sendInfo( String info ); // Sends a message of type MessageType::INFO
146-
void sendError( String error ); // Sends a message of type MessageType::ERROR
147-
void sendPing(); // Sends a message of type MessageType::PING, contains a "reply"
148-
void sendStatus(); // Sends a message of type MessageType::STATUS, contains the current status
149-
void sendId(); // Sends a message of type MessageType::ID, contains the current ID
150-
void sendIsActive(); // Sends a message of type MessageType::ACTIVE, contains the current active status
151-
void sendEcho( String echo ); // Sends a message of type MessageType::ECHO
152-
void sendCommands(); // Sends a message of type MessageType::COMMANDS, contains a list of currently registered commands
145+
void sendResponse( String response ); // Sends a message of type MessageType::RESPONSE.
146+
void sendInfo( String info ); // Sends a message of type MessageType::INFO.
147+
void sendError( String error ); // Sends a message of type MessageType::ERROR.
148+
void sendPing(); // Sends a message of type MessageType::PING, contains a "reply".
149+
void sendStatus(); // Sends a message of type MessageType::STATUS, contains the current status.
150+
void sendId(); // Sends a message of type MessageType::ID, contains the current ID.
151+
void sendIsActive(); // Sends a message of type MessageType::ACTIVE, contains the current active status.
152+
void sendEcho( String echo ); // Sends a message of type MessageType::ECHO.
153+
void sendCommands(); // Sends a message of type MessageType::COMMANDS, contains a list of currently registered commands.
153154
};
154155

155156
#endif // STREAMCOMMANDER_HPP

0 commit comments

Comments
 (0)