-
Notifications
You must be signed in to change notification settings - Fork 2
/
Menu.ino
282 lines (254 loc) · 7.97 KB
/
Menu.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
byte readLine(int timeout, Stream *stream);
void onESPvESPrino();
void sendNow();
bool espStopOnReady = false;
void processUserInput() {
if (Serial.available()) {
if (readLine(30000, &Serial) > 0) {
handleCommand(FROM_SERIAL);
//Serial <<"rtrttt" << endl;
}
}
//Serial <<"next" << endl;
int i=0, k=0;
//esp.listen();
if (espIsOn && esp.available()) {
//Serial <<"aloooo" << endl;
//String rcv;
espLastActivity = millis();
while (espIsOn && esp.available() && readLine(1000, &esp) > 0) {
//i++;
// k+= strlen(line);
//rcv += line;
//rcv += "|";
handleCommand(FROM_ESP);
//Serial <<" hmmm" << endl;
}
// Serial << "Received:" << i << "lines, " << k << " bytes" << endl;
//Serial << rcv << endl;
}
}
byte readLine(int timeout, Stream *stream) {
unsigned long deadline = millis() + timeout;
byte i = 0;
while (millis() < deadline) {
if (stream->available()) {
line[i++] = (char) stream->read();
//Serial << (int)line[i-1];
if (line[i-1] == '\r') i--;
else if (i == LINE_LEN - 1) break;
else if (line[i-1] == '\n') {i--; break;}
}
}
line[i] = 0;
return i;
}
void handleCommand(CommandSource cs) {
String x = String(line);
if (cs == FROM_SERIAL) {
Serial << line << endl;
//if (x.startsWith(F("wifi"))) doConnect();
if (x.startsWith(F( "lt" ))) menuEnterColorRanges(trim(&line[2]));
else if (x.startsWith(F("debug"))) switchDebugInfoPrint();
else if (x.startsWith(F("factory"))) menuMainFactoryReset();
else if (x.startsWith(F("sendNow"))) sendNow();
// else if (x.startsWith(F("tskey"))) saveLineToEE(trim(&line[5]), EE_40B_TSKEY);
// else if (x.startsWith(F("ubik"))) saveLineToEE(trim(&line[4]), EE_40B_UBIKEY);
// else if (x.startsWith(F("ubiv"))) saveLineToEE(trim(&line[4]), EE_40B_UBIVAR);
// else if (x.startsWith(F("test"))) sendToThingSpeak(567);
#ifdef BRG
else if (x.startsWith(F("brg" ))) menuEnterLedBrightness(trim(&line[3]));
#endif
else if (x.startsWith(F("brf" ))) EEPROM.put(EE_1B_BRG_FACTOR, (byte)(atoi(&line[4])));
// else if (x.startsWith(F("gray" ))) switchGrayBox();
else if (x.startsWith(F("sim" ))) simulateCO2();
//else if (x.startsWith(F("ota" ))) espOTA();
// else if (x.startsWith(F("ping" ))) espPing();
else if (x.startsWith(F("proxy"))) startSerialProxy();
else if (x.startsWith(F("ppm" ))) setPPM(trim(&line[3]));
#ifdef TGS4161
else if (x.startsWith(F("ppg" ))) setPPG(trim(&line[3]));
else if (x.startsWith(F("ppx" ))) setPPX(trim(&line[3]));
else if (x.startsWith(F("heat" ))) testHeating(true);
else if (x.startsWith(F("cool" ))) testHeating(false);
#endif
else if (x.startsWith(F("rco" ))) resetCO2();
else if (x.startsWith(F("wsi" ))) setWifiSendInterval(trim(&line[3]));
else if (x.startsWith(F("eoff" ))) espOFF();
else if (x.startsWith(F("test" ))) processSendData();
// else if (x.startsWith(F("esp" ))) onlyESP();
// else if (x.startsWith(F("sap "))) EEPROM.put(EE_1B_HASSAPCFG, (byte)(line[4]-'0'));
//else if (x.startsWith(F("ccc"))) Serial << rawReadCM1106_CO2() << endl;
else if (x.startsWith(F("esp"))) esp << &x[4] << endl;
else if (x.indexOf(F("LED##")) > -1) onLED(x);
Serial << F(">") << endl;
} else {
// Serial << F("e:") << line[0] << endl;
if (x.indexOf(F("vESPrino")) > -1) onESPvESPrino();
if (x.startsWith(F("wifi.ssid="))) espWifiConfigured = true;
if (x.indexOf(F("Device ini")) > -1) onESPDeviceInitialized();
if (x.indexOf(F("GOT IP")) > -1) espWifiConnected = true;
if (x.indexOf(F("ready")) > -1) onESPReady();
if (x.indexOf(F("LED##")) > -1) onLED(x);
if (x.startsWith(F("ppm##" ))) setPPM(&line[5]));
Serial << F("e:") << line << endl;
}
}
void onESPDeviceInitialized() {
sendNow();
if (espWasTurnedOff) {
espStopOnReady = true;
}
}
void onESPReady() {
if (espStopOnReady) {
espStopOnReady = false;
espOFF();
}
}
void onESPvESPrino() {
hasESP = true;
if (espSentInit) return;
espSentInit = true;
//Serial << "detected ESP" << endl;
String s = String(F("***prop_setr \"send.interval\",\"0\"***prop_setr \"logger.slow\",\"1\"***prop_list wifi.ssid***prop_jsetr \"serial.dump\"0***\r\n"));
if (espWasTurnedOff) {
s = String(F("***slave")) + s;
}
espSend(s.c_str());
s = "prop_jsetr \"led.ard\"1";
espSend(s.c_str());
//espSend(String(F("prop_list\r\n")).c_str());
}
//
//void makeBeep() {
//// Serial << F("BEEP\n");
//
// for (int i=0; i<1; i++) {
// tone(A4, 200, 50);
// delay(200);
// tone(A4, 150, 50);
// delay(200);
// tone(A4, 100, 50);
// delay(200);
// tone(A4, 70, 50);
// delay(200);
// }
//
//}
//
void saveLineToEE(const char *str, int address) {
while (*str) EEPROM.put(address++, (byte)*(str++));
EEPROM.put(address, 0);
}
char* trim(const char *str) {
while (*str == ' ') str++;
return (char*)str;
}
//
void setPPM(char *val) {
sPPM = String(val).toInt();
processColors();
oledCO2Level();
}
void setWifiSendInterval(char *val) {
EEPROM.put(EE_2B_WIFI_SND_INT_S, atoi(val));
}
#ifdef TGS4161
void setPPX(char *val) {
currentCO2MaxMv = getTGSEstMaxMv(atoi(val), raCO2mv.getAverage());
currentMaxMvTemp = raTempC.getAverage();
storeABC();
}
void setPPG(char *val) {
//int realPPM = atoi(val);
double mv = getCO2MaxMv() - raCO2mv.getAverage();
findAndStoreCorrectGradient(mv, 200000L);
// currentCO2MaxMv = getTGSEstMaxMv(atoi(val), raCO2mv.getAverage());
// storeCurrentCO2MaxMv();
}
void testHeating(bool turnOn) {
if (turnOn) espON();
else espOFF();
ledHeat(turnOn);
float startMv = raCO2mvNoTempCorr.getAverage();
float startTemp = raTempC.getAverage();
for (int i=0; i < 250; i++) {
char s[100];
float diffMv = startMv - raCO2mvNoTempCorr.getAverage();
float diffTemp = startTemp - raTempC.getAverage();
Serial << i << ", mvDiff: " << diffMv << ", tempDiff:" << diffTemp << ", mvNow:" << raCO2mvNoTempCorr.getAverage() << ", tempNow:" << raTempC.getAverage() << endl;
// sprintf(s, "%d, mv %d.%d, temp:%d,%2d\n", i, (int)diffMv, getFloat(diffMv), (int)diffTemp, getFloat(diffTemp));
// displayDebugInfo();
// Serial << s;
if ((i%5) == 0 && turnOn) espToggle();
processCO2();
delay(4000);
}
}
#endif
void resetCO2() {
#ifdef TGS4161
EEPROM.put(EE_FLT_CURRENT_PERIOD_CO2_HIGHESTMV, (double)0);
EEPROM.put(EE_FLT_PREV_PERIOD_CO2_HIGHESTMV, (double)0);
prevCO2MaxMv = currentCO2MaxMv = 0;
#else
Serial << F("Put the device at fresh air. Once it worked for 7 minutes after it startted it will start the calibration procedure, which is 2 minutes. ") << endl;
EEPROM.put(EE_1B_RESET_CO2, (byte)1);
softwareReset();
#endif
}
//void onlyESP() {
// pinMode(ESP_RX, INPUT);
// pinMode(ESP_TX, INPUT);
// espToggle();
//}
int menuEnterLedBrightness(const char *str) {
int res = atoi(str);
if (res < 255) res = constrain(res, 0, 150);
overrideBrightness = res;
Serial << F("New Brightness value: ") << overrideBrightness << endl;
EEPROM.write(EE_1B_BRG, overrideBrightness);
//menuHandler = menuPrintMain;
return 0;
}
void menuEnterColorRanges(char *s) {
storeColorRanges(s);
processNeopixels();
Serial << F("Done\n");
}
int menuMainFactoryReset() {
clearEEPROM();
Serial << F("\nConfiguration reset...\n");
softwareReset();
return 0;
}
#ifdef GRAY
int switchGrayBox() {
byte val = EEPROM.read(EE_1B_ISGRAY);
if (val == 255) val = 1;
else val = 255;
EEPROM.update(EE_1B_ISGRAY, val);
Serial << F("\nBox:");
if (val == 1) Serial << F("G\n");
else Serial << F("W\n");
//Serial << endl;
return 0;
}
#endif
int switchDebugInfoPrint() {
//DEBUG=true;
dumpDebuggingInfo = !dumpDebuggingInfo;
Serial << endl << F("Debug Info is: ") << dumpDebuggingInfo << endl;
if (dumpDebuggingInfo) displayDebugInfo();
return 0;
}
//
int simulateCO2() {
for (int i = 400; i <=3000; i+=100) {
sPPM = i;
processNeopixels();
oledCO2Level();
delay(250);
}
}