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

Added a nullHandler function to deal with blank line input. #5

Open
wants to merge 3 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
32 changes: 24 additions & 8 deletions SerialCommand.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
/**
* SerialCommand - A Wiring/Arduino library to tokenize and parse commands
* received over a serial port.
*
*
* Copyright (C) 2012 Stefan Rado
* Copyright (C) 2011 Steven Cogswell <[email protected]>
* http://husks.wordpress.com
*
* Version 20120522
*
*
* Updated for blank line support, an alternate line terminator, and a
* non-printable character in the input buffer by DeKay, Feb 2014
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
Expand All @@ -30,7 +31,9 @@ SerialCommand::SerialCommand()
: commandList(NULL),
commandCount(0),
defaultHandler(NULL),
nullHandler(NULL),
term('\n'), // default terminator for commands, newline character
term2('\r'), // default alternate terminator for commands, carriage return character
last(NULL)
{
strcpy(delim, " "); // strtok_r needs a null-terminated string
Expand Down Expand Up @@ -64,6 +67,13 @@ void SerialCommand::setDefaultHandler(void (*function)(const char *)) {
defaultHandler = function;
}

/**
* This sets up a handler to be called in the event that the user hits enter on a
* blank line
*/
void SerialCommand::setNullHandler(void (*function)()) {
nullHandler = function;
}

/**
* This checks the Serial stream for characters, and assembles them into a buffer.
Expand All @@ -77,7 +87,7 @@ void SerialCommand::readSerial() {
Serial.print(inChar); // Echo back to serial stream
#endif

if (inChar == term) { // Check for the terminator (default '\r') meaning end of command
if ((inChar == term) || (inChar == term2)) { // Check for the terminators ('\n') or ('\r') meaning end of command
#ifdef SERIALCOMMAND_DEBUG
Serial.print("Received: ");
Serial.println(buffer);
Expand Down Expand Up @@ -111,10 +121,16 @@ void SerialCommand::readSerial() {
if (!matched && (defaultHandler != NULL)) {
(*defaultHandler)(command);
}
} else {
(*nullHandler)();
}
clearBuffer();
}
else if (isprint(inChar)) { // Only printable characters into the buffer
// Only printable characters into the buffer EXCEPT for 0x12.
// This is necessary to implement the WRD<0x12><0x4d> command used by
// WeeWx when talking to a Davis weather station. Note 0x4d is printable
// so it does not need an exception below.
else if (isprint(inChar) || (inChar == 0x12)) {
if (bufPos < SERIALCOMMAND_BUFFER) {
buffer[bufPos++] = inChar; // Put character into buffer
buffer[bufPos] = '\0'; // Null terminate
Expand Down
6 changes: 5 additions & 1 deletion SerialCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Copyright (C) 2011 Steven Cogswell <[email protected]>
* http://husks.wordpress.com
*
* Version 20120522
* Updated for blank line support & an alternate line terminator by DeKay, Feb 2014
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -47,6 +47,7 @@ class SerialCommand {
SerialCommand(); // Constructor
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 setNullHandler(void (*function)()); // A handler to call when empty command received.

void readSerial(); // Main entry point.
void clearBuffer(); // Clears the input buffer.
Expand All @@ -63,9 +64,12 @@ class SerialCommand {

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

char delim[2]; // null-terminated list of character to be used as delimeters for tokenizing (default " ")
char term; // Character that signals end of command (default '\n')
char term2; // Alternate character that signals end of command (default '\r')

char buffer[SERIALCOMMAND_BUFFER + 1]; // Buffer of stored characters while waiting for terminator character
byte bufPos; // Current position in the buffer
Expand Down
7 changes: 7 additions & 0 deletions examples/SerialCommandExample/SerialCommandExample.pde
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Demo Code for SerialCommand Library
// Steven Cogswell
// May 2011
// Updated for blank line support by DeKay, Feb 2014

#include <SerialCommand.h>

Expand All @@ -20,6 +21,7 @@ void setup() {
sCmd.addCommand("HELLO", sayHello); // Echos the string argument back
sCmd.addCommand("P", processCommand); // Converts two arguments to integers and echos them back
sCmd.setDefaultHandler(unrecognized); // Handler for command that isn't matched (says "What?")
sCmd.setNullHandler(blankLine); // Handler for a blank line (says "No Input.")
Serial.println("Ready");
}

Expand Down Expand Up @@ -81,3 +83,8 @@ void processCommand() {
void unrecognized(const char *command) {
Serial.println("What?");
}

// This gets set as the null handler, and gets called a blank line is received.
void blankLine() {
Serial.println("No input.");
}
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ SerialCommand KEYWORD1

addCommand KEYWORD2
setDefaultHandler KEYWORD2
setNullHandler KEYWORD2
readSerial KEYWORD2
clearBuffer KEYWORD2
next KEYWORD2
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
SerialCommand
=============
A Wiring/Arduino library to tokenize and parse commands received over a serial port.
A Wiring/Arduino library to tokenize and parse commands received over a serial port.

The original version of this library was written by [Steven Cogswell](http://husks.wordpress.com) (published May 23, 2011 in his blog post ["A Minimal Arduino Library for Processing Serial Commands"](http://husks.wordpress.com/2011/05/23/a-minimal-arduino-library-for-processing-serial-commands/)).

This is a heavily modified version with smaller footprint and a cleaned up code by Stefan Rado.
My changes are based on a heavily modified version with smaller footprint and cleaned up code by Stefan Rado. They add support for a return entered on an empty line, a second line terminator, and the allowance of non-printable characters in the input buffer when necessary when talking to a Davis weather station.