forked from pr3y/Bruce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
394 lines (342 loc) · 12.3 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
#include "core/globals.h"
#include "core/main_menu.h"
#include <iostream>
#include <functional>
#include <vector>
#include <string>
#include "esp32-hal-psram.h"
BruceConfig bruceConfig;
MainMenu mainMenu;
SPIClass sdcardSPI;
SPIClass CC_NRF_SPI;
// Public Globals Variables
unsigned long previousMillis = millis();
int prog_handler; // 0 - Flash, 1 - LittleFS, 3 - Download
String cachedPassword="";
bool interpreter_start = false;
bool sdcardMounted = false;
bool gpsConnected = false;
// wifi globals
// TODO put in a namespace
bool wifiConnected = false;
String wifiIP;
bool BLEConnected = false;
bool returnToMenu;
bool isSleeping = false;
bool isScreenOff = false;
bool dimmer = false;
char timeStr[10];
time_t localTime;
struct tm* timeInfo;
#if defined(HAS_RTC)
cplus_RTC _rtc;
bool clock_set = true;
#else
ESP32Time rtc;
bool clock_set = false;
#endif
std::vector<Option> options;
const int bufSize = 1024;
uint8_t buff[1024] = {0};
// Protected global variables
#if defined(HAS_SCREEN)
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
TFT_eSprite sprite = TFT_eSprite(&tft);
TFT_eSprite draw = TFT_eSprite(&tft);
#else
SerialDisplayClass tft;
SerialDisplayClass& sprite = tft;
SerialDisplayClass& draw = tft;
#endif
#include "Wire.h"
#include "core/display.h"
#include "core/mykeyboard.h"
#include "core/sd_functions.h"
#include "core/settings.h"
#include "core/serialcmds.h"
#include "core/wifi_common.h"
#include "modules/others/audio.h" // for playAudioFile
#include "modules/rf/rf.h" // for initCC1101once
#include "modules/bjs_interpreter/interpreter.h" // for JavaScript interpreter
/*********************************************************************
** Function: begin_storage
** Config LittleFS and SD storage
*********************************************************************/
void begin_storage() {
if(!LittleFS.begin(true)) { LittleFS.format(), LittleFS.begin();}
setupSdCard();
}
/*********************************************************************
** Function: _setup_gpio()
** Sets up a weak (empty) function to be replaced by /ports/* /interface.h
*********************************************************************/
void _setup_gpio() __attribute__((weak));
void _setup_gpio() { }
/*********************************************************************
** Function: setup_gpio
** Setup GPIO pins
*********************************************************************/
void setup_gpio() {
//init setup from /ports/*/interface.h
_setup_gpio();
#if defined(BACKLIGHT)
pinMode(BACKLIGHT, OUTPUT);
#endif
#ifdef USE_CC1101_VIA_SPI
#if CC1101_MOSI_PIN==TFT_MOSI // (T_EMBED), CORE2 and others
initCC1101once(&tft.getSPIinstance());
#elif CC1101_MOSI_PIN==SDCARD_MOSI // (CARDPUTER) and (ESP32S3DEVKITC1) and devices that share CC1101 pin with only SDCard
initCC1101once(&sdcardSPI);
#else // (STICK_C_PLUS) || (STICK_C_PLUS2) and others that doesn´t share SPI with other devices (need to change it when Bruce board comes to shore)
initCC1101once(NULL);
#endif
#endif
}
/*********************************************************************
** Function: begin_tft
** Config tft
*********************************************************************/
void begin_tft(){
#if defined(HAS_SCREEN)// && !defined(CORE) //Need to test if it will work on Core Fire etc..
tft.init();
#endif
tft.fillScreen(TFT_BLACK);
tft.setRotation(bruceConfig.rotation);
resetTftDisplay();
setBrightness(bruceConfig.bright);
}
/*********************************************************************
** Function: boot_screen
** Draw boot screen
*********************************************************************/
void boot_screen() {
tft.setTextColor(bruceConfig.priColor, TFT_BLACK);
tft.setTextSize(FM);
tft.drawPixel(0,0,TFT_BLACK);
tft.drawCentreString("Bruce", WIDTH / 2, 10, SMOOTH_FONT);
tft.setTextSize(FP);
tft.drawCentreString(BRUCE_VERSION, WIDTH / 2, 25, SMOOTH_FONT);
tft.setTextSize(FM);
tft.drawCentreString("PREDATORY FIRMWARE", WIDTH / 2, HEIGHT+2, SMOOTH_FONT); // will draw outside the screen on non touch devices
}
/*********************************************************************
** Function: boot_screen_anim
** Draw boot screen
*********************************************************************/
void boot_screen_anim() {
boot_screen();
int i = millis();
// checks for boot.jpg in SD and LittleFS for customization
bool boot_img=false;
if(SD.exists("/boot.jpg")) boot_img = true;
else if(LittleFS.exists("/boot.jpg")) boot_img = true;
else if(SD.exists("/boot.gif")) boot_img = true;
else if(LittleFS.exists("/boot.gif")) boot_img = true;
// Start image loop
while(millis()<i+7000) { // boot image lasts for 5 secs
#if !defined(LITE_VERSION)
bool drawn=false;
if((millis()-i>2000) && (millis()-i)<2200){
tft.fillRect(0,45,WIDTH,HEIGHT-45,bruceConfig.bgColor);
if(boot_img && !drawn) {
if(showJpeg(SD,"/boot.jpg") && (millis()-i>2000) && (millis()-i<2200)) { boot_img=true; Serial.println("Image from SD"); }
else if (showJpeg(LittleFS,"/boot.jpg") && (millis()-i>2000) && (millis()-i<2100)) { boot_img=true; Serial.println("Image from LittleFS"); }
else if (showGIF(SD,"/boot.gif") && (millis()-i>2000) && (millis()-i<2200)) { boot_img=true; Serial.println("Image from SD"); }
else if (showGIF(LittleFS,"/boot.gif") && (millis()-i>2000) && (millis()-i<2100)) { boot_img=true; Serial.println("Image from LittleFS"); }
drawn=true;
}
}
if(!boot_img && (millis()-i>2200) && (millis()-i)<2700) tft.drawRect(2*WIDTH/3,HEIGHT/2,2,2,bruceConfig.priColor);
if(!boot_img && (millis()-i>2700) && (millis()-i)<2900) tft.fillRect(0,45,WIDTH,HEIGHT-45,bruceConfig.bgColor);
if(!boot_img && (millis()-i>2900) && (millis()-i)<3400) tft.drawXBitmap(2*WIDTH/3 - 30 ,5+HEIGHT/2,bruce_small_bits, bruce_small_width, bruce_small_height,TFT_BLACK,bruceConfig.priColor);
if(!boot_img && (millis()-i>3400) && (millis()-i)<3600) tft.fillRect(0,0,WIDTH,HEIGHT,bruceConfig.bgColor);
if(!boot_img && (millis()-i>3600)) tft.drawXBitmap((WIDTH-238)/2,(HEIGHT-133)/2,bits, bits_width, bits_height,TFT_BLACK,bruceConfig.priColor);
#endif
if(checkAnyKeyPress()) // If any key or M5 key is pressed, it'll jump the boot screen
{
tft.fillScreen(TFT_BLACK);
tft.fillScreen(TFT_BLACK);
delay(10);
return;
}
}
// Clear splashscreen
tft.fillScreen(TFT_BLACK);
// Clear splashscreen
tft.fillScreen(TFT_BLACK);
}
/*********************************************************************
** Function: init_clock
** Clock initialisation for propper display in menu
*********************************************************************/
void init_clock() {
#if defined(HAS_RTC)
RTC_TimeTypeDef _time;
cplus_RTC _rtc;
_rtc.begin();
_rtc.GetBm8563Time();
_rtc.GetTime(&_time);
#endif
}
/*********************************************************************
** Function: startup_sound
** Play sound or tone depending on device hardware
*********************************************************************/
void startup_sound() {
#if !defined(LITE_VERSION)
#if defined(BUZZ_PIN)
// Bip M5 just because it can. Does not bip if splashscreen is bypassed
_tone(5000, 50);
delay(200);
_tone(5000, 50);
/* 2fix: menu infinite loop */
#elif defined(HAS_NS4168_SPKR)
// play a boot sound
if(SD.exists("/boot.wav")) playAudioFile(&SD, "/boot.wav");
else if(LittleFS.exists("/boot.wav")) playAudioFile(&LittleFS, "/boot.wav");
setup_gpio(); // temp fix for menu inf. loop
#endif
#endif
}
/*********************************************************************
** Function: setup
** Where the devices are started and variables set
*********************************************************************/
void setup() {
Serial.setRxBufferSize(SAFE_STACK_BUFFER_SIZE); // Must be invoked before Serial.begin(). Default is 256 chars
Serial.begin(115200);
log_d("Total heap: %d", ESP.getHeapSize());
log_d("Free heap: %d", ESP.getFreeHeap());
if(psramInit()) log_d("PSRAM Started");
if(psramFound()) log_d("PSRAM Found");
else log_d("PSRAM Not Found");
log_d("Total PSRAM: %d", ESP.getPsramSize());
log_d("Free PSRAM: %d", ESP.getFreePsram());
// declare variables
prog_handler=0;
sdcardMounted=false;
wifiConnected=false;
BLEConnected=false;
setup_gpio();
bruceConfig.bright=100; // theres is no value yet
begin_tft();
boot_screen();
begin_storage();
bruceConfig.fromFile();
resetTftDisplay();
tft.setRotation(bruceConfig.rotation);
setBrightness(bruceConfig.bright);
init_clock();
boot_screen_anim();
startup_sound();
if (bruceConfig.wifiAtStartup) {
displayInfo("Connecting WiFi...");
wifiConnectTask();
}
#if ! defined(HAS_SCREEN)
// start a task to handle serial commands while the webui is running
startSerialCommandsHandlerTask();
#endif
delay(200);
previousMillis = millis();
setBrightness(bruceConfig.bright);
}
/**********************************************************************
** Function: loop
** Main loop
**********************************************************************/
#if defined(HAS_SCREEN)
void loop() {
#if defined(HAS_RTC)
RTC_TimeTypeDef _time;
#endif
bool redraw = true;
long clock_update=0;
mainMenu.begin();
// Interpreter must be ran in the loop() function, otherwise it breaks
// called by 'stack canary watchpoint triggered (loopTask)'
#if !defined(LITE_VERSION)
#if !defined(CORE) && !defined(CORE2)
if(interpreter_start) {
interpreter_start=false;
interpreter();
previousMillis = millis(); // ensure that will not dim screen when get back to menu
//goto END;
}
#endif
#endif
tft.fillRect(0,0,WIDTH,HEIGHT,bruceConfig.bgColor);
bruceConfig.fromFile();
while(1){
if(interpreter_start) goto END;
if (returnToMenu) {
returnToMenu = false;
tft.fillScreen(bruceConfig.bgColor); //fix any problem with the mainMenu screen when coming back from submenus or functions
redraw=true;
}
if (redraw) {
mainMenu.draw();
clock_update=0; // forces clock drawing
redraw = false;
delay(REDRAW_DELAY);
}
handleSerialCommands();
#ifdef HAS_KEYBOARD
checkShortcutPress(); // shortctus to quickly start apps without navigating the menus
#endif
if (checkPrevPress()) {
checkReboot();
mainMenu.previous();
redraw = true;
}
/* DW Btn to next item */
if (checkNextPress()) {
mainMenu.next();
redraw = true;
}
/* Select and run function */
if (checkSelPress()) {
mainMenu.openMenuOptions();
drawMainBorder(true);
redraw=true;
}
// update battery and clock once every 30 seconds
// it was added to avoid delays in btns readings from Core and improves overall performance
if(millis()-clock_update>30000) {
drawBatteryStatus();
if (clock_set) {
#if defined(HAS_RTC)
_rtc.GetTime(&_time);
setTftDisplay(12, 12, bruceConfig.priColor, 1, bruceConfig.bgColor);
snprintf(timeStr, sizeof(timeStr), "%02d:%02d", _time.Hours, _time.Minutes);
tft.print(timeStr);
#else
updateTimeStr(rtc.getTimeStruct());
setTftDisplay(12, 12, bruceConfig.priColor, 1, bruceConfig.bgColor);
tft.print(timeStr);
#endif
}
else {
setTftDisplay(12, 12, bruceConfig.priColor, 1, bruceConfig.bgColor);
tft.print("BRUCE " + String(BRUCE_VERSION));
}
clock_update=millis();
}
}
END:
delay(1);
}
#else
// alternative loop function for headless boards
#include "modules/others/webInterface.h"
void loop() {
setupSdCard();
bruceConfig.fromFile();
if(!wifiConnected) {
Serial.println("wifiConnect");
wifiConnectMenu(WIFI_AP); // TODO: read mode from config file
}
Serial.println("startWebUi");
startWebUi(true); // MEMO: will quit when checkEscPress
}
#endif