Erriez Serial Terminal library for Arduino  1.1.2
This is a Serial Terminal library for Arduino.
ErriezSerialTerminal.cpp
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2018-2020 Erriez
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
33 #include "ErriezSerialTerminal.h"
34 
44 SerialTerminal::SerialTerminal(char newlineChar, char delimiterChar) :
45  _commandList(NULL),
46  _numCommands(0),
47  _newlineChar(newlineChar),
48  _lastPos(NULL),
49  _defaultHandler(NULL)
50 {
51  // strtok_r needs a null-terminated string
52  _delimiter[0] = delimiterChar;
53  _delimiter[1] = '\0';
54 
55  // Clear receive buffer
56  clearBuffer();
57 }
58 
66 void SerialTerminal::addCommand(const char *command, void (*function)())
67 {
68  // Increase size command list by one
69  _commandList = (SerialTerminalCallback *)realloc(_commandList,
70  sizeof(SerialTerminalCallback) * (_numCommands + 1));
71 
72  // Copy command and store command callback handler
73  strncpy(_commandList[_numCommands].command, command, ST_NUM_COMMAND_CHARS);
74  _commandList[_numCommands].function = function;
75 
76  // Increment number of commands
77  _numCommands++;
78 }
79 
88 void SerialTerminal::setDefaultHandler(void (*function)(const char *))
89 {
90  _defaultHandler = function;
91 }
92 
99 {
100  bool matched = false;
101  char *command;
102  char c;
103 
104  while (Serial.available() > 0) {
105  // Read one character from serial port
106  c = (char)Serial.read();
107 
108  // Check newline character \c '\\r' or \c '\\n' at the end of a command
109  if (c == _newlineChar) {
110  // Search for command at start of buffer
111  command = strtok_r(_rxBuffer, _delimiter, &_lastPos);
112 
113  if (command != NULL) {
114  for (int i = 0; i < _numCommands; i++) {
115  // Compare the found command against the list of known commands for a match
116  if (strncmp(command, _commandList[i].command, ST_NUM_COMMAND_CHARS) == 0) {
117  // Call command callback handler
118  (*_commandList[i].function)();
119  matched = true;
120  break;
121  }
122  }
123 
124  if (!matched && (_defaultHandler != NULL)) {
125  (*_defaultHandler)(command);
126  }
127  }
128  clearBuffer();
129  } else if (isprint(c)) {
130  // Store printable characters in serial receive buffer
131  if (_rxBufferIndex < ST_RX_BUFFER_SIZE) {
132  _rxBuffer[_rxBufferIndex++] = c;
133  _rxBuffer[_rxBufferIndex] = '\0';
134  }
135  }
136  }
137 }
138 
143 {
144  _rxBuffer[0] = '\0';
145  _rxBufferIndex = 0;
146 }
147 
155 {
156  return strtok_r(NULL, _delimiter, &_lastPos);
157 }
158 
166 {
167  return strtok_r(NULL, "", &_lastPos);
168 }
void setDefaultHandler(void(*function)(const char *))
Set default callback handler for unknown commands.
void clearBuffer()
Clear serial receive buffer.
char * getNext()
Get next argument.
void addCommand(const char *command, void(*function)())
Add command with callback handler.
#define ST_NUM_COMMAND_CHARS
Number of command characters.
void readSerial()
Read from serial port.
#define ST_RX_BUFFER_SIZE
Size of the serial receive buffer in bytes (Maximum length of one command plus arguments) ...
Serial terminal library for Arduino.
SerialTerminal(char newlineChar='\n', char delimiterChar=' ')
SerialTerminal constructor.
char * getRemaining()
Get all remaining characters from serial buffer.