diff --git a/radio/src/functions.cpp b/radio/src/functions.cpp index 99516aef6..6fdd9b646 100644 --- a/radio/src/functions.cpp +++ b/radio/src/functions.cpp @@ -367,8 +367,10 @@ void evalFunctions() } #if defined(CPUARM) if (CFN_PARAM(cfn)>=FUNC_RESET_PARAM_FIRST_TELEM) { - TelemetryItem * telemetryItem = & telemetryItems[CFN_PARAM(cfn)-FUNC_RESET_PARAM_FIRST_TELEM]; - telemetryItem->clear(); + uint8_t item = CFN_PARAM(cfn)-FUNC_RESET_PARAM_FIRST_TELEM; + if (item < MAX_SENSORS) { + telemetryItems[item].clear(); + } } #endif break; diff --git a/radio/src/gui/9X/lcd.cpp b/radio/src/gui/9X/lcd.cpp index f07ed892e..4e8d27ccb 100644 --- a/radio/src/gui/9X/lcd.cpp +++ b/radio/src/gui/9X/lcd.cpp @@ -881,6 +881,7 @@ void displayGpsCoords(coord_t x, coord_t y, TelemetryItem & telemetryItem, LcdFl void putsTelemetryChannelValue(coord_t x, coord_t y, uint8_t channel, lcdint_t value, LcdFlags att) { + if (channel >= MAX_SENSORS) return; TelemetryItem & telemetryItem = telemetryItems[channel]; TelemetrySensor & telemetrySensor = g_model.telemetrySensors[channel]; if (telemetrySensor.unit == UNIT_DATETIME) { diff --git a/radio/src/gui/Taranis/lcd.cpp b/radio/src/gui/Taranis/lcd.cpp index 738726564..6ef2339cc 100644 --- a/radio/src/gui/Taranis/lcd.cpp +++ b/radio/src/gui/Taranis/lcd.cpp @@ -952,6 +952,7 @@ void displayGpsCoords(coord_t x, coord_t y, TelemetryItem & telemetryItem, LcdFl void putsTelemetryChannelValue(coord_t x, coord_t y, uint8_t channel, lcdint_t value, LcdFlags att) { + if (channel >= MAX_SENSORS) return; //Lua luaLcdDrawChannel() can call us with a bad value TelemetryItem & telemetryItem = telemetryItems[channel]; TelemetrySensor & telemetrySensor = g_model.telemetrySensors[channel]; if (telemetrySensor.unit == UNIT_DATETIME) { diff --git a/radio/src/gui/Taranis/view_main.cpp b/radio/src/gui/Taranis/view_main.cpp index 4b6f9b256..b5a441d9d 100644 --- a/radio/src/gui/Taranis/view_main.cpp +++ b/radio/src/gui/Taranis/view_main.cpp @@ -236,22 +236,26 @@ void displayTopBar() /* Rx voltage */ altitude_icon_x = batt_icon_x+7*FW+3; if (g_model.frsky.voltsSource) { - TelemetryItem & voltsItem = telemetryItems[g_model.frsky.voltsSource-1]; - if (voltsItem.isAvailable()) { - putsTelemetryChannelValue(batt_icon_x+7*FW+2, BAR_Y+1, g_model.frsky.voltsSource-1, voltsItem.value, LEFT); - altitude_icon_x = lcdLastPos+1; + uint8_t item = g_model.frsky.voltsSource-1; + if (item < MAX_SENSORS) { + TelemetryItem & voltsItem = telemetryItems[item]; + if (voltsItem.isAvailable()) { + putsTelemetryChannelValue(batt_icon_x+7*FW+2, BAR_Y+1, item, voltsItem.value, LEFT); + altitude_icon_x = lcdLastPos+1; + } } } /* Altitude */ if (g_model.frsky.altitudeSource) { - TelemetryItem & altitudeItem = telemetryItems[g_model.frsky.altitudeSource-1]; - if (altitudeItem.isAvailable()) { - LCD_ICON(altitude_icon_x, BAR_Y, ICON_ALTITUDE); - int32_t value = altitudeItem.value; - TelemetrySensor & sensor = g_model.telemetrySensors[g_model.frsky.altitudeSource-1]; - if (sensor.prec) value /= sensor.prec == 2 ? 100 : 10; - putsValueWithUnit(altitude_icon_x+2*FW-1, BAR_Y+1, value, UNIT_METERS, LEFT); + uint8_t item = g_model.frsky.altitudeSource-1; + if (item < MAX_SENSORS) { + TelemetryItem & altitudeItem = telemetryItems[item]; + if (altitudeItem.isAvailable()) { + LCD_ICON(altitude_icon_x, BAR_Y, ICON_ALTITUDE); + int32_t value = altitudeItem.value / g_model.telemetrySensors[item].getPrecDivisor(); + putsValueWithUnit(altitude_icon_x+2*FW-1, BAR_Y+1, value, UNIT_METERS, LEFT); + } } } } diff --git a/radio/src/lua_api.cpp b/radio/src/lua_api.cpp index 4b2d99839..4c94ef1e9 100644 --- a/radio/src/lua_api.cpp +++ b/radio/src/lua_api.cpp @@ -196,7 +196,7 @@ static void luaGetValueAndPush(int src) break; default: if (telemetrySensor.prec > 0) - lua_pushnumber(L, float(value)/(telemetrySensor.prec == 2 ? 100.0 : 10.0)); + lua_pushnumber(L, float(value)/telemetrySensor.getPrecDivisor()); else lua_pushinteger(L, value); break; diff --git a/radio/src/myeeprom.h b/radio/src/myeeprom.h index dd86250ec..9417574de 100644 --- a/radio/src/myeeprom.h +++ b/radio/src/myeeprom.h @@ -1313,10 +1313,12 @@ PACK(typedef struct { }; void init(const char *label, uint8_t unit=UNIT_RAW, uint8_t prec=0); void init(uint16_t id); - bool isAvailable(); + bool isAvailable() const; int32_t getValue(int32_t value, uint8_t unit, uint8_t prec) const; - bool isConfigurable(); - bool isPrecConfigurable(); + bool isConfigurable() const; + bool isPrecConfigurable() const; + uint32_t getPrecMultiplier() const; + uint32_t getPrecDivisor() const; }) TelemetrySensor; #endif diff --git a/radio/src/telemetry/telemetry.cpp b/radio/src/telemetry/telemetry.cpp index 872e389c2..bbde9fd95 100644 --- a/radio/src/telemetry/telemetry.cpp +++ b/radio/src/telemetry/telemetry.cpp @@ -366,10 +366,7 @@ void TelemetryItem::eval(const TelemetrySensor & sensor) result += dist*dist; if (altItem) { - dist = abs(altItem->value); - uint8_t prec = g_model.telemetrySensors[sensor.dist.alt-1].prec; - if (prec > 0) - dist /= (prec==2 ? 100 : 10); + dist = abs(altItem->value) / g_model.telemetrySensors[sensor.dist.alt-1].getPrecDivisor(); result += dist*dist; } @@ -545,7 +542,7 @@ void TelemetrySensor::init(uint16_t id) init(label); } -bool TelemetrySensor::isAvailable() +bool TelemetrySensor::isAvailable() const { return ZLEN(label) > 0; } @@ -622,7 +619,7 @@ int32_t TelemetrySensor::getValue(int32_t value, uint8_t unit, uint8_t prec) con return value; } -bool TelemetrySensor::isConfigurable() +bool TelemetrySensor::isConfigurable() const { if (type == TELEM_TYPE_CALCULATED) { if (formula >= TELEM_FORMULA_CELL) { @@ -637,7 +634,7 @@ bool TelemetrySensor::isConfigurable() return true; } -bool TelemetrySensor::isPrecConfigurable() +bool TelemetrySensor::isPrecConfigurable() const { if (isConfigurable()) { return true; @@ -649,3 +646,17 @@ bool TelemetrySensor::isPrecConfigurable() return false; } } + +uint32_t TelemetrySensor::getPrecMultiplier() const +{ + if (prec == 2) return 1; + if (prec == 1) return 10; + return 100; +} + +uint32_t TelemetrySensor::getPrecDivisor() const +{ + if (prec == 2) return 100; + if (prec == 1) return 10; + return 1; +} diff --git a/radio/src/vario.cpp b/radio/src/vario.cpp index 4d5d362e4..c410b2a5a 100644 --- a/radio/src/vario.cpp +++ b/radio/src/vario.cpp @@ -47,10 +47,10 @@ void varioWakeup() int verticalSpeed = 0; if (g_model.frsky.varioSource) { - TelemetryItem & varioItem = telemetryItems[g_model.frsky.varioSource-1]; - verticalSpeed = varioItem.value; - TelemetrySensor & sensor = g_model.telemetrySensors[g_model.frsky.varioSource-1]; - if (sensor.prec != 2) verticalSpeed *= sensor.prec == 0 ? 100 : 10; + uint8_t item = g_model.frsky.varioSource-1; + if (item < MAX_SENSORS) { + verticalSpeed = telemetryItems[item].value * g_model.telemetrySensors[item].getPrecMultiplier(); + } } int varioCenterMin = (int)g_model.frsky.varioCenterMin * 10 - 50;