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
+10 -8
View File
@@ -1086,9 +1086,9 @@ extern const char eeprom_stamp[];
extern const char vers_stamp[];
#endif
extern uint8_t g_vbat100mV;
extern uint8_t g_vbat100mV;
#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
#define GET_TXBATT_BARS() (limit<int8_t>(2, 20 * (g_vbat100mV - g_eeGeneral.vBatMin - 90) / (30 + g_eeGeneral.vBatMax - g_eeGeneral.vBatMin), 20))
#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
#endif
template<int I>
getvalue_t div_and_round(getvalue_t value)
inline int div_and_round(int num, int den)
{
if (value >= 0 ) {
value += I / 2;
if (den == 0) {
return 0;
}
else if (num >= 0) {
num += den / 2;
}
else {
value -= I / 2;
num -= den / 2;
}
return value / I;
return num / den;
}
#if defined(FRSKY)