-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP32-SimpleCLI-teraterm.ino
130 lines (115 loc) · 3.38 KB
/
ESP32-SimpleCLI-teraterm.ino
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
/*
ESP32-SimpleCLI-teraterm.ino
This is an example code for Arduino Command Interpreter
based on SimpleCLI by spacehuhn at https://github.com/spacehuhn/SimpleCLI
When ESP32devkitC is used as an embeded device, USB serial port is
connected to Tera Term without echo back mode.
This program echo backs key input and waits for CR or LF to process CLI.
Only three commands are supported.
? (for help)
- sleep -s 3 ( wait 3 seconds)
- reboot
backspace key works.
MIT License
Copyright by coniferconifer
*/
#define SERIAL_SPEED 115200
#include <SimpleCLI.h>
SimpleCLI cli;
// Commands
Command cmdSleep;
Command cmdMAC;
Command cmdHelp;
Command cmdReboot;
#define BACKSPACE 0x08
void printHelp() {
Serial.println();
Serial.println("SimpleCLI demo for Tera Term");
Serial.println(" ? for help");
Serial.println(" sleep -s 3 ...to sleep 3sec");
Serial.println(" reboot");
}
void setup() {
Serial.begin(SERIAL_SPEED);
doInitializeCLI();
}
void doInitializeCLI() {
printHelp();
cmdSleep = cli.addCmd("sleep");// set sleep time in sec
cmdSleep.addArg("s"); // sleep -s 10
cmdReboot = cli.addCmd("reboot");//reboot
cmdHelp = cli.addCommand("?");
Serial.print("# ");
}
#define KEY_BUFFERSIZE 80
char inputBuffer[KEY_BUFFERSIZE];
int inputBufferPointer = 0;
void checkCLI() {
String input;
if (Serial.available()) {// CR=\r LF=\n teraterm's default is CR by return key
char c = Serial.read();
Serial.print(c);//echo back
if ( inputBufferPointer < KEY_BUFFERSIZE - 2 ) { //prevent buffer overflow
inputBuffer[inputBufferPointer++] = c;
}
if (c == BACKSPACE) { //Backspace key
inputBufferPointer = inputBufferPointer - 2;
if (inputBufferPointer < 0 ) inputBufferPointer = 0;//prevent buffer underflow
}
if (c == '\r' || c == '\n') {
inputBuffer[inputBufferPointer++] = 0x00;
input = inputBuffer;//char array to String
inputBufferPointer = 0;
if (input.length() > 0) {
Serial.print("# ");
Serial.println(input);
cli.parse(input);
}
}
if (cli.available()) {
Command c = cli.getCmd();
int argNum = c.countArgs();
Serial.print("> ");
Serial.print(c.getName());
Serial.print(' ');
for (int i = 0; i < argNum; ++i) {
Argument arg = c.getArgument(i);
// if(arg.isSet()) {
Serial.print(arg.toString());
Serial.print(' ');
// }
}
Serial.println();
if (c == cmdSleep) {
int sleepSecond = c.getArgument("s").getValue().toInt();
Serial.println("sleep " + String(sleepSecond) );
delay(1000 * sleepSecond);
Serial.print("# ");
} else if (c == cmdHelp) {
Serial.println("Help:");
printHelp();
Serial.print("# ");
} else if (c == cmdReboot) {
Serial.println("Rebooting now");
delay(3000);
ESP.restart();
}
}
if (cli.errored()) {
CommandError cmdError = cli.getError();
Serial.print("ERROR: ");
Serial.println(cmdError.toString());
if (cmdError.hasCommand()) {
Serial.print("Did you mean \"");
Serial.print(cmdError.getCommand().toString());
Serial.println("\"?");
}
Serial.print("# ");
}
}
}
void loop() {
// put your main code here, to run repeatedly:
// check USB serial and command line interpreter works here
checkCLI();
}