diff --git a/SerialCommand.cpp b/SerialCommand.cpp index bbea5ba..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. @@ -70,9 +87,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..a373742 100644 --- a/SerialCommand.h +++ b/SerialCommand.h @@ -46,9 +46,10 @@ 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 readSerial(); // Main entry point. + 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 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; diff --git a/keywords.txt b/keywords.txt index 50b7076..8ef8ec0 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)