From f8e4d02c991b2ff831429ce5acba870b70025471 Mon Sep 17 00:00:00 2001 From: Dan Nixon Date: Wed, 10 Jan 2018 18:21:50 +0000 Subject: [PATCH 1/3] Add core (MCU) temperature to OSD --- src/main/interface/settings.c | 1 + src/main/io/osd.c | 10 ++++++++++ src/main/io/osd.h | 1 + src/test/Makefile | 3 ++- src/test/unit/osd_unittest.cc | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/main/interface/settings.c b/src/main/interface/settings.c index f363e5a93b..028cdbf8a4 100644 --- a/src/main/interface/settings.c +++ b/src/main/interface/settings.c @@ -767,6 +767,7 @@ const clivalue_t valueTable[] = { { "osd_esc_rpm_pos", VAR_UINT16 | MASTER_VALUE, .config.minmax = { 0, OSD_POSCFG_MAX }, PG_OSD_CONFIG, offsetof(osdConfig_t, item_pos[OSD_ESC_RPM]) }, { "osd_rtc_date_time_pos", VAR_UINT16 | MASTER_VALUE, .config.minmax = { 0, OSD_POSCFG_MAX }, PG_OSD_CONFIG, offsetof(osdConfig_t, item_pos[OSD_RTC_DATETIME]) }, { "osd_adjustment_range_pos", VAR_UINT16 | MASTER_VALUE, .config.minmax = { 0, OSD_POSCFG_MAX }, PG_OSD_CONFIG, offsetof(osdConfig_t, item_pos[OSD_ADJUSTMENT_RANGE]) }, + { "osd_core_temp_pos", VAR_UINT16 | MASTER_VALUE, .config.minmax = { 0, OSD_POSCFG_MAX }, PG_OSD_CONFIG, offsetof(osdConfig_t, item_pos[OSD_CORE_TEMPERATURE]) }, { "osd_stat_max_spd", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_OSD_CONFIG, offsetof(osdConfig_t, enabled_stats[OSD_STAT_MAX_SPEED])}, { "osd_stat_max_dist", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_OSD_CONFIG, offsetof(osdConfig_t, enabled_stats[OSD_STAT_MAX_DISTANCE])}, diff --git a/src/main/io/osd.c b/src/main/io/osd.c index 1c7eba41b1..cbbf34ddc0 100644 --- a/src/main/io/osd.c +++ b/src/main/io/osd.c @@ -78,6 +78,7 @@ #include "rx/rx.h" +#include "sensors/adcinternal.h" #include "sensors/barometer.h" #include "sensors/battery.h" #include "sensors/esc_sensor.h" @@ -756,7 +757,12 @@ static bool osdDrawSingleElement(uint8_t item) #ifdef USE_OSD_ADJUSTMENTS case OSD_ADJUSTMENT_RANGE: tfp_sprintf(buff, "%s: %3d", adjustmentRangeName, adjustmentRangeValue); + break; +#endif +#ifdef USE_ADC_INTERNAL + case OSD_CORE_TEMPERATURE: + tfp_sprintf(buff, "%02dC", getCoreTemperatureCelsius()); break; #endif @@ -835,6 +841,10 @@ static void osdDrawElements(void) #ifdef USE_OSD_ADJUSTMENTS osdDrawSingleElement(OSD_ADJUSTMENT_RANGE); #endif + +#ifdef USE_ADC_INTERNAL + osdDrawSingleElement(OSD_CORE_TEMPERATURE); +#endif } void pgResetFn_osdConfig(osdConfig_t *osdConfig) diff --git a/src/main/io/osd.h b/src/main/io/osd.h index 608ee9c6e6..2dd18130d4 100644 --- a/src/main/io/osd.h +++ b/src/main/io/osd.h @@ -85,6 +85,7 @@ typedef enum { OSD_REMAINING_TIME_ESTIMATE, OSD_RTC_DATETIME, OSD_ADJUSTMENT_RANGE, + OSD_CORE_TEMPERATURE, OSD_ITEM_COUNT // MUST BE LAST } osd_items_e; diff --git a/src/test/Makefile b/src/test/Makefile index d07b7a1f32..34409ac110 100644 --- a/src/test/Makefile +++ b/src/test/Makefile @@ -160,7 +160,8 @@ osd_unittest_SRC := \ osd_unittest_DEFINES := \ USE_OSD \ - USE_RTC_TIME + USE_RTC_TIME \ + USE_ADC_INTERNAL pg_unittest_SRC := \ diff --git a/src/test/unit/osd_unittest.cc b/src/test/unit/osd_unittest.cc index f3049187f7..eda5d1a163 100644 --- a/src/test/unit/osd_unittest.cc +++ b/src/test/unit/osd_unittest.cc @@ -77,6 +77,7 @@ extern "C" { uint32_t simulationMahDrawn; int32_t simulationAltitude; int32_t simulationVerticalSpeed; + uint16_t simulationCoreTemperature; } /* #define DEBUG_OSD */ @@ -96,6 +97,7 @@ void setDefualtSimulationState() simulationMahDrawn = 0; simulationAltitude = 0; simulationVerticalSpeed = 0; + simulationCoreTemperature = 0; } /* @@ -743,6 +745,35 @@ TEST(OsdTest, TestElementAltitude) displayPortTestBufferSubstring(23, 7, " -2.4%c", SYM_M); } +/* + * Tests the core temperature OSD element. + */ +TEST(OsdTest, TestElementCoreTemperature) +{ + // given + osdConfigMutable()->item_pos[OSD_CORE_TEMPERATURE] = OSD_POS(1, 8) | VISIBLE_FLAG; + + // and + simulationCoreTemperature = 0; + + // when + displayClearScreen(&testDisplayPort); + osdRefresh(simulationTime); + + // then + displayPortTestBufferSubstring(1, 8, "00C"); + + // given + simulationCoreTemperature = 33; + + // when + displayClearScreen(&testDisplayPort); + osdRefresh(simulationTime); + + // then + displayPortTestBufferSubstring(1, 8, "33C"); +} + /* * Tests the battery notifications shown on the warnings OSD element. */ @@ -958,4 +989,6 @@ extern "C" { } uint16_t getRssi(void) { return rssi; } + + uint16_t getCoreTemperatureCelsius(void) { return simulationCoreTemperature; } } From 52d45ff72b81b9307949b3649adef745d5efb1f5 Mon Sep 17 00:00:00 2001 From: Dan Nixon Date: Sat, 13 Jan 2018 13:00:01 +0000 Subject: [PATCH 2/3] Add temperature unit conversion Selects unit based on OSD unit system setting: metric is degC and imperial is degF. --- src/main/io/osd.c | 22 +++++++++++++++++++++- src/test/unit/osd_unittest.cc | 24 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/main/io/osd.c b/src/main/io/osd.c index cbbf34ddc0..1cfd720eec 100644 --- a/src/main/io/osd.c +++ b/src/main/io/osd.c @@ -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 diff --git a/src/test/unit/osd_unittest.cc b/src/test/unit/osd_unittest.cc index eda5d1a163..0f9d712867 100644 --- a/src/test/unit/osd_unittest.cc +++ b/src/test/unit/osd_unittest.cc @@ -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" { From ed42d59c9474c08b4dd97db0ca36fcb984a53ff7 Mon Sep 17 00:00:00 2001 From: Dan Nixon Date: Sun, 14 Jan 2018 20:56:40 +0000 Subject: [PATCH 3/3] Add temperature conversion for ESC temp, tidy formatting --- src/main/io/osd.c | 6 ++++-- src/test/unit/osd_unittest.cc | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/io/osd.c b/src/main/io/osd.c index 1cfd720eec..e6091dea5c 100644 --- a/src/main/io/osd.c +++ b/src/main/io/osd.c @@ -207,6 +207,7 @@ static int32_t osdGetMetersToSelectedUnit(int32_t meters) } } +#if defined(USE_ADC_INTERNAL) || defined(USE_ESC_SENSOR) STATIC_UNIT_TESTED int osdConvertTemperatureToSelectedUnit(int tempInDeciDegrees) { switch (osdConfig()->units) { @@ -226,6 +227,7 @@ static char osdGetTemperatureSymbolForSelectedUnit(void) return 'C'; } } +#endif static void osdFormatAltitudeString(char * buff, int altitude, bool pad) { @@ -760,7 +762,7 @@ static bool osdDrawSingleElement(uint8_t item) #ifdef USE_ESC_SENSOR case OSD_ESC_TMP: - tfp_sprintf(buff, "%3d%c", escData == NULL ? 0 : escData->temperature, SYM_TEMP_C); + tfp_sprintf(buff, "%3d%c", osdConvertTemperatureToSelectedUnit(escData->temperature * 10) / 10, osdGetTemperatureSymbolForSelectedUnit()); break; case OSD_ESC_RPM: @@ -782,7 +784,7 @@ static bool osdDrawSingleElement(uint8_t item) #ifdef USE_ADC_INTERNAL case OSD_CORE_TEMPERATURE: - tfp_sprintf(buff, "%02d%c", osdConvertTemperatureToSelectedUnit(getCoreTemperatureCelsius() * 10) / 10, osdGetTemperatureSymbolForSelectedUnit()); + tfp_sprintf(buff, "%3d%c", osdConvertTemperatureToSelectedUnit(getCoreTemperatureCelsius() * 10) / 10, osdGetTemperatureSymbolForSelectedUnit()); break; #endif diff --git a/src/test/unit/osd_unittest.cc b/src/test/unit/osd_unittest.cc index 0f9d712867..50371c56be 100644 --- a/src/test/unit/osd_unittest.cc +++ b/src/test/unit/osd_unittest.cc @@ -765,7 +765,7 @@ TEST(OsdTest, TestElementCoreTemperature) osdRefresh(simulationTime); // then - displayPortTestBufferSubstring(1, 8, "00C"); + displayPortTestBufferSubstring(1, 8, " 0C"); // given simulationCoreTemperature = 33; @@ -775,7 +775,7 @@ TEST(OsdTest, TestElementCoreTemperature) osdRefresh(simulationTime); // then - displayPortTestBufferSubstring(1, 8, "33C"); + displayPortTestBufferSubstring(1, 8, " 33C"); // given osdConfigMutable()->units = OSD_UNIT_IMPERIAL; @@ -785,7 +785,7 @@ TEST(OsdTest, TestElementCoreTemperature) osdRefresh(simulationTime); // then - displayPortTestBufferSubstring(1, 8, "91F"); + displayPortTestBufferSubstring(1, 8, " 91F"); } /*