-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.cpp
More file actions
199 lines (158 loc) · 6.32 KB
/
main.cpp
File metadata and controls
199 lines (158 loc) · 6.32 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
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
// SPDX-License-Identifier: GPL-3.0-or-later
/*
* GeekMagic Open Firmware
* Copyright (C) 2026 Times-Z
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#include <LittleFS.h>
#include <Arduino_GFX_Library.h>
#include <SPI.h>
#include <ESP8266HTTPUpdateServer.h>
#include <Logger.h>
#include "project_version.h"
#include "config/ConfigManager.h"
#include "wireless/WiFiManager.h"
#include "display/DisplayManager.h"
#include "web/Webserver.h"
#include "web/Api.h"
#include "ntp/NTPClient.h"
#include <array>
ConfigManager configManager;
const char* AP_SSID = "GeekMagic";
const char* AP_PASSWORD = "$str0ngPa$$w0rd";
WiFiManager* wifiManager = nullptr;
ESP8266HTTPUpdateServer httpUpdater;
static String KV_SALT = "GeekMagicOpenFirmwareIsAwesome";
static size_t initial_free_heap = 0;
static constexpr size_t FREE_BUF_SIZE = 32;
static constexpr size_t MSG_BUF_SIZE = 96;
static constexpr uint32_t SERIAL_BAUD_RATE = 115200;
static constexpr uint32_t BOOT_DELAY_MS = 200;
static constexpr int LOADING_BAR_TEXT_X = 50;
static constexpr int LOADING_BAR_TEXT_Y = 80;
static constexpr int LOADING_BAR_Y = 110;
static constexpr int LOADING_DELAY_MS = 1000;
Webserver* webserver = nullptr;
NTPClient* ntpClient = nullptr;
/**
* @brief Formats bytes into a human-readable string
*
* @param value Size in bytes
* @return Formatted string
*/
static void formatBytes(size_t value, char* outBuf, size_t outBufSize) {
constexpr std::array<const char*, 5> UNITS = {"B", "KB", "MB", "GB", "TB"};
constexpr double THRESHOLD = 1024.0;
auto val = static_cast<double>(value);
int unit = 0;
while (val >= THRESHOLD && unit < static_cast<int>(UNITS.size()) - 1) {
val /= THRESHOLD;
++unit;
}
if (unit == 0) {
snprintf(outBuf, outBufSize, "%u %s", static_cast<unsigned int>(value), UNITS[unit]);
} else {
snprintf(outBuf, outBufSize, "%.1f %s", val, UNITS[unit]);
}
}
/**
* @brief Initializes the system
*
*/
void setup() {
Serial.begin(SERIAL_BAUD_RATE);
delay(BOOT_DELAY_MS);
Serial.println("");
Logger::info(("GeekMagic Open Firmware " + String(PROJECT_VER_STR)).c_str());
DisplayManager::begin();
constexpr int TOTAL_STEPS = 6;
int step = 0;
if (!LittleFS.begin()) {
DisplayManager::drawLoadingBar((float)step / TOTAL_STEPS, LOADING_BAR_Y);
Logger::error("Failed to mount LittleFS");
return;
}
step++;
SecureStorage::setSalt(KV_SALT);
if (configManager.secure.begin()) {
Logger::info("SecureStorage initialized successfully", "ConfigManager");
}
step++;
if (configManager.load()) {
Logger::info("Configuration loaded successfully");
}
step++;
DisplayManager::drawTextWrapped(LOADING_BAR_TEXT_X, LOADING_BAR_TEXT_Y, "Starting...", 2, LCD_WHITE, LCD_BLACK,
true);
DisplayManager::drawLoadingBar((float)step / TOTAL_STEPS, LOADING_BAR_Y);
step++;
wifiManager = new WiFiManager(configManager.getSSID(), configManager.getPassword(), AP_SSID, AP_PASSWORD);
wifiManager->begin();
ntpClient = new NTPClient();
ntpClient->begin();
DisplayManager::drawLoadingBar((float)step / TOTAL_STEPS, LOADING_BAR_Y);
step++;
webserver = new Webserver();
webserver->begin();
initial_free_heap = ESP.getFreeHeap(); // NOLINT(readability-static-accessed-through-instance)
DisplayManager::drawLoadingBar((float)step / TOTAL_STEPS, LOADING_BAR_Y);
step++;
registerApiEndpoints(webserver);
httpUpdater.setup(&webserver->raw(), "/legacyupdate");
webserver->serveStaticC("/", "/web/index.html", "text/html");
webserver->serveStaticC("/header.html", "/web/header.html", "text/html");
webserver->serveStaticC("/footer.html", "/web/footer.html", "text/html");
webserver->serveStaticC("/index.html", "/web/index.html", "text/html");
webserver->serveStaticC("/update.html", "/web/update.html", "text/html");
webserver->serveStaticC("/gif_upload.html", "/web/gif_upload.html", "text/html");
webserver->serveStaticC("/wifi.html", "/web/wifi.html", "text/html");
webserver->serveStaticC("/token.html", "/web/token.html", "text/html");
webserver->serveStaticC("/ntp.html", "/web/ntp.html", "text/html");
webserver->serveStaticC("/logs.html", "/web/logs.html", "text/html");
webserver->serveStaticC("/config.json", "/config.json", "application/json");
webserver->registerStaticDir("/web/css", "/css", "text/css");
webserver->registerStaticDir("/web/js", "/js", "application/javascript");
DisplayManager::drawLoadingBar(1.0F, LOADING_BAR_Y);
delay(LOADING_DELAY_MS);
DisplayManager::drawStartup(wifiManager->getIP().toString());
// enable watchdog before going to loop()
// 2 seconds should be way more than the main loop needs to do stuff
EspClass::wdtEnable(WDTO_2S);
}
void loop() {
if (webserver != nullptr) {
webserver->handleClient();
}
if (ntpClient != nullptr) {
ntpClient->loop();
}
DisplayManager::update();
static unsigned long last_free_heap_log = 0;
static constexpr unsigned long FREE_HEAP_LOG_INTERVAL_MS = 10000UL;
unsigned long now = millis();
if (now - last_free_heap_log >= FREE_HEAP_LOG_INTERVAL_MS) {
last_free_heap_log = now;
char freeBuf[FREE_BUF_SIZE];
char initBuf[FREE_BUF_SIZE];
char msgBuf[MSG_BUF_SIZE];
formatBytes(ESP.getFreeHeap(), freeBuf, // NOLINT(readability-static-accessed-through-instance)
sizeof(freeBuf));
formatBytes(initial_free_heap, initBuf, sizeof(initBuf));
snprintf(msgBuf, sizeof(msgBuf), "Free heap: %s (initial: %s)", freeBuf, initBuf);
Logger::info(msgBuf);
}
EspClass::wdtFeed(); // kick watchdog
}