Simplification for div_and_round function

This commit is contained in:
Bertrand Songis
2016-04-16 18:26:14 +02:00
parent 740a2a71f5
commit 9c0e0a136a
5 changed files with 28 additions and 27 deletions
+9 -10
View File
@@ -19,7 +19,6 @@
*/ */
#include "opentx.h" #include "opentx.h"
#include "timers.h"
CustomFunctionsContext modelFunctionsContext = { 0 }; CustomFunctionsContext modelFunctionsContext = { 0 };
@@ -57,16 +56,16 @@ PLAY_FUNCTION(playValue, source_t idx)
if (telemetrySensor.prec > 0) { if (telemetrySensor.prec > 0) {
if (telemetrySensor.prec == 2) { if (telemetrySensor.prec == 2) {
if (val >= 5000) { if (val >= 5000) {
val = div_and_round<100>(val); val = div_and_round(val, 100);
} }
else { else {
val = div_and_round<10>(val); val = div_and_round(val, 10);
attr = PREC1; attr = PREC1;
} }
} }
else { else {
if (val >= 500) { if (val >= 500) {
val = div_and_round<10>(val); val = div_and_round(val, 10);
} }
else { else {
attr = PREC1; attr = PREC1;
@@ -113,7 +112,7 @@ PLAY_FUNCTION(playValue, source_t idx)
if (TELEMETRY_STREAMING()) { if (TELEMETRY_STREAMING()) {
idx -= (MIXSRC_FIRST_TELEM+TELEM_A1-1); idx -= (MIXSRC_FIRST_TELEM+TELEM_A1-1);
uint8_t att = 0; uint8_t att = 0;
int16_t converted_value = div_and_round<10>(applyChannelRatio(idx, val));; int16_t converted_value = div_and_round(applyChannelRatio(idx, val), 10);
if (ANA_CHANNEL_UNIT(idx) < UNIT_RAW) { if (ANA_CHANNEL_UNIT(idx) < UNIT_RAW) {
att = PREC1; att = PREC1;
} }
@@ -122,7 +121,7 @@ PLAY_FUNCTION(playValue, source_t idx)
break; break;
case MIXSRC_FIRST_TELEM+TELEM_CELL-1: case MIXSRC_FIRST_TELEM+TELEM_CELL-1:
case MIXSRC_FIRST_TELEM+TELEM_MIN_CELL-1: case MIXSRC_FIRST_TELEM+TELEM_MIN_CELL-1:
PLAY_NUMBER(div_and_round<10>(val), 1+UNIT_VOLTS, PREC1); PLAY_NUMBER(div_and_round(val, 10), 1+UNIT_VOLTS, PREC1);
break; break;
case MIXSRC_FIRST_TELEM+TELEM_VFAS-1: case MIXSRC_FIRST_TELEM+TELEM_VFAS-1:
@@ -140,11 +139,11 @@ PLAY_FUNCTION(playValue, source_t idx)
case MIXSRC_FIRST_TELEM+TELEM_ACCx-1: case MIXSRC_FIRST_TELEM+TELEM_ACCx-1:
case MIXSRC_FIRST_TELEM+TELEM_ACCy-1: case MIXSRC_FIRST_TELEM+TELEM_ACCy-1:
case MIXSRC_FIRST_TELEM+TELEM_ACCz-1: case MIXSRC_FIRST_TELEM+TELEM_ACCz-1:
PLAY_NUMBER(div_and_round<10>(val), 1+UNIT_G, PREC1); PLAY_NUMBER(div_and_round(val, 10), 1+UNIT_G, PREC1);
break; break;
case MIXSRC_FIRST_TELEM+TELEM_VSPEED-1: case MIXSRC_FIRST_TELEM+TELEM_VSPEED-1:
PLAY_NUMBER(div_and_round<10>(val), 1+UNIT_METERS_PER_SECOND, PREC1); PLAY_NUMBER(div_and_round(val, 10), 1+UNIT_METERS_PER_SECOND, PREC1);
break; break;
case MIXSRC_FIRST_TELEM+TELEM_ASPEED-1: case MIXSRC_FIRST_TELEM+TELEM_ASPEED-1:
@@ -176,9 +175,9 @@ PLAY_FUNCTION(playValue, source_t idx)
{ {
getvalue_t rpm = val; getvalue_t rpm = val;
if (rpm > 100) if (rpm > 100)
rpm = 10 * div_and_round<10>(rpm); rpm = 10 * div_and_round(rpm, 10);
if (rpm > 1000) if (rpm > 1000)
rpm = 10 * div_and_round<10>(rpm); rpm = 10 * div_and_round(rpm, 10);
PLAY_NUMBER(rpm, 1+UNIT_RPMS, 0); PLAY_NUMBER(rpm, 1+UNIT_RPMS, 0);
break; break;
} }
+4 -4
View File
@@ -973,14 +973,14 @@ void putsTelemetryChannelValue(coord_t x, coord_t y, uint8_t channel, lcdint_t v
{ {
lcdint_t converted_value = applyChannelRatio(channel, val); lcdint_t converted_value = applyChannelRatio(channel, val);
if (ANA_CHANNEL_UNIT(channel) >= UNIT_RAW) { if (ANA_CHANNEL_UNIT(channel) >= UNIT_RAW) {
converted_value = div_and_round<10>(converted_value); converted_value = div_and_round(converted_value, 10);
} }
else { else {
if (abs(converted_value) < 1000) { if (abs(converted_value) < 1000) {
att |= PREC2; att |= PREC2;
} }
else { else {
converted_value = div_and_round<10>(converted_value); converted_value = div_and_round(converted_value, 10);
att |= PREC1; att |= PREC1;
} }
} }
@@ -1023,7 +1023,7 @@ void putsTelemetryChannelValue(coord_t x, coord_t y, uint8_t channel, lcdint_t v
break; break;
case TELEM_VSPEED-1: case TELEM_VSPEED-1:
putsValueWithUnit(x, y, div_and_round<10>(val), UNIT_RAW, att|PREC1); putsValueWithUnit(x, y, div_and_round(val, 10), UNIT_RAW, att|PREC1);
break; break;
case TELEM_ASPEED-1: case TELEM_ASPEED-1:
@@ -1045,7 +1045,7 @@ void putsTelemetryChannelValue(coord_t x, coord_t y, uint8_t channel, lcdint_t v
#if defined(FRSKY_SPORT) #if defined(FRSKY_SPORT)
case TELEM_ALT-1: case TELEM_ALT-1:
putsValueWithUnit(x, y, div_and_round<10>(val), UNIT_DIST, att|PREC1); putsValueWithUnit(x, y, div_and_round(val, 10), UNIT_DIST, att|PREC1);
break; break;
#elif defined(WS_HOW_HIGH) #elif defined(WS_HOW_HIGH)
case TELEM_ALT-1: case TELEM_ALT-1:
+4 -4
View File
@@ -108,7 +108,7 @@ void applyExpos(int16_t * anas, uint8_t mode APPLY_EXPOS_EXTRA_PARAMS)
//========== WEIGHT =============== //========== WEIGHT ===============
#if defined(CPUARM) #if defined(CPUARM)
int32_t weight = GET_GVAR_PREC1(ed->weight, MIN_EXPO_WEIGHT, 100, mixerCurrentFlightMode); int32_t weight = GET_GVAR_PREC1(ed->weight, MIN_EXPO_WEIGHT, 100, mixerCurrentFlightMode);
v = div_and_round<1000>((int32_t)v * weight); v = div_and_round((int32_t)v * weight, 1000);
#else #else
int16_t weight = GET_GVAR(ed->weight, MIN_EXPO_WEIGHT, 100, mixerCurrentFlightMode); int16_t weight = GET_GVAR(ed->weight, MIN_EXPO_WEIGHT, 100, mixerCurrentFlightMode);
weight = calc100to256(weight); weight = calc100to256(weight);
@@ -118,7 +118,7 @@ void applyExpos(int16_t * anas, uint8_t mode APPLY_EXPOS_EXTRA_PARAMS)
#if defined(VIRTUALINPUTS) #if defined(VIRTUALINPUTS)
//========== OFFSET =============== //========== OFFSET ===============
int32_t offset = GET_GVAR_PREC1(ed->offset, -100, 100, mixerCurrentFlightMode); int32_t offset = GET_GVAR_PREC1(ed->offset, -100, 100, mixerCurrentFlightMode);
if (offset) v += div_and_round<10>(calc100toRESX(offset)); if (offset) v += div_and_round(calc100toRESX(offset), 10);
//========== TRIMS ================ //========== TRIMS ================
if (ed->carryTrim < TRIM_ON) if (ed->carryTrim < TRIM_ON)
@@ -895,14 +895,14 @@ void evalFlightModeMixes(uint8_t mode, uint8_t tick10ms)
//========== WEIGHT =============== //========== WEIGHT ===============
int32_t dv = (int32_t)v * weight; int32_t dv = (int32_t)v * weight;
#if defined(CPUARM) #if defined(CPUARM)
dv = div_and_round<10>(dv); dv = div_and_round(dv, 10);
#endif #endif
//========== OFFSET / AFTER =============== //========== OFFSET / AFTER ===============
if (apply_offset_and_curve) { if (apply_offset_and_curve) {
#if defined(CPUARM) #if defined(CPUARM)
int32_t offset = GET_GVAR_PREC1(MD_OFFSET(md), GV_RANGELARGE_NEG, GV_RANGELARGE, mixerCurrentFlightMode); int32_t offset = GET_GVAR_PREC1(MD_OFFSET(md), GV_RANGELARGE_NEG, GV_RANGELARGE, mixerCurrentFlightMode);
if (offset) dv += div_and_round<10>(calc100toRESX_16Bits(offset)) << 8; if (offset) dv += div_and_round(calc100toRESX_16Bits(offset), 10) << 8;
#else #else
int16_t offset = GET_GVAR(MD_OFFSET(md), GV_RANGELARGE_NEG, GV_RANGELARGE, mixerCurrentFlightMode); int16_t offset = GET_GVAR(MD_OFFSET(md), GV_RANGELARGE_NEG, GV_RANGELARGE, mixerCurrentFlightMode);
if (offset) dv += int32_t(calc100toRESX_16Bits(offset)) << 8; if (offset) dv += int32_t(calc100toRESX_16Bits(offset)) << 8;
+10 -8
View File
@@ -1086,9 +1086,9 @@ extern const char eeprom_stamp[];
extern const char vers_stamp[]; extern const char vers_stamp[];
#endif #endif
extern uint8_t g_vbat100mV; extern uint8_t g_vbat100mV;
#if defined(PCBTARANIS) #if defined(PCBTARANIS)
#define GET_TXBATT_BARS() (limit<int8_t>(0, 10 * (g_vbat100mV - g_eeGeneral.vBatMin - 90) / (30 + g_eeGeneral.vBatMax - g_eeGeneral.vBatMin), 10)) #define GET_TXBATT_BARS() (limit<int8_t>(0, div_and_round(10 * (g_vbat100mV - g_eeGeneral.vBatMin - 90), 30 + g_eeGeneral.vBatMax - g_eeGeneral.vBatMin), 10))
#else #else
#define GET_TXBATT_BARS() (limit<int8_t>(2, 20 * (g_vbat100mV - g_eeGeneral.vBatMin - 90) / (30 + g_eeGeneral.vBatMax - g_eeGeneral.vBatMin), 20)) #define GET_TXBATT_BARS() (limit<int8_t>(2, 20 * (g_vbat100mV - g_eeGeneral.vBatMin - 90) / (30 + g_eeGeneral.vBatMax - g_eeGeneral.vBatMin), 20))
#endif #endif
@@ -1628,16 +1628,18 @@ lcdint_t applyChannelRatio(source_t channel, lcdint_t val);
#define ANA_CHANNEL_UNIT(channel) g_model.frsky.channels[channel].type #define ANA_CHANNEL_UNIT(channel) g_model.frsky.channels[channel].type
#endif #endif
template<int I> inline int div_and_round(int num, int den)
getvalue_t div_and_round(getvalue_t value)
{ {
if (value >= 0 ) { if (den == 0) {
value += I / 2; return 0;
}
else if (num >= 0) {
num += den / 2;
} }
else { else {
value -= I / 2; num -= den / 2;
} }
return value / I; return num / den;
} }
#if defined(FRSKY) #if defined(FRSKY)
+1 -1
View File
@@ -4,7 +4,7 @@
# on 2.1 release: make PCB=9X EXT=FRSKY VOICE=YES AUDIO=YES AUTOSOURCE=YES AUTOSWITCH=YES HELI=YES TEMPLATES=YES GAUGES=NO # on 2.1 release: make PCB=9X EXT=FRSKY VOICE=YES AUDIO=YES AUTOSOURCE=YES AUTOSWITCH=YES HELI=YES TEMPLATES=YES GAUGES=NO
# => 64818 (program) + 3236 (data) # => 64818 (program) + 3236 (data)
# on 2.2 release: cmake -DPCB=9X -DEXT=FRSKY -DAUDIO=YES -DVOICE=YES -DHELI=YES -DTEMPLATES=YES ~/git/opentx # on 2.2 release: cmake -DPCB=9X -DEXT=FRSKY -DAUDIO=YES -DVOICE=YES -DHELI=YES -DTEMPLATES=YES ~/git/opentx
# => 64832 (program) + 3236 (data) # => 64828 (program) + 3236 (data)
import os import os
import sys import sys