Add temperature unit conversion

Selects unit based on OSD unit system setting: metric is degC and
imperial is degF.
This commit is contained in:
Dan Nixon 2018-01-13 13:00:01 +00:00
parent f8e4d02c99
commit 52d45ff72b
2 changed files with 45 additions and 1 deletions

View File

@ -207,6 +207,26 @@ static int32_t osdGetMetersToSelectedUnit(int32_t meters)
}
}
STATIC_UNIT_TESTED int osdConvertTemperatureToSelectedUnit(int tempInDeciDegrees)
{
switch (osdConfig()->units) {
case OSD_UNIT_IMPERIAL:
return ((tempInDeciDegrees * 9) / 5) + 320;
default:
return tempInDeciDegrees;
}
}
static char osdGetTemperatureSymbolForSelectedUnit(void)
{
switch (osdConfig()->units) {
case OSD_UNIT_IMPERIAL:
return 'F';
default:
return 'C';
}
}
static void osdFormatAltitudeString(char * buff, int altitude, bool pad)
{
const int alt = osdGetMetersToSelectedUnit(altitude);
@ -762,7 +782,7 @@ static bool osdDrawSingleElement(uint8_t item)
#ifdef USE_ADC_INTERNAL
case OSD_CORE_TEMPERATURE:
tfp_sprintf(buff, "%02dC", getCoreTemperatureCelsius());
tfp_sprintf(buff, "%02d%c", osdConvertTemperatureToSelectedUnit(getCoreTemperatureCelsius() * 10) / 10, osdGetTemperatureSymbolForSelectedUnit());
break;
#endif

View File

@ -52,6 +52,7 @@ extern "C" {
void osdRefresh(timeUs_t currentTimeUs);
void osdFormatTime(char * buff, osd_timer_precision_e precision, timeUs_t time);
void osdFormatTimer(char *buff, bool showSymbol, int timerIndex);
int osdConvertTemperatureToSelectedUnit(int tempInDeciDegrees);
uint16_t rssi;
attitudeEulerAngles_t attitude;
@ -753,6 +754,9 @@ TEST(OsdTest, TestElementCoreTemperature)
// given
osdConfigMutable()->item_pos[OSD_CORE_TEMPERATURE] = OSD_POS(1, 8) | VISIBLE_FLAG;
// and
osdConfigMutable()->units = OSD_UNIT_METRIC;
// and
simulationCoreTemperature = 0;
@ -772,6 +776,16 @@ TEST(OsdTest, TestElementCoreTemperature)
// then
displayPortTestBufferSubstring(1, 8, "33C");
// given
osdConfigMutable()->units = OSD_UNIT_IMPERIAL;
// when
displayClearScreen(&testDisplayPort);
osdRefresh(simulationTime);
// then
displayPortTestBufferSubstring(1, 8, "91F");
}
/*
@ -905,6 +919,16 @@ TEST(OsdTest, TestFormatTimeString)
EXPECT_EQ(0, strcmp("01:59.00", buff));
}
TEST(OsdTest, TestConvertTemperatureUnits)
{
/* In Celsius */
osdConfigMutable()->units = OSD_UNIT_METRIC;
EXPECT_EQ(osdConvertTemperatureToSelectedUnit(330), 330);
/* In Fahrenheit */
osdConfigMutable()->units = OSD_UNIT_IMPERIAL;
EXPECT_EQ(osdConvertTemperatureToSelectedUnit(330), 914);
}
// STUBS
extern "C" {