-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialConfig.cpp
144 lines (123 loc) · 3.77 KB
/
SerialConfig.cpp
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
#include <Log.h>
#include <Config.h>
#define TIMEOUT_ENTER 50
#define ENTER 13 // byte value of the Enter key (LF)
void readLine(String& str, int timeout)
{
int index = 0;
if (timeout == 0)
timeout = 9999999;
unsigned long end = millis() + timeout;
while (true) {
if (millis() > end) {
Serial.println("\r\n TIMEOUT ! ");
return;
}
if (Serial.available()) {
char c = Serial.read();
switch (c) {
case '\n':
case '\r':
Serial.print("\r\n");
return;
break;
case '\b':
// backspace
if (str.length() > 0) {
str.remove(str.length() - 1);
Serial.print(' ');
}
break;
default:
// normal character entered. add it to the buffer
Serial.print(c);
str.concat(c);
break;
}
}
}
}
void ask(String question, String& input, int timeout)
{
Serial.print(question);
readLine(input, timeout);
}
void showFullConfig()
{
Str summary(1024);
config.printPretty(summary);
Serial.println(summary.c_str());
}
#include <src/ArduinoJson/Polyfills/isFloat.hpp>
void configMenu()
{
while (true) {
// showFullConfig();
// object.printTo(show);
Serial.println(
"\r\n__________________ Config JSON ________________________________________");
showFullConfig();
Serial.printf(
"\r\n current namespace : %s \r\n_______________________________________________________________________",
config.getNameSpace());
Serial.print("\r\n Create, Update, Delete, Save, eXit , eraseAll, Namespace [cudsxan] : ");
String line;
readLine(line, 0);
Serial.println("");
if (line.length() == 0)
return;
else if (line.equals("c") || line.equals("u")) {
String key, value;
ask("\r key : ", key, 0);
ask("\r value : ", value, 0);
if ( ArduinoJson::Polyfills::isFloat(value.c_str())) {
INFO(" value : %s ",value.c_str());
float floatValue = atof(value.c_str());
INFO(" float : %f ",floatValue);
config.set(key.c_str(),floatValue);
} else {
INFO(" no double found");
config.set(key.c_str(),value.c_str());
}
} else if (line.equals("s")) {
Serial.println("\r\n Saving.. ");
showFullConfig();
Serial.println("done.\r\n ");
} else if (line.equals("d")) {
String key;
ask("\r key : ", key, 0);
config.remove(key.c_str());
} else if (line.equals("x")) {
Serial.println("\r\n Exit config");
return;
} else if (line.equals("a")) {
config.clear();
config.load();
Serial.println("\r\n All cleared.");
} else if (line.equals("n")) {
String key;
ask("\r namespace : ", key, 0);
config.setNameSpace(key.c_str());
}
}
}
void waitConfig()
{
String line;
unsigned long end = millis() + 5000;
while (true) {
if (millis() > end) {
Serial.println(" input timed out, starting...");
break;
}
Serial.printf(" Hit 'x' + Enter for menu within 5 seconds : ");
readLine(line, 10000);
if (line.equals("x")) {
configMenu();
return;
} else {
Serial.printf(" invalid input >>%s<< , try again.\n", line.c_str());
}
line = "";
}
}