-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLine.cpp
More file actions
242 lines (201 loc) · 7.31 KB
/
Copy pathCommandLine.cpp
File metadata and controls
242 lines (201 loc) · 7.31 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "CommandLine.h"
CommandLine::CommandLine(Stream &_serial, char *_token)
: serial(_serial), token(_token) {
input.index = 0;
commands.index = 0;
#if COMMANDLINE_HISTORY
history.index = 0;
#endif
}
bool CommandLine::update() {
bool success = false;
if (this->serial.available()) {
char input = this->serial.read();
switch (input) {
case KEYCODE_ENTER:
// Write newline
this->serial.println("");
// Parse command
if (this->input.index > 0) {
// Add to history if it is not the same as the previous item added.
#if COMMANDLINE_HISTORY > 0
if (this->history.index == 0 ||
strncmp(this->history.items[0], this->input.buffer,
COMMANDLINE_BUFFER) != 0) {
if (this->history.index < COMMANDLINE_HISTORY) {
this->history.index++;
}
// Dequeue first item added by shifting each item one index
// up.
for (int i = this->history.index - 1; i > 0; i--) {
strncpy(this->history.items[i],
this->history.items[i - 1], COMMANDLINE_BUFFER);
}
// Current buffer is inserted at position zero.
strncpy(this->history.items[0], this->input.buffer,
COMMANDLINE_BUFFER);
}
#endif
// Split command name
char *split = strtok(this->input.buffer, " ");
uint8_t length = strnlen(split, COMMANDLINE_BUFFER);
// Handle pre command callback.
#ifdef COMMANDLINE_PRE_POST
if (this->preCallback != NULL) {
this->preCallback(this->input.buffer);
}
#endif
// Find the first matching command and invoke callback.
for (int i = 0; i < this->commands.index; i++) {
if (strncmp(split, this->commands.items[i]->command,
length) == 0) {
if (strlen(this->commands.items[i]->command) ==
length) {
this->commands.items[i]->callback(
&this->input.buffer[this->input.index]);
success = true;
break;
}
}
}
// Handle post command callback.
#ifdef COMMANDLINE_PRE_POST
if (this->postCallback != NULL) {
this->postCallback(this->input.buffer, success);
}
#endif
}
// Reset the byte buffer index.
this->input.index = 0;
// Point to last history item added. By setting it to the history length, the
// up/down
// handler will treat the first keypress as a special one.
#if COMMANDLINE_HISTORY > 0
this->history.current = this->history.index;
#endif
// Write a new input token.
this->serial.print(this->token);
break;
case KEYCODE_BACKSPACE:
case KEYCODE_DELETE:
if (this->input.index > 0) {
// Reduce byte buffer index.
this->input.index--;
// Clear last char on screen.
this->serial.write(KEYCODE_BACKSPACE);
this->serial.write(KEYCODE_SPACE);
this->serial.write(KEYCODE_BACKSPACE);
}
break;
#if COMMANDLINE_HISTORY > 0
case KEYCODE_UP:
if (this->history.index > 0) {
// Decide on item to show. If current is equal to the index,
// show the
// newest item first.
if (this->history.current == this->history.index) {
this->history.current = 0;
} else {
this->history.current =
(this->history.current + 1) % this->history.index;
}
// Restore from history.
this->restore();
}
break;
case KEYCODE_DOWN:
if (this->history.index > 0) {
// Decide on item to show. If current is equal to the index,
// show the
// oldest item first.
if (this->history.current == this->history.index) {
this->history.current = this->history.index - 1;
} else {
this->history.current =
(this->history.current == 0 ? this->history.index
: this->history.current) -
1;
}
// Restore from history.
this->restore();
}
break;
#endif
default:
if (input > 31 && input < 127) {
if (this->input.index < COMMANDLINE_BUFFER) {
// Store input, append NULL char after input for safety
// reasons.
this->input.buffer[this->input.index] = input;
this->input.buffer[this->input.index + 1] = '\0';
// Move pointer
this->input.index++;
// Repeat char
this->serial.write(input);
}
}
break;
}
}
// Done
return success;
}
bool CommandLine::add(Command &command) {
// Check if command was already added.
for (int i = 0; i < COMMANDLINE_COUNT; i++) {
if (this->commands.items[i] == &command) {
// Command was already added
return true;
}
}
// Add it to the list.
if (this->commands.index < COMMANDLINE_COUNT) {
this->commands.items[this->commands.index] = &command;
this->commands.index++;
// Command added
return true;
}
// No space left
return false;
}
bool CommandLine::add(char *command, void (*callback)(char *)) {
Command *cmd = (Command *)malloc(sizeof(Command));
cmd->command = command;
cmd->callback = callback;
this->add(*cmd);
}
bool CommandLine::remove(Command &command) {
for (int i = 0; i < this->commands.index; i++) {
if (this->commands.items[i] == &command) {
// Move other commands one index to the left
for (int j = i + 1; j < this->commands.index; j++) {
this->commands.items[j - 1] = this->commands.items[j];
}
this->commands.index--;
// Done
return true;
}
}
// Command not found
return false;
}
#if COMMANDLINE_HISTORY > 0
void CommandLine::restore() {
// Copy history to buffer
strncpy(this->input.buffer, this->history.items[this->history.current],
COMMANDLINE_BUFFER);
this->input.index = strnlen(this->input.buffer, COMMANDLINE_BUFFER);
// Show new line
this->serial.write("\33[2K\r");
this->serial.print(this->token);
this->serial.print(this->input.buffer);
}
#endif
#ifdef COMMANDLINE_PRE_POST
void CommandLine::attachPre(void (*callback)(char *)) {
this->preCallback = callback;
}
void CommandLine::attachPost(void (*callback)(char *, bool)) {
this->postCallback = callback;
}
#endif