Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Serial Addressing #14

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions SerialCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions SerialCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand All @@ -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
5 changes: 5 additions & 0 deletions examples/SerialCommandExample/SerialCommandExample.pde
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -50,6 +52,9 @@ void sayHello() {
}
}

void sayGoodbye(){
Serial.println("Adios");
}

void processCommand() {
int aNumber;
Expand Down
11 changes: 6 additions & 5 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down