Skip to content
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
50 changes: 31 additions & 19 deletions SerialCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ SerialCommand::SerialCommand(int maxCommands)
defaultHandler(NULL),
term('\n'), // default terminator for commands, newline character
last(NULL),
maxCommands(maxCommands)
maxCommands(maxCommands),
target(&Serial)
{
strcpy(delim, " "); // strtok_r needs a null-terminated string
clearBuffer();
Expand All @@ -46,15 +47,15 @@ SerialCommand::SerialCommand(int maxCommands)
*/
void SerialCommand::addCommand(const char *command, void (*function)()) {
#ifdef SERIALCOMMAND_DEBUG
Serial.print("Adding command (");
Serial.print(commandCount);
Serial.print("): ");
Serial.println(command);
target->print(F("Adding command ("));
target->print(commandCount);
target->print(F("): "));
target->println(command);
#endif

if (commandCount >= maxCommands){
#ifdef SERIALCOMMAND_DEBUG
Serial.print("Error: maxCommands was exceeded");
target->print(F("Error: maxCommands was exceeded"));
#endif
return;
}
Expand All @@ -78,35 +79,35 @@ void SerialCommand::setDefaultHandler(void (*function)(const char *)) {
* 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
while (target->available() > 0) {
char inChar = target->read(); // Read single available character, there may be more waiting
#ifdef SERIALCOMMAND_DEBUG
Serial.print(inChar); // Echo back to serial stream
target->print(inChar); // Echo back to serial stream
#endif

if (inChar == term) { // Check for the terminator (default '\r') meaning end of command
#ifdef SERIALCOMMAND_DEBUG
Serial.print("Received: ");
Serial.println(buffer);
target->print(F("Received: "));
target->println(buffer);
#endif

char *command = strtok_r(buffer, delim, &last); // Search for command at start of buffer
if (command != NULL) {
boolean matched = false;
for (int i = 0; i < commandCount; i++) {
#ifdef SERIALCOMMAND_DEBUG
Serial.print("Comparing [");
Serial.print(command);
Serial.print("] to [");
Serial.print(commandList[i].command);
Serial.println("]");
target->print(F("Comparing ["));
target->print(command);
target->print(F("] to ["));
target->print(commandList[i].command);
target->println(F("]"));
#endif

// Compare the found command against the list of known commands for a match
if (strncmp(command, commandList[i].command, SERIALCOMMAND_MAXCOMMANDLENGTH) == 0) {
#ifdef SERIALCOMMAND_DEBUG
Serial.print("Matched Command: ");
Serial.println(command);
target->print(F("Matched Command: "));
target->println(command);
#endif

// Execute the stored handler function for the command
Expand All @@ -127,7 +128,7 @@ void SerialCommand::readSerial() {
buffer[bufPos] = '\0'; // Null terminate
} else {
#ifdef SERIALCOMMAND_DEBUG
Serial.println("Line buffer is full - increase SERIALCOMMAND_BUFFER");
target->println(F("Line buffer is full - increase SERIALCOMMAND_BUFFER"));
#endif
}
}
Expand All @@ -149,3 +150,14 @@ void SerialCommand::clearBuffer() {
char *SerialCommand::next() {
return strtok_r(NULL, delim, &last);
}

/**
* Sets the target to send output to and read input from
*/
void SerialCommand::setTarget(Stream* newTarget) {
this->target = newTarget;
}

Stream* SerialCommand::getTarget() {
return this->target;
}
5 changes: 5 additions & 0 deletions SerialCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class SerialCommand {
void clearBuffer(); // Clears the input buffer.
char *next(); // Returns pointer to next token found in command buffer (for getting arguments to commands).

void setTarget(Stream* target);
Stream* getTarget();

private:
// Command/handler dictionary
struct SerialCommandCallback {
Expand All @@ -63,6 +66,8 @@ class SerialCommand {
SerialCommandCallback *commandList; // Actual definition for command/handler array
byte commandCount;

Stream* target;

// Pointer to the default handler function
void (*defaultHandler)(const char *);

Expand Down