-
-
Notifications
You must be signed in to change notification settings - Fork 272
Expand file tree
/
Copy pathmain.cpp
More file actions
298 lines (238 loc) · 6.42 KB
/
Copy pathmain.cpp
File metadata and controls
298 lines (238 loc) · 6.42 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
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
/* MIT License - Copyright (c) 2019-2024 Francis Van Roie
For full license information read the LICENSE file in the project folder */
/*
#ifdef CORE_DEBUG_LEVEL
#undef CORE_DEBUG_LEVEL
#endif
#define CORE_DEBUG_LEVEL 3
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
#include "esp_log.h"
*/
#include "hasplib.h"
#include "hasp_oobe.h"
#include "sys/net/hasp_network.h"
#include "sys/net/hasp_time.h"
#include "dev/device.h"
#if HASP_USE_GPIO > 0
#include "sys/gpio/hasp_gpio.h"
#endif
#if HASP_USE_CONFIG > 0
#include "hasp_debug.h"
#include "hasp_macro.h"
#endif
#if HASP_USE_CONFIG > 0
#include "hasp_gui.h"
#endif
static bool isConnected;
static uint8_t mainLoopCounter = 0;
static unsigned long mainLastLoopTime = 0;
#ifdef HASP_USE_STAT_COUNTER
uint16_t statLoopCounter = 0; // measures the average looptime
#endif
void setup()
{
// hal_setup();
#if HASP_TARGET_ARDUINO
esp_log_level_set("*", ESP_LOG_NONE); // set all components to ERROR level
// esp_log_level_set("wifi", ESP_LOG_NONE); // enable WARN logs from WiFi stack
// esp_log_level_set("dhcpc", ESP_LOG_INFO); // enable INFO logs from DHCP client
// esp_log_level_set("esp_crt_bundle", ESP_LOG_VERBOSE); // enable WARN logs from WiFi stack
// esp_log_level_set("esp_tls", ESP_LOG_VERBOSE); // enable WARN logs from WiFi stack
#elif HASP_TARGET_PC
// Initialize lvgl environment
lv_init();
lv_log_register_print_cb(debugLvglLogEvent);
#if HASP_USE_CONFIG
// initialize FS before running configSetup()
// normally, it's initialized in guiSetup(), but Arduino doesn't need FS in configSetup()
lv_fs_if_init();
#endif
#endif
haspDevice.init();
/****************************
* Storage initializations
***************************/
// nvs_setup();
#if HASP_USE_EEPROM > 0
eepromSetup(); // Don't start at boot, only at write
#endif
// #if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0
// filesystemSetup(); // FS mount is done in configSetup()
// #endif
// #if HASP_USE_SDCARD > 0
// sdcardSetup();
// #endif
/****************************
* Read & Apply User Configuration
***************************/
#if HASP_USE_CONFIG > 0
configSetup(); // also runs debugSetup(), debugStart() and consoleSetup()
#endif
#if HASP_USE_WIFI > 0 || HASP_USE_ETHERNET > 0
networkSetup();
timeSetup();
#endif
dispatchSetup(); // before hasp and oobe, asap after logging starts
guiSetup();
bool oobe = false;
#if HASP_USE_CONFIG > 0
oobe = oobeSetup();
#endif
if(!oobe) {
haspSetup();
}
/****************************
* Apply User Configuration
***************************/
// #if HASP_USE_MQTT > 0
// mqttSetup(); // Load Hostname before starting WiFi
// #endif
#if HASP_USE_GPIO > 0
gpioSetup();
#endif
#if HASP_USE_MDNS > 0
mdnsSetup();
#endif
#if HASP_USE_ARDUINOOTA > 0 || HASP_USE_HTTP_UPDATE > 0
otaSetup();
#endif
#if HASP_USE_HTTP > 0 || HASP_USE_HTTP_ASYNC > 0
httpSetup();
#endif
// #if HASP_USE_CONSOLE > 0
// consoleSetup(); // the consoleSetup is called in debugSetup
// #endif
#if HASP_USE_TELNET > 0
telnetSetup();
#endif
#if HASP_USE_FTP > 0
ftpSetup();
#endif
#if HASP_USE_TASMOTA_CLIENT > 0
slaveSetup();
#endif
#if defined(HASP_USE_CUSTOM) && HASP_USE_CUSTOM > 0
custom_setup();
#endif
// guiStart();
delay(20);
if(!oobe) {
dispatch_run_script(NULL, "L:/boot.cmd", TAG_HASP);
#if HASP_USE_WIFI > 0 || HASP_USE_ETHERNET > 0
network_run_scripts();
#endif
}
#if HASP_USE_MQTT > 0
mqttSetup();
#endif
#if HASP_USE_LVGL_TASK && defined(ESP32)
gui_setup_lvgl_task();
#endif // HASP_USE_LVGL_TASK
mainLastLoopTime = 0; // reset loop counter
}
IRAM_ATTR void loop()
{
#if defined(ESP32) && defined(HASP_USE_ESP_MQTT)
if(!gui_acquire(portMAX_DELAY)) {
// LOG_ERROR(TAG_MAIN, F("TAKE Mutex"));
delay(5); // ms
return;
}
#endif
#if HASP_TARGET_PC
/* Process jsonl/json commands deferred from MQTT thread (LVGL not thread-safe on PC). */
dispatch_process_deferred();
#endif
#if HASP_USE_LVGL_TASK == 0
guiLoop();
#endif
#if HASP_USE_WIFI > 0 || HASP_USE_ETHERNET > 0
networkLoop();
#endif
#if HASP_USE_GPIO > 0
gpioLoop();
#endif // GPIO
#if HASP_USE_MQTT > 0
mqttLoop();
#endif
// haspDevice.loop();
#if HASP_USE_CONSOLE > 0
// debugLoop();
consoleLoop();
#endif
#if defined(HASP_USE_CUSTOM) && HASP_USE_CUSTOM > 0
custom_loop();
#endif
#ifdef HASP_USE_STAT_COUNTER
statLoopCounter++; // measures the average looptime
#endif
/* Timer Loop */
if(millis() - mainLastLoopTime >= 1000) {
mainLastLoopTime = millis();
/* Runs Every Second */
haspEverySecond(); // sleep timer & statusupdate
#if HASP_USE_GPIO > 0
gpioEverySecond();
#endif
#if HASP_USE_MQTT > 0
mqttEverySecond();
#endif
#if HASP_USE_FTP > 0
ftpEverySecond();
#endif
#if HASP_USE_TELNET > 0
telnetEverySecond();
#endif
#if defined(HASP_USE_CUSTOM) && HASP_USE_CUSTOM > 0
custom_every_second();
#endif
// debugEverySecond();
switch(++mainLoopCounter) {
case 1:
haspDevice.loop_5s();
break;
case 2:
#if HASP_USE_HTTP_ASYNC > 0
httpEvery5Seconds();
#endif
break;
case 3:
#if HASP_USE_GPIO > 0
// gpioEvery5Seconds();
#endif
#if defined(HASP_USE_CUSTOM) && HASP_USE_CUSTOM > 0
custom_every_5seconds();
#endif
break;
case 4:
#if HASP_USE_WIFI > 0 || HASP_USE_ETHERNET > 0
isConnected = networkEvery5Seconds(); // Check connection
#endif
#if HASP_USE_MQTT > 0
mqttEvery5Seconds(isConnected);
#endif
break;
case 5:
mainLoopCounter = 0;
#ifdef HASP_USE_STAT_COUNTER
if(statLoopCounter)
LOG_DEBUG(TAG_MAIN, F("%d millis per loop, %d counted"), 5000 / statLoopCounter, statLoopCounter);
statLoopCounter = 0;
#endif
break;
}
}
#if defined(ESP32) && defined(HASP_USE_ESP_MQTT)
gui_release();
#endif
// allow the cpu to switch to other tasks
#if HASP_USE_LVGL_TASK == 0
#ifdef ARDUINO_ARCH_ESP8266
delay(2); // ms
#else
delay(2); // ms
#endif
#else // HASP_USE_LVGL_TASK != 0
delay(2); // ms
#endif
}