-
-
Notifications
You must be signed in to change notification settings - Fork 356
/
Copy pathopentx.cpp
1878 lines (1574 loc) · 44.9 KB
/
opentx.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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*/
#if !defined(SIMU)
#include "stm32_ws2812.h"
#include "boards/generic_stm32/rgb_leds.h"
#endif
#include "opentx.h"
#include "io/frsky_firmware_update.h"
#include "hal/adc_driver.h"
#include "hal/switch_driver.h"
#include "hal/storage.h"
#include "hal/watchdog_driver.h"
#include "hal/abnormal_reboot.h"
#include "hal/usb_driver.h"
#include "timers_driver.h"
#include "switches.h"
#include "inactivity_timer.h"
#include "input_mapping.h"
#include "tasks.h"
#include "tasks/mixer_task.h"
#if defined(BLUETOOTH)
#include "bluetooth_driver.h"
#endif
#if defined(LIBOPENUI)
#include "libopenui.h"
#include "radio_calibration.h"
#include "view_main.h"
#include "view_text.h"
#include "theme.h"
#include "switch_warn_dialog.h"
#include "gui/colorlcd/LvglWrapper.h"
#endif
#if !defined(SIMU)
#include <malloc.h>
#endif
extern void startSplash();
extern void waitSplash();
RadioData g_eeGeneral;
ModelData g_model;
#if defined(SDCARD)
Clipboard clipboard;
#endif
GlobalData globalData;
uint32_t maxMixerDuration; // microseconds
constexpr uint8_t HEART_TIMER_10MS = 0x01;
uint8_t heartbeat;
#if defined(OVERRIDE_CHANNEL_FUNCTION)
safetych_t safetyCh[MAX_OUTPUT_CHANNELS];
#endif
// __DMA for the MSC_BOT_Data member
union ReusableBuffer reusableBuffer __DMA;
#if !defined(SIMU)
uint8_t* MSC_BOT_Data = reusableBuffer.MSC_BOT_Data;
#endif
#if defined(DEBUG_LATENCY)
uint8_t latencyToggleSwitch = 0;
#endif
volatile uint8_t rtc_count = 0;
#if defined(DEBUG_LATENCY)
void toggleLatencySwitch()
{
latencyToggleSwitch ^= 1;
#if defined(PCBHORUS)
if (latencyToggleSwitch)
GPIO_ResetBits(EXTMODULE_TX_GPIO, EXTMODULE_TX_GPIO_PIN);
else
GPIO_SetBits(EXTMODULE_TX_GPIO, EXTMODULE_TX_GPIO_PIN);
#else
if (latencyToggleSwitch)
sportUpdatePowerOn();
else
sportUpdatePowerOff();
#endif
}
#endif
void checkValidMCU(void)
{
#if !defined(SIMU) && !defined(BOOT)
// Checks the radio MCU type matches intended firmware type
uint32_t idcode = DBGMCU->IDCODE & 0xFFF;
#if defined(RADIO_TLITE)
#define TARGET_IDCODE_SECONDARY 0x413
// Tlite ELRS have a CKS F4 run as an F2 (F4 firmware won't run on those)
#endif
#if defined(STM32F205xx)
#define TARGET_IDCODE 0x411
#elif defined(STM32F407xx)
#define TARGET_IDCODE 0x413
#elif defined(STM32F429xx)
#define TARGET_IDCODE 0x419
#elif defined(STM32F413xx)
#define TARGET_IDCODE 0x463
#else
// Ensure new radio get registered :)
#define TARGET_IDCODE 0x0
#endif
#if defined(TARGET_IDCODE_SECONDARY)
if(idcode != TARGET_IDCODE && idcode != TARGET_IDCODE_SECONDARY) {
runFatalErrorScreen("Wrong MCU");
}
#else
if(idcode != TARGET_IDCODE) {
runFatalErrorScreen("Wrong MCU");
}
#endif
#endif
}
void per10ms()
{
DEBUG_TIMER_START(debugTimerPer10ms);
DEBUG_TIMER_SAMPLE(debugTimerPer10msPeriod);
g_tmr10ms++;
#if defined(GUI)
if (lightOffCounter) lightOffCounter--;
if (flashCounter) flashCounter--;
#if !defined(LIBOPENUI)
if (noHighlightCounter) noHighlightCounter--;
#endif
#endif
if (trimsCheckTimer) trimsCheckTimer--;
trainerDecTimer();
if (trimsDisplayTimer)
trimsDisplayTimer--;
else
trimsDisplayMask = 0;
#if defined(DEBUG_LATENCY_END_TO_END)
static tmr10ms_t lastLatencyToggle = 0;
if (g_tmr10ms - lastLatencyToggle == 10) {
lastLatencyToggle = g_tmr10ms;
toggleLatencySwitch();
}
#endif
#if defined(RTCLOCK)
/* Update global Date/Time every 100 per10ms cycles */
if (++g_ms100 == 100) {
g_rtcTime++; // inc global unix timestamp one second
g_ms100 = 0;
}
#endif
if (keysPollingCycle()) {
inactivityTimerReset(ActivitySource::Keys);
}
#if defined(FUNCTION_SWITCHES)
evalFunctionSwitches();
#endif
#if defined(ROTARY_ENCODER_NAVIGATION) && !defined(LIBOPENUI)
if (rotaryEncoderPollingCycle()) {
inactivityTimerReset(ActivitySource::Keys);
}
#endif
telemetryInterrupt10ms();
// These moved here from evalFlightModeMixes() to improve beep trigger reliability.
#if defined(PWM_BACKLIGHT)
if ((g_tmr10ms&0x03) == 0x00)
backlightFade(); // increment or decrement brightness until target brightness is reached
#endif
#if !defined(AUDIO)
if (mixWarning & 1) if(((g_tmr10ms&0xFF)== 0)) AUDIO_MIX_WARNING(1);
if (mixWarning & 2) if(((g_tmr10ms&0xFF)== 64) || ((g_tmr10ms&0xFF)== 72)) AUDIO_MIX_WARNING(2);
if (mixWarning & 4) if(((g_tmr10ms&0xFF)==128) || ((g_tmr10ms&0xFF)==136) || ((g_tmr10ms&0xFF)==144)) AUDIO_MIX_WARNING(3);
#endif
outputTelemetryBuffer.per10ms();
heartbeat |= HEART_TIMER_10MS;
DEBUG_TIMER_STOP(debugTimerPer10ms);
}
FlightModeData *flightModeAddress(uint8_t idx)
{
return &g_model.flightModeData[idx];
}
ExpoData *expoAddress(uint8_t idx )
{
return &g_model.expoData[idx];
}
LimitData *limitAddress(uint8_t idx)
{
return &g_model.limitData[idx];
}
USBJoystickChData *usbJChAddress(uint8_t idx)
{
return &g_model.usbJoystickCh[idx];
}
void memswap(void * a, void * b, uint8_t size)
{
uint8_t * x = (uint8_t *)a;
uint8_t * y = (uint8_t *)b;
uint8_t temp ;
while (size--) {
temp = *x;
*x++ = *y;
*y++ = temp;
}
}
#if defined(PXX2)
void setDefaultOwnerId()
{
uint8_t ch;
for (uint8_t i = 0; i < PXX2_LEN_REGISTRATION_ID; i++) {
ch = ((uint8_t *)cpu_uid)[4+i]&0x7f;
if(ch<0x20 || ch==0x7f) ch='-';
g_eeGeneral.ownerRegistrationID[PXX2_LEN_REGISTRATION_ID-1-i] = ch;
}
}
#endif
void generalDefault()
{
memclear(&g_eeGeneral, sizeof(g_eeGeneral));
#if defined(COLORLCD)
g_eeGeneral.blOffBright = 20;
#endif
#if defined(LCD_CONTRAST_DEFAULT)
g_eeGeneral.contrast = LCD_CONTRAST_DEFAULT;
#endif
#if defined(LCD_BRIGHTNESS_DEFAULT)
g_eeGeneral.backlightBright = LCD_BRIGHTNESS_DEFAULT;
#endif
#if defined(DEFAULT_INTERNAL_MODULE)
g_eeGeneral.internalModule = DEFAULT_INTERNAL_MODULE;
#endif
adcCalibDefaults();
g_eeGeneral.potsConfig = adcGetDefaultPotsConfig();
g_eeGeneral.switchConfig = switchGetDefaultConfig();
#if defined(STICK_DEAD_ZONE)
g_eeGeneral.stickDeadZone = DEFAULT_STICK_DEADZONE;
#endif
// vBatWarn is voltage in 100mV, vBatMin is in 100mV but with -9V offset,
// vBatMax has a -12V offset
g_eeGeneral.vBatWarn = BATTERY_WARN;
if (BATTERY_MIN != 90)
g_eeGeneral.vBatMin = BATTERY_MIN - 90;
if (BATTERY_MAX != 120)
g_eeGeneral.vBatMax = BATTERY_MAX - 120;
#if defined(SURFACE_RADIO)
g_eeGeneral.stickMode = 0;
g_eeGeneral.templateSetup = 0;
#elif defined(DEFAULT_MODE)
g_eeGeneral.stickMode = DEFAULT_MODE - 1;
g_eeGeneral.templateSetup = DEFAULT_TEMPLATE_SETUP;
#endif
g_eeGeneral.backlightMode = e_backlight_mode_all;
g_eeGeneral.lightAutoOff = 2;
g_eeGeneral.inactivityTimer = 10;
g_eeGeneral.ttsLanguage[0] = 'e';
g_eeGeneral.ttsLanguage[1] = 'n';
g_eeGeneral.wavVolume = 2;
g_eeGeneral.backgroundVolume = 1;
auto controls = adcGetMaxInputs(ADC_INPUT_MAIN);
for (int i = 0; i < controls; ++i) {
g_eeGeneral.trainer.mix[i].mode = 2;
g_eeGeneral.trainer.mix[i].srcChn = inputMappingChannelOrder(i);
g_eeGeneral.trainer.mix[i].studWeight = 100;
}
#if defined(PCBX9E)
const int8_t defaultName[] = { 20, -1, -18, -1, -14, -9, -19 };
memcpy(g_eeGeneral.bluetoothName, defaultName, sizeof(defaultName));
#endif
#if defined(STORAGE_MODELSLIST)
strcpy(g_eeGeneral.currModelFilename, DEFAULT_MODEL_FILENAME);
#endif
#if defined(PXX2)
setDefaultOwnerId();
#endif
#if defined(RADIOMASTER_RTF_RELEASE)
// Those settings are for headless radio
g_eeGeneral.USBMode = USB_JOYSTICK_MODE;
g_eeGeneral.disableRtcWarning = 1;
g_eeGeneral.splashMode = 3; // Disable splash
g_eeGeneral.pwrOnSpeed = 1; // 1 second
#endif
#if defined(IFLIGHT_RELEASE)
g_eeGeneral.splashMode = 3;
g_eeGeneral.pwrOnSpeed = 2;
g_eeGeneral.pwrOffSpeed = 2;
#endif
#if defined(MANUFACTURER_RADIOMASTER)
g_eeGeneral.audioMuteEnable = 1;
#endif
// disable Custom Script
g_eeGeneral.modelCustomScriptsDisabled = true;
g_eeGeneral.hatsMode = HATSMODE_SWITCHABLE;
g_eeGeneral.chkSum = 0xFFFF;
}
uint16_t evalChkSum()
{
uint16_t sum = 0;
auto main_calib_bytes = adcGetMaxInputs(ADC_INPUT_MAIN) * sizeof(CalibData);
const uint8_t *calibValues = (const uint8_t *)&g_eeGeneral.calib[0];
for (unsigned i = 0; i < main_calib_bytes; i++) {
sum += calibValues[i];
}
return sum;
}
bool isInputRecursive(int index)
{
ExpoData * line = expoAddress(0);
for (int i=0; i<MAX_EXPOS; i++, line++) {
if (line->chn > index)
break;
else if (line->chn < index)
continue;
else if (line->srcRaw >= MIXSRC_FIRST_LOGICAL_SWITCH)
return true;
}
return false;
}
#if defined(AUTOSOURCE)
constexpr int MULTIPOS_STEP_SIZE = (2 * RESX) / XPOTS_MULTIPOS_COUNT;
int8_t getMovedSource(uint8_t min)
{
int8_t result = 0;
static tmr10ms_t s_move_last_time = 0;
static int16_t inputsStates[MAX_INPUTS];
if (min <= MIXSRC_FIRST_INPUT) {
for (uint8_t i = 0; i < MAX_INPUTS; i++) {
if (abs(anas[i] - inputsStates[i]) > MULTIPOS_STEP_SIZE) {
if (!isInputRecursive(i)) {
result = MIXSRC_FIRST_INPUT + i;
break;
}
}
}
}
static int16_t sourcesStates[MAX_ANALOG_INPUTS];
if (result == 0) {
for (uint8_t i = 0; i < MAX_ANALOG_INPUTS; i++) {
if (abs(calibratedAnalogs[i] - sourcesStates[i]) > MULTIPOS_STEP_SIZE) {
auto offset = adcGetInputOffset(ADC_INPUT_FLEX);
if (i >= offset) {
result = MIXSRC_FIRST_POT + i - offset;
break;
}
result = MIXSRC_FIRST_STICK + inputMappingConvertMode(i);
break;
}
}
}
bool recent = ((tmr10ms_t)(get_tmr10ms() - s_move_last_time) > 10);
if (recent) {
result = 0;
}
if (result || recent) {
memcpy(inputsStates, anas, sizeof(inputsStates));
memcpy(sourcesStates, calibratedAnalogs, sizeof(sourcesStates));
}
s_move_last_time = get_tmr10ms();
return result;
}
#endif
#if defined(FLIGHT_MODES)
uint8_t getFlightMode()
{
for (uint8_t i=1; i<MAX_FLIGHT_MODES; i++) {
FlightModeData *phase = &g_model.flightModeData[i];
if (phase->swtch && getSwitch(phase->swtch)) {
return i;
}
}
return 0;
}
#endif
trim_t getRawTrimValue(uint8_t phase, uint8_t idx)
{
FlightModeData * p = flightModeAddress(phase);
return p->trim[idx];
}
int getTrimValue(uint8_t phase, uint8_t idx)
{
int result = 0;
for (uint8_t i=0; i<MAX_FLIGHT_MODES; i++) {
trim_t v = getRawTrimValue(phase, idx);
if (v.mode == TRIM_MODE_NONE || v.mode == TRIM_MODE_3POS) {
return result;
}
else {
unsigned int p = v.mode >> 1;
if (p == phase || phase == 0) {
return result + v.value;
}
else {
phase = p;
if (v.mode % 2 != 0) {
result += v.value;
}
}
}
}
return 0;
}
bool setTrimValue(uint8_t phase, uint8_t idx, int trim)
{
for (uint8_t i=0; i<MAX_FLIGHT_MODES; i++) {
trim_t & v = flightModeAddress(phase)->trim[idx];
if (v.mode == TRIM_MODE_NONE || v.mode == TRIM_MODE_3POS)
return false;
unsigned int p = v.mode >> 1;
if (p == phase || phase == 0) {
v.value = trim;
break;
}
else if (v.mode % 2 == 0) {
phase = p;
}
else {
v.value = limit<int>(TRIM_EXTENDED_MIN, trim - getTrimValue(p, idx), TRIM_EXTENDED_MAX);
break;
}
}
storageDirty(EE_MODEL);
return true;
}
getvalue_t convert16bitsTelemValue(source_t channel, ls_telemetry_value_t value)
{
return value;
}
ls_telemetry_value_t maxTelemValue(source_t channel)
{
return 30000;
}
void checkBacklight()
{
static uint8_t tmr10ms ;
uint8_t x = g_blinkTmr10ms;
if (tmr10ms != x) {
tmr10ms = x;
if (inactivityCheckInputs()) {
inactivityTimerReset(ActivitySource::MainControls);
}
if (requiredBacklightBright == BACKLIGHT_FORCED_ON) {
currentBacklightBright = g_eeGeneral.getBrightness();
BACKLIGHT_ENABLE();
} else {
bool backlightOn = ((g_eeGeneral.backlightMode == e_backlight_mode_on) ||
(g_eeGeneral.backlightMode != e_backlight_mode_off &&
lightOffCounter) ||
(g_eeGeneral.backlightMode == e_backlight_mode_off &&
isFunctionActive(FUNCTION_BACKLIGHT)));
if (flashCounter) {
backlightOn = !backlightOn;
}
if (backlightOn) {
currentBacklightBright = requiredBacklightBright;
BACKLIGHT_ENABLE();
} else {
BACKLIGHT_DISABLE();
}
}
}
}
void resetBacklightTimeout()
{
uint16_t autoOff = g_eeGeneral.lightAutoOff;
#if defined(COLORLCD)
// prevent the timeout from being 0 seconds on color lcd radios
autoOff = std::max<uint16_t>(1, autoOff);
#endif
lightOffCounter = (autoOff*250) << 1;
}
#if defined(MULTIMODULE)
void checkMultiLowPower()
{
bool low_power_warning = false;
for (uint8_t i = 0; i < MAX_MODULES; i++) {
if (isModuleMultimodule(i) &&
g_model.moduleData[i].multi.lowPowerMode) {
low_power_warning = true;
}
}
if (low_power_warning) {
ALERT("MULTI", STR_WARN_MULTI_LOWPOWER, AU_ERROR);
}
}
#endif
static void checkRTCBattery()
{
if (!mixerTaskRunning()) getADC();
if (getRTCBatteryVoltage() < 200) {
ALERT(STR_BATTERY, STR_WARN_RTC_BATTERY_LOW, AU_ERROR);
}
}
void checkSDfreeStorage() {
if(sdIsFull()) {
ALERT(STR_SD_CARD, STR_SDCARD_FULL, AU_ERROR);
}
}
#if defined(PCBFRSKY) || defined(PCBFLYSKY)
static void checkFailsafe()
{
for (int i=0; i<NUM_MODULES; i++) {
#if defined(MULTIMODULE)
// use delayed check for MPM
if (isModuleMultimodule(i)) break;
#endif
if (isModuleFailsafeAvailable(i)) {
ModuleData & moduleData = g_model.moduleData[i];
if (moduleData.failsafeMode == FAILSAFE_NOT_SET) {
ALERT(STR_FAILSAFEWARN, STR_NO_FAILSAFE, AU_ERROR);
break;
}
}
}
}
#else
#define checkFailsafe()
#endif
#if defined(GUI)
void checkAll()
{
#if defined(EEPROM_RLC) && !defined(SDCARD_RAW) && !defined(SDCARD_YAML)
checkLowEEPROM();
#endif
checkSDfreeStorage();
// we don't check the throttle stick if the radio is not calibrated
if (g_eeGeneral.chkSum == evalChkSum()) {
checkThrottleStick();
}
checkSwitches();
checkFailsafe();
if (isVBatBridgeEnabled() && !g_eeGeneral.disableRtcWarning) {
// only done once at board start
checkRTCBattery();
}
disableVBatBridge();
if (g_model.displayChecklist && modelHasNotes()) {
readModelNotes();
}
#if defined(MULTIMODULE)
checkMultiLowPower();
#endif
#if defined(COLORLCD)
if (!waitKeysReleased()) {
auto dlg = new FullScreenDialog(WARNING_TYPE_ALERT, STR_KEYSTUCK);
LED_ERROR_BEGIN();
AUDIO_ERROR_MESSAGE(AU_ERROR);
tmr10ms_t tgtime = get_tmr10ms() + 500;
uint32_t keys = readKeys();
std::string strKeys;
for (int i = 0; i < (int)MAX_KEYS; i++) {
if (keys & (1 << i)) {
strKeys += std::string(keysGetLabel(EnumKeys(i)));
}
}
dlg->setMessage(strKeys.c_str());
dlg->setCloseCondition([tgtime]() {
if (tgtime >= get_tmr10ms() && keyDown()) {
return false;
} else {
return true;
}
});
dlg->runForever();
LED_ERROR_END();
}
#else
if (!waitKeysReleased()) {
showMessageBox(STR_KEYSTUCK);
tmr10ms_t tgtime = get_tmr10ms() + 500;
while (tgtime != get_tmr10ms()) {
RTOS_WAIT_MS(1);
WDG_RESET();
}
}
#endif
#if defined(EXTERNAL_ANTENNA) && defined(INTERNAL_MODULE_PXX1)
checkExternalAntenna();
#endif
START_SILENCE_PERIOD();
}
#endif // GUI
#if defined(EEPROM_RLC) && !defined(SDCARD_RAW) && !defined(SDCARD_YAML)
void checkLowEEPROM()
{
if (g_eeGeneral.disableMemoryWarning) return;
if (EeFsGetFree() < 100) {
ALERT(STR_STORAGE_WARNING, STR_EEPROMLOWMEM, AU_ERROR);
}
}
#endif
bool isThrottleWarningAlertNeeded()
{
if (g_model.disableThrottleWarning) {
return false;
}
uint8_t thr_src = throttleSource2Source(g_model.thrTraceSrc);
// in case an output channel is choosen as throttle source
// we assume the throttle stick is the input (no computed channels yet)
if (thr_src >= MIXSRC_FIRST_CH) {
thr_src = throttleSource2Source(0);
}
if (!mixerTaskRunning()) getADC();
evalInputs(e_perout_mode_notrainer); // let do evalInputs do the job
int16_t v = getValue(thr_src);
// TODO: this looks fishy....
if (g_model.thrTraceSrc && g_model.throttleReversed) {
v = -v;
}
if (g_model.enableCustomThrottleWarning) {
int16_t idleValue = (int32_t)RESX *
(int32_t)g_model.customThrottleWarningPosition /
(int32_t)100;
return abs(v - idleValue) > THRCHK_DEADBAND;
} else {
#if defined(SURFACE_RADIO) // surface radio, stick centered
return v > THRCHK_DEADBAND;
#else
return v > THRCHK_DEADBAND - RESX;
#endif
}
}
#if defined(COLORLCD)
void checkThrottleStick()
{
char throttleNotIdle[strlen(STR_THROTTLE_NOT_IDLE) + 9];
if (isThrottleWarningAlertNeeded()) {
if (g_model.enableCustomThrottleWarning) {
sprintf(throttleNotIdle, "%s (%d%%)", STR_THROTTLE_NOT_IDLE, g_model.customThrottleWarningPosition);
}
else {
strcpy(throttleNotIdle, STR_THROTTLE_NOT_IDLE);
}
LED_ERROR_BEGIN();
auto dialog = new ThrottleWarnDialog(throttleNotIdle);
dialog->runForever();
}
LED_ERROR_END();
}
#else
void checkThrottleStick()
{
char throttleNotIdle[strlen(STR_THROTTLE_NOT_IDLE) + 9];
if (!isThrottleWarningAlertNeeded()) {
return;
}
if (g_model.enableCustomThrottleWarning) {
sprintf(throttleNotIdle, "%s (%d%%)", STR_THROTTLE_NOT_IDLE, g_model.customThrottleWarningPosition);
}
else {
strcpy(throttleNotIdle, STR_THROTTLE_NOT_IDLE);
}
// first - display warning; also deletes inputs if any have been before
LED_ERROR_BEGIN();
RAISE_ALERT(TR_THROTTLE_UPPERCASE, throttleNotIdle, STR_PRESS_ANY_KEY_TO_SKIP, AU_THROTTLE_ALERT);
#if defined(PWR_BUTTON_PRESS)
bool refresh = false;
#endif
while (!keyDown()) {
if (!isThrottleWarningAlertNeeded()) {
return;
}
#if defined(PWR_BUTTON_PRESS)
uint32_t power = pwrCheck();
if (power == e_power_off) {
drawSleepBitmap();
boardOff();
break;
}
else if (power == e_power_press) {
refresh = true;
}
else if (power == e_power_on && refresh) {
RAISE_ALERT(TR_THROTTLE_UPPERCASE, throttleNotIdle, STR_PRESS_ANY_KEY_TO_SKIP, AU_NONE);
refresh = false;
}
#else
if (pwrCheck() == e_power_off) {
break;
}
#endif
checkBacklight();
WDG_RESET();
RTOS_WAIT_MS(10);
}
LED_ERROR_END();
}
#endif
void checkAlarm() // added by Gohst
{
if (g_eeGeneral.disableAlarmWarning) {
return;
}
if (IS_SOUND_OFF()) {
ALERT(STR_ALARMSWARN, STR_ALARMSDISABLED, AU_ERROR);
}
}
void alert(const char * title, const char * msg , uint8_t sound)
{
LED_ERROR_BEGIN();
TRACE("ALERT %s: %s", title, msg);
RAISE_ALERT(title, msg, STR_PRESSANYKEY, sound);
#if defined(PWR_BUTTON_PRESS)
bool refresh = false;
#endif
while (true) {
RTOS_WAIT_MS(10);
if (getEvent()) // wait for key release
break;
checkBacklight();
WDG_RESET();
const uint32_t pwr_check = pwrCheck();
if (pwr_check == e_power_off) {
drawSleepBitmap();
boardOff();
return; // only happens in SIMU, required for proper shutdown
}
#if defined(PWR_BUTTON_PRESS)
else if (pwr_check == e_power_press) {
refresh = true;
}
else if (pwr_check == e_power_on && refresh) {
RAISE_ALERT(title, msg, STR_PRESSANYKEY, AU_NONE);
refresh = false;
}
#endif
}
LED_ERROR_END();
}
#if defined(GVARS)
#if MAX_TRIMS == 8
int8_t trimGvar[MAX_TRIMS] = { -1, -1, -1, -1, -1, -1, -1, -1 };
#elif MAX_TRIMS == 6
int8_t trimGvar[MAX_TRIMS] = { -1, -1, -1, -1, -1, -1 };
#elif MAX_TRIMS == 4
int8_t trimGvar[MAX_TRIMS] = { -1, -1, -1, -1 };
#elif MAX_TRIMS == 2
int8_t trimGvar[MAX_TRIMS] = { -1, -1 };
#endif
#endif
void checkTrims()
{
event_t event = getTrimEvent();
if (event && !IS_KEY_BREAK(event)) {
int8_t k = EVT_KEY_MASK(event);
uint8_t idx = inputMappingConvertMode(uint8_t(k / 2));
uint8_t phase;
int before;
bool thro;
trimsDisplayTimer = 200; // 2 seconds
trimsDisplayMask |= (1<<idx);
#if defined(GVARS)
if (TRIM_REUSED(idx)) {
phase = getGVarFlightMode(mixerCurrentFlightMode, trimGvar[idx]);
before = GVAR_VALUE(trimGvar[idx], phase);
thro = false;
}
else {
phase = getTrimFlightMode(mixerCurrentFlightMode, idx);
before = getTrimValue(phase, idx);
thro = (idx == (g_model.getThrottleStickTrimSource() - MIXSRC_FIRST_TRIM) && g_model.thrTrim);
}
#else
phase = getTrimFlightMode(mixerCurrentFlightMode, idx);
before = getTrimValue(phase, idx);
thro = (idx==inputMappingConvertMode(inputMappingGetThrottle()) && g_model.thrTrim);
#endif
int8_t trimInc = g_model.trimInc + 1;
int8_t v = (trimInc==-1) ? min(32, abs(before)/4+1) : (1 << trimInc); // TODO flash saving if (trimInc < 0)
if (thro) v = 4; // if throttle trim and trim throttle then step=4
#if defined(GVARS)
if (TRIM_REUSED(idx)) v = 1;
#endif
int16_t after = (k&1) ? before + v : before - v; // positive = k&1
bool beepTrim = true;
if (!thro && before!=0 && ((!(after < 0) == (before < 0)) || after==0)) { //forcing a stop at centered trim when changing sides
after = 0;
beepTrim = true;
AUDIO_TRIM_MIDDLE();
pauseTrimEvents(event);
}
#if defined(GVARS)
if (TRIM_REUSED(idx)) {
int8_t gvar = trimGvar[idx];
int16_t vmin = GVAR_MIN + g_model.gvars[gvar].min;
int16_t vmax = GVAR_MAX - g_model.gvars[gvar].max;
if (after < vmin) {
after = vmin;
beepTrim = false;
AUDIO_TRIM_MIN();
killTrimEvents(event);
}
else if (after > vmax) {
after = vmax;
beepTrim = false;
AUDIO_TRIM_MAX();
killTrimEvents(event);
}
SET_GVAR_VALUE(gvar, phase, after);
}
else
#endif
{
// Determine Max and Min trim values based on Extended Trim setting
int16_t tMax = g_model.extendedTrims ? TRIM_EXTENDED_MAX : TRIM_MAX;
int16_t tMin = g_model.extendedTrims ? TRIM_EXTENDED_MIN : TRIM_MIN;
// Play warning whe going past limits and remove any buffered trim moves
if (before >= tMin && after <= tMin) {
beepTrim = false;
AUDIO_TRIM_MIN();
killTrimEvents(event);
}
else if (before <= tMax && after >= tMax) {
beepTrim = false;
AUDIO_TRIM_MAX();
killTrimEvents(event);
}
// If the new value is outside the limit, set it to the limit. This could have
// been done while playing the warning above but this way it catches any other
// scenarios
if (after < tMin) {
after = tMin;
}
else if (after > tMax) {
after = tMax;
}
if (!setTrimValue(phase, idx, after)) {
// we don't play a beep, so we exit now the function
return;
}
}
if (beepTrim) {
AUDIO_TRIM_PRESS(after);
}
}
}
uint8_t g_vbat100mV = 0;
uint16_t lightOffCounter;
uint8_t flashCounter = 0;
uint16_t sessionTimer;
uint16_t s_timeCumThr; // THR in 1/16 sec