Skip to content

Commit

Permalink
Code optimization by request
Browse files Browse the repository at this point in the history
Moved setting to the end of the struct

osd.c optmization

Changed bool into uint8t to mainatin fixed PG size
  • Loading branch information
dkustec committed Feb 17, 2021
1 parent aeb2a4f commit 78e4e93
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/main/cli/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,7 @@ const clivalue_t valueTable[] = {
{ "osd_rcchannels", VAR_INT8 | MASTER_VALUE | MODE_ARRAY, .config.array.length = OSD_RCCHANNELS_COUNT, PG_OSD_CONFIG, offsetof(osdConfig_t, rcChannels) },
{ "osd_camera_frame_width", VAR_UINT8 | MASTER_VALUE, .config.minmaxUnsigned = { OSD_CAMERA_FRAME_MIN_WIDTH, OSD_CAMERA_FRAME_MAX_WIDTH }, PG_OSD_CONFIG, offsetof(osdConfig_t, camera_frame_width) },
{ "osd_camera_frame_height", VAR_UINT8 | MASTER_VALUE, .config.minmaxUnsigned = { OSD_CAMERA_FRAME_MIN_HEIGHT, OSD_CAMERA_FRAME_MAX_HEIGHT }, PG_OSD_CONFIG, offsetof(osdConfig_t, camera_frame_height) },
{ "osd_stat_avg_cell_value", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_OSD_CONFIG, offsetof(osdConfig_t, stat_show_cell_value) },
{ "osd_task_frequency", VAR_UINT16 | MASTER_VALUE, .config.minmaxUnsigned = { OSD_TASK_FREQUENCY_MIN, OSD_TASK_FREQUENCY_MAX }, PG_OSD_CONFIG, offsetof(osdConfig_t, task_frequency) },
{ "osd_menu_background", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_CMS_BACKGROUND }, PG_OSD_CONFIG, offsetof(osdConfig_t, cms_background_type) },
#endif // end of #ifdef USE_OSD
Expand Down
28 changes: 19 additions & 9 deletions src/main/osd/osd.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ void pgResetFn_osdConfig(osdConfig_t *osdConfig)
osdConfig->camera_frame_width = 24;
osdConfig->camera_frame_height = 11;

osdConfig->stat_show_cell_value = false;
osdConfig->task_frequency = OSD_TASK_FREQUENCY_DEFAULT;
osdConfig->cms_background_type = DISPLAY_BACKGROUND_TRANSPARENT;
}
Expand Down Expand Up @@ -477,6 +478,11 @@ static int32_t getAverageEscRpm(void)
}
#endif

static uint16_t getStatsVoltage(void)
{
return osdConfig()->stat_show_cell_value ? getBatteryAverageCellVoltage() : getBatteryVoltage();
}

static void osdUpdateStats(void)
{
int16_t value = 0;
Expand All @@ -492,7 +498,7 @@ static void osdUpdateStats(void)
}
#endif

value = getBatteryVoltage();
value = getStatsVoltage();
if (stats.min_voltage > value) {
stats.min_voltage = value;
}
Expand Down Expand Up @@ -687,19 +693,23 @@ static bool osdDisplayStat(int statistic, uint8_t displayRow)

case OSD_STAT_MIN_BATTERY:
tfp_sprintf(buff, "%d.%02d%c", stats.min_voltage / 100, stats.min_voltage % 100, SYM_VOLT);
osdDisplayStatisticLabel(displayRow, "MIN BATTERY", buff);
osdDisplayStatisticLabel(displayRow, osdConfig()->stat_show_cell_value? "MIN AVG CELL" : "MIN BATTERY", buff);
return true;

case OSD_STAT_END_BATTERY:
tfp_sprintf(buff, "%d.%02d%c", stats.end_voltage / 100, stats.end_voltage % 100, SYM_VOLT);
osdDisplayStatisticLabel(displayRow, "END BATTERY", buff);
return true;

case OSD_STAT_BATTERY:
tfp_sprintf(buff, "%d.%02d%c", getBatteryVoltage() / 100, getBatteryVoltage() % 100, SYM_VOLT);
osdDisplayStatisticLabel(displayRow, "BATTERY", buff);
osdDisplayStatisticLabel(displayRow, osdConfig()->stat_show_cell_value ? "END AVG CELL" : "END BATTERY", buff);
return true;

case OSD_STAT_BATTERY:
{
const uint16_t statsVoltage = getStatsVoltage();
tfp_sprintf(buff, "%d.%02d%c", statsVoltage / 100, statsVoltage % 100, SYM_VOLT);
osdDisplayStatisticLabel(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, "%");
Expand Down Expand Up @@ -907,7 +917,7 @@ STATIC_UNIT_TESTED void osdRefresh(timeUs_t currentTimeUs)
|| !VISIBLE(osdElementConfig()->item_pos[OSD_WARNINGS]))) { // suppress stats if runaway takeoff triggered disarm and WARNINGS element is visible
osdStatsEnabled = true;
resumeRefreshAt = currentTimeUs + (60 * REFRESH_1S);
stats.end_voltage = getBatteryVoltage();
stats.end_voltage = getStatsVoltage();
osdStatsRowCount = 0; // reset to 0 so it will be recalculated on the next stats refresh
}

Expand Down
1 change: 1 addition & 0 deletions src/main/osd/osd.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ typedef struct osdConfig_s {
uint8_t camera_frame_height; // The height of the box for the camera frame element
uint16_t task_frequency;
uint8_t cms_background_type; // For supporting devices, determines whether the CMS background is transparent or opaque
uint8_t stat_show_cell_value;
} osdConfig_t;

PG_DECLARE(osdConfig_t, osdConfig);
Expand Down

0 comments on commit 78e4e93

Please sign in to comment.