From b700ed30ff6411276ecb72f266b779046ed0e145 Mon Sep 17 00:00:00 2001 From: dzid26 Date: Thu, 11 Jul 2024 21:09:09 +0100 Subject: [PATCH] optimize apply_speed_limit() --- src/ebike_app.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/ebike_app.c b/src/ebike_app.c index 0b1bf7e2..4fefeefc 100644 --- a/src/ebike_app.c +++ b/src/ebike_app.c @@ -1444,16 +1444,18 @@ static void apply_temperature_limiting(void) } -static void apply_speed_limit(void) -{ - if (m_configuration_variables.ui8_wheel_speed_max) { - // set battery current target - ui8_adc_battery_current_target = map_ui16((uint16_t) ui16_wheel_speed_x10, - (uint16_t) (((uint8_t)(m_configuration_variables.ui8_wheel_speed_max) * (uint8_t)10U) - (uint8_t)20U), - (uint16_t) (((uint8_t)(m_configuration_variables.ui8_wheel_speed_max) * (uint8_t)10U) + (uint8_t)20U), +static void apply_speed_limit(void) { + if (m_configuration_variables.ui8_wheel_speed_max > 0U) { + uint16_t speed_limit_low = (uint16_t)(m_configuration_variables.ui8_wheel_speed_max * (uint8_t)10U) - 20U; // casting literal to uint8_t ensures usage of MUL X,A + uint16_t speed_limit_high = (uint16_t)(m_configuration_variables.ui8_wheel_speed_max * (uint8_t)10U) + 20U; + + ui8_adc_battery_current_target = (uint8_t)map_ui16(ui16_wheel_speed_x10, + speed_limit_low, + speed_limit_high, ui8_adc_battery_current_target, - 0); - if (ui16_wheel_speed_x10 > ((uint16_t) (((uint8_t)(m_configuration_variables.ui8_wheel_speed_max) * (uint8_t)10U) - (uint8_t)20U))) { + 0U); + + if (ui16_wheel_speed_x10 > speed_limit_high) { ui8_duty_cycle_target = 0; } }