From 11c03d89cbcfc6e2048f52a6e386677a4cb07dd8 Mon Sep 17 00:00:00 2001 From: hicat Date: Sun, 4 Dec 2016 12:06:34 +0800 Subject: [PATCH 1/5] able to use different stream --- SerialCommand.cpp | 6 +++--- SerialCommand.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/SerialCommand.cpp b/SerialCommand.cpp index bbea5ba..74e0527 100644 --- a/SerialCommand.cpp +++ b/SerialCommand.cpp @@ -70,9 +70,9 @@ void SerialCommand::setDefaultHandler(void (*function)(const char *)) { * When the terminator character (default '\n') is seen, it starts parsing the * buffer for a prefix command, and calls handlers setup by addCommand() member */ -void SerialCommand::readSerial() { - while (Serial.available() > 0) { - char inChar = Serial.read(); // Read single available character, there may be more waiting +void SerialCommand::readSerial(Stream &stream) { + while (stream.available() > 0) { + char inChar = stream.read(); // Read single available character, there may be more waiting #ifdef SERIALCOMMAND_DEBUG Serial.print(inChar); // Echo back to serial stream #endif diff --git a/SerialCommand.h b/SerialCommand.h index e00dd29..b106146 100644 --- a/SerialCommand.h +++ b/SerialCommand.h @@ -48,7 +48,7 @@ class SerialCommand { void addCommand(const char *command, void(*function)()); // Add a command to the processing dictionary. void setDefaultHandler(void (*function)(const char *)); // A handler to call when no valid command received. - void readSerial(); // Main entry point. + void readSerial(Stream &stream); // Main entry point. void clearBuffer(); // Clears the input buffer. char *next(); // Returns pointer to next token found in command buffer (for getting arguments to commands). From 5c9c29cfaeb42b9eb050820a576b66c7bfae4500 Mon Sep 17 00:00:00 2001 From: Michael Moskie Date: Wed, 25 Jan 2017 18:17:16 -0800 Subject: [PATCH 2/5] Add Serial as default stream for readSerial() to support original users of the library. --- SerialCommand.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SerialCommand.h b/SerialCommand.h index b106146..7714945 100644 --- a/SerialCommand.h +++ b/SerialCommand.h @@ -48,7 +48,7 @@ class SerialCommand { void addCommand(const char *command, void(*function)()); // Add a command to the processing dictionary. void setDefaultHandler(void (*function)(const char *)); // A handler to call when no valid command received. - void readSerial(Stream &stream); // Main entry point. + void readSerial(Stream &stream = Serial); // Main entry point. void clearBuffer(); // Clears the input buffer. char *next(); // Returns pointer to next token found in command buffer (for getting arguments to commands). From 46444dbfaf965b1f871920cd6dd678d0e12d5035 Mon Sep 17 00:00:00 2001 From: Michael Moskie Date: Sat, 28 Jan 2017 22:03:11 -0800 Subject: [PATCH 3/5] Serial Addressing These changes allow the developer to easily add an address to the commands that are added with the addCommand() function. This is particularly useful in systems using RS-485 serial with multiple devices. --- SerialCommand.cpp | 17 +++++++++++++++++ SerialCommand.h | 4 +++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/SerialCommand.cpp b/SerialCommand.cpp index 74e0527..bd830c2 100644 --- a/SerialCommand.cpp +++ b/SerialCommand.cpp @@ -56,6 +56,23 @@ void SerialCommand::addCommand(const char *command, void (*function)()) { commandCount++; } +/** + * Similar to the addCommand() function above, this appends char address to the beginning of the command. + * This is intended to assist in RS-485 situations where addressing is required. + */ +void SerialCommand::addCommandWithAddr(const char *command, void (*function)()){ + String tmp = String(address)+String(command); + char charBuf[50]; + tmp.toCharArray(charBuf,50); + commandList = (SerialCommandCallback *) realloc(commandList, (commandCount + 1) * sizeof(SerialCommandCallback)); + strncpy(commandList[commandCount].command, charBuf, SERIALCOMMAND_MAXCOMMANDLENGTH); + commandList[commandCount].function = function; + commandCount++; +} + +void SerialCommand::setAddress(char addin){ + address = addin; +} /** * This sets up a handler to be called in the event that the receveived command string * isn't in the list of commands. diff --git a/SerialCommand.h b/SerialCommand.h index 7714945..a373742 100644 --- a/SerialCommand.h +++ b/SerialCommand.h @@ -46,8 +46,9 @@ class SerialCommand { public: SerialCommand(); // Constructor void addCommand(const char *command, void(*function)()); // Add a command to the processing dictionary. + void addCommandWithAddr(const char *command, void(*function)());//Add a command to the processing dictionary with address. void setDefaultHandler(void (*function)(const char *)); // A handler to call when no valid command received. - + void setAddress(char addin); void readSerial(Stream &stream = Serial); // Main entry point. void clearBuffer(); // Clears the input buffer. char *next(); // Returns pointer to next token found in command buffer (for getting arguments to commands). @@ -70,6 +71,7 @@ class SerialCommand { char buffer[SERIALCOMMAND_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 address; }; #endif //SerialCommand_h From 32491c840479187b6cc1ab93e78c672cedfa869d Mon Sep 17 00:00:00 2001 From: Michael Moskie Date: Sat, 28 Jan 2017 22:10:55 -0800 Subject: [PATCH 4/5] Address Example Added demonstration of setAddress() and addCommandWithAddr() --- examples/SerialCommandExample/SerialCommandExample.pde | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/examples/SerialCommandExample/SerialCommandExample.pde b/examples/SerialCommandExample/SerialCommandExample.pde index 8d0bf4a..1373dcb 100644 --- a/examples/SerialCommandExample/SerialCommandExample.pde +++ b/examples/SerialCommandExample/SerialCommandExample.pde @@ -19,6 +19,8 @@ void setup() { sCmd.addCommand("OFF", LED_off); // Turns LED off sCmd.addCommand("HELLO", sayHello); // Echos the string argument back sCmd.addCommand("P", processCommand); // Converts two arguments to integers and echos them back + sCmd.setAddress('A'); // Sets the address for any calls to addCommandWithAddr() + sCmd.addCommandWithAddr("GOODBYE", sayGoodbye); //Adds the command "AGOODBYE" to the list of available commands sCmd.setDefaultHandler(unrecognized); // Handler for command that isn't matched (says "What?") Serial.println("Ready"); } @@ -50,6 +52,9 @@ void sayHello() { } } +void sayGoodbye(){ + Serial.println("Adios"); +} void processCommand() { int aNumber; From 25ab28fc9eb81281dfb37c393db0ae446979d45b Mon Sep 17 00:00:00 2001 From: Michael Moskie Date: Sun, 29 Jan 2017 20:12:34 -0800 Subject: [PATCH 5/5] Keywords Updated keywords to include addCommandWithAddr --- keywords.txt | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/keywords.txt b/keywords.txt index 45ada90..45eaf48 100644 --- a/keywords.txt +++ b/keywords.txt @@ -8,11 +8,12 @@ SerialCommand KEYWORD1 # Methods and Functions (KEYWORD2) ####################################### -addCommand KEYWORD2 -setDefaultHandler KEYWORD2 -readSerial KEYWORD2 -clearBuffer KEYWORD2 -next KEYWORD2 +addCommand KEYWORD2 +addCommandWithAddr KEYWORD2 +setDefaultHandler KEYWORD2 +readSerial KEYWORD2 +clearBuffer KEYWORD2 +next KEYWORD2 ####################################### # Instances (KEYWORD2)