-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
osd.c
1665 lines (1404 loc) · 51.2 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 and Betaflight.
*
* Cleanflight and Betaflight are free software. You can redistribute
* this software and/or modify this software 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 and Betaflight are distributed in the hope that they
* 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 this software.
*
* 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 "platform.h"
#ifdef USE_OSD
#include "blackbox/blackbox.h"
#include "blackbox/blackbox_io.h"
#include "build/build_config.h"
#include "build/version.h"
#include "cms/cms.h"
#include "common/axis.h"
#include "common/maths.h"
#include "common/printf.h"
#include "common/typeconversion.h"
#include "common/utils.h"
#include "common/unit.h"
#include "config/feature.h"
#include "drivers/display.h"
#include "drivers/dshot.h"
#include "drivers/flash.h"
#include "drivers/osd_symbols.h"
#include "drivers/sdcard.h"
#include "drivers/time.h"
#include "fc/core.h"
#include "fc/gps_lap_timer.h"
#include "fc/rc_controls.h"
#include "fc/rc_modes.h"
#include "fc/runtime_config.h"
#if defined(USE_DYN_NOTCH_FILTER)
#include "flight/dyn_notch_filter.h"
#endif
#include "flight/failsafe.h"
#include "flight/imu.h"
#include "flight/mixer.h"
#include "flight/position.h"
#include "io/asyncfatfs/asyncfatfs.h"
#include "io/beeper.h"
#include "io/flashfs.h"
#include "io/gps.h"
#include "osd/osd.h"
#include "osd/osd_elements.h"
#include "osd/osd_warnings.h"
#include "pg/motor.h"
#include "pg/pg.h"
#include "pg/pg_ids.h"
#include "pg/stats.h"
#include "rx/crsf.h"
#include "rx/rc_stats.h"
#include "rx/rx.h"
#include "scheduler/scheduler.h"
#include "sensors/acceleration.h"
#include "sensors/battery.h"
#include "sensors/sensors.h"
#ifdef USE_HARDWARE_REVISION_DETECTION
#include "hardware_revision.h"
#endif
typedef enum {
OSD_LOGO_ARMING_OFF,
OSD_LOGO_ARMING_ON,
OSD_LOGO_ARMING_FIRST
} osd_logo_on_arming_e;
const char * const osdTimerSourceNames[] = {
"ON TIME ",
"TOTAL ARM",
"LAST ARM ",
"ON/ARM "
};
#define OSD_LOGO_ROWS 4
#define OSD_LOGO_COLS 24
// Things in both OSD and CMS
#define IS_HI(X) (rcData[X] > 1750)
#define IS_LO(X) (rcData[X] < 1250)
#define IS_MID(X) (rcData[X] > 1250 && rcData[X] < 1750)
timeUs_t osdFlyTime = 0;
#if defined(USE_ACC)
float osdGForce = 0;
#endif
uint16_t osdAuxValue = 0;
static bool showVisualBeeper = false;
static statistic_t stats;
timeUs_t resumeRefreshAt = 0;
#define REFRESH_1S 1000 * 1000
static uint8_t armState;
#ifdef USE_OSD_PROFILES
static uint8_t osdProfile = 1;
#endif
static displayPort_t *osdDisplayPort;
static osdDisplayPortDevice_e osdDisplayPortDeviceType;
static bool osdIsReady;
static bool suppressStatsDisplay = false;
static bool backgroundLayerSupported = false;
#ifdef USE_ESC_SENSOR
escSensorData_t *osdEscDataCombined;
#endif
STATIC_ASSERT(OSD_POS_MAX == OSD_POS(63,31), OSD_POS_MAX_incorrect);
PG_REGISTER_WITH_RESET_FN(osdConfig_t, osdConfig, PG_OSD_CONFIG, 12);
PG_REGISTER_WITH_RESET_FN(osdElementConfig_t, osdElementConfig, PG_OSD_ELEMENT_CONFIG, 1);
// Controls the display order of the OSD post-flight statistics.
// Adjust the ordering here to control how the post-flight stats are presented.
// Every entry in osd_stats_e should be represented. Any that are missing will not
// be shown on the the post-flight statistics page.
// If you reorder the stats it's likely that you'll need to make likewise updates
// to the unit tests.
// If adding new stats, please add to the osdStatsNeedAccelerometer() function
// if the statistic utilizes the accelerometer.
//
const osd_stats_e osdStatsDisplayOrder[OSD_STAT_COUNT] = {
OSD_STAT_RTC_DATE_TIME,
OSD_STAT_TIMER_1,
OSD_STAT_TIMER_2,
OSD_STAT_MAX_ALTITUDE,
OSD_STAT_MAX_SPEED,
OSD_STAT_MAX_DISTANCE,
OSD_STAT_FLIGHT_DISTANCE,
OSD_STAT_MIN_BATTERY,
OSD_STAT_END_BATTERY,
OSD_STAT_BATTERY,
OSD_STAT_MIN_RSSI,
OSD_STAT_MAX_CURRENT,
OSD_STAT_USED_MAH,
OSD_STAT_BLACKBOX,
OSD_STAT_BLACKBOX_NUMBER,
OSD_STAT_MAX_G_FORCE,
OSD_STAT_MAX_ESC_TEMP,
OSD_STAT_MAX_ESC_RPM,
OSD_STAT_MIN_LINK_QUALITY,
OSD_STAT_MAX_FFT,
OSD_STAT_MIN_RSSI_DBM,
OSD_STAT_MIN_RSNR,
OSD_STAT_TOTAL_FLIGHTS,
OSD_STAT_TOTAL_TIME,
OSD_STAT_TOTAL_DIST,
OSD_STAT_WATT_HOURS_DRAWN,
OSD_STAT_BEST_3_CONSEC_LAPS,
OSD_STAT_BEST_LAP,
OSD_STAT_FULL_THROTTLE_TIME,
OSD_STAT_FULL_THROTTLE_COUNTER,
OSD_STAT_AVG_THROTTLE,
};
// Group elements in a number of groups to reduce task scheduling overhead
#define OSD_GROUP_COUNT OSD_ITEM_COUNT
// Aim to render a group of elements within a target time
#define OSD_ELEMENT_RENDER_TARGET 30
#define OSD_TASK_MARGIN 1
// Decay the estimated max task duration by 1/(1 << OSD_EXEC_TIME_SHIFT) on every invocation
#define OSD_EXEC_TIME_SHIFT 8
// Format a float to the specified number of decimal places with optional rounding.
// OSD symbols can optionally be placed before and after the formatted number (use SYM_NONE for no symbol).
// The formatString can be used for customized formatting of the integer part. Follow the printf style.
// Pass an empty formatString for default.
int osdPrintFloat(char *buffer, char leadingSymbol, float value, char *formatString, unsigned decimalPlaces, bool round, char trailingSymbol)
{
char mask[7];
int pos = 0;
int multiplier = 1;
for (unsigned i = 0; i < decimalPlaces; i++) {
multiplier *= 10;
}
value *= multiplier;
const int scaledValueAbs = abs(round ? (int)lrintf(value) : (int)value);
const int integerPart = scaledValueAbs / multiplier;
const int fractionalPart = scaledValueAbs % multiplier;
if (leadingSymbol != SYM_NONE) {
buffer[pos++] = leadingSymbol;
}
if (value < 0 && (integerPart || fractionalPart)) {
buffer[pos++] = '-';
}
pos += tfp_sprintf(buffer + pos, (strlen(formatString) ? formatString : "%01u"), integerPart);
if (decimalPlaces) {
tfp_sprintf((char *)&mask, ".%%0%uu", decimalPlaces); // builds up the format string to be like ".%03u" for decimalPlaces == 3 as an example
pos += tfp_sprintf(buffer + pos, mask, fractionalPart);
}
if (trailingSymbol != SYM_NONE) {
buffer[pos++] = trailingSymbol;
}
buffer[pos] = '\0';
return pos;
}
void osdStatSetState(uint8_t statIndex, bool enabled)
{
if (enabled) {
osdConfigMutable()->enabled_stats |= (1 << statIndex);
} else {
osdConfigMutable()->enabled_stats &= ~(1 << statIndex);
}
}
bool osdStatGetState(uint8_t statIndex)
{
return osdConfig()->enabled_stats & (1 << statIndex);
}
void osdWarnSetState(uint8_t warningIndex, bool enabled)
{
if (enabled) {
osdConfigMutable()->enabledWarnings |= (1 << warningIndex);
} else {
osdConfigMutable()->enabledWarnings &= ~(1 << warningIndex);
}
}
bool osdWarnGetState(uint8_t warningIndex)
{
return osdConfig()->enabledWarnings & (1 << warningIndex);
}
#ifdef USE_OSD_PROFILES
void setOsdProfile(uint8_t value)
{
// 1 ->> 001
// 2 ->> 010
// 3 ->> 100
if (value <= OSD_PROFILE_COUNT) {
if (value == 0) {
osdProfile = 1;
} else {
osdProfile = 1 << (value - 1);
}
}
}
uint8_t getCurrentOsdProfileIndex(void)
{
return osdConfig()->osdProfileIndex;
}
void changeOsdProfileIndex(uint8_t profileIndex)
{
if (profileIndex <= OSD_PROFILE_COUNT) {
osdConfigMutable()->osdProfileIndex = profileIndex;
setOsdProfile(profileIndex);
osdAnalyzeActiveElements();
}
}
#endif
void osdAnalyzeActiveElements(void)
{
/* This code results in a total RX task RX_STATE_MODES state time of ~68us on an F411 overclocked to 108MHz
* This upsets the scheduler task duration estimation and will break SPI RX communication. This can
* occur in flight, e.g. when the OSD profile is changed by switch so can be ignored, or GPS sensor comms
* is lost - only causing one late task instance.
*/
schedulerIgnoreTaskExecTime();
osdAddActiveElements();
osdDrawActiveElementsBackground(osdDisplayPort);
}
const uint16_t osdTimerDefault[OSD_TIMER_COUNT] = {
OSD_TIMER(OSD_TIMER_SRC_ON, OSD_TIMER_PREC_SECOND, 10),
OSD_TIMER(OSD_TIMER_SRC_TOTAL_ARMED, OSD_TIMER_PREC_SECOND, 10)
};
#ifdef USE_RACE_PRO
#define RACE_PRO true
#else
#define RACE_PRO false
#endif
void pgResetFn_osdConfig(osdConfig_t *osdConfig)
{
// Enable the default stats
osdConfig->enabled_stats = 0; // reset all to off and enable only a few initially
osdStatSetState(OSD_STAT_MAX_SPEED, !RACE_PRO);
osdStatSetState(OSD_STAT_MIN_BATTERY, true);
osdStatSetState(OSD_STAT_MIN_RSSI, !RACE_PRO);
osdStatSetState(OSD_STAT_MAX_CURRENT, !RACE_PRO);
osdStatSetState(OSD_STAT_USED_MAH, !RACE_PRO);
osdStatSetState(OSD_STAT_BLACKBOX, !RACE_PRO);
osdStatSetState(OSD_STAT_BLACKBOX_NUMBER, !RACE_PRO);
osdStatSetState(OSD_STAT_TIMER_2, true);
osdConfig->units = UNIT_METRIC;
// Enable all warnings by default
for (int i=0; i < OSD_WARNING_COUNT; i++) {
osdWarnSetState(i, true);
}
// turn off RSSI & Link Quality warnings by default
osdWarnSetState(OSD_WARNING_RSSI, false);
osdWarnSetState(OSD_WARNING_LINK_QUALITY, false);
osdWarnSetState(OSD_WARNING_RSSI_DBM, false);
osdWarnSetState(OSD_WARNING_RSNR, false);
// turn off the over mah capacity warning
osdWarnSetState(OSD_WARNING_OVER_CAP, false);
#ifdef USE_RC_STATS
osdStatSetState(OSD_STAT_FULL_THROTTLE_TIME, true);
osdStatSetState(OSD_STAT_FULL_THROTTLE_COUNTER, true);
osdStatSetState(OSD_STAT_AVG_THROTTLE, true);
#endif
osdConfig->timers[OSD_TIMER_1] = osdTimerDefault[OSD_TIMER_1];
osdConfig->timers[OSD_TIMER_2] = osdTimerDefault[OSD_TIMER_2];
osdConfig->overlay_radio_mode = 2;
osdConfig->rssi_alarm = 20;
osdConfig->link_quality_alarm = 80;
osdConfig->cap_alarm = 2200;
osdConfig->alt_alarm = 100; // meters or feet depend on configuration
osdConfig->esc_temp_alarm = ESC_TEMP_ALARM_OFF; // off by default
osdConfig->esc_rpm_alarm = ESC_RPM_ALARM_OFF; // off by default
osdConfig->esc_current_alarm = ESC_CURRENT_ALARM_OFF; // off by default
osdConfig->core_temp_alarm = 70; // a temperature above 70C should produce a warning, lockups have been reported above 80C
osdConfig->ahMaxPitch = 20; // 20 degrees
osdConfig->ahMaxRoll = 40; // 40 degrees
osdConfig->osdProfileIndex = 1;
osdConfig->ahInvert = false;
for (int i=0; i < OSD_PROFILE_COUNT; i++) {
osdConfig->profile[i][0] = '\0';
}
osdConfig->rssi_dbm_alarm = -60;
osdConfig->rsnr_alarm = 4;
osdConfig->gps_sats_show_pdop = false;
for (int i = 0; i < OSD_RCCHANNELS_COUNT; i++) {
osdConfig->rcChannels[i] = -1;
}
osdConfig->distance_alarm = 0;
osdConfig->logo_on_arming = OSD_LOGO_ARMING_OFF;
osdConfig->logo_on_arming_duration = 5; // 0.5 seconds
osdConfig->camera_frame_width = 24;
osdConfig->camera_frame_height = 11;
osdConfig->stat_show_cell_value = false;
osdConfig->framerate_hz = OSD_FRAMERATE_DEFAULT_HZ;
osdConfig->cms_background_type = DISPLAY_BACKGROUND_TRANSPARENT;
#ifdef USE_CRAFTNAME_MSGS
osdConfig->osd_craftname_msgs = false; // Insert LQ/RSSI-dBm and warnings into CraftName
#endif //USE_CRAFTNAME_MSGS
osdConfig->aux_channel = 1;
osdConfig->aux_scale = 200;
osdConfig->aux_symbol = 'A';
// Make it obvious on the configurator that the FC doesn't support HD
#ifdef USE_OSD_HD
osdConfig->displayPortDevice = OSD_DISPLAYPORT_DEVICE_MSP;
osdConfig->canvas_cols = OSD_HD_COLS;
osdConfig->canvas_rows = OSD_HD_ROWS;
#else
osdConfig->displayPortDevice = OSD_DISPLAYPORT_DEVICE_AUTO;
osdConfig->canvas_cols = OSD_SD_COLS;
osdConfig->canvas_rows = OSD_SD_ROWS;
#endif
#ifdef USE_OSD_QUICK_MENU
osdConfig->osd_use_quick_menu = true;
#endif // USE_OSD_QUICK_MENU
#ifdef USE_RACE_PRO
osdConfig->osd_show_spec_prearm = true;
#endif // USE_RACE_PRO
}
void pgResetFn_osdElementConfig(osdElementConfig_t *osdElementConfig)
{
// If user includes OSD_HD in the build assume they want to use it as default
#ifdef USE_OSD_HD
uint8_t midRow = 10;
uint8_t midCol = 26;
#else
uint8_t midRow = 7;
uint8_t midCol = 15;
#endif
// Position elements near centre of screen and disabled by default
for (int i = 0; i < OSD_ITEM_COUNT; i++) {
osdElementConfig->item_pos[i] = OSD_POS((midCol - 5), midRow);
}
// Always enable warnings elements by default
uint16_t profileFlags = 0;
for (unsigned i = 1; i <= OSD_PROFILE_COUNT; i++) {
profileFlags |= OSD_PROFILE_FLAG(i);
}
osdElementConfig->item_pos[OSD_WARNINGS] = OSD_POS((midCol - 6), (midRow + 3)) | profileFlags;
// Default to old fixed positions for these elements
osdElementConfig->item_pos[OSD_CROSSHAIRS] = OSD_POS((midCol - 2), (midRow - 1));
osdElementConfig->item_pos[OSD_ARTIFICIAL_HORIZON] = OSD_POS((midCol - 1), (midRow - 5));
osdElementConfig->item_pos[OSD_HORIZON_SIDEBARS] = OSD_POS((midCol - 1), (midRow - 1));
osdElementConfig->item_pos[OSD_CAMERA_FRAME] = OSD_POS((midCol - 12), (midRow - 6));
osdElementConfig->item_pos[OSD_UP_DOWN_REFERENCE] = OSD_POS((midCol - 2), (midRow - 1));
}
static void osdDrawLogo(int x, int y, displayPortSeverity_e fontSel)
{
// display logo and help
int fontOffset = 160;
for (int row = 0; row < OSD_LOGO_ROWS; row++) {
for (int column = 0; column < OSD_LOGO_COLS; column++) {
if (fontOffset <= SYM_END_OF_FONT)
displayWriteChar(osdDisplayPort, x + column, y + row, fontSel, fontOffset++);
}
}
}
static void osdCompleteInitialization(void)
{
uint8_t midRow = osdDisplayPort->rows / 2;
uint8_t midCol = osdDisplayPort->cols / 2;
armState = ARMING_FLAG(ARMED);
osdResetAlarms();
backgroundLayerSupported = displayLayerSupported(osdDisplayPort, DISPLAYPORT_LAYER_BACKGROUND);
displayLayerSelect(osdDisplayPort, DISPLAYPORT_LAYER_FOREGROUND);
displayBeginTransaction(osdDisplayPort, DISPLAY_TRANSACTION_OPT_RESET_DRAWING);
displayClearScreen(osdDisplayPort, DISPLAY_CLEAR_WAIT);
// Display betaflight logo
osdDrawLogo(midCol - (OSD_LOGO_COLS) / 2, midRow - 5, DISPLAYPORT_SEVERITY_NORMAL);
char string_buffer[30];
tfp_sprintf(string_buffer, "V%s", FC_VERSION_STRING);
displayWrite(osdDisplayPort, midCol + 5, midRow, DISPLAYPORT_SEVERITY_NORMAL, string_buffer);
#ifdef USE_CMS
displayWrite(osdDisplayPort, midCol - 8, midRow + 2, DISPLAYPORT_SEVERITY_NORMAL, CMS_STARTUP_HELP_TEXT1);
displayWrite(osdDisplayPort, midCol - 4, midRow + 3, DISPLAYPORT_SEVERITY_NORMAL, CMS_STARTUP_HELP_TEXT2);
displayWrite(osdDisplayPort, midCol - 4, midRow + 4, DISPLAYPORT_SEVERITY_NORMAL, CMS_STARTUP_HELP_TEXT3);
#endif
#ifdef USE_RTC_TIME
char dateTimeBuffer[FORMATTED_DATE_TIME_BUFSIZE];
if (osdFormatRtcDateTime(&dateTimeBuffer[0])) {
displayWrite(osdDisplayPort, midCol - 10, midRow + 6, DISPLAYPORT_SEVERITY_NORMAL, dateTimeBuffer);
}
#endif
resumeRefreshAt = micros() + (4 * REFRESH_1S);
#ifdef USE_OSD_PROFILES
setOsdProfile(osdConfig()->osdProfileIndex);
#endif
osdElementsInit(backgroundLayerSupported);
osdAnalyzeActiveElements();
osdIsReady = true;
}
void osdInit(displayPort_t *osdDisplayPortToUse, osdDisplayPortDevice_e displayPortDeviceType)
{
osdDisplayPortDeviceType = displayPortDeviceType;
if (!osdDisplayPortToUse) {
return;
}
osdDisplayPort = osdDisplayPortToUse;
#ifdef USE_CMS
cmsDisplayPortRegister(osdDisplayPort);
#endif
if (osdDisplayPort->cols && osdDisplayPort->rows) {
// Ensure that osd_canvas_width/height are correct
if (osdConfig()->canvas_cols != osdDisplayPort->cols) {
osdConfigMutable()->canvas_cols = osdDisplayPort->cols;
}
if (osdConfig()->canvas_rows != osdDisplayPort->rows) {
osdConfigMutable()->canvas_rows = osdDisplayPort->rows;
}
// Ensure that all OSD elements are on the canvas once number of row/columns is known
for (int i = 0; i < OSD_ITEM_COUNT; i++) {
uint16_t itemPos = osdElementConfig()->item_pos[i];
uint8_t elemPosX = OSD_X(itemPos);
uint8_t elemPosY = OSD_Y(itemPos);
uint16_t elemProfileType = itemPos & (OSD_PROFILE_MASK | OSD_TYPE_MASK);
bool pos_reset = false;
if (elemPosX >= osdDisplayPort->cols) {
elemPosX = osdDisplayPort->cols - 1;
pos_reset = true;
}
if (elemPosY >= osdDisplayPort->rows) {
elemPosY = osdDisplayPort->rows - 1;
pos_reset = true;
}
if (pos_reset) {
osdElementConfigMutable()->item_pos[i] = elemProfileType | OSD_POS(elemPosX, elemPosY);
}
}
}
}
#ifdef USE_GPS_LAP_TIMER
void printLapTime(char *buffer, const uint32_t timeMs) {
if (timeMs != 0) {
const uint32_t timeRoundMs = timeMs + 5; // round value in division by 10
const int timeSeconds = timeRoundMs / 1000;
const int timeDecimals = (timeRoundMs % 1000) / 10;
tfp_sprintf(buffer, "%3u.%02u", timeSeconds, timeDecimals);
} else {
tfp_sprintf(buffer, " -.--");
}
}
#endif // USE_GPS_LAP_TIMER
static void osdResetStats(void)
{
stats.max_current = 0;
stats.max_speed = 0;
stats.min_voltage = 5000;
stats.end_voltage = 0;
stats.min_rssi = 99; // percent
stats.max_altitude = 0;
stats.max_distance = 0;
stats.armed_time = 0;
stats.max_g_force = 0;
stats.max_esc_temp_ix = 0;
stats.max_esc_temp = 0;
stats.max_esc_rpm = 0;
stats.min_link_quality = (linkQualitySource == LQ_SOURCE_NONE) ? 99 : 100; // percent
stats.min_rssi_dbm = CRSF_RSSI_MAX;
stats.min_rsnr = CRSF_SNR_MAX;
}
#if defined(USE_ESC_SENSOR) || defined(USE_DSHOT_TELEMETRY)
static int32_t getAverageEscRpm(void)
{
#ifdef USE_ESC_SENSOR
if (featureIsEnabled(FEATURE_ESC_SENSOR)) {
return lrintf(erpmToRpm(osdEscDataCombined->rpm));
}
#endif
#ifdef USE_DSHOT_TELEMETRY
if (useDshotTelemetry) {
return lrintf(getDshotRpmAverage());
}
#endif
return 0;
}
#endif
static uint16_t getStatsVoltage(void)
{
return osdConfig()->stat_show_cell_value ? getBatteryAverageCellVoltage() : getBatteryVoltage();
}
static void osdUpdateStats(void)
{
int16_t value = 0;
#ifdef USE_GPS
if (gpsConfig()->gps_use_3d_speed) {
value = gpsSol.speed3d;
} else {
value = gpsSol.groundSpeed;
}
if (stats.max_speed < value) {
stats.max_speed = value;
}
#endif
value = getStatsVoltage();
if (stats.min_voltage > value) {
stats.min_voltage = value;
}
value = getAmperage() / 100;
if (stats.max_current < value) {
stats.max_current = value;
}
value = getRssiPercent();
if (stats.min_rssi > value) {
stats.min_rssi = value;
}
int32_t altitudeCm = getEstimatedAltitudeCm();
if (stats.max_altitude < altitudeCm) {
stats.max_altitude = altitudeCm;
}
#if defined(USE_ACC)
if (stats.max_g_force < osdGForce) {
stats.max_g_force = osdGForce;
}
#endif
#ifdef USE_RX_LINK_QUALITY_INFO
value = rxGetLinkQualityPercent();
if (stats.min_link_quality > value) {
stats.min_link_quality = value;
}
#endif
#ifdef USE_RX_RSSI_DBM
value = getRssiDbm();
if (stats.min_rssi_dbm > value) {
stats.min_rssi_dbm = value;
}
#endif
#ifdef USE_RX_RSNR
value = getRsnr();
if (stats.min_rsnr > value) {
stats.min_rsnr = value;
}
#endif
#ifdef USE_GPS
if (STATE(GPS_FIX) && STATE(GPS_FIX_HOME)) {
if (stats.max_distance < GPS_distanceToHome) {
stats.max_distance = GPS_distanceToHome;
}
}
#endif
#if defined(USE_ESC_SENSOR)
if (featureIsEnabled(FEATURE_ESC_SENSOR)) {
value = osdEscDataCombined->temperature;
if (stats.max_esc_temp < value) {
stats.max_esc_temp = value;
}
} else
#endif
#if defined(USE_DSHOT_TELEMETRY)
{
// Take max temp from dshot telemetry
for (uint8_t k = 0; k < getMotorCount(); k++) {
if (dshotTelemetryState.motorState[k].maxTemp > stats.max_esc_temp) {
stats.max_esc_temp_ix = k + 1;
stats.max_esc_temp = dshotTelemetryState.motorState[k].maxTemp;
}
}
}
#else
{}
#endif
#if defined(USE_ESC_SENSOR) || defined(USE_DSHOT_TELEMETRY)
int32_t rpm = getAverageEscRpm();
if (stats.max_esc_rpm < rpm) {
stats.max_esc_rpm = rpm;
}
#endif
}
#ifdef USE_BLACKBOX
static void osdGetBlackboxStatusString(char * buff)
{
bool storageDeviceIsWorking = isBlackboxDeviceWorking();
uint32_t storageUsed = 0;
uint32_t storageTotal = 0;
switch (blackboxConfig()->device) {
#ifdef USE_SDCARD
case BLACKBOX_DEVICE_SDCARD:
if (storageDeviceIsWorking) {
storageTotal = sdcard_getMetadata()->numBlocks / 2000;
storageUsed = storageTotal - (afatfs_getContiguousFreeSpace() / 1024000);
}
break;
#endif
#ifdef USE_FLASHFS
case BLACKBOX_DEVICE_FLASH:
if (storageDeviceIsWorking) {
const flashPartition_t *flashPartition = flashPartitionFindByType(FLASH_PARTITION_TYPE_FLASHFS);
const flashGeometry_t *flashGeometry = flashGetGeometry();
storageTotal = ((FLASH_PARTITION_SECTOR_COUNT(flashPartition) * flashGeometry->sectorSize) / 1024);
storageUsed = flashfsGetOffset() / 1024;
}
break;
#endif
default:
break;
}
if (storageDeviceIsWorking) {
const uint16_t storageUsedPercent = (storageUsed * 100) / storageTotal;
tfp_sprintf(buff, "%d%%", storageUsedPercent);
} else {
tfp_sprintf(buff, "FAULT");
}
}
#endif
static void osdDisplayStatisticLabel(uint8_t x, uint8_t y, const char * text, const char * value)
{
displayWrite(osdDisplayPort, x - 13, y, DISPLAYPORT_SEVERITY_NORMAL, text);
displayWrite(osdDisplayPort, x + 5, y, DISPLAYPORT_SEVERITY_NORMAL, ":");
displayWrite(osdDisplayPort, x + 7, y, DISPLAYPORT_SEVERITY_NORMAL, value);
}
/*
* Test if there's some stat enabled
*/
static bool isSomeStatEnabled(void)
{
return (osdConfig()->enabled_stats != 0);
}
// *** IMPORTANT ***
// The stats display order was previously required to match the enumeration definition so it matched
// the order shown in the configurator. However, to allow reordering this screen without breaking the
// compatibility, this requirement has been relaxed to a best effort approach. Reordering the elements
// on the stats screen will have to be more beneficial than the hassle of not matching exactly to the
// configurator list.
static bool osdDisplayStat(int statistic, uint8_t displayRow)
{
uint8_t midCol = osdDisplayPort->cols / 2;
char buff[OSD_ELEMENT_BUFFER_LENGTH];
switch (statistic) {
case OSD_STAT_RTC_DATE_TIME: {
bool success = false;
#ifdef USE_RTC_TIME
success = osdFormatRtcDateTime(&buff[0]);
#endif
if (!success) {
tfp_sprintf(buff, "NO RTC");
}
displayWrite(osdDisplayPort, midCol - 13, displayRow, DISPLAYPORT_SEVERITY_NORMAL, buff);
return true;
}
case OSD_STAT_TIMER_1:
osdFormatTimer(buff, false, (OSD_TIMER_SRC(osdConfig()->timers[OSD_TIMER_1]) == OSD_TIMER_SRC_ON ? false : true), OSD_TIMER_1);
osdDisplayStatisticLabel(midCol, displayRow, osdTimerSourceNames[OSD_TIMER_SRC(osdConfig()->timers[OSD_TIMER_1])], buff);
return true;
case OSD_STAT_TIMER_2:
osdFormatTimer(buff, false, (OSD_TIMER_SRC(osdConfig()->timers[OSD_TIMER_2]) == OSD_TIMER_SRC_ON ? false : true), OSD_TIMER_2);
osdDisplayStatisticLabel(midCol, displayRow, osdTimerSourceNames[OSD_TIMER_SRC(osdConfig()->timers[OSD_TIMER_2])], buff);
return true;
case OSD_STAT_MAX_ALTITUDE: {
osdPrintFloat(buff, SYM_NONE, osdGetMetersToSelectedUnit(stats.max_altitude) / 100.0f, "", 1, true, osdGetMetersToSelectedUnitSymbol());
osdDisplayStatisticLabel(midCol, displayRow, "MAX ALTITUDE", buff);
return true;
}
#ifdef USE_GPS
case OSD_STAT_MAX_SPEED:
if (featureIsEnabled(FEATURE_GPS)) {
tfp_sprintf(buff, "%d%c", osdGetSpeedToSelectedUnit(stats.max_speed), osdGetSpeedToSelectedUnitSymbol());
osdDisplayStatisticLabel(midCol, displayRow, "MAX SPEED", buff);
return true;
}
break;
case OSD_STAT_MAX_DISTANCE:
if (featureIsEnabled(FEATURE_GPS)) {
osdFormatDistanceString(buff, stats.max_distance, SYM_NONE);
osdDisplayStatisticLabel(midCol, displayRow, "MAX DISTANCE", buff);
return true;
}
break;
case OSD_STAT_FLIGHT_DISTANCE:
if (featureIsEnabled(FEATURE_GPS)) {
const int distanceFlown = GPS_distanceFlownInCm / 100;
osdFormatDistanceString(buff, distanceFlown, SYM_NONE);
osdDisplayStatisticLabel(midCol, displayRow, "FLIGHT DISTANCE", buff);
return true;
}
break;
#endif
case OSD_STAT_MIN_BATTERY:
osdPrintFloat(buff, SYM_NONE, stats.min_voltage / 100.0f, "", 2, true, SYM_VOLT);
osdDisplayStatisticLabel(midCol, displayRow, osdConfig()->stat_show_cell_value? "MIN AVG CELL" : "MIN BATTERY", buff);
return true;
case OSD_STAT_END_BATTERY:
osdPrintFloat(buff, SYM_NONE, stats.end_voltage / 100.0f, "", 2, true, SYM_VOLT);
osdDisplayStatisticLabel(midCol, displayRow, osdConfig()->stat_show_cell_value ? "END AVG CELL" : "END BATTERY", buff);
return true;
case OSD_STAT_BATTERY:
{
const uint16_t statsVoltage = getStatsVoltage();
osdPrintFloat(buff, SYM_NONE, statsVoltage / 100.0f, "", 2, true, SYM_VOLT);
osdDisplayStatisticLabel(midCol, displayRow, osdConfig()->stat_show_cell_value ? "AVG BATT CELL" : "BATTERY", buff);
return true;
}
break;
case OSD_STAT_MIN_RSSI:
itoa(stats.min_rssi, buff, 10);
strcat(buff, "%");
osdDisplayStatisticLabel(midCol, displayRow, "MIN RSSI", buff);
return true;
case OSD_STAT_MAX_CURRENT:
if (batteryConfig()->currentMeterSource != CURRENT_METER_NONE) {
tfp_sprintf(buff, "%d%c", stats.max_current, SYM_AMP);
osdDisplayStatisticLabel(midCol, displayRow, "MAX CURRENT", buff);
return true;
}
break;
case OSD_STAT_USED_MAH:
if (batteryConfig()->currentMeterSource != CURRENT_METER_NONE) {
tfp_sprintf(buff, "%d%c", getMAhDrawn(), SYM_MAH);
osdDisplayStatisticLabel(midCol, displayRow, "USED MAH", buff);
return true;
}
break;
case OSD_STAT_WATT_HOURS_DRAWN:
if (batteryConfig()->currentMeterSource != CURRENT_METER_NONE) {
osdPrintFloat(buff, SYM_NONE, getWhDrawn(), "", 2, true, SYM_NONE);
osdDisplayStatisticLabel(midCol, displayRow, "USED WATT HOURS", buff);
return true;
}
break;
#ifdef USE_BLACKBOX
case OSD_STAT_BLACKBOX:
if (blackboxConfig()->device && blackboxConfig()->device != BLACKBOX_DEVICE_SERIAL) {
osdGetBlackboxStatusString(buff);
osdDisplayStatisticLabel(midCol, displayRow, "BLACKBOX", buff);
return true;
}
break;
case OSD_STAT_BLACKBOX_NUMBER:
{
int32_t logNumber = blackboxGetLogNumber();
if (logNumber >= 0) {
itoa(logNumber, buff, 10);
osdDisplayStatisticLabel(midCol, displayRow, "BB LOG NUM", buff);
return true;
}
}
break;
#endif
#if defined(USE_ACC)
case OSD_STAT_MAX_G_FORCE:
if (sensors(SENSOR_ACC)) {
osdPrintFloat(buff, SYM_NONE, stats.max_g_force, "", 1, true, 'G');
osdDisplayStatisticLabel(midCol, displayRow, "MAX G-FORCE", buff);
return true;
}
break;
#endif
#ifdef USE_ESC_SENSOR
case OSD_STAT_MAX_ESC_TEMP:
{
uint16_t ix = 0;
if (stats.max_esc_temp_ix > 0) {
ix = tfp_sprintf(buff, "%d ", stats.max_esc_temp_ix);
}
tfp_sprintf(buff + ix, "%d%c", osdConvertTemperatureToSelectedUnit(stats.max_esc_temp), osdGetTemperatureSymbolForSelectedUnit());
osdDisplayStatisticLabel(midCol, displayRow, "MAX ESC TEMP", buff);
return true;
}
#endif
#if defined(USE_ESC_SENSOR) || defined(USE_DSHOT_TELEMETRY)
case OSD_STAT_MAX_ESC_RPM:
itoa(stats.max_esc_rpm, buff, 10);
osdDisplayStatisticLabel(midCol, displayRow, "MAX ESC RPM", buff);
return true;
#endif
#ifdef USE_RX_LINK_QUALITY_INFO
case OSD_STAT_MIN_LINK_QUALITY:
tfp_sprintf(buff, "%d", stats.min_link_quality);
strcat(buff, "%");
osdDisplayStatisticLabel(midCol, displayRow, "MIN LINK", buff);
return true;
#endif
#if defined(USE_DYN_NOTCH_FILTER)
case OSD_STAT_MAX_FFT:
if (isDynNotchActive()) {
int value = getMaxFFT();
if (value > 0) {
tfp_sprintf(buff, "%dHZ", value);
osdDisplayStatisticLabel(midCol, displayRow, "PEAK FFT", buff);
} else {
osdDisplayStatisticLabel(midCol, displayRow, "PEAK FFT", "THRT<20%");
}
return true;
}
break;
#endif
#ifdef USE_RX_RSSI_DBM
case OSD_STAT_MIN_RSSI_DBM:
tfp_sprintf(buff, "%3d", stats.min_rssi_dbm);
osdDisplayStatisticLabel(midCol, displayRow, "MIN RSSI DBM", buff);
return true;
#endif
#ifdef USE_RX_RSNR
case OSD_STAT_MIN_RSNR:
tfp_sprintf(buff, "%3d", stats.min_rsnr);
osdDisplayStatisticLabel(midCol, displayRow, "MIN RSNR", buff);
return true;
#endif
#ifdef USE_GPS_LAP_TIMER
case OSD_STAT_BEST_3_CONSEC_LAPS: {
printLapTime(buff, gpsLapTimerData.best3Consec);
osdDisplayStatisticLabel(midCol, displayRow, "BEST 3 CON", buff);
return true;
}
case OSD_STAT_BEST_LAP: {
printLapTime(buff, gpsLapTimerData.bestLapTime);
osdDisplayStatisticLabel(midCol, displayRow, "BEST LAP", buff);
return true;