Erriez Serial Terminal library for Arduino  1.1.2
This is a Serial Terminal library for Arduino.
ErriezSerialTerminal.h
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 #ifndef ERRIEZ_SERIAL_TERMINAL_H_
34 #define ERRIEZ_SERIAL_TERMINAL_H_
35 
36 #include <Arduino.h>
37 #include <string.h>
38 
42 #define ST_RX_BUFFER_SIZE 32
43 
47 #define ST_NUM_COMMAND_CHARS 8
48 
53 {
54 public:
55  explicit SerialTerminal(char newlineChar='\n', char delimiterChar=' ');
56 
57  void addCommand(const char *command, void(*function)());
58  void setDefaultHandler(void (*function)(const char *));
59 
60  void readSerial();
61  void clearBuffer();
62 
63  char *getNext();
64  char *getRemaining();
65 
66 private:
67  struct SerialTerminalCallback {
68  char command[ST_NUM_COMMAND_CHARS + 1];
69  void (*function)();
70  };
71 
72  SerialTerminalCallback *_commandList;
73  byte _numCommands;
74  char _newlineChar;
75  char _delimiter[2];
76  char _rxBuffer[ST_RX_BUFFER_SIZE + 1];
77  byte _rxBufferIndex;
78  char *_lastPos;
79 
80  void (*_defaultHandler)(const char *);
81 };
82 
83 #endif // ERRIEZ_SERIAL_TERMINAL_H_
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.
SerialTerminal class.
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) ...
SerialTerminal(char newlineChar='\n', char delimiterChar=' ')
SerialTerminal constructor.
char * getRemaining()
Get all remaining characters from serial buffer.