-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandLine.h
153 lines (133 loc) · 3.75 KB
/
CommandLine.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Command line processor for Arduino Streams
* Copyright (C) 2012 Mark Jones (markalanj@gmail.com)
*
* 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 2.1 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 Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _COMMANDLINE_H_
#define _COMMANDLINE_H_
#include <Arduino.h>
typedef void (*CommandLineCallback)(Stream&, byte, char**);
/**
* Command line processing class for Arduino Streams
*/
template <byte BufferSize = 64, byte MaxArguments = 8>
class CommandLine
{
private:
Stream* _stream;
char _buffer[BufferSize];
byte _count;
char* _argv[MaxArguments];
byte _argc;
char* _tokens;
CommandLineCallback _func;
bool _echo;
String _prompt;
void read()
{
while (_stream->available() > 0)
{
char c = _stream->read();
if (_echo)
{
_stream->write(c);
}
/* overflow check */
if (_count >= BufferSize - 1)
{
_stream->println(F("command too long"));
_count = 0;
}
else
{
/* backspace */
if (c == '\b' && _count > 0)
{
_count--;
_buffer[_count] = 0;
if (_echo)
{
_stream->print(" \b");
}
}
/* line terminator */
else if (c == '\r' || c == '\n')
{
_buffer[_count] = 0;
split();
displayPrompt();
_count = 0;
}
/* normal character */
else
{
_buffer[_count++] = c;
}
}
}
}
void split()
{
char* state;
_argc = 0;
_tokens = strtok_r(_buffer, " ", &state);
if (_tokens)
{
do
{
if (_argc >= MaxArguments)
{
_stream->println(F("too many arguments"));
return;
}
_argv[_argc++] = _tokens;
_tokens = strtok_r(NULL, " ", &state);
}
while (_tokens);
}
_func(*_stream, _argc, _argv);
}
void displayPrompt()
{
if (_prompt.length() > 0)
_stream->print(_prompt);
}
public:
CommandLine()
{
}
/**
* Resets the command line processor with a new stream and callback
* and options.
*/
void begin(Stream* stream, CommandLineCallback func,
bool echo = true, String prompt = String())
{
_stream = stream;
_echo = echo;
_func = func;
_count = 0;
_prompt = prompt;
displayPrompt();
}
/**
* Executes command processing code
* Call from your sketch's loop() function
*/
void loop()
{
read();
}
};
#endif // _CONSOLE_H_