diff --git a/radio/src/lcd.cpp b/radio/src/lcd.cpp index ca7389578..3413cbcea 100644 --- a/radio/src/lcd.cpp +++ b/radio/src/lcd.cpp @@ -1190,7 +1190,7 @@ void putsTelemetryChannel(xcoord_t x, uint8_t y, uint8_t channel, lcdint_t val, { lcdint_t converted_value = applyChannelRatio(channel, val); if (g_model.frsky.channels[channel].type >= UNIT_RAW) { - converted_value /= 10; + converted_value = div10_and_round(converted_value); } else { #if !defined(PCBTARANIS) @@ -1198,7 +1198,7 @@ void putsTelemetryChannel(xcoord_t x, uint8_t y, uint8_t channel, lcdint_t val, att |= PREC2; } else { - converted_value /= 10; + converted_value = div10_and_round(converted_value); att |= PREC1; } #else @@ -1255,7 +1255,7 @@ void putsTelemetryChannel(xcoord_t x, uint8_t y, uint8_t channel, lcdint_t val, #if defined(FRSKY_SPORT) case TELEM_ALT-1: - putsTelemetryValue(x, y, val/10, UNIT_DIST, att|PREC1); + putsTelemetryValue(x, y, div10_and_round(val), UNIT_DIST, att|PREC1); break; #elif defined(WS_HOW_HIGH) case TELEM_ALT-1: diff --git a/radio/src/maths.cpp b/radio/src/maths.cpp index 66ccea7a4..2cd8597f4 100644 --- a/radio/src/maths.cpp +++ b/radio/src/maths.cpp @@ -36,6 +36,29 @@ #include "opentx.h" + +/* + Division by 10 and rounding or fixed point arithmetic values + + Examples: + value -> result + 105 -> 11 + 104 -> 10 + -205 -> -21 + -204 -> -20 +*/ +getvalue_t div10_and_round(getvalue_t value) +{ + if (value >= 0 ) { + value += 5; + } + else { + value -= 5; + } + return value/10; +} + + #if defined(FRSKY) uint16_t getChannelRatio(uint8_t channel) { diff --git a/radio/src/opentx.cpp b/radio/src/opentx.cpp index 650e66220..4a7c2330f 100644 --- a/radio/src/opentx.cpp +++ b/radio/src/opentx.cpp @@ -3253,7 +3253,7 @@ PLAY_FUNCTION(playValue, uint8_t idx) { if (TELEMETRY_STREAMING()) { uint8_t att = 0; - int16_t converted_value = applyChannelRatio(idx, val) / 10; + int16_t converted_value = div10_and_round(applyChannelRatio(idx, val));; if (g_model.frsky.channels[idx].type < UNIT_RAW) { att = PREC1; } @@ -3264,7 +3264,7 @@ PLAY_FUNCTION(playValue, uint8_t idx) case MIXSRC_FIRST_TELEM+TELEM_CELL-1: case MIXSRC_FIRST_TELEM+TELEM_MIN_CELL-1: - PLAY_NUMBER(val/10, 1+UNIT_VOLTS, PREC1); + PLAY_NUMBER(div10_and_round(val), 1+UNIT_VOLTS, PREC1); break; case MIXSRC_FIRST_TELEM+TELEM_VFAS-1: @@ -3282,12 +3282,12 @@ PLAY_FUNCTION(playValue, uint8_t idx) case MIXSRC_FIRST_TELEM+TELEM_ACCx-1: case MIXSRC_FIRST_TELEM+TELEM_ACCy-1: case MIXSRC_FIRST_TELEM+TELEM_ACCz-1: - PLAY_NUMBER(val/10, 1+UNIT_G, PREC1); + PLAY_NUMBER(div10_and_round(val), 1+UNIT_G, PREC1); break; case MIXSRC_FIRST_TELEM+TELEM_VSPD-1: case MIXSRC_FIRST_TELEM+TELEM_ASPD-1: - PLAY_NUMBER(val/10, 1+UNIT_METERS_PER_SECOND, PREC1); + PLAY_NUMBER(div10_and_round(val), 1+UNIT_METERS_PER_SECOND, PREC1); break; case MIXSRC_FIRST_TELEM+TELEM_CONSUMPTION-1: @@ -3300,7 +3300,7 @@ PLAY_FUNCTION(playValue, uint8_t idx) case MIXSRC_FIRST_TELEM+TELEM_ALT-1: #if defined(PCBTARANIS) - PLAY_NUMBER(val/10, 1+UNIT_DIST, PREC1); + PLAY_NUMBER(div10_and_round(val), 1+UNIT_DIST, PREC1); break; #endif case MIXSRC_FIRST_TELEM+TELEM_MIN_ALT-1: diff --git a/radio/src/opentx.h b/radio/src/opentx.h index d1fc73358..9a641efcd 100644 --- a/radio/src/opentx.h +++ b/radio/src/opentx.h @@ -1514,6 +1514,9 @@ getvalue_t convertCswTelemValue(LogicalSwitchData * cs); lcdint_t applyChannelRatio(uint8_t channel, lcdint_t val); #endif +getvalue_t div10_and_round(getvalue_t value); + + #if defined(FRSKY) NOINLINE uint8_t getRssiAlarmValue(uint8_t alarm); diff --git a/radio/src/translations/tts_cz.cpp b/radio/src/translations/tts_cz.cpp index fb45783ab..1eb7e6107 100644 --- a/radio/src/translations/tts_cz.cpp +++ b/radio/src/translations/tts_cz.cpp @@ -129,7 +129,7 @@ I18N_PLAY_FUNCTION(cz, playNumber, getvalue_t number, uint8_t unit, uint8_t att) } #if defined(CPUARM) if ((att & PREC1) && (unit == UNIT_FEET || (unit == UNIT_DIST && number >= 100))) { - number /= 10; + number = div10_and_round(number); att -= PREC1; } #endif diff --git a/radio/src/translations/tts_de.cpp b/radio/src/translations/tts_de.cpp index af09eeb53..62ff70f31 100644 --- a/radio/src/translations/tts_de.cpp +++ b/radio/src/translations/tts_de.cpp @@ -119,7 +119,7 @@ I18N_PLAY_FUNCTION(de, playNumber, getvalue_t number, uint8_t unit, uint8_t att) } #if defined(CPUARM) if ((att & PREC1) && (unit == UNIT_FEET || (unit == UNIT_DIST && number >= 100))) { - number /= 10; + number = div10_and_round(number); att -= PREC1; } #endif diff --git a/radio/src/translations/tts_en.cpp b/radio/src/translations/tts_en.cpp index 92bc53d00..5810b4b3e 100644 --- a/radio/src/translations/tts_en.cpp +++ b/radio/src/translations/tts_en.cpp @@ -106,7 +106,7 @@ I18N_PLAY_FUNCTION(en, playNumber, getvalue_t number, uint8_t unit, uint8_t att) } #if defined(CPUARM) if ((att & PREC1) && (unit == UNIT_FEET || (unit == UNIT_DIST && number >= 100))) { - number /= 10; + number = div10_and_round(number); att -= PREC1; } #endif diff --git a/radio/src/translations/tts_es.cpp b/radio/src/translations/tts_es.cpp index 9a9b7ca74..db1e8ff2b 100644 --- a/radio/src/translations/tts_es.cpp +++ b/radio/src/translations/tts_es.cpp @@ -143,7 +143,7 @@ I18N_PLAY_FUNCTION(es, playNumber, getvalue_t number, uint8_t unit, uint8_t att) } #if defined(CPUARM) if ((att & PREC1) && (unit == UNIT_FEET || (unit == UNIT_DIST && number >= 100))) { - number /= 10; + number = div10_and_round(number); att -= PREC1; } #endif diff --git a/radio/src/translations/tts_fr.cpp b/radio/src/translations/tts_fr.cpp index a5eca4723..17258042c 100644 --- a/radio/src/translations/tts_fr.cpp +++ b/radio/src/translations/tts_fr.cpp @@ -125,7 +125,7 @@ I18N_PLAY_FUNCTION(fr, playNumber, getvalue_t number, uint8_t unit, uint8_t att) } #if defined(CPUARM) if ((att & PREC1) && (unit == UNIT_FEET || (unit == UNIT_DIST && number >= 100))) { - number /= 10; + number = div10_and_round(number); att -= PREC1; } #endif diff --git a/radio/src/translations/tts_it.cpp b/radio/src/translations/tts_it.cpp index 080b6ae10..b3d90e6b6 100644 --- a/radio/src/translations/tts_it.cpp +++ b/radio/src/translations/tts_it.cpp @@ -115,7 +115,7 @@ I18N_PLAY_FUNCTION(it, playNumber, getvalue_t number, uint8_t unit, uint8_t att) } #if defined(CPUARM) if ((att & PREC1) && (unit == UNIT_FEET || (unit == UNIT_DIST && number >= 100))) { - number /= 10; + number = div10_and_round(number); att -= PREC1; } #endif diff --git a/radio/src/translations/tts_pl.cpp b/radio/src/translations/tts_pl.cpp index 3772c6054..da5f2ffe6 100644 --- a/radio/src/translations/tts_pl.cpp +++ b/radio/src/translations/tts_pl.cpp @@ -138,7 +138,7 @@ I18N_PLAY_FUNCTION(pl, playNumber, getvalue_t number, uint8_t unit, uint8_t att) } #if defined(CPUARM) if ((att & PREC1) && (unit == UNIT_FEET || (unit == UNIT_DIST && number >= 100))) { - number /= 10; + number = div10_and_round(number); att -= PREC1; } #endif diff --git a/radio/src/translations/tts_pt.cpp b/radio/src/translations/tts_pt.cpp index 0744cefd3..f48f59917 100644 --- a/radio/src/translations/tts_pt.cpp +++ b/radio/src/translations/tts_pt.cpp @@ -128,7 +128,7 @@ I18N_PLAY_FUNCTION(pt, playNumber, getvalue_t number, uint8_t unit, uint8_t att) } #if defined(CPUARM) if ((att & PREC1) && (unit == UNIT_FEET || (unit == UNIT_DIST && number >= 100))) { - number /= 10; + number = div10_and_round(number); att -= PREC1; } #endif diff --git a/radio/src/translations/tts_se.cpp b/radio/src/translations/tts_se.cpp index c4c10c548..d8b029d62 100644 --- a/radio/src/translations/tts_se.cpp +++ b/radio/src/translations/tts_se.cpp @@ -93,7 +93,7 @@ I18N_PLAY_FUNCTION(se, playNumber, getvalue_t number, uint8_t unit, uint8_t att) } #if defined(CPUARM) if ((att & PREC1) && (unit == UNIT_FEET || (unit == UNIT_DIST && number >= 100))) { - number /= 10; + number = div10_and_round(number); att -= PREC1; } #endif diff --git a/radio/src/translations/tts_sk.cpp b/radio/src/translations/tts_sk.cpp index 1e10bddef..32d46dcb9 100644 --- a/radio/src/translations/tts_sk.cpp +++ b/radio/src/translations/tts_sk.cpp @@ -144,7 +144,7 @@ I18N_PLAY_FUNCTION(sk, playNumber, getvalue_t number, uint8_t unit, uint8_t att) } #if defined(CPUARM) if ((att & PREC1) && (unit == UNIT_FEET || (unit == UNIT_DIST && number >= 100))) { - number /= 10; + number = div10_and_round(number); att -= PREC1; } #endif