-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLine.h
More file actions
149 lines (125 loc) · 3.34 KB
/
Copy pathCommandLine.h
File metadata and controls
149 lines (125 loc) · 3.34 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
#ifndef __COMMANDLINE_H__
#define __COMMANDLINE_H__
#include <inttypes.h>
#include <string.h>
#include "Arduino.h"
// Number of commands to support.
#ifndef COMMANDLINE_COUNT
#define COMMANDLINE_COUNT 8
#endif
// Maximum number of characters in input buffer.
#ifndef COMMANDLINE_BUFFER
#define COMMANDLINE_BUFFER 32
#endif
// Maximum number of commands to keep in history (for up/down support).
#ifndef COMMANDLINE_HISTORY
#define COMMANDLINE_HISTORY 2
#endif
// Add support for pre and post command execution.
#define COMMANDLINE_PRE_POST
// Keycode defines.
#define KEYCODE_BACKSPACE 8
#define KEYCODE_TAB 9
#define KEYCODE_ENTER 13
#define KEYCODE_SPACE 32
#define KEYCODE_UP 65
#define KEYCODE_DOWN 66
#define KEYCODE_DELETE 127
/**
* Command definition
*/
struct Command {
char *command;
void (*callback)(char *);
/**
* Construct a new command
*
* @param command Name of the command
* @param callback Function pointer
*/
Command(char *_command, void (*_callback)(char *))
: command(_command), callback(_callback) {}
};
/**
* CommandLine instance.
*/
class CommandLine {
public:
/**
* Construct a new CommandLine instance
*
* @param serial Serial stream to wrap
* @param token Command line token to indicate new line (e.g. "> ")
*/
CommandLine(Stream &serial, char *token);
/**
* Read the serial stream and evaluate commands.
*
* @return True if a command was evaluated and executed, false otherwise.
*/
bool update(void);
/**
* Add a new command.
*
* @param Comand instance to add.
* @return True on success, false otherwise.
*/
bool add(Command &command);
/**
* Add a new command, dynamically.
*
* @param command Name of the command.
* @param callback Function pointer.
* @return True on success, false otherwise.
*/
bool add(char *command, void (*callback)(char *));
/**
* Remove a command instance
*
* @param Comand instance to remove.
* @return True on success, false otherwise.
*/
bool remove(Command &command);
#ifdef COMMANDLINE_PRE_POST
/**
* Attach pre-command execution handler. This callback is invoked
* before each non-empty input command. Set to NULL to clear.
*
* @param callback Function pointer callback. Parameter is input the
* string.
*/
void attachPre(void (*callback)(char *));
/**
* Attach post-command execution handler. This callback is invoked
* after each non-empty input command. Set to NULL to clear.
*
* @param callback Function pointer callback. First parameter is the
* input string, second parameter indicates success.
*/
void attachPost(void (*callback)(char *, bool));
#endif
private:
Stream &serial;
struct {
uint8_t index;
char buffer[COMMANDLINE_BUFFER + 1];
} input;
struct {
uint8_t index;
Command *items[COMMANDLINE_COUNT];
} commands;
#if COMMANDLINE_HISTORY > 0
struct {
uint8_t current;
uint8_t index;
char items[COMMANDLINE_HISTORY][COMMANDLINE_BUFFER + 1];
} history;
void restore();
#endif
char *token;
#ifdef COMMANDLINE_PRE_POST
void (*preCallback)(char *);
void (*postCallback)(char *, bool);
#endif
};
#endif