Make percentage() safe for 32-bit values. Fixes pwLimit overflow regression

This commit is contained in:
Josh Stewart 2024-01-16 16:22:56 +11:00
parent 0a5182f353
commit 288b98d31c
2 changed files with 5 additions and 3 deletions

View File

@ -111,7 +111,7 @@ bool initialisationComplete = false; ///< Tracks whether the setup() function ha
byte fpPrimeTime = 0; ///< The time (in seconds, based on @ref statuses.secl) that the fuel pump started priming
uint8_t softLimitTime = 0; //The time (in 0.1 seconds, based on seclx10) that the soft limiter started
volatile uint16_t mainLoopCount; //Main loop counter (incremented at each main loop rev., used for maintaining currentStatus.loopsPerSecond)
unsigned long revolutionTime; //The time in uS that one revolution would take at current speed (The time tooth 1 was last seen, minus the time it was seen prior to that)
uint32_t revolutionTime; //The time in uS that one revolution would take at current speed (The time tooth 1 was last seen, minus the time it was seen prior to that)
volatile unsigned long timer5_overflow_count = 0; //Increments every time counter 5 overflows. Used for the fast version of micros()
volatile unsigned long ms_counter = 0; //A counter that increments once per ms
uint16_t fixedCrankingOverride = 0;

View File

@ -183,10 +183,12 @@ static inline uint32_t div360(uint32_t n) {
* @param value The value to operate on
* @return uint32_t
*/
static inline uint16_t percentage(uint8_t percent, uint16_t value) {
return (uint16_t)div100((uint32_t)value * (uint32_t)percent);
static inline uint32_t percentage(uint8_t percent, uint32_t value)
{
return (uint32_t)div100((uint32_t)value * (uint32_t)percent);
}
/**
* @brief Integer based half-percentage calculation.
*