forked from iNavFlight/inav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosd.c
6427 lines (5692 loc) · 248 KB
/
osd.c
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
/*
* This file is part of Cleanflight.
*
* Cleanflight 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.
*
* Cleanflight 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 Cleanflight. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Created by Marcin Baliniak
some functions based on MinimOSD
OSD-CMS separation by jflyper
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <inttypes.h>
#include "platform.h"
#ifdef USE_OSD
#include "build/debug.h"
#include "build/version.h"
#include "cms/cms.h"
#include "cms/cms_types.h"
#include "cms/cms_menu_osd.h"
#include "common/axis.h"
#include "common/constants.h"
#include "common/filter.h"
#include "common/log.h"
#include "common/olc.h"
#include "common/printf.h"
#include "common/string_light.h"
#include "common/time.h"
#include "common/typeconversion.h"
#include "common/utils.h"
#include "config/feature.h"
#include "config/parameter_group.h"
#include "config/parameter_group_ids.h"
#include "drivers/display.h"
#include "drivers/display_canvas.h"
#include "drivers/display_font_metadata.h"
#include "drivers/osd_symbols.h"
#include "drivers/time.h"
#include "drivers/vtx_common.h"
#include "drivers/gimbal_common.h"
#include "io/adsb.h"
#include "io/flashfs.h"
#include "io/gps.h"
#include "io/osd.h"
#include "io/osd_common.h"
#include "io/osd_hud.h"
#include "io/osd_utils.h"
#include "io/displayport_msp_dji_compat.h"
#include "io/vtx.h"
#include "io/vtx_string.h"
#include "io/osd/custom_elements.h"
#include "fc/config.h"
#include "fc/controlrate_profile.h"
#include "fc/fc_core.h"
#include "fc/fc_tasks.h"
#include "fc/multifunction.h"
#include "fc/rc_adjustments.h"
#include "fc/rc_controls.h"
#include "fc/rc_modes.h"
#include "fc/runtime_config.h"
#include "fc/settings.h"
#include "flight/imu.h"
#include "flight/mixer.h"
#include "flight/pid.h"
#include "flight/power_limits.h"
#include "flight/rth_estimator.h"
#include "flight/servos.h"
#include "flight/wind_estimator.h"
#include "navigation/navigation.h"
#include "navigation/navigation_private.h"
#include "rx/rx.h"
#include "rx/msp_override.h"
#include "sensors/acceleration.h"
#include "sensors/battery.h"
#include "sensors/boardalignment.h"
#include "sensors/compass.h"
#include "sensors/diagnostics.h"
#include "sensors/sensors.h"
#include "sensors/pitotmeter.h"
#include "sensors/temperature.h"
#include "sensors/esc_sensor.h"
#include "sensors/rangefinder.h"
#include "programming/logic_condition.h"
#include "programming/global_variables.h"
#ifdef USE_BLACKBOX
#include "blackbox/blackbox_io.h"
#endif
#ifdef USE_HARDWARE_REVISION_DETECTION
#include "hardware_revision.h"
#endif
#define VIDEO_BUFFER_CHARS_PAL 480
#define VIDEO_BUFFER_CHARS_HDZERO 900
#define VIDEO_BUFFER_CHARS_DJIWTF 1320
#define GFORCE_FILTER_TC 0.2
#define OSD_STATS_SINGLE_PAGE_MIN_ROWS 18
#define IS_HI(X) (rxGetChannelValue(X) > 1750)
#define IS_LO(X) (rxGetChannelValue(X) < 1250)
#define IS_MID(X) (rxGetChannelValue(X) > 1250 && rxGetChannelValue(X) < 1750)
#define OSD_RESUME_UPDATES_STICK_COMMAND (checkStickPosition(THR_HI) || checkStickPosition(PIT_HI))
#define STATS_PAGE2 (checkStickPosition(ROL_HI))
#define STATS_PAGE1 (checkStickPosition(ROL_LO))
#define SPLASH_SCREEN_DISPLAY_TIME 4000 // ms
#define STATS_SCREEN_DISPLAY_TIME 60000 // ms
#define EFFICIENCY_UPDATE_INTERVAL (5 * 1000)
// Adjust OSD_MESSAGE's default position when
// changing OSD_MESSAGE_LENGTH
#define OSD_MESSAGE_LENGTH 28
#define OSD_ALTERNATING_CHOICES(ms, num_choices) ((millis() / ms) % num_choices)
#define _CONST_STR_SIZE(s) ((sizeof(s)/sizeof(s[0]))-1) // -1 to avoid counting final '\0'
// Wrap all string constants intenteded for display as messages with
// this macro to ensure compile time length validation.
#define OSD_MESSAGE_STR(x) ({ \
STATIC_ASSERT(_CONST_STR_SIZE(x) <= OSD_MESSAGE_LENGTH, message_string_ ## __COUNTER__ ## _too_long); \
x; \
})
#define OSD_CHR_IS_NUM(c) (c >= '0' && c <= '9')
#define OSD_CENTER_LEN(x) ((osdDisplayPort->cols - x) / 2)
#define OSD_CENTER_S(s) OSD_CENTER_LEN(strlen(s))
#define OSD_MIN_FONT_VERSION 3
static timeMs_t linearDescentMessageMs = 0;
static timeMs_t notify_settings_saved = 0;
static bool savingSettings = false;
static unsigned currentLayout = 0;
static int layoutOverride = -1;
static bool hasExtendedFont = false; // Wether the font supports characters > 256
static timeMs_t layoutOverrideUntil = 0;
static pt1Filter_t GForceFilter, GForceFilterAxis[XYZ_AXIS_COUNT];
static float GForce, GForceAxis[XYZ_AXIS_COUNT];
typedef struct statistic_s {
uint16_t max_speed;
uint16_t max_3D_speed;
uint16_t max_air_speed;
uint16_t min_voltage; // /100
int16_t max_current;
int32_t max_power;
uint8_t min_rssi;
int16_t min_lq; // for CRSF
int16_t min_rssi_dbm; // for CRSF
int32_t max_altitude;
uint32_t max_distance;
uint8_t min_sats;
uint8_t max_sats;
int16_t max_esc_temp;
int16_t min_esc_temp;
int32_t flightStartMAh;
int32_t flightStartMWh;
} statistic_t;
static statistic_t stats;
static timeUs_t resumeRefreshAt = 0;
static bool refreshWaitForResumeCmdRelease;
static bool fullRedraw = false;
static uint8_t armState;
static textAttributes_t osdGetMultiFunctionMessage(char *buff);
static uint8_t osdWarningsFlags = 0;
typedef struct osdMapData_s {
uint32_t scale;
char referenceSymbol;
} osdMapData_t;
static osdMapData_t osdMapData;
static displayPort_t *osdDisplayPort;
static bool osdDisplayIsReady = false;
#if defined(USE_CANVAS)
static displayCanvas_t osdCanvas;
static bool osdDisplayHasCanvas;
#else
#define osdDisplayHasCanvas false
#endif
#define AH_MAX_PITCH_DEFAULT 20 // Specify default maximum AHI pitch value displayed (degrees)
PG_REGISTER_WITH_RESET_TEMPLATE(osdConfig_t, osdConfig, PG_OSD_CONFIG, 14);
PG_REGISTER_WITH_RESET_FN(osdLayoutsConfig_t, osdLayoutsConfig, PG_OSD_LAYOUTS_CONFIG, 3);
void osdStartedSaveProcess(void) {
savingSettings = true;
}
void osdShowEEPROMSavedNotification(void) {
savingSettings = false;
notify_settings_saved = millis() + 5000;
}
bool osdDisplayIsPAL(void)
{
return displayScreenSize(osdDisplayPort) == VIDEO_BUFFER_CHARS_PAL;
}
bool osdDisplayIsHD(void)
{
if (displayScreenSize(osdDisplayPort) >= VIDEO_BUFFER_CHARS_HDZERO)
{
return true;
}
return false;
}
bool osdIsNotMetric(void) {
return !(osdConfig()->units == OSD_UNIT_METRIC || osdConfig()->units == OSD_UNIT_METRIC_MPH);
}
/**
* Converts distance into a string based on the current unit system
* prefixed by a a symbol to indicate the unit used.
* @param dist Distance in centimeters
*/
static void osdFormatDistanceSymbol(char *buff, int32_t dist, uint8_t decimals, uint8_t digits)
{
if (digits == 0) // Total number of digits (including decimal point)
digits = 3U;
uint8_t sym_index = digits; // Position (index) at buffer of units symbol
uint8_t symbol_m = SYM_DIST_M;
uint8_t symbol_km = SYM_DIST_KM;
uint8_t symbol_ft = SYM_DIST_FT;
uint8_t symbol_mi = SYM_DIST_MI;
uint8_t symbol_nm = SYM_DIST_NM;
#ifndef DISABLE_MSP_DJI_COMPAT // IF DJICOMPAT is not supported, there's no need to check for it and change the values
if (isDJICompatibleVideoSystem(osdConfig())) {
// Add one digit so up no switch to scaled decimal occurs above 99
digits = 4U;
sym_index = 4U;
// Use altitude symbols on purpose, as it seems distance symbols are not defined in DJICOMPAT mode
symbol_m = SYM_ALT_M;
symbol_km = SYM_ALT_KM;
symbol_ft = SYM_ALT_FT;
symbol_mi = SYM_MI;
symbol_nm = SYM_MI;
}
#endif
switch ((osd_unit_e)osdConfig()->units) {
case OSD_UNIT_UK:
FALLTHROUGH;
case OSD_UNIT_IMPERIAL:
if (osdFormatCentiNumber(buff, CENTIMETERS_TO_CENTIFEET(dist), FEET_PER_MILE, decimals, 3, digits, false)) {
buff[sym_index] = symbol_mi;
} else {
buff[sym_index] = symbol_ft;
}
buff[sym_index + 1] = '\0';
break;
case OSD_UNIT_METRIC_MPH:
FALLTHROUGH;
case OSD_UNIT_METRIC:
if (osdFormatCentiNumber(buff, dist, METERS_PER_KILOMETER, decimals, 3, digits, false)) {
buff[sym_index] = symbol_km;
} else {
buff[sym_index] = symbol_m;
}
buff[sym_index + 1] = '\0';
break;
case OSD_UNIT_GA:
if (osdFormatCentiNumber(buff, CENTIMETERS_TO_CENTIFEET(dist), (uint32_t)FEET_PER_NAUTICALMILE, decimals, 3, digits, false)) {
buff[sym_index] = symbol_nm;
} else {
buff[sym_index] = symbol_ft;
}
buff[sym_index + 1] = '\0';
break;
}
}
/**
* Converts distance into a string based on the current unit system.
* @param dist Distance in centimeters
*/
static void osdFormatDistanceStr(char *buff, int32_t dist)
{
int32_t centifeet;
switch ((osd_unit_e)osdConfig()->units) {
case OSD_UNIT_UK:
FALLTHROUGH;
case OSD_UNIT_IMPERIAL:
centifeet = CENTIMETERS_TO_CENTIFEET(dist);
if (abs(centifeet) < FEET_PER_MILE * 100 / 2) {
// Show feet when dist < 0.5mi
tfp_sprintf(buff, "%d%c", (int)(centifeet / 100), SYM_FT);
} else {
// Show miles when dist >= 0.5mi
tfp_sprintf(buff, "%d.%02d%c", (int)(centifeet / (100*FEET_PER_MILE)),
(abs(centifeet) % (100 * FEET_PER_MILE)) / FEET_PER_MILE, SYM_MI);
}
break;
case OSD_UNIT_METRIC_MPH:
FALLTHROUGH;
case OSD_UNIT_METRIC:
if (abs(dist) < METERS_PER_KILOMETER * 100) {
// Show meters when dist < 1km
tfp_sprintf(buff, "%d%c", (int)(dist / 100), SYM_M);
} else {
// Show kilometers when dist >= 1km
tfp_sprintf(buff, "%d.%02d%c", (int)(dist / (100*METERS_PER_KILOMETER)),
(abs(dist) % (100 * METERS_PER_KILOMETER)) / METERS_PER_KILOMETER, SYM_KM);
}
break;
case OSD_UNIT_GA:
centifeet = CENTIMETERS_TO_CENTIFEET(dist);
if (abs(centifeet) < 100000) {
// Show feet when dist < 1000ft
tfp_sprintf(buff, "%d%c", (int)(centifeet / 100), SYM_FT);
} else {
// Show nautical miles when dist >= 1000ft
tfp_sprintf(buff, "%d.%02d%c", (int)(centifeet / (100 * FEET_PER_NAUTICALMILE)),
(int)((abs(centifeet) % (int)(100 * FEET_PER_NAUTICALMILE)) / FEET_PER_NAUTICALMILE), SYM_NM);
}
break;
}
}
/**
* Converts velocity based on the current unit system (kmh or mph).
* @param alt Raw velocity (i.e. as taken from gpsSol.groundSpeed in centimeters/second)
*/
static int32_t osdConvertVelocityToUnit(int32_t vel)
{
switch ((osd_unit_e)osdConfig()->units) {
case OSD_UNIT_UK:
FALLTHROUGH;
case OSD_UNIT_METRIC_MPH:
FALLTHROUGH;
case OSD_UNIT_IMPERIAL:
return CMSEC_TO_CENTIMPH(vel) / 100; // Convert to mph
case OSD_UNIT_METRIC:
return CMSEC_TO_CENTIKPH(vel) / 100; // Convert to kmh
case OSD_UNIT_GA:
return CMSEC_TO_CENTIKNOTS(vel) / 100; // Convert to Knots
}
// Unreachable
return -1;
}
/**
* Converts velocity into a string based on the current unit system.
* @param vel Raw velocity (i.e. as taken from gpsSol.groundSpeed in centimeters/seconds)
* @param _3D is a 3D velocity
* @param _max is a maximum velocity
*/
void osdFormatVelocityStr(char* buff, int32_t vel, bool _3D, bool _max)
{
switch ((osd_unit_e)osdConfig()->units) {
case OSD_UNIT_UK:
FALLTHROUGH;
case OSD_UNIT_METRIC_MPH:
FALLTHROUGH;
case OSD_UNIT_IMPERIAL:
if (_max) {
tfp_sprintf(buff, "%c%3d%c", SYM_MAX, (int)osdConvertVelocityToUnit(vel), (_3D ? SYM_3D_MPH : SYM_MPH));
} else {
tfp_sprintf(buff, "%3d%c", (int)osdConvertVelocityToUnit(vel), (_3D ? SYM_3D_MPH : SYM_MPH));
}
break;
case OSD_UNIT_METRIC:
if (_max) {
tfp_sprintf(buff, "%c%3d%c", SYM_MAX, (int)osdConvertVelocityToUnit(vel), (_3D ? SYM_3D_KMH : SYM_KMH));
} else {
tfp_sprintf(buff, "%3d%c", (int)osdConvertVelocityToUnit(vel), (_3D ? SYM_3D_KMH : SYM_KMH));
}
break;
case OSD_UNIT_GA:
if (_max) {
tfp_sprintf(buff, "%c%3d%c", SYM_MAX, (int)osdConvertVelocityToUnit(vel), (_3D ? SYM_3D_KT : SYM_KT));
} else {
tfp_sprintf(buff, "%3d%c", (int)osdConvertVelocityToUnit(vel), (_3D ? SYM_3D_KT : SYM_KT));
}
break;
}
}
/**
* Returns the average velocity. This always uses stats, so can be called as an OSD element later if wanted, to show a real time average
*/
static void osdGenerateAverageVelocityStr(char* buff) {
uint32_t cmPerSec = getTotalTravelDistance() / getFlightTime();
osdFormatVelocityStr(buff, cmPerSec, false, false);
}
/**
* Converts wind speed into a string based on the current unit system, using
* always 3 digits and an additional character for the unit at the right. buff
* is null terminated.
* @param ws Raw wind speed in cm/s
*/
#ifdef USE_WIND_ESTIMATOR
static void osdFormatWindSpeedStr(char *buff, int32_t ws, bool isValid)
{
int32_t centivalue;
char suffix;
switch (osdConfig()->units) {
case OSD_UNIT_UK:
FALLTHROUGH;
case OSD_UNIT_METRIC_MPH:
FALLTHROUGH;
case OSD_UNIT_IMPERIAL:
centivalue = CMSEC_TO_CENTIMPH(ws);
suffix = SYM_MPH;
break;
case OSD_UNIT_GA:
centivalue = CMSEC_TO_CENTIKNOTS(ws);
suffix = SYM_KT;
break;
default:
case OSD_UNIT_METRIC:
if (osdConfig()->estimations_wind_mps)
{
centivalue = ws;
suffix = SYM_MS;
}
else
{
centivalue = CMSEC_TO_CENTIKPH(ws);
suffix = SYM_KMH;
}
break;
}
osdFormatCentiNumber(buff, centivalue, 0, 2, 0, 3, false);
if (!isValid && ((millis() / 1000) % 4 < 2))
suffix = '*';
buff[3] = suffix;
buff[4] = '\0';
}
#endif
/**
* Converts altitude into a string based on the current unit system
* prefixed by a a symbol to indicate the unit used.
* @param alt Raw altitude/distance (i.e. as taken from baro.BaroAlt in centimeters)
*/
void osdFormatAltitudeSymbol(char *buff, int32_t alt)
{
uint8_t digits = osdConfig()->decimals_altitude;
uint8_t totalDigits = digits + 1;
uint8_t symbolIndex = digits + 1;
uint8_t symbolKFt = SYM_ALT_KFT;
if (alt >= 0) {
buff[0] = ' ';
}
#ifndef DISABLE_MSP_DJI_COMPAT // IF DJICOMPAT is not supported, there's no need to check for it and change the values
if (isDJICompatibleVideoSystem(osdConfig())) {
totalDigits++;
digits++;
symbolIndex++;
symbolKFt = SYM_ALT_FT;
}
#endif
switch ((osd_unit_e)osdConfig()->units) {
case OSD_UNIT_UK:
FALLTHROUGH;
case OSD_UNIT_GA:
FALLTHROUGH;
case OSD_UNIT_IMPERIAL:
if (osdFormatCentiNumber(buff + totalDigits - digits, CENTIMETERS_TO_CENTIFEET(alt), 1000, 0, 2, digits, false)) {
// Scaled to kft
buff[symbolIndex++] = symbolKFt;
} else {
// Formatted in feet
buff[symbolIndex++] = SYM_ALT_FT;
}
buff[symbolIndex] = '\0';
break;
case OSD_UNIT_METRIC_MPH:
FALLTHROUGH;
case OSD_UNIT_METRIC:
// alt is alredy in cm
if (osdFormatCentiNumber(buff + totalDigits - digits, alt, 1000, 0, 2, digits, false)) {
// Scaled to km
buff[symbolIndex++] = SYM_ALT_KM;
} else {
// Formatted in m
buff[symbolIndex++] = SYM_ALT_M;
}
buff[symbolIndex] = '\0';
break;
}
}
/**
* Converts altitude into a string based on the current unit system.
* @param alt Raw altitude/distance (i.e. as taken from baro.BaroAlt in centimeters)
*/
static void osdFormatAltitudeStr(char *buff, int32_t alt)
{
int32_t value;
switch ((osd_unit_e)osdConfig()->units) {
case OSD_UNIT_UK:
FALLTHROUGH;
case OSD_UNIT_GA:
FALLTHROUGH;
case OSD_UNIT_IMPERIAL:
value = CENTIMETERS_TO_FEET(alt);
tfp_sprintf(buff, "%d%c", (int)value, SYM_FT);
break;
case OSD_UNIT_METRIC_MPH:
FALLTHROUGH;
case OSD_UNIT_METRIC:
value = CENTIMETERS_TO_METERS(alt);
tfp_sprintf(buff, "%d%c", (int)value, SYM_M);
break;
}
}
static void osdFormatTime(char *buff, uint32_t seconds, char sym_m, char sym_h)
{
uint32_t value = seconds;
char sym = sym_m;
// Maximum value we can show in minutes is 99 minutes and 59 seconds
if (seconds > (99 * 60) + 59) {
sym = sym_h;
value = seconds / 60;
}
buff[0] = sym;
tfp_sprintf(buff + 1, "%02d:%02d", (int)(value / 60), (int)(value % 60));
}
static inline void osdFormatOnTime(char *buff)
{
osdFormatTime(buff, micros() / 1000000, SYM_ON_M, SYM_ON_H);
}
static inline void osdFormatFlyTime(char *buff, textAttributes_t *attr)
{
uint32_t seconds = getFlightTime();
osdFormatTime(buff, seconds, SYM_FLY_M, SYM_FLY_H);
if (attr && osdConfig()->time_alarm > 0) {
if (seconds / 60 >= osdConfig()->time_alarm && ARMING_FLAG(ARMED)) {
TEXT_ATTRIBUTES_ADD_BLINK(*attr);
}
}
}
/**
* Trim whitespace from string.
* Used in Stats screen on lines with multiple values.
*/
char *osdFormatTrimWhiteSpace(char *buff)
{
char *end;
// Trim leading spaces
while(isspace((unsigned char)*buff)) buff++;
// All spaces?
if(*buff == 0)
return buff;
// Trim trailing spaces
end = buff + strlen(buff) - 1;
while(end > buff && isspace((unsigned char)*end)) end--;
// Write new null terminator character
end[1] = '\0';
return buff;
}
/**
* Converts RSSI into a % value used by the OSD.
* Range is [0, 100]
*/
static uint8_t osdConvertRSSI(void)
{
return constrain(getRSSI() * 100 / RSSI_MAX_VALUE, 0, 100);
}
static uint16_t osdGetCrsfLQ(void)
{
int16_t statsLQ = rxLinkStatistics.uplinkLQ;
int16_t scaledLQ = scaleRange(constrain(statsLQ, 0, 100), 0, 100, 170, 300);
int16_t displayedLQ = 0;
switch (osdConfig()->crsf_lq_format) {
case OSD_CRSF_LQ_TYPE1:
displayedLQ = statsLQ;
break;
case OSD_CRSF_LQ_TYPE2:
displayedLQ = statsLQ;
break;
case OSD_CRSF_LQ_TYPE3:
displayedLQ = rxLinkStatistics.rfMode >= 2 ? scaledLQ : statsLQ;
break;
}
return displayedLQ;
}
static int16_t osdGetCrsfdBm(void)
{
return rxLinkStatistics.uplinkRSSI;
}
/**
* Displays a temperature postfixed with a symbol depending on the current unit system
* @param label to display
* @param valid true if measurement is valid
* @param temperature in deciDegrees Celcius
*/
static void osdDisplayTemperature(uint8_t elemPosX, uint8_t elemPosY, uint16_t symbol, const char *label, bool valid, int16_t temperature, int16_t alarm_min, int16_t alarm_max)
{
char buff[TEMPERATURE_LABEL_LEN + 2 < 6 ? 6 : TEMPERATURE_LABEL_LEN + 2];
textAttributes_t elemAttr = valid ? TEXT_ATTRIBUTES_NONE : _TEXT_ATTRIBUTES_BLINK_BIT;
uint8_t valueXOffset = 0;
if (symbol) {
buff[0] = symbol;
buff[1] = '\0';
displayWriteWithAttr(osdDisplayPort, elemPosX, elemPosY, buff, elemAttr);
valueXOffset = 1;
}
#ifdef USE_TEMPERATURE_SENSOR
else if (label[0] != '\0') {
uint8_t label_len = strnlen(label, TEMPERATURE_LABEL_LEN);
memcpy(buff, label, label_len);
memset(buff + label_len, ' ', TEMPERATURE_LABEL_LEN + 1 - label_len);
buff[5] = '\0';
displayWriteWithAttr(osdDisplayPort, elemPosX, elemPosY, buff, elemAttr);
valueXOffset = osdConfig()->temp_label_align == OSD_ALIGN_LEFT ? 5 : label_len + 1;
}
#else
UNUSED(label);
#endif
if (valid) {
if ((temperature <= alarm_min) || (temperature >= alarm_max)) TEXT_ATTRIBUTES_ADD_BLINK(elemAttr);
if (osdConfig()->units == OSD_UNIT_IMPERIAL) temperature = temperature * 9 / 5.0f + 320;
tfp_sprintf(buff, "%3d", temperature / 10);
} else
strcpy(buff, "---");
buff[3] = osdConfig()->units == OSD_UNIT_IMPERIAL ? SYM_TEMP_F : SYM_TEMP_C;
buff[4] = '\0';
displayWriteWithAttr(osdDisplayPort, elemPosX + valueXOffset, elemPosY, buff, elemAttr);
}
#ifdef USE_TEMPERATURE_SENSOR
static void osdDisplayTemperatureSensor(uint8_t elemPosX, uint8_t elemPosY, uint8_t sensorIndex)
{
int16_t temperature;
const bool valid = getSensorTemperature(sensorIndex, &temperature);
const tempSensorConfig_t *sensorConfig = tempSensorConfig(sensorIndex);
uint16_t symbol = sensorConfig->osdSymbol ? SYM_TEMP_SENSOR_FIRST + sensorConfig->osdSymbol - 1 : 0;
osdDisplayTemperature(elemPosX, elemPosY, symbol, sensorConfig->label, valid, temperature, sensorConfig->alarm_min, sensorConfig->alarm_max);
}
#endif
static void osdFormatCoordinate(char *buff, char sym, int32_t val)
{
// up to 4 for number + 1 for the symbol + null terminator + fill the rest with decimals
const int coordinateLength = osdConfig()->coordinate_digits + 1;
buff[0] = sym;
int32_t integerPart = val / GPS_DEGREES_DIVIDER;
// Latitude maximum integer width is 3 (-90) while
// longitude maximum integer width is 4 (-180).
int integerDigits = tfp_sprintf(buff + 1, (integerPart == 0 && val < 0) ? "-%d" : "%d", (int)integerPart);
// We can show up to 7 digits in decimalPart.
int32_t decimalPart = abs(val % (int)GPS_DEGREES_DIVIDER);
STATIC_ASSERT(GPS_DEGREES_DIVIDER == 1e7, adjust_max_decimal_digits);
int decimalDigits;
bool djiCompat = false; // Assume DJICOMPAT mode is no enabled
#ifndef DISABLE_MSP_DJI_COMPAT // IF DJICOMPAT is not supported, there's no need to check for it
if(isDJICompatibleVideoSystem(osdConfig())) {
djiCompat = true;
}
#endif
if (!djiCompat) {
decimalDigits = tfp_sprintf(buff + 1 + integerDigits, "%07d", (int)decimalPart);
// Embbed the decimal separator
buff[1 + integerDigits - 1] += SYM_ZERO_HALF_TRAILING_DOT - '0';
buff[1 + integerDigits] += SYM_ZERO_HALF_LEADING_DOT - '0';
} else {
// DJICOMPAT mode enabled
decimalDigits = tfp_sprintf(buff + 1 + integerDigits, ".%06d", (int)decimalPart);
}
// Fill up to coordinateLength with zeros
int total = 1 + integerDigits + decimalDigits;
while(total < coordinateLength) {
buff[total] = '0';
total++;
}
buff[coordinateLength] = '\0';
}
static void osdFormatCraftName(char *buff)
{
if (strlen(systemConfig()->craftName) == 0)
strcpy(buff, "CRAFT_NAME");
else {
for (int i = 0; i < MAX_NAME_LENGTH; i++) {
buff[i] = sl_toupper((unsigned char)systemConfig()->craftName[i]);
if (systemConfig()->craftName[i] == 0)
break;
}
}
}
void osdFormatPilotName(char *buff)
{
if (strlen(systemConfig()->pilotName) == 0)
strcpy(buff, "PILOT_NAME");
else {
for (int i = 0; i < MAX_NAME_LENGTH; i++) {
buff[i] = sl_toupper((unsigned char)systemConfig()->pilotName[i]);
if (systemConfig()->pilotName[i] == 0)
break;
}
}
}
static const char * osdArmingDisabledReasonMessage(void)
{
const char *message = NULL;
static char messageBuf[OSD_MESSAGE_LENGTH+1];
switch (isArmingDisabledReason()) {
case ARMING_DISABLED_FAILSAFE_SYSTEM:
// See handling of FAILSAFE_RX_LOSS_MONITORING in failsafe.c
if (failsafePhase() == FAILSAFE_RX_LOSS_MONITORING) {
if (failsafeIsReceivingRxData()) {
// reminder to disarm to exit FAILSAFE_RX_LOSS_MONITORING once timeout period ends
if (IS_RC_MODE_ACTIVE(BOXARM)) {
return OSD_MESSAGE_STR(OSD_MSG_TURN_ARM_SW_OFF);
}
} else {
// Not receiving RX data
return OSD_MESSAGE_STR(OSD_MSG_RC_RX_LINK_LOST);
}
}
return OSD_MESSAGE_STR(OSD_MSG_DISABLED_BY_FS);
case ARMING_DISABLED_NOT_LEVEL:
return OSD_MESSAGE_STR(OSD_MSG_AIRCRAFT_UNLEVEL);
case ARMING_DISABLED_SENSORS_CALIBRATING:
return OSD_MESSAGE_STR(OSD_MSG_SENSORS_CAL);
case ARMING_DISABLED_SYSTEM_OVERLOADED:
return OSD_MESSAGE_STR(OSD_MSG_SYS_OVERLOADED);
case ARMING_DISABLED_NAVIGATION_UNSAFE:
// Check the exact reason
switch (navigationIsBlockingArming(NULL)) {
char buf[6];
case NAV_ARMING_BLOCKER_NONE:
break;
case NAV_ARMING_BLOCKER_MISSING_GPS_FIX:
return OSD_MESSAGE_STR(OSD_MSG_WAITING_GPS_FIX);
case NAV_ARMING_BLOCKER_NAV_IS_ALREADY_ACTIVE:
return OSD_MESSAGE_STR(OSD_MSG_DISABLE_NAV_FIRST);
case NAV_ARMING_BLOCKER_FIRST_WAYPOINT_TOO_FAR:
osdFormatDistanceSymbol(buf, distanceToFirstWP(), 0, 3);
tfp_sprintf(messageBuf, "FIRST WP TOO FAR (%s)", buf);
return message = messageBuf;
case NAV_ARMING_BLOCKER_JUMP_WAYPOINT_ERROR:
return OSD_MESSAGE_STR(OSD_MSG_JUMP_WP_MISCONFIG);
}
break;
case ARMING_DISABLED_COMPASS_NOT_CALIBRATED:
return OSD_MESSAGE_STR(OSD_MSG_MAG_NOT_CAL);
case ARMING_DISABLED_ACCELEROMETER_NOT_CALIBRATED:
return OSD_MESSAGE_STR(OSD_MSG_ACC_NOT_CAL);
case ARMING_DISABLED_ARM_SWITCH:
return OSD_MESSAGE_STR(OSD_MSG_DISARM_1ST);
case ARMING_DISABLED_HARDWARE_FAILURE:
{
if (!HW_SENSOR_IS_HEALTHY(getHwGyroStatus())) {
return OSD_MESSAGE_STR(OSD_MSG_GYRO_FAILURE);
}
if (!HW_SENSOR_IS_HEALTHY(getHwAccelerometerStatus())) {
return OSD_MESSAGE_STR(OSD_MSG_ACC_FAIL);
}
if (!HW_SENSOR_IS_HEALTHY(getHwCompassStatus())) {
return OSD_MESSAGE_STR(OSD_MSG_MAG_FAIL);
}
if (!HW_SENSOR_IS_HEALTHY(getHwBarometerStatus())) {
return OSD_MESSAGE_STR(OSD_MSG_BARO_FAIL);
}
if (!HW_SENSOR_IS_HEALTHY(getHwGPSStatus())) {
return OSD_MESSAGE_STR(OSD_MSG_GPS_FAIL);
}
if (!HW_SENSOR_IS_HEALTHY(getHwRangefinderStatus())) {
return OSD_MESSAGE_STR(OSD_MSG_RANGEFINDER_FAIL);
}
if (!HW_SENSOR_IS_HEALTHY(getHwPitotmeterStatus())) {
return OSD_MESSAGE_STR(OSD_MSG_PITOT_FAIL);
}
}
return OSD_MESSAGE_STR(OSD_MSG_HW_FAIL);
case ARMING_DISABLED_BOXFAILSAFE:
return OSD_MESSAGE_STR(OSD_MSG_FS_EN);
case ARMING_DISABLED_RC_LINK:
return OSD_MESSAGE_STR(OSD_MSG_NO_RC_LINK);
case ARMING_DISABLED_THROTTLE:
return OSD_MESSAGE_STR(OSD_MSG_THROTTLE_NOT_LOW);
case ARMING_DISABLED_ROLLPITCH_NOT_CENTERED:
return OSD_MESSAGE_STR(OSD_MSG_ROLLPITCH_OFFCENTER);
case ARMING_DISABLED_SERVO_AUTOTRIM:
return OSD_MESSAGE_STR(OSD_MSG_AUTOTRIM_ACTIVE);
case ARMING_DISABLED_OOM:
return OSD_MESSAGE_STR(OSD_MSG_NOT_ENOUGH_MEMORY);
case ARMING_DISABLED_INVALID_SETTING:
return OSD_MESSAGE_STR(OSD_MSG_INVALID_SETTING);
case ARMING_DISABLED_CLI:
return OSD_MESSAGE_STR(OSD_MSG_CLI_ACTIVE);
case ARMING_DISABLED_PWM_OUTPUT_ERROR:
return OSD_MESSAGE_STR(OSD_MSG_PWM_INIT_ERROR);
case ARMING_DISABLED_NO_PREARM:
return OSD_MESSAGE_STR(OSD_MSG_NO_PREARM);
case ARMING_DISABLED_DSHOT_BEEPER:
return OSD_MESSAGE_STR(OSD_MSG_DSHOT_BEEPER);
case ARMING_DISABLED_GEOZONE:
#ifdef USE_GEOZONE
return OSD_MESSAGE_STR(OSD_MSG_NFZ);
#else
FALLTHROUGH;
#endif
// Cases without message
case ARMING_DISABLED_LANDING_DETECTED:
FALLTHROUGH;
case ARMING_DISABLED_CMS_MENU:
FALLTHROUGH;
case ARMING_DISABLED_OSD_MENU:
FALLTHROUGH;
case ARMING_DISABLED_ALL_FLAGS:
FALLTHROUGH;
case ARMED:
FALLTHROUGH;
case SIMULATOR_MODE_HITL:
FALLTHROUGH;
case SIMULATOR_MODE_SITL:
FALLTHROUGH;
case WAS_EVER_ARMED:
break;
}
return NULL;
}
static const char * osdFailsafePhaseMessage(void)
{
// See failsafe.h for each phase explanation
switch (failsafePhase()) {
case FAILSAFE_RETURN_TO_HOME:
// XXX: Keep this in sync with OSD_FLYMODE.
return OSD_MESSAGE_STR(OSD_MSG_RTH_FS);
case FAILSAFE_LANDING:
// This should be considered an emergengy landing
return OSD_MESSAGE_STR(OSD_MSG_EMERG_LANDING_FS);
case FAILSAFE_RX_LOSS_MONITORING:
// Only reachable from FAILSAFE_LANDED, which performs
// a disarm. Since aircraft has been disarmed, we no
// longer show failsafe details.
FALLTHROUGH;
case FAILSAFE_LANDED:
// Very brief, disarms and transitions into
// FAILSAFE_RX_LOSS_MONITORING. Note that it prevents
// further rearming via ARMING_DISABLED_FAILSAFE_SYSTEM,
// so we'll show the user how to re-arm in when
// that flag is the reason to prevent arming.
FALLTHROUGH;
case FAILSAFE_RX_LOSS_IDLE:
// This only happens when user has chosen NONE as FS
// procedure. The recovery messages should be enough.
FALLTHROUGH;
case FAILSAFE_IDLE:
// Failsafe not active
FALLTHROUGH;
case FAILSAFE_RX_LOSS_DETECTED:
// Very brief, changes to FAILSAFE_RX_LOSS_RECOVERED
// or the FS procedure immediately.
FALLTHROUGH;
case FAILSAFE_RX_LOSS_RECOVERED:
// Exiting failsafe
break;
}
return NULL;
}
static const char * osdFailsafeInfoMessage(void)
{
if (failsafeIsReceivingRxData() && !FLIGHT_MODE(NAV_FW_AUTOLAND)) {
// User must move sticks to exit FS mode
return OSD_MESSAGE_STR(OSD_MSG_MOVE_EXIT_FS);
}
return OSD_MESSAGE_STR(OSD_MSG_RC_RX_LINK_LOST);
}
#if defined(USE_SAFE_HOME)
static const char * divertingToSafehomeMessage(void)
{
#ifdef USE_FW_AUTOLAND
if (!posControl.fwLandState.landWp && (NAV_Status.state != MW_NAV_STATE_HOVER_ABOVE_HOME && posControl.safehomeState.isApplied)) {
#else
if (NAV_Status.state != MW_NAV_STATE_HOVER_ABOVE_HOME && posControl.safehomeState.isApplied) {
#endif
return OSD_MESSAGE_STR(OSD_MSG_DIVERT_SAFEHOME);
}
#endif
return NULL;
}
static const char * navigationStateMessage(void)
{
if (!posControl.rthState.rthLinearDescentActive && linearDescentMessageMs != 0)
linearDescentMessageMs = 0;
switch (NAV_Status.state) {
case MW_NAV_STATE_NONE:
break;
case MW_NAV_STATE_RTH_START:
return OSD_MESSAGE_STR(OSD_MSG_STARTING_RTH);
case MW_NAV_STATE_RTH_CLIMB:
return OSD_MESSAGE_STR(OSD_MSG_RTH_CLIMB);
case MW_NAV_STATE_RTH_ENROUTE:
if (posControl.flags.rthTrackbackActive) {
return OSD_MESSAGE_STR(OSD_MSG_RTH_TRACKBACK);
} else {
if (posControl.rthState.rthLinearDescentActive && (linearDescentMessageMs == 0 || linearDescentMessageMs > millis())) {
if (linearDescentMessageMs == 0)
linearDescentMessageMs = millis() + 5000; // Show message for 5 seconds.
return OSD_MESSAGE_STR(OSD_MSG_RTH_LINEAR_DESCENT);
} else
return OSD_MESSAGE_STR(OSD_MSG_HEADING_HOME);
}
case MW_NAV_STATE_HOLD_INFINIT:
// Used by HOLD flight modes. No information to add.
break;
case MW_NAV_STATE_HOLD_TIMED:
// "HOLDING WP FOR xx S" Countdown added in osdGetSystemMessage
break;
case MW_NAV_STATE_WP_ENROUTE:
// "TO WP" + WP countdown added in osdGetSystemMessage
break;
case MW_NAV_STATE_PROCESS_NEXT:
return OSD_MESSAGE_STR(OSD_MSG_PREPARE_NEXT_WP);
case MW_NAV_STATE_DO_JUMP:
// Not used
break;
case MW_NAV_STATE_LAND_START: