-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
487 lines (377 loc) · 12.5 KB
/
Main.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/**
* CrowOs Main implementation
* @author error23
*/
#include "Main.hpp"
using namespace CrowOs::Core;
/** Home button */
OneButton homeButton(BUTTON_A_PIN, true);
/** Up button */
OneButton upButton(BUTTON_B_PIN, true);
/** Time helper */
Time timeHelper(120, 30);
/** Led helper */
Led ledHelper;
/** Screen helper */
Screen screenHelper;
/** SmartWifi helper */
SmartWifi smartWifi;
/** Web client helper */
WebClient webClient(BACKEND_HOST, BACKEND_PORT, BACKEND_USER_USERNAME, BACKEND_USER_PASSWORD, BACKEND_BASE_PATH);
/** List of permanent features **/
std::vector<Feature*> permanentFeatures;
/** Current feature */
Feature* currentFeature = NULL;
/** Current feature index */
int currentFeatureIndex = -1;
/** New feature index */
int newFeatureIndex = 0;
/** Indicates if device is sleeping */
bool sleeping = false;
/**
* Main setUp method
*/
void setup() {
M5.begin();
Serial.begin(115200);
M5.Axp.begin();
M5.Lcd.setSwapBytes(true);
M5.MPU6886.Init();
screenHelper.setUp();
screenHelper.showLogo();
timeHelper.setUp();
ledHelper.setUp();
setUpButtons();
smartWifi.setUp();
initialiseFeatureFactories();
initialiseFeatureData();
setUpPermanentFeatures();
if(LOG_INFO) Serial.println("Info : [Main] Setup Done");
}
/**
* Main loop method
*/
void loop() {
timeHelper.limitFps();
tickButtons();
if(timeHelper.shouldSleep() || sleeping) {
sleep();
}
else {
// Checks wifi status and try to reconnect it if is not connected
if(!smartWifi.checkStatus()) smartWifi.reconnect();
// loop permanent features
for(auto& feature : permanentFeatures) {
feature->loop();
}
// if there is any features implemented
if(FeatureFactory::featureFactories.size() != 0) {
// switch normal feature if index has changed
if(newFeatureIndex != currentFeatureIndex) {
if(currentFeature != NULL) killCurrentFeature();
currentFeature = startFeature(newFeatureIndex);
currentFeatureIndex = newFeatureIndex;
}
// loop current feature if exists
if(currentFeature != NULL) currentFeature->loop();
}
}
screenHelper.loop();
}
/**
* Main shutdown method
*/
void shutdown() {
if(LOG_INFO) Serial.println("Info : [Main] shutdown ...");
screenHelper.showLogo();
killCurrentFeature();
saveFeatureDataToServer(true);
shutdownPermanentFeatures();
shutdownFeatureFactories();
smartWifi.disconnect();
if(LOG_INFO) Serial.println("Info : [Main] shutdown Done");
delay(100);
M5.Axp.PowerOff();
}
/**
* Put device in sleep mode
*/
void sleep() {
if(LOG_INFO) Serial.println("Info : [Main] sleep");
if(sleeping) {
delay(500);
if(LOG_DEBUG) Serial.println("Debug : [Main] sleep delay = 500 ms");
return;
}
sleeping = true;
if(LOG_DEBUG) Serial.println("Debug : [Main] sleep sleeping = true");
saveFeatureDataToServer(false);
if(currentFeature != NULL) killCurrentFeature();
M5.Axp.SetSleep();
smartWifi.disconnect();
}
/**
* Wakes up device
*/
void wakeUp() {
if(!sleeping) return;
if(LOG_INFO) Serial.println("Info : [Main] wakeUp");
timeHelper.keepWokedUp();
M5.Axp.begin();
M5.Lcd.setSwapBytes(true);
screenHelper.setUp();
smartWifi.connect();
if(currentFeatureIndex != -1) currentFeature = startFeature(currentFeatureIndex);
sleeping = false;
}
/**
* Initialise feature saved data from the server
*/
void initialiseFeatureData() {
if(LOG_INFO) Serial.println("Info : [Main] initialiseFeatureData ...");
if(!smartWifi.waitUntilReconnect()) {
screenHelper.showError("Init failed !!", 10000);
return;
}
DynamicJsonDocument responseBody(MAX_JSON_DOCUMENT_SIZE * (FeatureFactory::featureFactories.size() + 1));
int status = webClient.sendGET("featureData", responseBody);
if(status != 200) {
char err[15];
sprintf(err, "server er:%d", status);
screenHelper.showError(err, 10000);
}
JsonArray featureDataDtos = responseBody.as<JsonArray>();
for(DynamicJsonDocument featureDataDto : featureDataDtos) {
for(auto& featureFactorySavedDataPair : FeatureFactory::featureFactories) {
if(featureFactorySavedDataPair.first->getFeatureFactoryName() == featureDataDto["featureFactoryName"]) {
featureFactorySavedDataPair.first->setFeatureFactoryId(featureDataDto["id"]);
String s = "\0";
if(!featureDataDto["savedData"].isNull()) {
serializeJson(featureDataDto["savedData"], s);
}
if(LOG_DEBUG) Serial.printf("Debug : [Main] initialiseFeatureData save second = %s for featureFactoryName = %s\n", s.c_str(), featureFactorySavedDataPair.first->getFeatureFactoryName());
featureFactorySavedDataPair.second = s;
break;
}
}
}
if(LOG_INFO) Serial.println("Info : [Main] initialiseFeatureData Done");
}
/**
* Sends feature saved data to the server
*
* @param waitForConnection if true method will call SmartWifi::waitUntilReconnect() method in the begining
*/
void saveFeatureDataToServer(boolean waitForConnection) {
if(LOG_INFO) Serial.println("Info : [Main] saveFeatureDataToServer ...");
if(waitForConnection && !smartWifi.waitUntilReconnect()) {
screenHelper.showError("Init failed !!", 10000);
return;
}
DynamicJsonDocument payload(MAX_JSON_DOCUMENT_SIZE * (FeatureFactory::featureFactories.size() + 1));
JsonArray featureDataDtos = payload.to<JsonArray>();
for(auto& featureFactorySavedDataPair : FeatureFactory::featureFactories) {
DynamicJsonDocument featureDataDto(MAX_JSON_DOCUMENT_SIZE);
if(featureFactorySavedDataPair.first->getFeatureFactoryId() != -1) featureDataDto["id"] = featureFactorySavedDataPair.first->getFeatureFactoryId();
featureDataDto["featureFactoryName"] = featureFactorySavedDataPair.first->getFeatureFactoryName();
DynamicJsonDocument savedData(MAX_JSON_DOCUMENT_SIZE);
deserializeJson(savedData, featureFactorySavedDataPair.second);
featureDataDto["savedData"] = savedData;
featureDataDtos.add(featureDataDto);
}
DynamicJsonDocument responseBody(MAX_JSON_DOCUMENT_SIZE * (FeatureFactory::featureFactories.size() + 1));
int status = webClient.sendPUT("featureData", payload, responseBody);
if(status != 202) {
char err[15];
sprintf(err, "server er:%d", status);
screenHelper.showError(err, 10000);
screenHelper.loop();
}
if(LOG_INFO) Serial.println("Info : [Main] saveFeatureDataToServer Done");
}
/**
* Sets up alwaysLoop = true features
*/
void setUpPermanentFeatures() {
if(LOG_INFO) Serial.println("Info : [Main] setUpPermanentFeatures ...");
for(int i = 0; i < FeatureFactory::featureFactories.size(); i++) {
if(FeatureFactory::featureFactories[i].first->isAlwaysLoop()) {
Feature* permanentFeature = FeatureFactory::featureFactories[i].first->createFeature();
delete FeatureFactory::featureFactories[i].first;
i--;
permanentFeature->onStart(&screenHelper, &timeHelper, &ledHelper, NULL);
permanentFeatures.push_back(permanentFeature);
if(LOG_DEBUG) Serial.printf("Debug : [Main] setUpPermanentFeatures %s added to permanentFeatures actualSize = %d\n", permanentFeature->getFeatureName(), permanentFeatures.size());
}
}
if(LOG_INFO) Serial.println("Info : [Main] setUpPermanentFeatures Done");
}
/**
* Deletes all alwaysLoop = true fatures
*/
void shutdownPermanentFeatures() {
if(LOG_INFO) Serial.println("Info : [Main] shutdownPermanentFeatures ...");
for(auto& permanentFeature : permanentFeatures) {
permanentFeature->onStop(NULL);
delete permanentFeature;
}
if(LOG_INFO) Serial.println("Info : [Main] shutdownPermanentFeatures Done");
}
/**
* Deletes all feature factories
*/
void shutdownFeatureFactories() {
if(LOG_INFO) Serial.println("Info : [Main] shutdownFeatureFactories ...");
for(int i = 0; i < FeatureFactory::featureFactories.size(); i++) {
if(FeatureFactory::featureFactories[i].first != NULL) {
delete FeatureFactory::featureFactories[i].first;
i--;
}
}
if(LOG_INFO) Serial.println("Info : [Main] shutdownFeatureFactories Done");
}
/**
* Increments feature index until featureFactories.size() than reinitialise it at 0
*/
void nextFeature() {
if(LOG_DEBUG) {
Serial.printf("Debug : [Main] nextFeature currentFeatureIndex = %d\n", currentFeatureIndex);
Serial.printf("Debug : [Main] nextFeature newFeatureIndex = %d\n", newFeatureIndex);
Serial.printf("Debug : [Main] nextFeature featureFactories.size() = %d\n", FeatureFactory::featureFactories.size());
}
newFeatureIndex++;
if(newFeatureIndex >= FeatureFactory::featureFactories.size()) {
newFeatureIndex = 0;
}
if(LOG_DEBUG) {
Serial.printf("Debug : [Main] nextFeature newFeatureIndex = %d\n", newFeatureIndex);
Serial.printf("Debug : [Main] nextFeature featureFactories.size() = %d\n", FeatureFactory::featureFactories.size());
}
}
/**
* Starts new feature from featureFactories
*
* @param featureIndex featureFactories index of feature to start
* @return started feature
*/
Feature* startFeature(const int featureIndex) {
// start new feature
Feature* feature = FeatureFactory::featureFactories[featureIndex].first->createFeature();
// if there is no saved data start feature with null
if(FeatureFactory::featureFactories[featureIndex].second == NULL) {
if(LOG_DEBUG) Serial.printf("Debug : [Main] startFeature name = %s new second = NULL\n", feature->getFeatureName());
feature->onStart(&screenHelper, &timeHelper, &ledHelper, NULL);
}
// Else deserialise json document and start feature with it
else {
DynamicJsonDocument json(MAX_JSON_DOCUMENT_SIZE);
deserializeJson(json, FeatureFactory::featureFactories[featureIndex].second);
if(LOG_DEBUG) Serial.printf("Debug : [Main] startFeature name = %s retrieve new second = %s\n", feature->getFeatureName(), FeatureFactory::featureFactories[featureIndex].second.c_str());
feature->onStart(&screenHelper, &timeHelper, &ledHelper, &json);
}
return feature;
}
/**
* Kill current feature
*/
void killCurrentFeature() {
// Deserialise saved data into json document
DynamicJsonDocument json(MAX_JSON_DOCUMENT_SIZE);
deserializeJson(json, FeatureFactory::featureFactories[currentFeatureIndex].second);
if(LOG_DEBUG) Serial.printf("Debug : [Main] killCurrentFeature retrieve second = %s\n", FeatureFactory::featureFactories[currentFeatureIndex].second.c_str());
// Stop feature
currentFeature->onStop(&json);
// Serialize json document into saved data
String s;
serializeJson(json, s);
FeatureFactory::featureFactories[currentFeatureIndex].second = s;
if(LOG_DEBUG) Serial.printf("Debug : [Main] killCurrentFeature save second = %s\n", FeatureFactory::featureFactories[currentFeatureIndex].second.c_str());
// kill current feature
delete currentFeature;
}
/**
* Sets up all buttons
*/
void setUpButtons() {
if(LOG_INFO) Serial.println("Info : [Main] setUpButtons ...");
homeButton.attachClick(onHomeButtonClick);
homeButton.attachDoubleClick(onHomeButtonDoubleClick);
homeButton.setDebounceTicks(DEBOUNCE_TICKS);
upButton.attachClick(onUpButtonClick);
upButton.attachDoubleClick(onUpButtonDoubleClick);
upButton.setDebounceTicks(DEBOUNCE_TICKS);
if(LOG_DEBUG) Serial.printf("Debug : [Main] setUpButtons DEBOUNCE_TICKS : %d\n", DEBOUNCE_TICKS);
if(LOG_INFO) Serial.println("Info : [Main] setUpButtons Done");
}
/**
* Tick all buttons
*/
void tickButtons() {
if(LOG_DEBUG) Serial.println("Debug : [Main] tickButtons");
homeButton.tick();
upButton.tick();
uint8_t event = M5.Axp.GetBtnPress();
if(event == 0x01) {
onPowerButtonLongClick();
}
else if(event == 0x02) {
onPowerButtonClick();
}
}
/**
* Called on home button click
*/
void onHomeButtonClick() {
if(LOG_DEBUG) Serial.println("Debug : [Main] onHomeClick general");
if(sleeping) return;
timeHelper.keepWokedUp();
if(currentFeature != NULL) currentFeature->onHomeClick();
}
/**
* Called on home button double click
*/
void onHomeButtonDoubleClick() {
if(LOG_DEBUG) Serial.println("Debug : [Main] onHomeDoubleClick general");
if(sleeping) return;
timeHelper.keepWokedUp();
if(currentFeature != NULL) currentFeature->onHomeDoubleClick();
}
/**
* Called on up button click
*/
void onUpButtonClick() {
if(LOG_DEBUG) Serial.println("Debug : [Main] onUpButtonClick general");
if(sleeping) return;
timeHelper.keepWokedUp();
nextFeature();
}
/**
* Called on up button double click
*/
void onUpButtonDoubleClick() {
if(LOG_DEBUG) Serial.println("Debug : [Main] onUpButtonDoubleClick general");
if(sleeping) return;
timeHelper.keepWokedUp();
screenHelper.changeBrightness();
}
/**
* Called on power button click
*/
void onPowerButtonClick() {
if(LOG_DEBUG) Serial.println("Debug : [Main] onPowerButtonClick general");
if(sleeping) {
wakeUp();
}
else {
sleep();
}
}
/**
* Called on power button long click
*/
void onPowerButtonLongClick() {
if(LOG_DEBUG) Serial.println("Debug : [Main] onPowerButtonLongClick general");
if(sleeping) return;
timeHelper.keepWokedUp();
shutdown();
}