-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathmain.cpp
More file actions
110 lines (87 loc) · 2.68 KB
/
Copy pathmain.cpp
File metadata and controls
110 lines (87 loc) · 2.68 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
#include "CaptivePortal.h"
#include "CommandHandler.h"
#include "Common.h"
#include "config/Config.h"
#include "EStopManager.h"
#include "event_handlers/Init.h"
#include "GatewayConnectionManager.h"
#include "Logging.h"
#include "OtaUpdateManager.h"
#include "serial/SerialInputHandler.h"
#include "util/TaskUtils.h"
#include "VisualStateManager.h"
#include "wifi/WiFiManager.h"
#include "wifi/WiFiScanManager.h"
#include <Arduino.h>
#include <memory>
const char* const TAG = "OpenShock";
// Internal setup function, returns true if setup succeeded, false otherwise.
bool trySetup() {
OpenShock::EventHandlers::Init();
if (!OpenShock::VisualStateManager::Init()) {
ESP_PANIC(TAG, "Unable to initialize VisualStateManager");
}
OpenShock::EStopManager::Init();
if (!OpenShock::SerialInputHandler::Init()) {
ESP_LOGE(TAG, "Unable to initialize SerialInputHandler");
return false;
}
if (!OpenShock::CommandHandler::Init()) {
ESP_LOGW(TAG, "Unable to initialize CommandHandler");
return false;
}
if (!OpenShock::WiFiManager::Init()) {
ESP_LOGE(TAG, "Unable to initialize WiFiManager");
return false;
}
if (!OpenShock::GatewayConnectionManager::Init()) {
ESP_LOGE(TAG, "Unable to initialize GatewayConnectionManager");
return false;
}
return true;
}
// OTA setup is the same as normal setup, but we invalidate the currently running app, and roll back if it fails.
void otaSetup() {
ESP_LOGI(TAG, "Validating OTA app");
if (!trySetup()) {
ESP_LOGE(TAG, "Unable to validate OTA app, rolling back");
OpenShock::OtaUpdateManager::InvalidateAndRollback();
}
ESP_LOGI(TAG, "Marking OTA app as valid");
OpenShock::OtaUpdateManager::ValidateApp();
ESP_LOGI(TAG, "Done validating OTA app");
}
// App setup is the same as normal setup, but we restart if it fails.
void appSetup() {
if (!trySetup()) {
ESP_LOGI(TAG, "Restarting in 5 seconds...");
vTaskDelay(pdMS_TO_TICKS(5000));
esp_restart();
}
}
// Arduino setup function
void setup() {
Serial.begin(115'200);
OpenShock::Config::Init();
OpenShock::OtaUpdateManager::Init();
if (OpenShock::OtaUpdateManager::IsValidatingApp()) {
otaSetup();
} else {
appSetup();
}
}
void main_app(void* arg) {
while (true) {
OpenShock::SerialInputHandler::Update();
OpenShock::CaptivePortal::Update();
OpenShock::GatewayConnectionManager::Update();
OpenShock::WiFiManager::Update();
vTaskDelay(5); // 5 ticks update interval
}
}
void loop() {
// Start the main task
OpenShock::TaskUtils::TaskCreateExpensive(main_app, "main_app", 8192, nullptr, 1, nullptr); // PROFILED: 6KB stack usage
// Kill the loop task (Arduino is stinky)
vTaskDelete(nullptr);
}