Skip to content

Commit

Permalink
fix(telemetry): temperature UNIT conversion when temperature has deci…
Browse files Browse the repository at this point in the history
…mals (#4728)
  • Loading branch information
frankiearzu authored and pfeerick committed Apr 3, 2024
1 parent d1840cb commit 30e8769
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion radio/src/gui/128x64/model_telemetry_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void menuModelSensor(event_t event)
sensor->type == TELEM_TYPE_CALCULATED ? (uint8_t)0 : (uint8_t)1, // ID / Formula
// sensor->type == TELEM_TYPE_CALCULATED ? HIDDEN_ROW : READONLY_ROW, // Receiver name
((sensor->type == TELEM_TYPE_CALCULATED && (sensor->formula == TELEM_FORMULA_DIST)) || sensor->isConfigurable() ? (uint8_t)0 : HIDDEN_ROW), // Unit
(sensor->isPrecConfigurable() && sensor->unit != UNIT_FAHRENHEIT ? (uint8_t)0 : HIDDEN_ROW), // Precision
(sensor->isPrecConfigurable()? (uint8_t)0 : HIDDEN_ROW), // Precision
(sensor->unit >= UNIT_FIRST_VIRTUAL ? HIDDEN_ROW : (uint8_t)0), // Param1
(sensor->unit == UNIT_GPS || sensor->unit == UNIT_DATETIME || sensor->unit == UNIT_CELLS || (sensor->type==TELEM_TYPE_CALCULATED && (sensor->formula==TELEM_FORMULA_CONSUMPTION || sensor->formula==TELEM_FORMULA_TOTALIZE)) ? HIDDEN_ROW : (uint8_t)0), // Param2
(sensor->type == TELEM_TYPE_CALCULATED && sensor->formula < TELEM_FORMULA_MULTIPLY) ? (uint8_t)0 : HIDDEN_ROW, // Param3
Expand Down
2 changes: 1 addition & 1 deletion radio/src/gui/212x64/model_telemetry_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ enum SensorFields {
#define SENSOR_3RD_COLUMN (18*FW)

#define SENSOR_UNIT_ROWS ((sensor->type == TELEM_TYPE_CALCULATED && (sensor->formula == TELEM_FORMULA_DIST)) || sensor->isConfigurable() ? (uint8_t)0 : HIDDEN_ROW)
#define SENSOR_PREC_ROWS (sensor->isPrecConfigurable() && sensor->unit != UNIT_FAHRENHEIT ? (uint8_t)0 : HIDDEN_ROW)
#define SENSOR_PREC_ROWS (sensor->isPrecConfigurable() ? (uint8_t)0 : HIDDEN_ROW)
#define SENSOR_PARAM1_ROWS (sensor->unit >= UNIT_FIRST_VIRTUAL ? HIDDEN_ROW : (uint8_t)0)
#define SENSOR_PARAM2_ROWS (sensor->unit == UNIT_GPS || sensor->unit == UNIT_DATETIME || sensor->unit == UNIT_CELLS || (sensor->type==TELEM_TYPE_CALCULATED && (sensor->formula==TELEM_FORMULA_CONSUMPTION || sensor->formula==TELEM_FORMULA_TOTALIZE)) ? HIDDEN_ROW : (uint8_t)0)
#define SENSOR_PARAM3_ROWS (sensor->type == TELEM_TYPE_CALCULATED && sensor->formula < TELEM_FORMULA_MULTIPLY) ? (uint8_t)0 : HIDDEN_ROW
Expand Down
2 changes: 1 addition & 1 deletion radio/src/gui/colorlcd/model_telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ class SensorEditWindow : public Page {
}

// Precision
if (sensor->isPrecConfigurable() && sensor->unit != UNIT_FAHRENHEIT) {
if (sensor->isPrecConfigurable()) {
lv_obj_clear_flag(paramLines[P_PREC]->getLvObj(), LV_OBJ_FLAG_HIDDEN);
}

Expand Down
36 changes: 28 additions & 8 deletions radio/src/telemetry/telemetry_sensors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,19 +672,30 @@ const UnitConversionRule unitConversionTable[] = {
{ 0, 0, 0, 0} // termination
};

// The MAX precision used in sensors is 3..support up to Prec4.
const int16_t power10[] = { 1, 10, 100, 1000, 10000 };

int32_t convertTelemetryValue(int32_t value, uint8_t unit, uint8_t prec, uint8_t destUnit, uint8_t destPrec)
{
for (int i=prec; i<destPrec; i++)
value *= 10;
// if we need to do Temperature conversions, we need to convert the constant 32 from
// Prec0 to max(prec,destPrec).
int8_t temp_prec = prec;

// Ex: Converting from Prec1 -> Prec2.. need to * 10 the value.
// Ex: Converting from Prec0 -> Prec2.. need to * 100 the value.
if (prec < destPrec) {
value *= power10[destPrec-prec];
temp_prec = destPrec;
}

if (unit == UNIT_CELSIUS) {
if (destUnit == UNIT_FAHRENHEIT) {
// T(°F) = T(°C)×1,8 + 32
value = 32 + (value*18) / 10;
value = (32*power10[temp_prec]) + (value*18) / 10;
}
} else if (unit == UNIT_FAHRENHEIT) {
if (destUnit == UNIT_CELSIUS) {
value = (value - 32) * 10/18;
value = (value - (32*power10[temp_prec])) * 10/18;
}
}
else {
Expand All @@ -698,8 +709,11 @@ int32_t convertTelemetryValue(int32_t value, uint8_t unit, uint8_t prec, uint8_t
}
}

for (int i=destPrec; i<prec; i++)
value /= 10;
// Ex: Converting from Prec2 -> Prec1.. need to / 10 the value.
// Ex: Converting from Prec2 -> Prec0.. need to / 100 the value.
if (destPrec < prec) {
value = value / power10[prec - destPrec];
}

return value;
}
Expand All @@ -720,7 +734,10 @@ int32_t TelemetrySensor::getValue(int32_t value, uint8_t unit, uint8_t prec) con
value = (custom.ratio * value + 122) / 255; // 122/255 (0.48) is to aproximate up (ceiling)
}

value = convertTelemetryValue(value, unit, prec, this->unit, this->prec);
// Does it needs any conversion ?
if ((unit != this->unit) || (prec != this->prec)) {
value = convertTelemetryValue(value, unit, prec, this->unit, this->prec);
}

if (type == TELEM_TYPE_CUSTOM) {
value += custom.offset;
Expand Down Expand Up @@ -749,7 +766,10 @@ bool TelemetrySensor::isConfigurable() const

bool TelemetrySensor::isPrecConfigurable() const
{
if (isConfigurable()) {
if (unit == UNIT_FAHRENHEIT) { // use Prec0
return false;
}
else if (isConfigurable()) {
return true;
}
else if (unit == UNIT_CELLS) {
Expand Down

0 comments on commit 30e8769

Please sign in to comment.