2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
22 Created by Marcin Baliniak
23 some functions based on MinimOSD
25 OSD-CMS separation by jflyper
39 #include "blackbox/blackbox.h"
40 #include "blackbox/blackbox_io.h"
42 #include "build/build_config.h"
43 #include "build/version.h"
47 #include "common/axis.h"
48 #include "common/maths.h"
49 #include "common/printf.h"
50 #include "common/typeconversion.h"
51 #include "common/utils.h"
52 #include "common/unit.h"
54 #include "config/feature.h"
56 #include "drivers/display.h"
57 #include "drivers/dshot.h"
58 #include "drivers/flash.h"
59 #include "drivers/osd_symbols.h"
60 #include "drivers/sdcard.h"
61 #include "drivers/time.h"
64 #include "fc/rc_controls.h"
65 #include "fc/rc_modes.h"
66 #include "fc/runtime_config.h"
68 #if defined(USE_DYN_NOTCH_FILTER)
69 #include "flight/dyn_notch_filter.h"
71 #include "flight/failsafe.h"
72 #include "flight/imu.h"
73 #include "flight/mixer.h"
74 #include "flight/position.h"
76 #include "io/asyncfatfs/asyncfatfs.h"
77 #include "io/beeper.h"
78 #include "io/flashfs.h"
82 #include "osd/osd_elements.h"
83 #include "osd/osd_warnings.h"
87 #include "pg/pg_ids.h"
93 #include "scheduler/scheduler.h"
95 #include "sensors/acceleration.h"
96 #include "sensors/battery.h"
97 #include "sensors/sensors.h"
99 #ifdef USE_HARDWARE_REVISION_DETECTION
100 #include "hardware_revision.h"
106 OSD_LOGO_ARMING_FIRST
107 } osd_logo_on_arming_e
;
109 const char * const osdTimerSourceNames
[] = {
116 #define OSD_LOGO_ROWS 4
117 #define OSD_LOGO_COLS 24
119 // Things in both OSD and CMS
121 #define IS_HI(X) (rcData[X] > 1750)
122 #define IS_LO(X) (rcData[X] < 1250)
123 #define IS_MID(X) (rcData[X] > 1250 && rcData[X] < 1750)
125 timeUs_t osdFlyTime
= 0;
129 uint16_t osdAuxValue
= 0;
131 static bool showVisualBeeper
= false;
133 static statistic_t stats
;
134 timeUs_t resumeRefreshAt
= 0;
135 #define REFRESH_1S 1000 * 1000
137 static uint8_t armState
;
138 #ifdef USE_OSD_PROFILES
139 static uint8_t osdProfile
= 1;
141 static displayPort_t
*osdDisplayPort
;
142 static osdDisplayPortDevice_e osdDisplayPortDeviceType
;
143 static bool osdIsReady
;
145 static bool suppressStatsDisplay
= false;
147 static bool backgroundLayerSupported
= false;
149 #ifdef USE_ESC_SENSOR
150 escSensorData_t
*osdEscDataCombined
;
153 STATIC_ASSERT(OSD_POS_MAX
== OSD_POS(63,31), OSD_POS_MAX_incorrect
);
155 PG_REGISTER_WITH_RESET_FN(osdConfig_t
, osdConfig
, PG_OSD_CONFIG
, 12);
157 PG_REGISTER_WITH_RESET_FN(osdElementConfig_t
, osdElementConfig
, PG_OSD_ELEMENT_CONFIG
, 1);
159 // Controls the display order of the OSD post-flight statistics.
160 // Adjust the ordering here to control how the post-flight stats are presented.
161 // Every entry in osd_stats_e should be represented. Any that are missing will not
162 // be shown on the the post-flight statistics page.
163 // If you reorder the stats it's likely that you'll need to make likewise updates
164 // to the unit tests.
166 // If adding new stats, please add to the osdStatsNeedAccelerometer() function
167 // if the statistic utilizes the accelerometer.
169 const osd_stats_e osdStatsDisplayOrder
[OSD_STAT_COUNT
] = {
170 OSD_STAT_RTC_DATE_TIME
,
173 OSD_STAT_MAX_ALTITUDE
,
175 OSD_STAT_MAX_DISTANCE
,
176 OSD_STAT_FLIGHT_DISTANCE
,
177 OSD_STAT_MIN_BATTERY
,
178 OSD_STAT_END_BATTERY
,
181 OSD_STAT_MAX_CURRENT
,
184 OSD_STAT_BLACKBOX_NUMBER
,
185 OSD_STAT_MAX_G_FORCE
,
186 OSD_STAT_MAX_ESC_TEMP
,
187 OSD_STAT_MAX_ESC_RPM
,
188 OSD_STAT_MIN_LINK_QUALITY
,
190 OSD_STAT_MIN_RSSI_DBM
,
192 OSD_STAT_TOTAL_FLIGHTS
,
195 OSD_STAT_WATT_HOURS_DRAWN
,
198 // Group elements in a number of groups to reduce task scheduling overhead
199 #define OSD_GROUP_COUNT OSD_ITEM_COUNT
200 // Aim to render a group of elements within a target time
201 #define OSD_ELEMENT_RENDER_TARGET 30
202 // Allow a margin by which a group render can exceed that of the sum of the elements before declaring insane
203 // This will most likely be violated by a USB interrupt whilst using the CLI
204 #if defined(STM32F411xE)
205 #define OSD_ELEMENT_RENDER_GROUP_MARGIN 7
207 #define OSD_ELEMENT_RENDER_GROUP_MARGIN 2
209 #define OSD_TASK_MARGIN 1
210 // Decay the estimated max task duration by 1/(1 << OSD_EXEC_TIME_SHIFT) on every invocation
211 #define OSD_EXEC_TIME_SHIFT 8
213 // Format a float to the specified number of decimal places with optional rounding.
214 // OSD symbols can optionally be placed before and after the formatted number (use SYM_NONE for no symbol).
215 // The formatString can be used for customized formatting of the integer part. Follow the printf style.
216 // Pass an empty formatString for default.
217 int osdPrintFloat(char *buffer
, char leadingSymbol
, float value
, char *formatString
, unsigned decimalPlaces
, bool round
, char trailingSymbol
)
222 for (unsigned i
= 0; i
< decimalPlaces
; i
++) {
227 const int scaledValueAbs
= abs(round
? (int)lrintf(value
) : (int)value
);
228 const int integerPart
= scaledValueAbs
/ multiplier
;
229 const int fractionalPart
= scaledValueAbs
% multiplier
;
231 if (leadingSymbol
!= SYM_NONE
) {
232 buffer
[pos
++] = leadingSymbol
;
234 if (value
< 0 && (integerPart
|| fractionalPart
)) {
238 pos
+= tfp_sprintf(buffer
+ pos
, (strlen(formatString
) ? formatString
: "%01u"), integerPart
);
240 tfp_sprintf((char *)&mask
, ".%%0%uu", decimalPlaces
); // builds up the format string to be like ".%03u" for decimalPlaces == 3 as an example
241 pos
+= tfp_sprintf(buffer
+ pos
, mask
, fractionalPart
);
244 if (trailingSymbol
!= SYM_NONE
) {
245 buffer
[pos
++] = trailingSymbol
;
252 void osdStatSetState(uint8_t statIndex
, bool enabled
)
255 osdConfigMutable()->enabled_stats
|= (1 << statIndex
);
257 osdConfigMutable()->enabled_stats
&= ~(1 << statIndex
);
261 bool osdStatGetState(uint8_t statIndex
)
263 return osdConfig()->enabled_stats
& (1 << statIndex
);
266 void osdWarnSetState(uint8_t warningIndex
, bool enabled
)
269 osdConfigMutable()->enabledWarnings
|= (1 << warningIndex
);
271 osdConfigMutable()->enabledWarnings
&= ~(1 << warningIndex
);
275 bool osdWarnGetState(uint8_t warningIndex
)
277 return osdConfig()->enabledWarnings
& (1 << warningIndex
);
280 #ifdef USE_OSD_PROFILES
281 void setOsdProfile(uint8_t value
)
286 if (value
<= OSD_PROFILE_COUNT
) {
290 osdProfile
= 1 << (value
- 1);
295 uint8_t getCurrentOsdProfileIndex(void)
297 return osdConfig()->osdProfileIndex
;
300 void changeOsdProfileIndex(uint8_t profileIndex
)
302 if (profileIndex
<= OSD_PROFILE_COUNT
) {
303 osdConfigMutable()->osdProfileIndex
= profileIndex
;
304 setOsdProfile(profileIndex
);
305 osdAnalyzeActiveElements();
310 void osdAnalyzeActiveElements(void)
312 /* This code results in a total RX task RX_STATE_MODES state time of ~68us on an F411 overclocked to 108MHz
313 * This upsets the scheduler task duration estimation and will break SPI RX communication. This can
314 * occur in flight, e.g. when the OSD profile is changed by switch so can be ignored, or GPS sensor comms
315 * is lost - only causing one late task instance.
317 schedulerIgnoreTaskExecTime();
319 osdAddActiveElements();
320 osdDrawActiveElementsBackground(osdDisplayPort
);
323 const uint16_t osdTimerDefault
[OSD_TIMER_COUNT
] = {
324 OSD_TIMER(OSD_TIMER_SRC_ON
, OSD_TIMER_PREC_SECOND
, 10),
325 OSD_TIMER(OSD_TIMER_SRC_TOTAL_ARMED
, OSD_TIMER_PREC_SECOND
, 10)
328 void pgResetFn_osdConfig(osdConfig_t
*osdConfig
)
330 // Enable the default stats
331 osdConfig
->enabled_stats
= 0; // reset all to off and enable only a few initially
332 osdStatSetState(OSD_STAT_MAX_SPEED
, true);
333 osdStatSetState(OSD_STAT_MIN_BATTERY
, true);
334 osdStatSetState(OSD_STAT_MIN_RSSI
, true);
335 osdStatSetState(OSD_STAT_MAX_CURRENT
, true);
336 osdStatSetState(OSD_STAT_USED_MAH
, true);
337 osdStatSetState(OSD_STAT_BLACKBOX
, true);
338 osdStatSetState(OSD_STAT_BLACKBOX_NUMBER
, true);
339 osdStatSetState(OSD_STAT_TIMER_2
, true);
341 osdConfig
->units
= UNIT_METRIC
;
343 // Enable all warnings by default
344 for (int i
=0; i
< OSD_WARNING_COUNT
; i
++) {
345 osdWarnSetState(i
, true);
347 // turn off RSSI & Link Quality warnings by default
348 osdWarnSetState(OSD_WARNING_RSSI
, false);
349 osdWarnSetState(OSD_WARNING_LINK_QUALITY
, false);
350 osdWarnSetState(OSD_WARNING_RSSI_DBM
, false);
351 osdWarnSetState(OSD_WARNING_RSNR
, false);
352 // turn off the over mah capacity warning
353 osdWarnSetState(OSD_WARNING_OVER_CAP
, false);
355 osdConfig
->timers
[OSD_TIMER_1
] = osdTimerDefault
[OSD_TIMER_1
];
356 osdConfig
->timers
[OSD_TIMER_2
] = osdTimerDefault
[OSD_TIMER_2
];
358 osdConfig
->overlay_radio_mode
= 2;
360 osdConfig
->rssi_alarm
= 20;
361 osdConfig
->link_quality_alarm
= 80;
362 osdConfig
->cap_alarm
= 2200;
363 osdConfig
->alt_alarm
= 100; // meters or feet depend on configuration
364 osdConfig
->esc_temp_alarm
= ESC_TEMP_ALARM_OFF
; // off by default
365 osdConfig
->esc_rpm_alarm
= ESC_RPM_ALARM_OFF
; // off by default
366 osdConfig
->esc_current_alarm
= ESC_CURRENT_ALARM_OFF
; // off by default
367 osdConfig
->core_temp_alarm
= 70; // a temperature above 70C should produce a warning, lockups have been reported above 80C
369 osdConfig
->ahMaxPitch
= 20; // 20 degrees
370 osdConfig
->ahMaxRoll
= 40; // 40 degrees
372 osdConfig
->osdProfileIndex
= 1;
373 osdConfig
->ahInvert
= false;
374 for (int i
=0; i
< OSD_PROFILE_COUNT
; i
++) {
375 osdConfig
->profile
[i
][0] = '\0';
377 osdConfig
->rssi_dbm_alarm
= -60;
378 osdConfig
->rsnr_alarm
= 4;
379 osdConfig
->gps_sats_show_hdop
= false;
381 for (int i
= 0; i
< OSD_RCCHANNELS_COUNT
; i
++) {
382 osdConfig
->rcChannels
[i
] = -1;
385 osdConfig
->displayPortDevice
= OSD_DISPLAYPORT_DEVICE_AUTO
;
387 osdConfig
->distance_alarm
= 0;
388 osdConfig
->logo_on_arming
= OSD_LOGO_ARMING_OFF
;
389 osdConfig
->logo_on_arming_duration
= 5; // 0.5 seconds
391 osdConfig
->camera_frame_width
= 24;
392 osdConfig
->camera_frame_height
= 11;
394 osdConfig
->stat_show_cell_value
= false;
395 osdConfig
->framerate_hz
= OSD_FRAMERATE_DEFAULT_HZ
;
396 osdConfig
->cms_background_type
= DISPLAY_BACKGROUND_TRANSPARENT
;
397 #ifdef USE_CRAFTNAME_MSGS
398 osdConfig
->osd_craftname_msgs
= false; // Insert LQ/RSSI-dBm and warnings into CraftName
399 #endif //USE_CRAFTNAME_MSGS
401 osdConfig
->aux_channel
= 1;
402 osdConfig
->aux_scale
= 200;
403 osdConfig
->aux_symbol
= 'A';
405 osdConfig
->canvas_cols
= OSD_HD_COLS
;
406 osdConfig
->canvas_rows
= OSD_HD_ROWS
;
409 void pgResetFn_osdElementConfig(osdElementConfig_t
*osdElementConfig
)
411 // Position elements near centre of screen and disabled by default
412 for (int i
= 0; i
< OSD_ITEM_COUNT
; i
++) {
413 osdElementConfig
->item_pos
[i
] = OSD_POS(10, 7);
416 // Always enable warnings elements by default
417 uint16_t profileFlags
= 0;
418 for (unsigned i
= 1; i
<= OSD_PROFILE_COUNT
; i
++) {
419 profileFlags
|= OSD_PROFILE_FLAG(i
);
421 osdElementConfig
->item_pos
[OSD_WARNINGS
] = OSD_POS(9, 10) | profileFlags
;
423 // Default to old fixed positions for these elements
424 osdElementConfig
->item_pos
[OSD_CROSSHAIRS
] = OSD_POS(13, 6);
425 osdElementConfig
->item_pos
[OSD_ARTIFICIAL_HORIZON
] = OSD_POS(14, 2);
426 osdElementConfig
->item_pos
[OSD_HORIZON_SIDEBARS
] = OSD_POS(14, 6);
427 osdElementConfig
->item_pos
[OSD_CAMERA_FRAME
] = OSD_POS(3, 1);
428 osdElementConfig
->item_pos
[OSD_UP_DOWN_REFERENCE
] = OSD_POS(13, 6);
431 static void osdDrawLogo(int x
, int y
)
433 // display logo and help
434 int fontOffset
= 160;
435 for (int row
= 0; row
< OSD_LOGO_ROWS
; row
++) {
436 for (int column
= 0; column
< OSD_LOGO_COLS
; column
++) {
437 if (fontOffset
<= SYM_END_OF_FONT
)
438 displayWriteChar(osdDisplayPort
, x
+ column
, y
+ row
, DISPLAYPORT_ATTR_NORMAL
, fontOffset
++);
443 static void osdCompleteInitialization(void)
445 uint8_t midRow
= osdDisplayPort
->rows
/ 2;
446 uint8_t midCol
= osdDisplayPort
->cols
/ 2;
448 armState
= ARMING_FLAG(ARMED
);
452 backgroundLayerSupported
= displayLayerSupported(osdDisplayPort
, DISPLAYPORT_LAYER_BACKGROUND
);
453 displayLayerSelect(osdDisplayPort
, DISPLAYPORT_LAYER_FOREGROUND
);
455 displayBeginTransaction(osdDisplayPort
, DISPLAY_TRANSACTION_OPT_RESET_DRAWING
);
456 displayClearScreen(osdDisplayPort
, DISPLAY_CLEAR_WAIT
);
458 osdDrawLogo(midCol
- (OSD_LOGO_COLS
) / 2, midRow
- 5);
460 char string_buffer
[30];
461 tfp_sprintf(string_buffer
, "V%s", FC_VERSION_STRING
);
462 displayWrite(osdDisplayPort
, midCol
+ 5, midRow
, DISPLAYPORT_ATTR_NORMAL
, string_buffer
);
464 displayWrite(osdDisplayPort
, midCol
- 8, midRow
+ 2, DISPLAYPORT_ATTR_NORMAL
, CMS_STARTUP_HELP_TEXT1
);
465 displayWrite(osdDisplayPort
, midCol
- 4, midRow
+ 3, DISPLAYPORT_ATTR_NORMAL
, CMS_STARTUP_HELP_TEXT2
);
466 displayWrite(osdDisplayPort
, midCol
- 4, midRow
+ 4, DISPLAYPORT_ATTR_NORMAL
, CMS_STARTUP_HELP_TEXT3
);
470 char dateTimeBuffer
[FORMATTED_DATE_TIME_BUFSIZE
];
471 if (osdFormatRtcDateTime(&dateTimeBuffer
[0])) {
472 displayWrite(osdDisplayPort
, midCol
- 10, midRow
+ 6, DISPLAYPORT_ATTR_NORMAL
, dateTimeBuffer
);
476 resumeRefreshAt
= micros() + (4 * REFRESH_1S
);
477 #ifdef USE_OSD_PROFILES
478 setOsdProfile(osdConfig()->osdProfileIndex
);
481 osdElementsInit(backgroundLayerSupported
);
482 osdAnalyzeActiveElements();
487 void osdInit(displayPort_t
*osdDisplayPortToUse
, osdDisplayPortDevice_e displayPortDeviceType
)
489 osdDisplayPortDeviceType
= displayPortDeviceType
;
491 if (!osdDisplayPortToUse
) {
495 osdDisplayPort
= osdDisplayPortToUse
;
497 cmsDisplayPortRegister(osdDisplayPort
);
500 // Ensure that all OSD elements are on the canvas
501 for (int i
= 0; i
< OSD_ITEM_COUNT
; i
++) {
502 uint16_t itemPos
= osdElementConfig()->item_pos
[i
];
503 uint8_t elemPosX
= OSD_X(itemPos
);
504 uint8_t elemPosY
= OSD_Y(itemPos
);
505 uint16_t elemProfileType
= itemPos
& (OSD_PROFILE_MASK
| OSD_TYPE_MASK
);
506 bool pos_reset
= false;
508 if (elemPosX
>= osdDisplayPort
->cols
) {
509 elemPosX
= osdDisplayPort
->cols
- 1;
513 if (elemPosY
>= osdDisplayPort
->rows
) {
514 elemPosY
= osdDisplayPort
->rows
- 1;
519 osdElementConfigMutable()->item_pos
[i
] = elemProfileType
| OSD_POS(elemPosX
, elemPosY
);
525 static void osdResetStats(void)
527 stats
.max_current
= 0;
529 stats
.min_voltage
= 5000;
530 stats
.end_voltage
= 0;
531 stats
.min_rssi
= 99; // percent
532 stats
.max_altitude
= 0;
533 stats
.max_distance
= 0;
534 stats
.armed_time
= 0;
535 stats
.max_g_force
= 0;
536 stats
.max_esc_temp_ix
= 0;
537 stats
.max_esc_temp
= 0;
538 stats
.max_esc_rpm
= 0;
539 stats
.min_link_quality
= (linkQualitySource
== LQ_SOURCE_NONE
) ? 99 : 100; // percent
540 stats
.min_rssi_dbm
= CRSF_RSSI_MAX
;
541 stats
.min_rsnr
= CRSF_SNR_MAX
;
544 #if defined(USE_ESC_SENSOR) || defined(USE_DSHOT_TELEMETRY)
545 static int32_t getAverageEscRpm(void)
547 #ifdef USE_ESC_SENSOR
548 if (featureIsEnabled(FEATURE_ESC_SENSOR
)) {
549 return erpmToRpm(osdEscDataCombined
->rpm
);
552 #ifdef USE_DSHOT_TELEMETRY
553 if (motorConfig()->dev
.useDshotTelemetry
) {
554 return getDshotAverageRpm();
561 static uint16_t getStatsVoltage(void)
563 return osdConfig()->stat_show_cell_value
? getBatteryAverageCellVoltage() : getBatteryVoltage();
566 static void osdUpdateStats(void)
571 if (gpsConfig()->gps_use_3d_speed
) {
572 value
= gpsSol
.speed3d
;
574 value
= gpsSol
.groundSpeed
;
576 if (stats
.max_speed
< value
) {
577 stats
.max_speed
= value
;
581 value
= getStatsVoltage();
582 if (stats
.min_voltage
> value
) {
583 stats
.min_voltage
= value
;
586 value
= getAmperage() / 100;
587 if (stats
.max_current
< value
) {
588 stats
.max_current
= value
;
591 value
= getRssiPercent();
592 if (stats
.min_rssi
> value
) {
593 stats
.min_rssi
= value
;
596 int32_t altitudeCm
= getEstimatedAltitudeCm();
597 if (stats
.max_altitude
< altitudeCm
) {
598 stats
.max_altitude
= altitudeCm
;
602 if (stats
.max_g_force
< osdGForce
) {
603 stats
.max_g_force
= osdGForce
;
607 #ifdef USE_RX_LINK_QUALITY_INFO
608 value
= rxGetLinkQualityPercent();
609 if (stats
.min_link_quality
> value
) {
610 stats
.min_link_quality
= value
;
614 #ifdef USE_RX_RSSI_DBM
615 value
= getRssiDbm();
616 if (stats
.min_rssi_dbm
> value
) {
617 stats
.min_rssi_dbm
= value
;
623 if (stats
.min_rsnr
> value
) {
624 stats
.min_rsnr
= value
;
629 if (STATE(GPS_FIX
) && STATE(GPS_FIX_HOME
)) {
630 if (stats
.max_distance
< GPS_distanceToHome
) {
631 stats
.max_distance
= GPS_distanceToHome
;
636 #if defined(USE_ESC_SENSOR)
637 if (featureIsEnabled(FEATURE_ESC_SENSOR
)) {
638 value
= osdEscDataCombined
->temperature
;
639 if (stats
.max_esc_temp
< value
) {
640 stats
.max_esc_temp
= value
;
644 #if defined(USE_DSHOT_TELEMETRY)
646 // Take max temp from dshot telemetry
647 for (uint8_t k
= 0; k
< getMotorCount(); k
++) {
648 if (dshotTelemetryState
.motorState
[k
].maxTemp
> stats
.max_esc_temp
) {
649 stats
.max_esc_temp_ix
= k
+ 1;
650 stats
.max_esc_temp
= dshotTelemetryState
.motorState
[k
].maxTemp
;
658 #if defined(USE_ESC_SENSOR) || defined(USE_DSHOT_TELEMETRY)
659 int32_t rpm
= getAverageEscRpm();
660 if (stats
.max_esc_rpm
< rpm
) {
661 stats
.max_esc_rpm
= rpm
;
668 static void osdGetBlackboxStatusString(char * buff
)
670 bool storageDeviceIsWorking
= isBlackboxDeviceWorking();
671 uint32_t storageUsed
= 0;
672 uint32_t storageTotal
= 0;
674 switch (blackboxConfig()->device
) {
676 case BLACKBOX_DEVICE_SDCARD
:
677 if (storageDeviceIsWorking
) {
678 storageTotal
= sdcard_getMetadata()->numBlocks
/ 2000;
679 storageUsed
= storageTotal
- (afatfs_getContiguousFreeSpace() / 1024000);
685 case BLACKBOX_DEVICE_FLASH
:
686 if (storageDeviceIsWorking
) {
688 const flashPartition_t
*flashPartition
= flashPartitionFindByType(FLASH_PARTITION_TYPE_FLASHFS
);
689 const flashGeometry_t
*flashGeometry
= flashGetGeometry();
691 storageTotal
= ((FLASH_PARTITION_SECTOR_COUNT(flashPartition
) * flashGeometry
->sectorSize
) / 1024);
692 storageUsed
= flashfsGetOffset() / 1024;
701 if (storageDeviceIsWorking
) {
702 const uint16_t storageUsedPercent
= (storageUsed
* 100) / storageTotal
;
703 tfp_sprintf(buff
, "%d%%", storageUsedPercent
);
705 tfp_sprintf(buff
, "FAULT");
710 static void osdDisplayStatisticLabel(uint8_t x
, uint8_t y
, const char * text
, const char * value
)
712 displayWrite(osdDisplayPort
, x
- 13, y
, DISPLAYPORT_ATTR_NORMAL
, text
);
713 displayWrite(osdDisplayPort
, x
+ 5, y
, DISPLAYPORT_ATTR_NORMAL
, ":");
714 displayWrite(osdDisplayPort
, x
+ 7, y
, DISPLAYPORT_ATTR_NORMAL
, value
);
718 * Test if there's some stat enabled
720 static bool isSomeStatEnabled(void)
722 return (osdConfig()->enabled_stats
!= 0);
726 // The stats display order was previously required to match the enumeration definition so it matched
727 // the order shown in the configurator. However, to allow reordering this screen without breaking the
728 // compatibility, this requirement has been relaxed to a best effort approach. Reordering the elements
729 // on the stats screen will have to be more beneficial than the hassle of not matching exactly to the
730 // configurator list.
732 static bool osdDisplayStat(int statistic
, uint8_t displayRow
)
734 uint8_t midCol
= osdDisplayPort
->cols
/ 2;
735 char buff
[OSD_ELEMENT_BUFFER_LENGTH
];
738 case OSD_STAT_RTC_DATE_TIME
: {
739 bool success
= false;
741 success
= osdFormatRtcDateTime(&buff
[0]);
744 tfp_sprintf(buff
, "NO RTC");
747 displayWrite(osdDisplayPort
, midCol
- 13, displayRow
, DISPLAYPORT_ATTR_NORMAL
, buff
);
751 case OSD_STAT_TIMER_1
:
752 osdFormatTimer(buff
, false, (OSD_TIMER_SRC(osdConfig()->timers
[OSD_TIMER_1
]) == OSD_TIMER_SRC_ON
? false : true), OSD_TIMER_1
);
753 osdDisplayStatisticLabel(midCol
, displayRow
, osdTimerSourceNames
[OSD_TIMER_SRC(osdConfig()->timers
[OSD_TIMER_1
])], buff
);
756 case OSD_STAT_TIMER_2
:
757 osdFormatTimer(buff
, false, (OSD_TIMER_SRC(osdConfig()->timers
[OSD_TIMER_2
]) == OSD_TIMER_SRC_ON
? false : true), OSD_TIMER_2
);
758 osdDisplayStatisticLabel(midCol
, displayRow
, osdTimerSourceNames
[OSD_TIMER_SRC(osdConfig()->timers
[OSD_TIMER_2
])], buff
);
761 case OSD_STAT_MAX_ALTITUDE
: {
762 osdPrintFloat(buff
, SYM_NONE
, osdGetMetersToSelectedUnit(stats
.max_altitude
) / 100.0f
, "", 1, true, osdGetMetersToSelectedUnitSymbol());
763 osdDisplayStatisticLabel(midCol
, displayRow
, "MAX ALTITUDE", buff
);
768 case OSD_STAT_MAX_SPEED
:
769 if (featureIsEnabled(FEATURE_GPS
)) {
770 tfp_sprintf(buff
, "%d%c", osdGetSpeedToSelectedUnit(stats
.max_speed
), osdGetSpeedToSelectedUnitSymbol());
771 osdDisplayStatisticLabel(midCol
, displayRow
, "MAX SPEED", buff
);
776 case OSD_STAT_MAX_DISTANCE
:
777 if (featureIsEnabled(FEATURE_GPS
)) {
778 osdFormatDistanceString(buff
, stats
.max_distance
, SYM_NONE
);
779 osdDisplayStatisticLabel(midCol
, displayRow
, "MAX DISTANCE", buff
);
784 case OSD_STAT_FLIGHT_DISTANCE
:
785 if (featureIsEnabled(FEATURE_GPS
)) {
786 const int distanceFlown
= GPS_distanceFlownInCm
/ 100;
787 osdFormatDistanceString(buff
, distanceFlown
, SYM_NONE
);
788 osdDisplayStatisticLabel(midCol
, displayRow
, "FLIGHT DISTANCE", buff
);
794 case OSD_STAT_MIN_BATTERY
:
795 osdPrintFloat(buff
, SYM_NONE
, stats
.min_voltage
/ 100.0f
, "", 2, true, SYM_VOLT
);
796 osdDisplayStatisticLabel(midCol
, displayRow
, osdConfig()->stat_show_cell_value
? "MIN AVG CELL" : "MIN BATTERY", buff
);
799 case OSD_STAT_END_BATTERY
:
800 osdPrintFloat(buff
, SYM_NONE
, stats
.end_voltage
/ 100.0f
, "", 2, true, SYM_VOLT
);
801 osdDisplayStatisticLabel(midCol
, displayRow
, osdConfig()->stat_show_cell_value
? "END AVG CELL" : "END BATTERY", buff
);
804 case OSD_STAT_BATTERY
:
806 const uint16_t statsVoltage
= getStatsVoltage();
807 osdPrintFloat(buff
, SYM_NONE
, statsVoltage
/ 100.0f
, "", 2, true, SYM_VOLT
);
808 osdDisplayStatisticLabel(midCol
, displayRow
, osdConfig()->stat_show_cell_value
? "AVG BATT CELL" : "BATTERY", buff
);
813 case OSD_STAT_MIN_RSSI
:
814 itoa(stats
.min_rssi
, buff
, 10);
816 osdDisplayStatisticLabel(midCol
, displayRow
, "MIN RSSI", buff
);
819 case OSD_STAT_MAX_CURRENT
:
820 if (batteryConfig()->currentMeterSource
!= CURRENT_METER_NONE
) {
821 tfp_sprintf(buff
, "%d%c", stats
.max_current
, SYM_AMP
);
822 osdDisplayStatisticLabel(midCol
, displayRow
, "MAX CURRENT", buff
);
827 case OSD_STAT_USED_MAH
:
828 if (batteryConfig()->currentMeterSource
!= CURRENT_METER_NONE
) {
829 tfp_sprintf(buff
, "%d%c", getMAhDrawn(), SYM_MAH
);
830 osdDisplayStatisticLabel(midCol
, displayRow
, "USED MAH", buff
);
835 case OSD_STAT_WATT_HOURS_DRAWN
:
836 if (batteryConfig()->currentMeterSource
!= CURRENT_METER_NONE
) {
837 osdPrintFloat(buff
, SYM_NONE
, getWhDrawn(), "", 2, true, SYM_NONE
);
838 osdDisplayStatisticLabel(midCol
, displayRow
, "USED WATT HOURS", buff
);
844 case OSD_STAT_BLACKBOX
:
845 if (blackboxConfig()->device
&& blackboxConfig()->device
!= BLACKBOX_DEVICE_SERIAL
) {
846 osdGetBlackboxStatusString(buff
);
847 osdDisplayStatisticLabel(midCol
, displayRow
, "BLACKBOX", buff
);
852 case OSD_STAT_BLACKBOX_NUMBER
:
854 int32_t logNumber
= blackboxGetLogNumber();
855 if (logNumber
>= 0) {
856 itoa(logNumber
, buff
, 10);
857 osdDisplayStatisticLabel(midCol
, displayRow
, "BB LOG NUM", buff
);
865 case OSD_STAT_MAX_G_FORCE
:
866 if (sensors(SENSOR_ACC
)) {
867 osdPrintFloat(buff
, SYM_NONE
, stats
.max_g_force
, "", 1, true, 'G');
868 osdDisplayStatisticLabel(midCol
, displayRow
, "MAX G-FORCE", buff
);
874 #ifdef USE_ESC_SENSOR
875 case OSD_STAT_MAX_ESC_TEMP
:
878 if (stats
.max_esc_temp_ix
> 0) {
879 ix
= tfp_sprintf(buff
, "%d ", stats
.max_esc_temp_ix
);
881 tfp_sprintf(buff
+ ix
, "%d%c", osdConvertTemperatureToSelectedUnit(stats
.max_esc_temp
), osdGetTemperatureSymbolForSelectedUnit());
882 osdDisplayStatisticLabel(midCol
, displayRow
, "MAX ESC TEMP", buff
);
887 #if defined(USE_ESC_SENSOR) || defined(USE_DSHOT_TELEMETRY)
888 case OSD_STAT_MAX_ESC_RPM
:
889 itoa(stats
.max_esc_rpm
, buff
, 10);
890 osdDisplayStatisticLabel(midCol
, displayRow
, "MAX ESC RPM", buff
);
894 #ifdef USE_RX_LINK_QUALITY_INFO
895 case OSD_STAT_MIN_LINK_QUALITY
:
896 tfp_sprintf(buff
, "%d", stats
.min_link_quality
);
898 osdDisplayStatisticLabel(midCol
, displayRow
, "MIN LINK", buff
);
902 #if defined(USE_DYN_NOTCH_FILTER)
903 case OSD_STAT_MAX_FFT
:
904 if (isDynNotchActive()) {
905 int value
= getMaxFFT();
907 tfp_sprintf(buff
, "%dHZ", value
);
908 osdDisplayStatisticLabel(midCol
, displayRow
, "PEAK FFT", buff
);
910 osdDisplayStatisticLabel(midCol
, displayRow
, "PEAK FFT", "THRT<20%");
917 #ifdef USE_RX_RSSI_DBM
918 case OSD_STAT_MIN_RSSI_DBM
:
919 tfp_sprintf(buff
, "%3d", stats
.min_rssi_dbm
);
920 osdDisplayStatisticLabel(midCol
, displayRow
, "MIN RSSI DBM", buff
);
925 case OSD_STAT_MIN_RSNR
:
926 tfp_sprintf(buff
, "%3d", stats
.min_rsnr
);
927 osdDisplayStatisticLabel(midCol
, displayRow
, "MIN RSNR", buff
);
931 #ifdef USE_PERSISTENT_STATS
932 case OSD_STAT_TOTAL_FLIGHTS
:
933 itoa(statsConfig()->stats_total_flights
, buff
, 10);
934 osdDisplayStatisticLabel(midCol
, displayRow
, "TOTAL FLIGHTS", buff
);
937 case OSD_STAT_TOTAL_TIME
: {
938 int minutes
= statsConfig()->stats_total_time_s
/ 60;
939 tfp_sprintf(buff
, "%d:%02dH", minutes
/ 60, minutes
% 60);
940 osdDisplayStatisticLabel(midCol
, displayRow
, "TOTAL FLIGHT TIME", buff
);
944 case OSD_STAT_TOTAL_DIST
:
945 #define METERS_PER_KILOMETER 1000
946 #define METERS_PER_MILE 1609
947 if (osdConfig()->units
== UNIT_IMPERIAL
) {
948 tfp_sprintf(buff
, "%d%c", statsConfig()->stats_total_dist_m
/ METERS_PER_MILE
, SYM_MILES
);
950 tfp_sprintf(buff
, "%d%c", statsConfig()->stats_total_dist_m
/ METERS_PER_KILOMETER
, SYM_KM
);
952 osdDisplayStatisticLabel(midCol
, displayRow
, "TOTAL DISTANCE", buff
);
959 typedef struct osdStatsRenderingState_s
{
963 } osdStatsRenderingState_t
;
965 static osdStatsRenderingState_t osdStatsRenderingState
;
967 static void osdRenderStatsReset(void)
969 // reset to 0 so it will be recalculated on the next stats refresh
970 osdStatsRenderingState
.rowCount
= 0;
973 static void osdRenderStatsBegin(void)
975 osdStatsRenderingState
.row
= 0;
976 osdStatsRenderingState
.index
= 0;
980 // call repeatedly until it returns true which indicates that all stats have been rendered.
981 static bool osdRenderStatsContinue(void)
983 uint8_t midCol
= osdDisplayPort
->cols
/ 2;
985 if (osdStatsRenderingState
.row
== 0) {
987 bool displayLabel
= false;
989 // if rowCount is 0 then we're running an initial analysis of the active stats items
990 if (osdStatsRenderingState
.rowCount
> 0) {
991 const int availableRows
= osdDisplayPort
->rows
;
992 int displayRows
= MIN(osdStatsRenderingState
.rowCount
, availableRows
);
993 if (osdStatsRenderingState
.rowCount
< availableRows
) {
997 osdStatsRenderingState
.row
= (availableRows
- displayRows
) / 2; // center the stats vertically
1001 displayWrite(osdDisplayPort
, midCol
- (strlen("--- STATS ---") / 2), osdStatsRenderingState
.row
++, DISPLAYPORT_ATTR_NORMAL
, "--- STATS ---");
1007 bool renderedStat
= false;
1009 while (osdStatsRenderingState
.index
< OSD_STAT_COUNT
) {
1010 int index
= osdStatsRenderingState
.index
;
1012 // prepare for the next call to the method
1013 osdStatsRenderingState
.index
++;
1015 // look for something to render
1016 if (osdStatGetState(osdStatsDisplayOrder
[index
])) {
1017 if (osdDisplayStat(osdStatsDisplayOrder
[index
], osdStatsRenderingState
.row
)) {
1018 osdStatsRenderingState
.row
++;
1019 renderedStat
= true;
1025 bool moreSpaceAvailable
= osdStatsRenderingState
.row
< osdDisplayPort
->rows
;
1027 if (renderedStat
&& moreSpaceAvailable
) {
1031 if (osdStatsRenderingState
.rowCount
== 0) {
1032 osdStatsRenderingState
.rowCount
= osdStatsRenderingState
.row
;
1038 // returns true when all phases are complete
1039 static bool osdRefreshStats(void)
1041 bool completed
= false;
1044 INITIAL_CLEAR_SCREEN
= 0,
1048 } osdRefreshStatsPhase_e
;
1050 static osdRefreshStatsPhase_e phase
= INITIAL_CLEAR_SCREEN
;
1054 case INITIAL_CLEAR_SCREEN
:
1055 osdRenderStatsBegin();
1056 if (osdStatsRenderingState
.rowCount
> 0) {
1057 phase
= RENDER_STATS
;
1059 phase
= COUNT_STATS
;
1061 displayClearScreen(osdDisplayPort
, DISPLAY_CLEAR_NONE
);
1065 // No stats row count has been set yet.
1066 // Go through the logic one time to determine how many stats are actually displayed.
1067 bool count_phase_complete
= osdRenderStatsContinue();
1068 if (count_phase_complete
) {
1069 phase
= CLEAR_SCREEN
;
1074 osdRenderStatsBegin();
1075 // Then clear the screen and commence with normal stats display which will
1076 // determine if the heading should be displayed and also center the content vertically.
1077 displayClearScreen(osdDisplayPort
, DISPLAY_CLEAR_NONE
);
1078 phase
= RENDER_STATS
;
1081 completed
= osdRenderStatsContinue();
1086 phase
= INITIAL_CLEAR_SCREEN
;
1092 static timeDelta_t
osdShowArmed(void)
1094 uint8_t midRow
= osdDisplayPort
->rows
/ 2;
1095 uint8_t midCol
= osdDisplayPort
->cols
/ 2;
1098 displayClearScreen(osdDisplayPort
, DISPLAY_CLEAR_WAIT
);
1100 if ((osdConfig()->logo_on_arming
== OSD_LOGO_ARMING_ON
) || ((osdConfig()->logo_on_arming
== OSD_LOGO_ARMING_FIRST
) && !ARMING_FLAG(WAS_EVER_ARMED
))) {
1101 uint8_t midRow
= osdDisplayPort
->rows
/ 2;
1102 uint8_t midCol
= osdDisplayPort
->cols
/ 2;
1103 osdDrawLogo(midCol
- (OSD_LOGO_COLS
) / 2, midRow
- 5);
1104 ret
= osdConfig()->logo_on_arming_duration
* 1e5
;
1106 ret
= (REFRESH_1S
/ 2);
1108 displayWrite(osdDisplayPort
, midCol
- (strlen("ARMED") / 2), midRow
, DISPLAYPORT_ATTR_NORMAL
, "ARMED");
1110 if (isFlipOverAfterCrashActive()) {
1111 displayWrite(osdDisplayPort
, midCol
- (strlen(CRASH_FLIP_WARNING
) / 2), midRow
+ 1, DISPLAYPORT_ATTR_NORMAL
, CRASH_FLIP_WARNING
);
1117 static bool osdStatsVisible
= false;
1118 static bool osdStatsEnabled
= false;
1120 STATIC_UNIT_TESTED
bool osdProcessStats1(timeUs_t currentTimeUs
)
1122 static timeUs_t lastTimeUs
= 0;
1123 static timeUs_t osdStatsRefreshTimeUs
;
1124 static timeUs_t osdAuxRefreshTimeUs
= 0;
1126 bool refreshStatsRequired
= false;
1128 // detect arm/disarm
1129 if (armState
!= ARMING_FLAG(ARMED
)) {
1130 if (ARMING_FLAG(ARMED
)) {
1131 osdStatsEnabled
= false;
1132 osdStatsVisible
= false;
1134 resumeRefreshAt
= osdShowArmed() + currentTimeUs
;
1135 } else if (isSomeStatEnabled()
1136 && !suppressStatsDisplay
1137 && !failsafeIsActive()
1138 && (!(getArmingDisableFlags() & (ARMING_DISABLED_RUNAWAY_TAKEOFF
| ARMING_DISABLED_CRASH_DETECTED
))
1139 || !VISIBLE(osdElementConfig()->item_pos
[OSD_WARNINGS
]))) { // suppress stats if runaway takeoff triggered disarm and WARNINGS element is visible
1140 osdStatsEnabled
= true;
1141 resumeRefreshAt
= currentTimeUs
+ (60 * REFRESH_1S
);
1142 stats
.end_voltage
= getStatsVoltage();
1143 osdRenderStatsReset();
1146 armState
= ARMING_FLAG(ARMED
);
1149 if (ARMING_FLAG(ARMED
)) {
1151 timeUs_t deltaT
= currentTimeUs
- lastTimeUs
;
1152 osdFlyTime
+= deltaT
;
1153 stats
.armed_time
+= deltaT
;
1154 } else if (osdStatsEnabled
) { // handle showing/hiding stats based on OSD disable switch position
1155 if (displayIsGrabbed(osdDisplayPort
)) {
1156 osdStatsEnabled
= false;
1157 resumeRefreshAt
= 0;
1158 stats
.armed_time
= 0;
1160 if (IS_RC_MODE_ACTIVE(BOXOSD
) && osdStatsVisible
) {
1161 osdStatsVisible
= false;
1162 displayClearScreen(osdDisplayPort
, DISPLAY_CLEAR_NONE
);
1163 } else if (!IS_RC_MODE_ACTIVE(BOXOSD
)) {
1164 if (!osdStatsVisible
) {
1165 osdStatsVisible
= true;
1166 osdStatsRefreshTimeUs
= 0;
1168 if (currentTimeUs
>= osdStatsRefreshTimeUs
) {
1169 osdStatsRefreshTimeUs
= currentTimeUs
+ REFRESH_1S
;
1170 refreshStatsRequired
= true;
1176 if (VISIBLE(osdElementConfig()->item_pos
[OSD_AUX_VALUE
])) {
1177 const uint8_t auxChannel
= osdConfig()->aux_channel
+ NON_AUX_CHANNEL_COUNT
- 1;
1178 if (currentTimeUs
> osdAuxRefreshTimeUs
) {
1179 // aux channel start after main channels
1180 osdAuxValue
= (constrain(rcData
[auxChannel
], PWM_RANGE_MIN
, PWM_RANGE_MAX
) - PWM_RANGE_MIN
) * osdConfig()->aux_scale
/ PWM_RANGE
;
1181 osdAuxRefreshTimeUs
= currentTimeUs
+ REFRESH_1S
;
1185 lastTimeUs
= currentTimeUs
;
1187 return refreshStatsRequired
;
1190 void osdProcessStats2(timeUs_t currentTimeUs
)
1192 displayBeginTransaction(osdDisplayPort
, DISPLAY_TRANSACTION_OPT_RESET_DRAWING
);
1194 if (resumeRefreshAt
) {
1195 if (cmp32(currentTimeUs
, resumeRefreshAt
) < 0) {
1196 // in timeout period, check sticks for activity or CRASH FLIP switch to resume display.
1197 if (!ARMING_FLAG(ARMED
) &&
1198 (IS_HI(THROTTLE
) || IS_HI(PITCH
) || IS_RC_MODE_ACTIVE(BOXFLIPOVERAFTERCRASH
))) {
1199 resumeRefreshAt
= currentTimeUs
;
1203 displayClearScreen(osdDisplayPort
, DISPLAY_CLEAR_NONE
);
1204 resumeRefreshAt
= 0;
1205 osdStatsEnabled
= false;
1206 stats
.armed_time
= 0;
1209 schedulerIgnoreTaskExecTime();
1211 #ifdef USE_ESC_SENSOR
1212 if (featureIsEnabled(FEATURE_ESC_SENSOR
)) {
1213 osdEscDataCombined
= getEscSensorData(ESC_SENSOR_COMBINED
);
1218 void osdProcessStats3(void)
1220 #if defined(USE_ACC)
1221 if (sensors(SENSOR_ACC
)
1222 && (VISIBLE(osdElementConfig()->item_pos
[OSD_G_FORCE
]) || osdStatGetState(OSD_STAT_MAX_G_FORCE
))) {
1223 // only calculate the G force if the element is visible or the stat is enabled
1224 for (int axis
= 0; axis
< XYZ_AXIS_COUNT
; axis
++) {
1225 const float a
= acc
.accADC
[axis
];
1228 osdGForce
= sqrtf(osdGForce
) * acc
.dev
.acc_1G_rec
;
1237 OSD_STATE_PROCESS_STATS1
,
1238 OSD_STATE_REFRESH_STATS
,
1239 OSD_STATE_PROCESS_STATS2
,
1240 OSD_STATE_PROCESS_STATS3
,
1241 OSD_STATE_UPDATE_ALARMS
,
1242 OSD_STATE_UPDATE_CANVAS
,
1243 OSD_STATE_GROUP_ELEMENTS
,
1244 OSD_STATE_UPDATE_ELEMENTS
,
1245 OSD_STATE_UPDATE_HEARTBEAT
,
1251 osdState_e osdState
= OSD_STATE_INIT
;
1253 #define OSD_UPDATE_INTERVAL_US (1000000 / osdConfig()->framerate_hz)
1255 // Called periodically by the scheduler
1256 bool osdUpdateCheck(timeUs_t currentTimeUs
, timeDelta_t currentDeltaTimeUs
)
1258 UNUSED(currentDeltaTimeUs
);
1259 static timeUs_t osdUpdateDueUs
= 0;
1261 if (osdState
== OSD_STATE_IDLE
) {
1262 // If the OSD is due a refresh, mark that as being the case
1263 if (cmpTimeUs(currentTimeUs
, osdUpdateDueUs
) > 0) {
1264 osdState
= OSD_STATE_CHECK
;
1266 // Determine time of next update
1267 if (osdUpdateDueUs
) {
1268 osdUpdateDueUs
+= OSD_UPDATE_INTERVAL_US
;
1270 osdUpdateDueUs
= currentTimeUs
+ OSD_UPDATE_INTERVAL_US
;
1275 return (osdState
!= OSD_STATE_IDLE
);
1278 // Called when there is OSD update work to be done
1279 void osdUpdate(timeUs_t currentTimeUs
)
1281 static uint16_t osdStateDurationFractionUs
[OSD_STATE_COUNT
] = { 0 };
1282 static uint32_t osdElementDurationUs
[OSD_ITEM_COUNT
] = { 0 };
1283 static uint8_t osdElementGroupMemberships
[OSD_ITEM_COUNT
];
1284 static uint16_t osdElementGroupTargetFractionUs
[OSD_GROUP_COUNT
] = { 0 };
1285 static uint16_t osdElementGroupDurationFractionUs
[OSD_GROUP_COUNT
] = { 0 };
1286 static uint8_t osdElementGroup
;
1287 static bool firstPass
= true;
1288 uint8_t osdCurrentElementGroup
= 0;
1289 timeUs_t executeTimeUs
;
1290 osdState_e osdCurrentState
= osdState
;
1292 if (osdState
!= OSD_STATE_UPDATE_CANVAS
) {
1293 schedulerIgnoreTaskExecRate();
1297 case OSD_STATE_INIT
:
1298 if (!displayCheckReady(osdDisplayPort
, false)) {
1299 // Frsky osd need a display redraw after search for MAX7456 devices
1300 if (osdDisplayPortDeviceType
== OSD_DISPLAYPORT_DEVICE_FRSKYOSD
) {
1301 displayRedraw(osdDisplayPort
);
1303 schedulerIgnoreTaskExecTime();
1308 osdCompleteInitialization();
1309 displayRedraw(osdDisplayPort
);
1310 osdState
= OSD_STATE_COMMIT
;
1314 case OSD_STATE_CHECK
:
1315 // don't touch buffers if DMA transaction is in progress
1316 if (displayIsTransferInProgress(osdDisplayPort
)) {
1320 osdState
= OSD_STATE_UPDATE_HEARTBEAT
;
1323 case OSD_STATE_UPDATE_HEARTBEAT
:
1324 if (displayHeartbeat(osdDisplayPort
)) {
1325 // Extraordinary action was taken, so return without allowing osdStateDurationFractionUs table to be updated
1329 osdState
= OSD_STATE_PROCESS_STATS1
;
1332 case OSD_STATE_PROCESS_STATS1
:
1334 bool refreshStatsRequired
= osdProcessStats1(currentTimeUs
);
1336 if (refreshStatsRequired
) {
1337 osdState
= OSD_STATE_REFRESH_STATS
;
1339 osdState
= OSD_STATE_PROCESS_STATS2
;
1343 case OSD_STATE_REFRESH_STATS
:
1345 bool completed
= osdRefreshStats();
1347 osdState
= OSD_STATE_PROCESS_STATS2
;
1351 case OSD_STATE_PROCESS_STATS2
:
1352 osdProcessStats2(currentTimeUs
);
1354 osdState
= OSD_STATE_PROCESS_STATS3
;
1356 case OSD_STATE_PROCESS_STATS3
:
1360 if (!displayIsGrabbed(osdDisplayPort
))
1363 osdState
= OSD_STATE_UPDATE_ALARMS
;
1367 osdState
= OSD_STATE_COMMIT
;
1370 case OSD_STATE_UPDATE_ALARMS
:
1373 if (resumeRefreshAt
) {
1374 osdState
= OSD_STATE_TRANSFER
;
1376 osdState
= OSD_STATE_UPDATE_CANVAS
;
1380 case OSD_STATE_UPDATE_CANVAS
:
1381 // Hide OSD when OSDSW mode is active
1382 if (IS_RC_MODE_ACTIVE(BOXOSD
)) {
1383 displayClearScreen(osdDisplayPort
, DISPLAY_CLEAR_NONE
);
1384 osdState
= OSD_STATE_COMMIT
;
1388 if (backgroundLayerSupported
) {
1389 // Background layer is supported, overlay it onto the foreground
1390 // so that we only need to draw the active parts of the elements.
1391 displayLayerCopy(osdDisplayPort
, DISPLAYPORT_LAYER_FOREGROUND
, DISPLAYPORT_LAYER_BACKGROUND
);
1393 // Background layer not supported, just clear the foreground in preparation
1394 // for drawing the elements including their backgrounds.
1395 displayClearScreen(osdDisplayPort
, DISPLAY_CLEAR_NONE
);
1399 static bool lastGpsSensorState
;
1400 // Handle the case that the GPS_SENSOR may be delayed in activation
1401 // or deactivate if communication is lost with the module.
1402 const bool currentGpsSensorState
= sensors(SENSOR_GPS
);
1403 if (lastGpsSensorState
!= currentGpsSensorState
) {
1404 lastGpsSensorState
= currentGpsSensorState
;
1405 osdAnalyzeActiveElements();
1411 osdState
= OSD_STATE_GROUP_ELEMENTS
;
1415 case OSD_STATE_GROUP_ELEMENTS
:
1417 uint8_t elementGroup
;
1418 uint8_t activeElements
= osdGetActiveElementCount();
1421 for (elementGroup
= 0; elementGroup
< OSD_GROUP_COUNT
; elementGroup
++) {
1422 if (osdElementGroupDurationFractionUs
[elementGroup
] > (OSD_ELEMENT_RENDER_TARGET
<< OSD_EXEC_TIME_SHIFT
)) {
1423 osdElementGroupDurationFractionUs
[elementGroup
] = 0;
1425 osdElementGroupTargetFractionUs
[elementGroup
] = 0;
1430 // Based on the current element rendering, group to execute in approx 40us
1431 for (uint8_t curElement
= 0; curElement
< activeElements
; curElement
++) {
1432 if ((osdElementGroupTargetFractionUs
[elementGroup
] == 0) ||
1433 (osdElementGroupTargetFractionUs
[elementGroup
] + (osdElementDurationUs
[curElement
]) <= (OSD_ELEMENT_RENDER_TARGET
<< OSD_EXEC_TIME_SHIFT
)) ||
1434 (elementGroup
== (OSD_GROUP_COUNT
- 1))) {
1435 osdElementGroupTargetFractionUs
[elementGroup
] += osdElementDurationUs
[curElement
];
1436 // If group membership changes, reset the stats for the group
1437 if (osdElementGroupMemberships
[curElement
] != elementGroup
) {
1438 osdElementGroupDurationFractionUs
[elementGroup
] = osdElementGroupTargetFractionUs
[elementGroup
] + (OSD_ELEMENT_RENDER_GROUP_MARGIN
<< OSD_EXEC_TIME_SHIFT
);
1440 osdElementGroupMemberships
[curElement
] = elementGroup
;
1443 // Try again for this element
1448 // Start with group 0
1449 osdElementGroup
= 0;
1451 if (activeElements
> 0) {
1452 osdState
= OSD_STATE_UPDATE_ELEMENTS
;
1454 osdState
= OSD_STATE_COMMIT
;
1459 case OSD_STATE_UPDATE_ELEMENTS
:
1461 osdCurrentElementGroup
= osdElementGroup
;
1462 bool moreElements
= true;
1465 timeUs_t startElementTime
= micros();
1466 uint8_t osdCurrentElement
= osdGetActiveElement();
1468 // This element should be rendered in the next group
1469 if (osdElementGroupMemberships
[osdCurrentElement
] != osdElementGroup
) {
1474 moreElements
= osdDrawNextActiveElement(osdDisplayPort
, currentTimeUs
);
1476 executeTimeUs
= micros() - startElementTime
;
1478 if (executeTimeUs
> (osdElementDurationUs
[osdCurrentElement
] >> OSD_EXEC_TIME_SHIFT
)) {
1479 osdElementDurationUs
[osdCurrentElement
] = executeTimeUs
<< OSD_EXEC_TIME_SHIFT
;
1480 } else if (osdElementDurationUs
[osdCurrentElement
] > 0) {
1481 // Slowly decay the max time
1482 osdElementDurationUs
[osdCurrentElement
]--;
1484 } while (moreElements
);
1487 // There are more elements to draw
1491 osdElementGroup
= 0;
1493 osdState
= OSD_STATE_COMMIT
;
1497 case OSD_STATE_COMMIT
:
1498 displayCommitTransaction(osdDisplayPort
);
1500 if (resumeRefreshAt
) {
1501 osdState
= OSD_STATE_IDLE
;
1503 osdState
= OSD_STATE_TRANSFER
;
1507 case OSD_STATE_TRANSFER
:
1508 // Wait for any current transfer to complete
1509 if (displayIsTransferInProgress(osdDisplayPort
)) {
1513 // Transfer may be broken into many parts
1514 if (displayDrawScreen(osdDisplayPort
)) {
1519 osdState
= OSD_STATE_IDLE
;
1523 case OSD_STATE_IDLE
:
1525 osdState
= OSD_STATE_IDLE
;
1529 if (!schedulerGetIgnoreTaskExecTime()) {
1530 executeTimeUs
= micros() - currentTimeUs
;
1533 // On the first pass no element groups will have been formed, so all elements will have been
1534 // rendered which is unrepresentative, so ignore
1536 if (osdCurrentState
== OSD_STATE_UPDATE_ELEMENTS
) {
1537 if (executeTimeUs
> (osdElementGroupDurationFractionUs
[osdCurrentElementGroup
] >> OSD_EXEC_TIME_SHIFT
)) {
1538 osdElementGroupDurationFractionUs
[osdCurrentElementGroup
] = executeTimeUs
<< OSD_EXEC_TIME_SHIFT
;
1539 } else if (osdElementGroupDurationFractionUs
[osdCurrentElementGroup
] > 0) {
1540 // Slowly decay the max time
1541 osdElementGroupDurationFractionUs
[osdCurrentElementGroup
]--;
1545 if (executeTimeUs
> (osdStateDurationFractionUs
[osdCurrentState
] >> OSD_EXEC_TIME_SHIFT
)) {
1546 osdStateDurationFractionUs
[osdCurrentState
] = executeTimeUs
<< OSD_EXEC_TIME_SHIFT
;
1547 } else if (osdStateDurationFractionUs
[osdCurrentState
] > 0) {
1548 // Slowly decay the max time
1549 osdStateDurationFractionUs
[osdCurrentState
]--;
1554 if (osdState
== OSD_STATE_UPDATE_ELEMENTS
) {
1555 schedulerSetNextStateTime((osdElementGroupDurationFractionUs
[osdElementGroup
] >> OSD_EXEC_TIME_SHIFT
) + OSD_ELEMENT_RENDER_GROUP_MARGIN
);
1557 if (osdState
== OSD_STATE_IDLE
) {
1558 schedulerSetNextStateTime((osdStateDurationFractionUs
[OSD_STATE_CHECK
] >> OSD_EXEC_TIME_SHIFT
) + OSD_TASK_MARGIN
);
1560 schedulerSetNextStateTime((osdStateDurationFractionUs
[osdState
] >> OSD_EXEC_TIME_SHIFT
) + OSD_TASK_MARGIN
);
1565 void osdSuppressStats(bool flag
)
1567 suppressStatsDisplay
= flag
;
1570 #ifdef USE_OSD_PROFILES
1571 bool osdElementVisible(uint16_t value
)
1573 return (bool)((((value
& OSD_PROFILE_MASK
) >> OSD_PROFILE_BITS_POS
) & osdProfile
) != 0);
1577 bool osdGetVisualBeeperState(void)
1579 return showVisualBeeper
;
1582 void osdSetVisualBeeperState(bool state
)
1584 showVisualBeeper
= state
;
1587 statistic_t
*osdGetStats(void)
1593 // Determine if there are any enabled stats that need
1594 // the ACC (currently only MAX_G_FORCE).
1595 static bool osdStatsNeedAccelerometer(void)
1597 return osdStatGetState(OSD_STAT_MAX_G_FORCE
);
1600 // Check if any enabled elements or stats need the ACC
1601 bool osdNeedsAccelerometer(void)
1603 return osdStatsNeedAccelerometer() || osdElementsNeedAccelerometer();
1607 displayPort_t
*osdGetDisplayPort(osdDisplayPortDevice_e
*displayPortDeviceType
)
1609 if (displayPortDeviceType
) {
1610 *displayPortDeviceType
= osdDisplayPortDeviceType
;
1612 return osdDisplayPort
;