-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUI.cpp
611 lines (519 loc) · 18.6 KB
/
UI.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//
// Created by aren on 30/1/21.
//
#include <UI/Menu.h>
#include "UI.h"
// UI Elements
Bar DiveTimeBar = {.OriginX=COLUMN_2, .OriginY=ROW_1 +
10, .Length=28, .Thickness=2, .Vertical=false, .Color=COLOR_WHITE, .MaxValue=1, .EndStop=true};
Bar TimeBar = {.OriginX=COLUMN_4 + 18, .OriginY=ROW_4 + VAL_OFFSET +
15, .Length=15, .Thickness=2, .Vertical=true, .Color=COLOR_WHITE, .MaxValue=60, .EndStop=true};
// Menu Items
MenuItem MenuSwitchGas = MenuItem("Switch Gas", &SwitchGasCallback, true);
MenuItem MenuEndDive = MenuItem("End Dive", &EndDiveCallback, false);
MenuItem MenuTurnOff = MenuItem("Turn Off", &TurnOffCallback, true);
MenuItem MenuStartWeb = MenuItem("Start Web", &StartWebCallback, true);
MenuItem MenuStartDive = MenuItem("Start Dive", &StartDiveCallback, false);
MenuItem CalibrateCompass = MenuItem("Calibrate Compass", &CalibrateCompassCallback, true);
// Menu Structure
MenuItem *SurfaceItems[] = {&MenuSwitchGas, &MenuStartDive, &MenuStartWeb, &CalibrateCompass, &MenuTurnOff};
MenuItem *DiveItems[] = {&MenuSwitchGas, &MenuEndDive};
MenuItem *GasItems[MAX_GAS_COUNT] = {};
MenuItem GasItemsActual[MAX_GAS_COUNT] = {};
Menu SurfaceMenu = Menu(SurfaceItems, 5, 0);
Menu DiveMenu = Menu(DiveItems, 2, 0);
Menu GasMenu = Menu(GasItems, MAX_GAS_COUNT, 0);
// Stage
UIState CurrUIState = {.Mode=SURFACE, .InMenu=false, .Menu=&SurfaceMenu, .ClearNeeded=true, .inDeco=false};
// Timeout Trackers
RealTime Timeout;
void UpdateUI(UIData &data) {
// Lock out end dive when below 1m
if (data.Depth > SURFACE_DIVE_THRESHOLD && MenuEndDive.Enabled) {
MenuEndDive.Enabled = false;
} else if (!MenuEndDive.Enabled) {
MenuEndDive.Enabled = true;
}
if (TimeDiff(data.Time, Timeout) > END_DIVE_TIMEOUT
&& CurrUIState.Mode == DIVE
&& data.Depth < SURFACE_DIVE_THRESHOLD) {
EndDiveCallback(MenuEndDive);
Timeout = ReadRTC();
} else if (data.Depth > SURFACE_DIVE_THRESHOLD && CurrUIState.Mode == DIVE) {
Timeout = ReadRTC();
}
if (TimeDiff(data.Time, Timeout) > TURN_OFF_TIMEOUT
&& CurrUIState.Mode == SURFACE) {
TurnOffCallback(MenuTurnOff);
}
if (TimeDiff(data.Time, Timeout) > MENU_TIMEOUT && CurrUIState.InMenu) {
CurrUIState.InMenu = false;
CurrUIState.Menu->CurrIndex = 0;
CurrUIState.ClearNeeded = true;
}
if ((data.NDL == -1) != CurrUIState.inDeco) {
CurrUIState.inDeco = (data.NDL == -1);
CurrUIState.ClearNeeded = true;
}
if (CurrUIState.ClearNeeded) {
Tft.clear();
CurrUIState.ClearNeeded = false;
}
if (CurrUIState.Mode == DIVE) {
if (CurrUIState.InMenu) {
ShowDiveTopRow(data);
ShowBottomRow(data);
ShowMenu(*CurrUIState.Menu);
}
else {
ShowDiveScreen(data);
}
} else if (CurrUIState.Mode == SURFACE) {
if (CurrUIState.InMenu) {
ShowBottomRow(data);
ShowMenu(*CurrUIState.Menu);
} else {
ShowSurfaceScreen(data);
}
} else if (CurrUIState.Mode == WIFI) {
server.handleClient();
ShowWifiScreen(data);
}
}
void ShowWifiScreen(UIData data) {
Tft.setFont(Terminal12x16);
Tft.drawText(COLUMN_1, ROW_1, "Web Server Active");
Tft.setFont(Terminal6x8);
Tft.drawText(COLUMN_1, ROW_1 + 20, "Press Left Button to exit");
Tft.drawText(COLUMN_1, ROW_1 + 30, "IP: " + local_ip.toString());
Tft.drawText(COLUMN_1, ROW_1 + 40, "SSID: " + String(AP_SSID));
Tft.drawText(COLUMN_1, ROW_1 + 50, "Password: " + String(AP_PASSWORD));
ShowBottomRow(data);
}
void ShowMenu(Menu &menu) {
Tft.setFont(Terminal12x16);
Tft.drawText(COLUMN_1, ROW_2, menu.Items[menu.CurrIndex]->MenuText);
}
void ShowBar(Bar &bar, double value) {
int EndX, EndY, ValX, ValY;
if (bar.Vertical) {
ValX = bar.OriginX + bar.Thickness;
ValY = bar.OriginY - (int) ceil(value / bar.MaxValue * bar.Length);
EndX = bar.OriginX + bar.Thickness;
EndY = bar.OriginY - bar.Length;
} else {
ValY = bar.OriginY + bar.Thickness;
ValX = bar.OriginX + (int) ceil(value / bar.MaxValue * bar.Length);
EndY = bar.OriginY + bar.Thickness;
EndX = bar.OriginX + bar.Length;
}
Tft.fillRectangle(bar.OriginX, bar.OriginY, ValX, ValY, bar.Color);
if ((int) ceil(value / bar.MaxValue * bar.Length) != bar.Length) {
Tft.fillRectangle(ValX - bar.Thickness, ValY - bar.Thickness, EndX, EndY,
COLOR_BLACK); // Blank Out any part of the bar that isn't displayed
}
if (bar.EndStop) {
if (bar.Vertical) {
Tft.fillRectangle(EndX - bar.Thickness, EndY - 1, EndX, EndY - 1, bar.Color);
} else {
Tft.fillRectangle(EndX - 1, EndY - bar.Thickness, EndX - 1, EndY, bar.Color);
}
}
}
void ShowDiveTopRow(UIData &data) {
Tft.setFont(Terminal6x8);
Tft.drawText(COLUMN_1, ROW_1, "Depth");
Tft.drawText(COLUMN_2, ROW_1, "Time");
if (CurrUIState.inDeco) {
Tft.drawText(COLUMN_3, ROW_1, "Stop");
Tft.drawText(COLUMN_4, ROW_1, "S Time");
} else {
Tft.drawText((COLUMN_3 + COLUMN_4) / 2, ROW_1, "NDL");
}
Tft.setFont(Terminal11x16);
// Depth
char depth[7];
sprintf(depth, "%04.1f", fabs(data.Depth));
Tft.drawText(COLUMN_1, ROW_1 + VAL_OFFSET, depth);
// Dive Time
char diveTime[7];
sprintf(diveTime, "%3.0f", floor(fabs(data.DiveTime)));
Tft.drawText(COLUMN_2, ROW_1 + VAL_OFFSET, diveTime);
ShowBar(DiveTimeBar, data.DiveTime - (double) ((int) data.DiveTime));
if (CurrUIState.inDeco) {
char stop[4];
sprintf(stop, "%.0f", data.Stop.Depth);
char stime[4];
sprintf(stime, "%.0f", data.Stop.Time);
Tft.drawText(COLUMN_3, ROW_1 + VAL_OFFSET, stop);
Tft.drawText(COLUMN_4, ROW_1 + VAL_OFFSET, stime);
} else {
char ndl[4];
sprintf(ndl, "%.0f", data.NDL);
Tft.drawText((COLUMN_3 + COLUMN_4) / 2, ROW_1 + VAL_OFFSET, ndl);
}
Tft.drawRectangle(0, ROW_1 + VAL_OFFSET + 18, Tft.maxX(), ROW_1 + VAL_OFFSET + 18, COLOR_WHITE);
}
void ShowBottomRow(UIData &data) {
Tft.setFont(Terminal6x8);
Tft.drawText((COLUMN_1 + COLUMN_2) / 2, ROW_4, "Gas");
Tft.drawText((COLUMN_3 + COLUMN_4) / 2, ROW_4, "Clock");
Tft.setFont(Terminal11x16);
// Gas
char gas[7];
sprintf(gas, "%02.0f/%02.0f", data.Gas.FrO2 * 100, data.Gas.FrHe * 100);
Tft.drawText(COLUMN_1 + 10, ROW_4 + VAL_OFFSET, gas);
// Real Time
char time[7];
sprintf(time, "%02d:%02d", data.Time.Hour, data.Time.Minute);
Tft.drawText(COLUMN_3 + 10, ROW_4 + VAL_OFFSET, time);
ShowBar(TimeBar, data.Time.Second);
Tft.drawRectangle(0, ROW_4 - 3, Tft.maxX(), ROW_4 - 3, COLOR_WHITE);
}
void ShowSurfaceScreen(UIData &data) {
ShowBottomRow(data);
Tft.setFont(Terminal6x8);
Tft.drawText(COLUMN_1, ROW_1, "Surface Pressure");
Tft.drawText(COLUMN_3 + 20, ROW_1, "Temperature");
Tft.drawText((COLUMN_1 + COLUMN_2) / 2 - 10, ROW_2, "GFL/GFH");
Tft.drawText((COLUMN_1 + COLUMN_2) / 2, ROW_3, "CNS");
Tft.drawText(COLUMN_3, ROW_2, "L Dpth");
Tft.drawText(COLUMN_4, ROW_2, "Bat V.");
Tft.drawText(COLUMN_3, ROW_3, "L Time");
Tft.drawText(COLUMN_4, ROW_3, "Head.");
Tft.setFont(Terminal11x16);
char pressure[10];
sprintf(pressure, "%.3fBar", fabs(data.AmbientPressure));
Tft.drawText(COLUMN_1, ROW_1 + VAL_OFFSET, pressure);
char temperature[10];
sprintf(temperature, "%02.1fC", data.Temperature);
Tft.drawText(COLUMN_3 + 25, ROW_1 + VAL_OFFSET, temperature);
char gradientFactors[10];
sprintf(gradientFactors, "%02.0f/%02.0f", DecoActual.GFLow * 100, DecoActual.GFHigh * 100);
Tft.drawText(COLUMN_1 + 10, ROW_2 + VAL_OFFSET, gradientFactors);
char cns[7];
sprintf(cns, "%02.0f", data.CNS);
Tft.drawText((COLUMN_1 + COLUMN_2) / 2, ROW_3 + VAL_OFFSET, cns);
char battery[7];
sprintf(battery, "%.2fV", ReadBatteryVoltage());
Tft.drawText(COLUMN_4, ROW_2 + VAL_OFFSET, battery);
char lastDiveDepth[7];
sprintf(lastDiveDepth, "%04.1f", LastDiveDepth);
Tft.drawText(COLUMN_3, ROW_2 + VAL_OFFSET, lastDiveDepth);
char LastTime[7];
sprintf(LastTime, "%.0f", floor(LastDiveTime));
Tft.drawText(COLUMN_3, ROW_3 + VAL_OFFSET, LastTime);
// heading
char heading[7];
sprintf(heading, "%03.0f", data.Heading);
Tft.drawText(COLUMN_4, ROW_3 + VAL_OFFSET, heading);
}
void ShowDiveScreen(UIData &data) {
CurrUIState.ClearNeeded = false;
ShowDiveTopRow(data);
ShowBottomRow(data);
// Draw headers
Tft.setFont(Terminal6x8);
Tft.drawText(COLUMN_1, ROW_2, "Temp");
Tft.drawText(COLUMN_2, ROW_2, "PPO2");
Tft.drawText(COLUMN_3, ROW_2, "Head.");
Tft.drawText(COLUMN_4, ROW_2, "Rate");
Tft.drawText(COLUMN_1, ROW_3, "Max D");
Tft.drawText(COLUMN_2, ROW_3, "Avg. D");
Tft.drawText(COLUMN_3, ROW_3, "TTS");
Tft.drawText(COLUMN_4, ROW_3, "CNS");
// Draw Numbers
Tft.setFont(Terminal11x16);
// Temp
char temp[7];
sprintf(temp, "%4.1f", data.Temperature);
Tft.drawText(COLUMN_1, ROW_2 + VAL_OFFSET, temp);
// PPO2
char ppo2[7];
sprintf(ppo2, "%4.2f", data.PPO2);
Tft.drawText(COLUMN_2, ROW_2 + VAL_OFFSET, ppo2);
// heading
char heading[7];
sprintf(heading, "%03.0f", data.Heading);
Tft.drawText(COLUMN_3, ROW_2 + VAL_OFFSET, heading);
// rate
char rate[7];
sprintf(rate, "%.1f", data.Rate);
Tft.drawText(COLUMN_4, ROW_2 + VAL_OFFSET, rate);
// maxDepth
char maxDepth[7];
sprintf(maxDepth, "%.1f", MaxDepth);
Tft.drawText(COLUMN_1, ROW_3 + VAL_OFFSET, maxDepth);
// Average Depth
char avgDepth[7];
sprintf(avgDepth, "%.1f", data.AverageDepth);
Tft.drawText(COLUMN_2, ROW_3 + VAL_OFFSET, avgDepth);
// TTS
char tts[7];
sprintf(tts, "%02.0f", data.TTS);
Tft.drawText(COLUMN_3, ROW_3 + VAL_OFFSET, tts);
// CNS
char cns[7];
sprintf(cns, "%02.0f", data.CNS);
Tft.drawText(COLUMN_4, ROW_3 + VAL_OFFSET, cns);
}
bool SelfTest() {
bool pass = true;
int textRow = 15;
Tft.clear();
Tft.setFont(Terminal6x8);
Tft.drawText(10, 10, "Starting Self Test:");
textRow += 15;
Tft.drawText(10, textRow, "RTC...");
if (!InitRTC()) {
Tft.drawText(130, textRow, "ERR", COLOR_RED);
pass = false;
} else {
Tft.drawText(130, textRow, "OK", COLOR_GREEN);
char time[15];
RealTime RTCReading = ReadRTC();
sprintf(time, "%02d:%02d", RTCReading.Hour, RTCReading.Minute);
Tft.drawText(150, textRow, time, COLOR_WHITE);
Timeout = RTCReading;
}
textRow += 15;
Tft.drawText(10, textRow, "Accelerometer...");
if (!Accel.begin()) {
Tft.drawText(130, textRow, "ERR", COLOR_RED);
pass = false;
} else {
Accel.setRange(LSM303_RANGE_4G);
Accel.setMode(LSM303_MODE_NORMAL);
Tft.drawText(130, textRow, "OK", COLOR_GREEN);
}
textRow += 15;
Tft.drawText(10, textRow, "Magnetometer...");
if (!Mag.begin()) {
Tft.drawText(130, textRow, "ERR", COLOR_RED);
pass = false;
} else {
Mag.enableAutoRange(true);
Tft.drawText(130, textRow, "OK", COLOR_GREEN);
}
textRow += 15;
Tft.drawText(10, textRow, "ADC...");
if (!InitADC()) {
Tft.drawText(130, textRow, "ERR", COLOR_RED);
pass = false;
} else {
Mag.enableAutoRange(true);
Tft.drawText(130, textRow, "OK", COLOR_GREEN);
}
textRow += 15;
Tft.drawText(10, textRow, "IO Manager...");
if (!InitIO()) {
Tft.drawText(130, textRow, "ERR", COLOR_RED);
pass = false;
} else {
Mag.enableAutoRange(true);
Tft.drawText(130, textRow, "OK", COLOR_GREEN);
}
textRow += 15;
Tft.drawText(10, textRow, "Depth Sensor...");
if (!InitDepth()) {
Tft.drawText(130, textRow, "ERR", COLOR_RED);
pass = false;
} else {
DepthSensor.setModel(MS5837::MS5837_30BA);
Tft.drawText(130, textRow, "OK", COLOR_GREEN);
char pressure[10];
sprintf(pressure, "%.3fBar", fabs(SurfacePressure));
Tft.drawText(150, textRow, pressure);
}
textRow += 15;
Tft.drawText(10, textRow, "Deco Model...");
Deco *testDeco = new Deco;
float depth = 45;
int time = 60;
testDeco->AddDecent(MeterToBar(depth), MeterToBar(testDeco->DecentRate));
testDeco->AddBottom(time);
std::vector<Deco::DecoStop> Schedule = testDeco->GetDecoSchedule(ResetWatchdog);
if (testDeco->Gases.empty()) {
Tft.drawText(130, textRow, "ERR", COLOR_RED);
pass = false;
} else {
Tft.drawText(130, textRow, "OK", COLOR_GREEN);
}
textRow += 15;
Tft.drawText(10, textRow, "File System...");
FSInfo fs_info;
if (!InitFS(fs_info)) {
Tft.drawText(130, textRow, "ERR", COLOR_RED);
pass = false;
} else {
Tft.drawText(130, textRow, "OK", COLOR_GREEN);
char space[10];
LittleFS.info(fs_info);
sprintf(space, "%zuk/%zuk", fs_info.usedBytes / 1024, fs_info.totalBytes / 1024);
Tft.drawText(150, textRow, space, COLOR_WHITE);
}
textRow += 15;
Tft.setFont(Terminal12x16);
if (pass) {
Tft.drawText(10, textRow, "ALL TESTS PASSED", COLOR_GREEN);
} else {
Tft.drawText(10, textRow, "FAILED TESTS: HALT", COLOR_RED);
}
return pass;
}
void NextMenuItem() {
Timeout = ReadRTC();
CurrUIState.Menu->CurrIndex += 1;
if (CurrUIState.Menu->CurrIndex >= CurrUIState.Menu->ItemsSize) {
RestoreMenu();
} else if (!CurrUIState.Menu->Items[CurrUIState.Menu->CurrIndex]->Enabled) // If a menu item is disabled, skip it
{
NextMenuItem();
} else {
Tft.fillRectangle(COLUMN_1, ROW_2, Tft.maxX(), ROW_3, COLOR_BLACK); // Clear prior menu item
ShowMenu(*CurrUIState.Menu);
}
}
void ButtonOne() {
if (CurrUIState.Mode == WIFI) {
MenuItem dummyItem{};
StopWebCallback(dummyItem);
} else {
if (CurrUIState.InMenu) {
NextMenuItem();
} else {
Timeout = ReadRTC();
CurrUIState.InMenu = true;
CurrUIState.ClearNeeded = true;
}
}
}
void ButtonTwo() {
//Serial.println("Button 2");
if (CurrUIState.InMenu) {
CurrUIState.Menu->Items[CurrUIState.Menu->CurrIndex]->Callback(
*CurrUIState.Menu->Items[CurrUIState.Menu->CurrIndex]);
}
}
void RestoreMenu() {
CurrUIState.InMenu = false;
GasMenu.CurrIndex = 0;
CurrUIState.ClearNeeded = true;
if (CurrUIState.Mode == DIVE) {
CurrUIState.Menu = &DiveMenu;
} else if (CurrUIState.Mode == SURFACE) {
CurrUIState.Menu = &SurfaceMenu;
}
}
void SwitchGasCallback(MenuItem &item) {
GasMenu.ItemsSize = DecoActual.Gases.size();
for (int i = 0; i < DecoActual.Gases.size() && i < GasMenu.ItemsSize; i++) {
char *gas = (char *) malloc(sizeof(char) * 20);
sprintf(gas, "%02.0f/%02.0f", DecoActual.Gases[i].FrO2 * 100, DecoActual.Gases[i].FrHe * 100);
GasItemsActual[i].MenuText = gas;
GasItemsActual[i].Callback = &GasMenuCallback;
GasItemsActual[i].Enabled = true;
GasMenu.Items[i] = &GasItemsActual[i];
}
CurrUIState.Menu = &GasMenu;
CurrUIState.ClearNeeded = true;
}
void GasMenuCallback(MenuItem &item) {
double FrO2, FrHe;
sscanf(item.MenuText, "%lf/%lf", &FrO2, &FrHe);
for (int i = 0; i < DecoActual.Gases.size(); i++) {
if (DecoActual.Gases[i].FrO2 * 100 == FrO2 && DecoActual.Gases[i].FrHe == FrHe) {
DecoActual.SwitchGas(i);
}
}
for(int i=0; i < GasMenu.ItemsSize; i++)
{
free((char *) GasMenu.Items[i]->MenuText);
}
RestoreMenu();
}
void EndDiveCallback(MenuItem &item) {
CurrUIState.Mode = SURFACE;
CurrUIState.ClearNeeded = true;
CurrUIState.ClearNeeded = true;
CurrUIState.Menu->CurrIndex = 0;
CurrUIState.Menu = &SurfaceMenu;
CurrUIState.InMenu = false;
EndDive();
}
void TurnOffCallback(MenuItem &item) {
TurnOff();
}
void StartWebCallback(MenuItem &item) {
StartWebServer();
CurrUIState.Mode = WIFI;
CurrUIState.ClearNeeded = true;
CurrUIState.Menu->CurrIndex = 0;
CurrUIState.InMenu = false;
}
void StopWebCallback(MenuItem &item) {
CurrUIState.Mode = SURFACE;
StopWebServer();
CurrUIState.ClearNeeded = true;
}
void StartDiveCallback(MenuItem &item) {
CurrUIState.Mode = DIVE;
CurrUIState.ClearNeeded = true;
CurrUIState.Menu->CurrIndex = 0;
CurrUIState.Menu = &DiveMenu;
CurrUIState.InMenu = false;
StartDive();
}
void CalibrateCompassCallback(MenuItem &item) {
CurrUIState.InMenu = false;
Tft.clear();
Tft.setFont(Terminal12x16);
Tft.drawText(COLUMN_1, ROW_1, "Compass Cal.");
Tft.setFont(Terminal6x8);
Tft.drawText(COLUMN_1, ROW_2, "Please spin the computer along");
Tft.drawText(COLUMN_1, ROW_2 + 10, "all axis for 30 seconds");
Tft.drawText(COLUMN_1, ROW_3, "Press left Button to exit");
RealTime startTime = ReadRTC();
RealTime currTime{};
float calibrationX = 0;
float calibrationY = 0;
float calibrationZ = 0;
//double LastButton1Val = Adc.readADC_SingleEnded(BUTTON_1_CHANNEL);
bool abort = false;
int samples = 0;
do {
ResetWatchdog();
currTime = ReadRTC();
sensors_event_t event;
Mag.getEvent(&event);
calibrationX += event.magnetic.x;
calibrationY += event.magnetic.y;
calibrationZ += event.magnetic.z;
samples++;
Tft.setFont(Terminal6x8);
char sampleCounter[5];
sprintf(sampleCounter, "%d", samples);
Tft.drawText(COLUMN_1, ROW_4, sampleCounter);
Tft.setFont(Terminal12x16);
char timeCounter[5];
sprintf(timeCounter, "%02.0f", TimeDiff(currTime, startTime) * 60);
Tft.drawText(COLUMN_4, ROW_4, timeCounter);
//double button1 = Adc.readADC_SingleEnded(BUTTON_1_CHANNEL);
//if(button1 < LastButton1Val - BUTTON_1_THRESHOLD)
//{
// abort = true;
//}
//LastButton1Val = button1;
} while (TimeDiff(currTime, startTime) < 3.0 / 6.0 && !abort);
if (!abort) {
CompassCalibration.x = calibrationX / (float) samples;
CompassCalibration.y = calibrationY / (float) samples;
CompassCalibration.z = calibrationZ / (float) samples;
Settings settings = GenerateSettings(); // Save the calibration
WriteSettingsFile(settings);
}
CurrUIState.ClearNeeded = true;
CurrUIState.Menu->CurrIndex = 0;
CurrUIState.InMenu = false;
}
MenuItem::MenuItem() = default;