From ad10f60529461fa81e685d65e9ad351269351827 Mon Sep 17 00:00:00 2001 From: Leon Date: Sat, 1 Jun 2019 20:37:44 +0200 Subject: [PATCH 1/5] Created Street Mode with throttle toggle --- src/controller/ebike_app.c | 22 ++++---- src/controller/ebike_app.h | 7 --- src/controller/eeprom.c | 26 ++-------- src/controller/eeprom.h | 7 +-- src/controller/main.h | 3 -- src/display/KT-LCD3/eeprom.c | 40 ++++++++------- src/display/KT-LCD3/eeprom.h | 13 ++--- src/display/KT-LCD3/lcd.c | 99 ++++++++++++++++++++---------------- src/display/KT-LCD3/lcd.h | 13 ++--- src/display/KT-LCD3/main.h | 13 ++--- src/display/KT-LCD3/uart.c | 39 +++++++++----- 11 files changed, 143 insertions(+), 139 deletions(-) diff --git a/src/controller/ebike_app.c b/src/controller/ebike_app.c index 33070a9d..0c495d58 100755 --- a/src/controller/ebike_app.c +++ b/src/controller/ebike_app.c @@ -446,18 +446,12 @@ static void uart_receive_package(void) case 3: // type of motor (36 volt, 48 volt or some experimental type) - m_configuration_variables.ui8_motor_type = (ui8_rx_buffer [5] & 3); + m_configuration_variables.ui8_motor_type = ui8_rx_buffer [5]; - // motor assistance without pedal rotation enable/disable when startup - m_configuration_variables.ui8_motor_assistance_startup_without_pedal_rotation = (ui8_rx_buffer [5] & 4) >> 2; - - // motor temperature limit function enable/disable - m_configuration_variables.ui8_temperature_limit_feature_enabled = (ui8_rx_buffer [5] & 8) >> 3; - - // startup motor boost state + // startup motor power boost state m_configuration_variables.ui8_startup_motor_power_boost_state = (ui8_rx_buffer [6] & 1); - // startup power boost max power limit + // startup power boost max power limit enabled m_configuration_variables.ui8_startup_motor_power_boost_limit_to_max_power = (ui8_rx_buffer [6] & 2) >> 1; break; @@ -474,7 +468,7 @@ static void uart_receive_package(void) m_configuration_variables.ui8_startup_motor_power_boost_fade_time = ui8_rx_buffer [5]; // startup motor boost enabled - m_configuration_variables.ui8_startup_motor_power_boost_feature_enabled = (ui8_rx_buffer [6] & 1); + m_configuration_variables.ui8_startup_motor_power_boost_feature_enabled = ui8_rx_buffer [6]; break; case 6: @@ -521,6 +515,14 @@ static void uart_receive_package(void) ui16_received_target_wheel_speed_x10 = (uint16_t) (ui8_rx_buffer [6] * 10); break; + case 8: + // motor temperature limit function or throttle + m_configuration_variables.ui8_temperature_limit_feature_enabled = ui8_rx_buffer [5]; + + // motor assistance without pedal rotation enable/disable when startup + m_configuration_variables.ui8_motor_assistance_startup_without_pedal_rotation = ui8_rx_buffer [6]; + break; + default: // nothing, should display error code break; diff --git a/src/controller/ebike_app.h b/src/controller/ebike_app.h index fe0bb182..e96ef743 100755 --- a/src/controller/ebike_app.h +++ b/src/controller/ebike_app.h @@ -28,7 +28,6 @@ typedef struct _configuration_variables uint16_t ui16_wheel_perimeter; uint8_t ui8_lights; uint8_t ui8_walk_assist; - uint8_t ui8_offroad_mode; uint8_t ui8_wheel_max_speed; uint8_t ui8_motor_type; uint8_t ui8_motor_assistance_startup_without_pedal_rotation; @@ -46,18 +45,12 @@ typedef struct _configuration_variables uint8_t ui8_temperature_current_limiting_value; uint16_t ui16_motor_temperature_x2; uint8_t ui8_motor_temperature; - uint8_t ui8_offroad_feature_enabled; - uint8_t ui8_offroad_enabled_on_startup; - uint8_t ui8_offroad_speed_limit; - uint8_t ui8_offroad_power_limit_enabled; - uint8_t ui8_offroad_power_limit_div25; uint8_t ui8_ramp_up_amps_per_second_x10; } struct_configuration_variables; extern volatile uint16_t ui16_current_ramp_up_inverse_step; - extern volatile uint8_t ui8_g_adc_torque_sensor_min_value; extern volatile uint8_t ui8_g_adc_torque_sensor_max_value; extern volatile uint8_t ui8_adc_battery_current_offset; diff --git a/src/controller/eeprom.c b/src/controller/eeprom.c index f0836778..5cc20e4a 100644 --- a/src/controller/eeprom.c +++ b/src/controller/eeprom.c @@ -24,10 +24,7 @@ static uint8_t array_default_values [EEPROM_BYTES_STORED] = { DEFAULT_VALUE_WHEEL_PERIMETER_1, // 8 + EEPROM_BASE_ADDRESS DEFAULT_VALUE_WHEEL_MAX_SPEED, // 9 + EEPROM_BASE_ADDRESS DEFAULT_VALUE_CONFIG_1, // 10 + EEPROM_BASE_ADDRESS - DEFAULT_VALUE_OFFROAD_CONFIG, // 11 + EEPROM_BASE_ADDRESS - DEFAULT_VALUE_OFFROAD_SPEED_LIMIT, // 12 + EEPROM_BASE_ADDRESS - DEFAULT_VALUE_OFFROAD_POWER_LIMIT_DIV25, // 13 + EEPROM_BASE_ADDRESS - DEFAULT_VALUE_RAMP_UP_AMPS_PER_SECOND_X10 // 14 + EEPROM_BASE_ADDRESS + DEFAULT_VALUE_RAMP_UP_AMPS_PER_SECOND_X10 // 11 + EEPROM_BASE_ADDRESS }; @@ -91,7 +88,6 @@ static void eeprom_read_values_to_variables (void) ui8_temp = FLASH_ReadByte (ADDRESS_CONFIG_0); p_configuration_variables->ui8_lights = ui8_temp & 1 ? 1 : 0; p_configuration_variables->ui8_walk_assist = ui8_temp & (1 << 1) ? 1 : 0; - p_configuration_variables->ui8_offroad_mode = ui8_temp & (1 << 2) ? 1 : 0; p_configuration_variables->ui8_battery_max_current = FLASH_ReadByte (ADDRESS_BATTERY_MAX_CURRENT); p_configuration_variables->ui8_motor_power_x10 = FLASH_ReadByte (ADDRESS_MOTOR_POWER_X10); @@ -113,14 +109,6 @@ static void eeprom_read_values_to_variables (void) ui8_temp = FLASH_ReadByte (ADDRESS_CONFIG_1); p_configuration_variables->ui8_motor_type = ui8_temp & 3; p_configuration_variables->ui8_motor_assistance_startup_without_pedal_rotation = (ui8_temp & 4) >> 2; - - ui8_temp = FLASH_ReadByte (ADDRESS_OFFROAD_CONFIG); - p_configuration_variables->ui8_offroad_feature_enabled = ui8_temp & 1; - p_configuration_variables->ui8_offroad_enabled_on_startup = ui8_temp & (1 << 1); - p_configuration_variables->ui8_offroad_power_limit_enabled = ui8_temp & (1 << 2); - - p_configuration_variables->ui8_offroad_speed_limit = FLASH_ReadByte (ADDRESS_OFFROAD_SPEED_LIMIT); - p_configuration_variables->ui8_offroad_power_limit_div25 = FLASH_ReadByte (ADDRESS_OFFROAD_POWER_LIMIT_DIV25); // ramp up, amps per second p_configuration_variables->ui8_ramp_up_amps_per_second_x10 = FLASH_ReadByte (ADDRESS_RAMP_UP_AMPS_PER_SECOND_X10); @@ -142,9 +130,8 @@ static void variables_to_array (uint8_t *ui8_array) ui8_array [0] = KEY; ui8_array [1] = p_configuration_variables->ui8_assist_level_factor_x10; - ui8_array [2] = (p_configuration_variables->ui8_lights & 1) | - ((p_configuration_variables->ui8_walk_assist & 1) << 1) | - ((p_configuration_variables->ui8_offroad_mode & 1) << 2); + ui8_array [2] = (p_configuration_variables->ui8_lights & 1) | + ((p_configuration_variables->ui8_walk_assist & 1) << 1); ui8_array [3] = p_configuration_variables->ui8_battery_max_current; ui8_array [4] = p_configuration_variables->ui8_motor_power_x10; ui8_array [5] = p_configuration_variables->ui16_battery_low_voltage_cut_off_x10 & 255; @@ -154,12 +141,7 @@ static void variables_to_array (uint8_t *ui8_array) ui8_array [9] = p_configuration_variables->ui8_wheel_max_speed; ui8_array [10] = (p_configuration_variables->ui8_motor_type & 3) | ((p_configuration_variables->ui8_motor_assistance_startup_without_pedal_rotation & 1) << 2); - ui8_array [11] = (p_configuration_variables->ui8_offroad_feature_enabled & 1) | - ((p_configuration_variables->ui8_offroad_enabled_on_startup & 1) << 1) | - ((p_configuration_variables->ui8_offroad_power_limit_enabled & 1) << 2); - ui8_array [12] = p_configuration_variables->ui8_offroad_speed_limit; - ui8_array [13] = p_configuration_variables->ui8_offroad_power_limit_div25; - ui8_array [14] = p_configuration_variables->ui8_ramp_up_amps_per_second_x10; + ui8_array [11] = p_configuration_variables->ui8_ramp_up_amps_per_second_x10; } diff --git a/src/controller/eeprom.h b/src/controller/eeprom.h index c53d6944..210466ce 100644 --- a/src/controller/eeprom.h +++ b/src/controller/eeprom.h @@ -28,11 +28,8 @@ #define ADDRESS_WHEEL_PERIMETER_1 8 + EEPROM_BASE_ADDRESS #define ADDRESS_WHEEL_MAX_SPEED 9 + EEPROM_BASE_ADDRESS #define ADDRESS_CONFIG_1 10 + EEPROM_BASE_ADDRESS -#define ADDRESS_OFFROAD_CONFIG 11 + EEPROM_BASE_ADDRESS -#define ADDRESS_OFFROAD_SPEED_LIMIT 12 + EEPROM_BASE_ADDRESS -#define ADDRESS_OFFROAD_POWER_LIMIT_DIV25 13 + EEPROM_BASE_ADDRESS -#define ADDRESS_RAMP_UP_AMPS_PER_SECOND_X10 14 + EEPROM_BASE_ADDRESS -#define EEPROM_BYTES_STORED 15 +#define ADDRESS_RAMP_UP_AMPS_PER_SECOND_X10 11 + EEPROM_BASE_ADDRESS +#define EEPROM_BYTES_STORED 12 diff --git a/src/controller/main.h b/src/controller/main.h index f1b6446d..63ffc338 100644 --- a/src/controller/main.h +++ b/src/controller/main.h @@ -128,9 +128,6 @@ #define DEFAULT_VALUE_WHEEL_PERIMETER_1 8 #define DEFAULT_VALUE_WHEEL_MAX_SPEED 50 // 50 km/h #define DEFAULT_VALUE_CONFIG_1 0 -#define DEFAULT_VALUE_OFFROAD_CONFIG 0 -#define DEFAULT_VALUE_OFFROAD_SPEED_LIMIT 25 // 25 km/h -#define DEFAULT_VALUE_OFFROAD_POWER_LIMIT_DIV25 10 // 10 * 25 = 250 W diff --git a/src/display/KT-LCD3/eeprom.c b/src/display/KT-LCD3/eeprom.c index 77832d4a..75d46a48 100644 --- a/src/display/KT-LCD3/eeprom.c +++ b/src/display/KT-LCD3/eeprom.c @@ -70,11 +70,11 @@ static uint8_t array_default_values [EEPROM_BYTES_STORED] = { DEFAULT_VALUE_LCD_BACKLIGHT_OFF_BRIGHTNESS, // 51 + EEPROM_BASE_ADDRESS (Array index) DEFAULT_VALUE_BATTERY_PACK_RESISTANCE_0, // 52 + EEPROM_BASE_ADDRESS (Array index) DEFAULT_VALUE_BATTERY_PACK_RESISTANCE_1, // 53 + EEPROM_BASE_ADDRESS (Array index) - DEFAULT_VALUE_OFFROAD_FEATURE_ENABLED, // 54 + EEPROM_BASE_ADDRESS (Array index) - DEFAULT_VALUE_OFFROAD_MODE_ENABLED_ON_STARTUP, // 55 + EEPROM_BASE_ADDRESS (Array index) - DEFAULT_VALUE_OFFROAD_SPEED_LIMIT, // 56 + EEPROM_BASE_ADDRESS (Array index) - DEFAULT_VALUE_OFFROAD_POWER_LIMIT_ENABLED, // 57 + EEPROM_BASE_ADDRESS (Array index) - DEFAULT_VALUE_OFFROAD_POWER_LIMIT_DIV25, // 58 + EEPROM_BASE_ADDRESS (Array index) + DEFAULT_VALUE_STREET_MODE_FUNCTION_ENABLED, // 54 + EEPROM_BASE_ADDRESS (Array index) + DEFAULT_VALUE_STREET_MODE_ENABLED_ON_STARTUP, // 55 + EEPROM_BASE_ADDRESS (Array index) + DEFAULT_VALUE_STREET_MODE_SPEED_LIMIT, // 56 + EEPROM_BASE_ADDRESS (Array index) + DEFAULT_VALUE_STREET_MODE_POWER_LIMIT_ENABLED, // 57 + EEPROM_BASE_ADDRESS (Array index) + DEFAULT_VALUE_STREET_MODE_POWER_LIMIT_DIV25, // 58 + EEPROM_BASE_ADDRESS (Array index) DEFAULT_VALUE_ODOMETER_X10, // 59 + EEPROM_BASE_ADDRESS (Array index) DEFAULT_VALUE_ODOMETER_X10, // 60 + EEPROM_BASE_ADDRESS (Array index) DEFAULT_VALUE_ODOMETER_X10, // 61 + EEPROM_BASE_ADDRESS (Array index) @@ -120,7 +120,8 @@ static uint8_t array_default_values [EEPROM_BYTES_STORED] = { DEFAULT_VALUE_SHOW_ENERGY_DATA_ODOMETER_FIELD, // 101 + EEPROM_BASE_ADDRESS (Array index) DEFAULT_VALUE_SHOW_MOTOR_TEMPERATURE_ODOMETER_FIELD, // 102 + EEPROM_BASE_ADDRESS (Array index) DEFAULT_VALUE_SHOW_BATTERY_SOC_ODOMETER_FIELD, // 103 + EEPROM_BASE_ADDRESS (Array index) - DEFAULT_VALUE_MAIN_SCREEN_POWER_MENU_ENABLED // 104 + EEPROM_BASE_ADDRESS (Array index) + DEFAULT_VALUE_MAIN_SCREEN_POWER_MENU_ENABLED, // 104 + EEPROM_BASE_ADDRESS (Array index) + DEFAULT_VALUE_STREET_MODE_THROTTLE_ENABLED // 105 + EEPROM_BASE_ADDRESS (Array index) }; @@ -302,11 +303,13 @@ static void eeprom_read_values_to_variables (void) ui16_temp += (((uint16_t) ui8_temp << 8) & 0xff00); p_configuration_variables->ui16_battery_pack_resistance_x1000 = ui16_temp; - p_configuration_variables->ui8_offroad_feature_enabled = FLASH_ReadByte (ADDRESS_OFFROAD_FEATURE_ENABLED); - p_configuration_variables->ui8_offroad_enabled_on_startup = FLASH_ReadByte (ADDRESS_OFFROAD_MODE_ENABLED_ON_STARTUP); - p_configuration_variables->ui8_offroad_speed_limit = FLASH_ReadByte (ADDRESS_OFFROAD_SPEED_LIMIT); - p_configuration_variables->ui8_offroad_power_limit_enabled = FLASH_ReadByte (ADDRESS_OFFROAD_POWER_LIMIT_ENABLED); - p_configuration_variables->ui8_offroad_power_limit_div25 = FLASH_ReadByte (ADDRESS_OFFROAD_POWER_LIMIT_DIV25); + // street mode variables + p_configuration_variables->ui8_street_mode_function_enabled = FLASH_ReadByte (ADDRESS_STREET_MODE_FUNCTION_ENABLED); + p_configuration_variables->ui8_street_mode_enabled_on_startup = FLASH_ReadByte (ADDRESS_STREET_MODE_ENABLED_ON_STARTUP); + p_configuration_variables->ui8_street_mode_speed_limit = FLASH_ReadByte (ADDRESS_STREET_MODE_SPEED_LIMIT); + p_configuration_variables->ui8_street_mode_power_limit_enabled = FLASH_ReadByte (ADDRESS_STREET_MODE_POWER_LIMIT_ENABLED); + p_configuration_variables->ui8_street_mode_power_limit_div25 = FLASH_ReadByte (ADDRESS_STREET_MODE_POWER_LIMIT_DIV25); + p_configuration_variables->ui8_street_mode_throttle_enabled = FLASH_ReadByte (ADDRESS_STREET_MODE_THROTTLE_ENABLED); // odometer variable @@ -446,12 +449,12 @@ static void variables_to_array (uint8_t *ui8_array) ui8_array [53] = (p_configuration_variables->ui16_battery_pack_resistance_x1000 >> 8) & 255; - // write offroad parameters - ui8_array [54] = p_configuration_variables->ui8_offroad_feature_enabled; - ui8_array [55] = p_configuration_variables->ui8_offroad_enabled_on_startup; - ui8_array [56] = p_configuration_variables->ui8_offroad_speed_limit; - ui8_array [57] = p_configuration_variables->ui8_offroad_power_limit_enabled; - ui8_array [58] = p_configuration_variables->ui8_offroad_power_limit_div25; + // write street mode parameters + ui8_array [54] = p_configuration_variables->ui8_street_mode_function_enabled; + ui8_array [55] = p_configuration_variables->ui8_street_mode_enabled_on_startup; + ui8_array [56] = p_configuration_variables->ui8_street_mode_speed_limit; + ui8_array [57] = p_configuration_variables->ui8_street_mode_power_limit_enabled; + ui8_array [58] = p_configuration_variables->ui8_street_mode_power_limit_div25; // write odometer variable @@ -527,6 +530,9 @@ static void variables_to_array (uint8_t *ui8_array) // write main screen power menu enable variable ui8_array [104] = p_configuration_variables->ui8_main_screen_power_menu_enabled; + + // write street mode parameters + ui8_array [105] = p_configuration_variables->ui8_street_mode_throttle_enabled; } diff --git a/src/display/KT-LCD3/eeprom.h b/src/display/KT-LCD3/eeprom.h index b527ec68..b2de9a47 100644 --- a/src/display/KT-LCD3/eeprom.h +++ b/src/display/KT-LCD3/eeprom.h @@ -70,11 +70,11 @@ #define ADDRESS_LCD_BACKLIGHT_OFF_BRIGHTNESS 51 + EEPROM_BASE_ADDRESS #define ADDRESS_BATTERY_PACK_RESISTANCE_0 52 + EEPROM_BASE_ADDRESS #define ADDRESS_BATTERY_PACK_RESISTANCE_1 53 + EEPROM_BASE_ADDRESS -#define ADDRESS_OFFROAD_FEATURE_ENABLED 54 + EEPROM_BASE_ADDRESS -#define ADDRESS_OFFROAD_MODE_ENABLED_ON_STARTUP 55 + EEPROM_BASE_ADDRESS -#define ADDRESS_OFFROAD_SPEED_LIMIT 56 + EEPROM_BASE_ADDRESS -#define ADDRESS_OFFROAD_POWER_LIMIT_ENABLED 57 + EEPROM_BASE_ADDRESS -#define ADDRESS_OFFROAD_POWER_LIMIT_DIV25 58 + EEPROM_BASE_ADDRESS +#define ADDRESS_STREET_MODE_FUNCTION_ENABLED 54 + EEPROM_BASE_ADDRESS +#define ADDRESS_STREET_MODE_ENABLED_ON_STARTUP 55 + EEPROM_BASE_ADDRESS +#define ADDRESS_STREET_MODE_SPEED_LIMIT 56 + EEPROM_BASE_ADDRESS +#define ADDRESS_STREET_MODE_POWER_LIMIT_ENABLED 57 + EEPROM_BASE_ADDRESS +#define ADDRESS_STREET_MODE_POWER_LIMIT_DIV25 58 + EEPROM_BASE_ADDRESS #define ADDRESS_ODOMETER_X10_0 59 + EEPROM_BASE_ADDRESS #define ADDRESS_ODOMETER_X10_1 60 + EEPROM_BASE_ADDRESS #define ADDRESS_ODOMETER_X10_2 61 + EEPROM_BASE_ADDRESS @@ -121,7 +121,8 @@ #define ADDRESS_SHOW_MOTOR_TEMPERATURE_ODOMETER_FIELD 102 + EEPROM_BASE_ADDRESS #define ADDRESS_SHOW_BATTERY_SOC_ODOMETER_FIELD 103 + EEPROM_BASE_ADDRESS #define ADDRESS_MAIN_SCREEN_POWER_MENU_ENABLED 104 + EEPROM_BASE_ADDRESS -#define EEPROM_BYTES_STORED 105 +#define ADDRESS_STREET_MODE_THROTTLE_ENABLED 105 + EEPROM_BASE_ADDRESS +#define EEPROM_BYTES_STORED 106 diff --git a/src/display/KT-LCD3/lcd.c b/src/display/KT-LCD3/lcd.c index 02cad95c..629180c4 100644 --- a/src/display/KT-LCD3/lcd.c +++ b/src/display/KT-LCD3/lcd.c @@ -91,7 +91,7 @@ static uint16_t ui16_pedal_power_filtered; static uint8_t ui8_pedal_cadence_filtered; static uint8_t ui8_lights_state = 0; -static uint8_t ui8_offroad_state = 0; +static uint8_t ui8_street_mode_enabled = 0; static uint8_t ui8_lcd_menu = 0; static uint8_t ui8_lcd_menu_config_submenu_state = 0; @@ -154,7 +154,7 @@ void low_pass_filter_pedal_torque_and_power (void); static void low_pass_filter_pedal_cadence (void); void lights_state (void); void walk_assist_state (void); -void offroad_mode (void); +void street_mode (void); void time_measurement (void); void energy_data (void); uint8_t reset_variable_check (void); @@ -172,7 +172,7 @@ void lcd_execute_menu_config_submenu_cruise (void); void lcd_execute_menu_config_main_screen_setup (void); void lcd_execute_menu_config_submenu_motor_startup_power_boost (void); void lcd_execute_menu_config_submenu_motor_temperature (void); -void lcd_execute_menu_config_submenu_offroad_mode (void); +void lcd_execute_menu_config_submenu_street_mode (void); void lcd_execute_menu_config_submenu_technical (void); void update_menu_flashing_state (void); void submenu_state_controller(uint8_t ui8_state_max_number); @@ -283,8 +283,8 @@ void lcd_clock (void) ui8_lcd_menu = 1; } - // enter power menu if enabled: ONOFF + UP click event - if (configuration_variables.ui8_main_screen_power_menu_enabled && buttons_get_onoff_state () && buttons_get_up_state ()) + // enter power menu if enabled and not in street mode: ONOFF + UP click event + if (buttons_get_onoff_state () && buttons_get_up_state () && configuration_variables.ui8_main_screen_power_menu_enabled && !ui8_street_mode_enabled) { buttons_clear_all_events (); @@ -340,7 +340,7 @@ void lcd_execute_main_screen (void) odometer (); wheel_speed (); walk_assist_state (); - offroad_mode (); + street_mode (); power (); battery_soc (); lights_state (); @@ -390,7 +390,7 @@ void lcd_execute_menu_config (void) break; case 8: - lcd_execute_menu_config_submenu_offroad_mode (); + lcd_execute_menu_config_submenu_street_mode (); break; case 9: @@ -1199,7 +1199,7 @@ void lcd_execute_menu_config_submenu_motor_startup_power_boost (void) lcd_var_number.ui8_odometer_field = ODOMETER_FIELD; lcd_configurations_print_number(&lcd_var_number); } - // enabled on startup when wheel speed is zero or always when cadence was zero + // enabled on startup when wheel speed is zero or always when cadence is zero else if (ui8_lcd_menu_config_submenu_state == 1) { ui8_temp = configuration_variables.ui8_startup_motor_power_boost_state & 1; @@ -1408,16 +1408,16 @@ void lcd_execute_menu_config_submenu_motor_temperature (void) } -void lcd_execute_menu_config_submenu_offroad_mode (void) +void lcd_execute_menu_config_submenu_street_mode (void) { var_number_t lcd_var_number; uint16_t ui16_temp; switch (ui8_lcd_menu_config_submenu_state) { - // enable/disable offroad mode + // enable/disable street mode case 0: - lcd_var_number.p_var_number = &configuration_variables.ui8_offroad_feature_enabled; + lcd_var_number.p_var_number = &configuration_variables.ui8_street_mode_function_enabled; lcd_var_number.ui8_size = 8; lcd_var_number.ui8_decimal_digit = 0; lcd_var_number.ui32_max_value = 1; @@ -1427,9 +1427,9 @@ void lcd_execute_menu_config_submenu_offroad_mode (void) lcd_configurations_print_number(&lcd_var_number); break; - // enable offroad mode on system startup + // enable street mode on system startup case 1: - lcd_var_number.p_var_number = &configuration_variables.ui8_offroad_enabled_on_startup; + lcd_var_number.p_var_number = &configuration_variables.ui8_street_mode_enabled_on_startup; lcd_var_number.ui8_size = 8; lcd_var_number.ui8_decimal_digit = 0; lcd_var_number.ui32_max_value = 1; @@ -1439,9 +1439,9 @@ void lcd_execute_menu_config_submenu_offroad_mode (void) lcd_configurations_print_number(&lcd_var_number); break; - // offroad speed limit + // street mode speed limit case 2: - lcd_var_number.p_var_number = &configuration_variables.ui8_offroad_speed_limit; + lcd_var_number.p_var_number = &configuration_variables.ui8_street_mode_speed_limit; lcd_var_number.ui8_size = 8; lcd_var_number.ui8_decimal_digit = 0; lcd_var_number.ui32_max_value = 99; @@ -1453,9 +1453,9 @@ void lcd_execute_menu_config_submenu_offroad_mode (void) lcd_enable_kmh_symbol (1); break; - // enable/disable power limit + // enable/disable street mode power limit case 3: - lcd_var_number.p_var_number = &configuration_variables.ui8_offroad_power_limit_enabled; + lcd_var_number.p_var_number = &configuration_variables.ui8_street_mode_power_limit_enabled; lcd_var_number.ui8_size = 8; lcd_var_number.ui8_decimal_digit = 0; lcd_var_number.ui32_max_value = 1; @@ -1465,9 +1465,9 @@ void lcd_execute_menu_config_submenu_offroad_mode (void) lcd_configurations_print_number(&lcd_var_number); break; - // offroad power limit + // street mode power limit case 4: - ui16_temp = ((uint16_t) configuration_variables.ui8_offroad_power_limit_div25) * 25; + ui16_temp = ((uint16_t) configuration_variables.ui8_street_mode_power_limit_div25) * 25; lcd_var_number.p_var_number = &ui16_temp; lcd_var_number.ui8_size = 16; @@ -1477,7 +1477,7 @@ void lcd_execute_menu_config_submenu_offroad_mode (void) lcd_var_number.ui32_increment_step = 25; lcd_var_number.ui8_odometer_field = BATTERY_POWER_FIELD; lcd_configurations_print_number(&lcd_var_number); - configuration_variables.ui8_offroad_power_limit_div25 = (uint8_t) (ui16_temp / 25); + configuration_variables.ui8_street_mode_power_limit_div25 = (uint8_t) (ui16_temp / 25); if (ui8_lcd_menu_flash_state || !ui8_lcd_menu_config_submenu_change_variable_enabled) @@ -1487,9 +1487,21 @@ void lcd_execute_menu_config_submenu_offroad_mode (void) } break; + + // enable/disable throttle during street mode + case 5: + lcd_var_number.p_var_number = &configuration_variables.ui8_street_mode_throttle_enabled; + lcd_var_number.ui8_size = 8; + lcd_var_number.ui8_decimal_digit = 0; + lcd_var_number.ui32_max_value = 1; + lcd_var_number.ui32_min_value = 0; + lcd_var_number.ui32_increment_step = 1; + lcd_var_number.ui8_odometer_field = ODOMETER_FIELD; + lcd_configurations_print_number(&lcd_var_number); + break; } - submenu_state_controller(4); + submenu_state_controller(5); if (ui8_lcd_menu_config_submenu_state != 2 && (ui8_lcd_menu_flash_state || ui8_lcd_menu_config_submenu_change_variable_enabled)) { @@ -1947,8 +1959,8 @@ void assist_level_state (void) // display assist level lcd_print (configuration_variables.ui8_assist_level, ASSIST_LEVEL_FIELD, 1); - // if offroad mode is disabled also display "assist" symbol - if (ui8_offroad_state == 0) + // if street mode is disabled display "assist" symbol + if (!ui8_street_mode_enabled) { lcd_enable_assist_symbol (1); } @@ -2046,51 +2058,52 @@ void walk_assist_state (void) } -void offroad_mode (void) +void street_mode (void) { - static uint8_t offroad_mode_assist_symbol_state; - static uint8_t offroad_mode_assist_symbol_state_blink_counter; + static uint8_t ui8_street_mode_assist_symbol_state; + static uint8_t ui8_street_mode_assist_symbol_state_counter; static uint8_t ui8_executed_on_startup; - if (configuration_variables.ui8_offroad_feature_enabled) + if (configuration_variables.ui8_street_mode_function_enabled) { - // enable offroad mode if user has enabled offroad mode on startup + // enable street mode if user has enabled street mode on startup if (!ui8_executed_on_startup) { ui8_executed_on_startup = 1; - if (configuration_variables.ui8_offroad_enabled_on_startup) + if (configuration_variables.ui8_street_mode_enabled_on_startup) { - ui8_offroad_state = 1; - motor_controller_data.ui8_offroad_state = 1; + ui8_street_mode_enabled = 1; + motor_controller_data.ui8_street_mode_enabled = 1; } } - if (buttons_get_onoff_state () && buttons_get_down_long_click_event ()) + if (buttons_get_onoff_state () && buttons_get_down_state ()) { buttons_clear_all_events (); - if (ui8_offroad_state == 0) + if (ui8_street_mode_enabled) { - ui8_offroad_state = 1; - motor_controller_data.ui8_offroad_state = 1; + ui8_street_mode_enabled = 0; + motor_controller_data.ui8_street_mode_enabled = 0; } else { - ui8_offroad_state = 0; - motor_controller_data.ui8_offroad_state = 0; + ui8_street_mode_enabled = 1; + motor_controller_data.ui8_street_mode_enabled = 1; } } - - if (ui8_offroad_state == 1) + + if (ui8_street_mode_enabled) { - if (++offroad_mode_assist_symbol_state_blink_counter > 50) + if (++ui8_street_mode_assist_symbol_state_counter > 50) { - offroad_mode_assist_symbol_state_blink_counter = 0; - offroad_mode_assist_symbol_state = !offroad_mode_assist_symbol_state; + ui8_street_mode_assist_symbol_state_counter = 0; + + ui8_street_mode_assist_symbol_state = !ui8_street_mode_assist_symbol_state; } - lcd_enable_assist_symbol (offroad_mode_assist_symbol_state); + lcd_enable_assist_symbol (ui8_street_mode_assist_symbol_state); } } } diff --git a/src/display/KT-LCD3/lcd.h b/src/display/KT-LCD3/lcd.h index 618a0ed2..86eaf7e7 100644 --- a/src/display/KT-LCD3/lcd.h +++ b/src/display/KT-LCD3/lcd.h @@ -30,7 +30,7 @@ typedef struct _motor_controller_data uint8_t ui8_braking; uint8_t ui8_pedal_cadence; uint8_t ui8_lights; - uint8_t ui8_offroad_state; + uint8_t ui8_street_mode_enabled; uint8_t ui8_walk_assist_level; uint16_t ui16_motor_speed_erps; uint8_t ui8_foc_angle; @@ -88,11 +88,12 @@ typedef struct _configuration_variables uint8_t ui8_lcd_power_off_time_minutes; uint8_t ui8_lcd_backlight_on_brightness; uint8_t ui8_lcd_backlight_off_brightness; - uint8_t ui8_offroad_feature_enabled; - uint8_t ui8_offroad_enabled_on_startup; - uint8_t ui8_offroad_speed_limit; - uint8_t ui8_offroad_power_limit_enabled; - uint8_t ui8_offroad_power_limit_div25; + uint8_t ui8_street_mode_function_enabled; + uint8_t ui8_street_mode_enabled_on_startup; + uint8_t ui8_street_mode_speed_limit; + uint8_t ui8_street_mode_power_limit_enabled; + uint8_t ui8_street_mode_power_limit_div25; + uint8_t ui8_street_mode_throttle_enabled; uint16_t ui16_distance_since_power_on_x10; uint32_t ui32_odometer_x10; uint32_t ui32_trip_x10; diff --git a/src/display/KT-LCD3/main.h b/src/display/KT-LCD3/main.h index a3d9c2c5..43948d90 100644 --- a/src/display/KT-LCD3/main.h +++ b/src/display/KT-LCD3/main.h @@ -113,12 +113,13 @@ -// default values for offroad function -#define DEFAULT_VALUE_OFFROAD_FEATURE_ENABLED 0 -#define DEFAULT_VALUE_OFFROAD_MODE_ENABLED_ON_STARTUP 0 -#define DEFAULT_VALUE_OFFROAD_SPEED_LIMIT 25 -#define DEFAULT_VALUE_OFFROAD_POWER_LIMIT_ENABLED 0 -#define DEFAULT_VALUE_OFFROAD_POWER_LIMIT_DIV25 10 // 10 * 25 = 250 W +// default values for street mode function +#define DEFAULT_VALUE_STREET_MODE_FUNCTION_ENABLED 0 +#define DEFAULT_VALUE_STREET_MODE_ENABLED_ON_STARTUP 0 +#define DEFAULT_VALUE_STREET_MODE_SPEED_LIMIT 25 +#define DEFAULT_VALUE_STREET_MODE_POWER_LIMIT_ENABLED 0 +#define DEFAULT_VALUE_STREET_MODE_POWER_LIMIT_DIV25 10 // 10 * 25 = 250 W +#define DEFAULT_VALUE_STREET_MODE_THROTTLE_ENABLED 0 // throttle is disabled in street mode by default diff --git a/src/display/KT-LCD3/uart.c b/src/display/KT-LCD3/uart.c index 65221e9a..7621eaa1 100644 --- a/src/display/KT-LCD3/uart.c +++ b/src/display/KT-LCD3/uart.c @@ -17,7 +17,7 @@ #define UART_NUMBER_DATA_BYTES_TO_RECEIVE 25 // change this value depending on how many data bytes there is to receive ( Package = one start byte + data bytes + two bytes 16 bit CRC ) #define UART_NUMBER_DATA_BYTES_TO_SEND 6 // change this value depending on how many data bytes there is to send ( Package = one start byte + data bytes + two bytes 16 bit CRC ) -#define UART_MAX_NUMBER_MESSAGE_ID 7 +#define UART_MAX_NUMBER_MESSAGE_ID 8 // change this value depending on how many different packages there is to send volatile uint8_t ui8_received_package_flag = 0; volatile uint8_t ui8_rx_buffer[UART_NUMBER_DATA_BYTES_TO_RECEIVE + 3]; @@ -215,9 +215,9 @@ void uart_data_clock (void) ((p_motor_controller_data->ui8_walk_assist_level & 1) << 1)); // battery power limit - if (p_motor_controller_data->ui8_offroad_state && p_configuration_variables->ui8_offroad_power_limit_enabled) + if (p_motor_controller_data->ui8_street_mode_enabled && p_configuration_variables->ui8_street_mode_power_limit_enabled) { - ui8_tx_buffer[4] = p_configuration_variables->ui8_offroad_power_limit_div25; + ui8_tx_buffer[4] = p_configuration_variables->ui8_street_mode_power_limit_div25; } else { @@ -240,9 +240,9 @@ void uart_data_clock (void) case 2: // wheel max speed - if (p_motor_controller_data->ui8_offroad_state) + if (p_motor_controller_data->ui8_street_mode_enabled) { - ui8_tx_buffer[5] = p_configuration_variables->ui8_offroad_speed_limit; + ui8_tx_buffer[5] = p_configuration_variables->ui8_street_mode_speed_limit; } else { @@ -255,18 +255,14 @@ void uart_data_clock (void) case 3: // set motor type - // enable/disable motor assistance without pedal rotation - // enable/disable motor temperature limit function - ui8_tx_buffer[5] = ((p_configuration_variables->ui8_motor_type & 3) | - ((p_configuration_variables->ui8_motor_assistance_startup_without_pedal_rotation & 1) << 2) | - ((p_configuration_variables->ui8_temperature_limit_feature_enabled & 3) << 3)); - - // motor power boost startup state + ui8_tx_buffer[5] = p_configuration_variables->ui8_motor_type; + + // motor power boost startup state and limit to max ui8_tx_buffer[6] = p_configuration_variables->ui8_startup_motor_power_boost_state; break; case 4: - // startup motor power boost + // startup motor power boost factor ui8_tx_buffer[5] = p_configuration_variables->ui8_startup_motor_power_boost_factor [((p_configuration_variables->ui8_assist_level) - 1)]; // startup motor power boost time @@ -278,7 +274,7 @@ void uart_data_clock (void) ui8_tx_buffer[5] = p_configuration_variables->ui8_startup_motor_power_boost_fade_time; // boost feature enabled - ui8_tx_buffer[6] = (p_configuration_variables->ui8_startup_motor_power_boost_feature_enabled & 1) ? 1 : 0; + ui8_tx_buffer[6] = p_configuration_variables->ui8_startup_motor_power_boost_feature_enabled; break; case 6: @@ -304,6 +300,21 @@ void uart_data_clock (void) } break; + case 8: + // enable/disable motor temperature limit function or throttle + if (p_motor_controller_data->ui8_street_mode_enabled && !p_configuration_variables->ui8_street_mode_throttle_enabled && p_configuration_variables->ui8_temperature_limit_feature_enabled == 2) + { + ui8_tx_buffer[5] = 0; + } + else + { + ui8_tx_buffer[5] = p_configuration_variables->ui8_temperature_limit_feature_enabled; + } + + // enable/disable motor assistance without pedal rotation + ui8_tx_buffer[6] = p_configuration_variables->ui8_motor_assistance_startup_without_pedal_rotation; + break; + default: ui8_message_ID = 0; break; From cb7c13090036b258a5d9d35a47b27ed6e72584f0 Mon Sep 17 00:00:00 2001 From: Leon Date: Wed, 5 Jun 2019 03:29:28 +0200 Subject: [PATCH 2/5] Optimized, improved, solved button bugs --- src/display/KT-LCD3/buttons.c | 180 +++++++++++++----- src/display/KT-LCD3/buttons.h | 12 +- src/display/KT-LCD3/lcd.c | 337 ++++++++++++++-------------------- src/display/KT-LCD3/lcd.h | 1 - src/display/KT-LCD3/main.c | 2 +- src/display/KT-LCD3/main.h | 4 +- src/display/KT-LCD3/uart.c | 11 +- 7 files changed, 289 insertions(+), 258 deletions(-) diff --git a/src/display/KT-LCD3/buttons.c b/src/display/KT-LCD3/buttons.c index a481619c..bdd938aa 100644 --- a/src/display/KT-LCD3/buttons.c +++ b/src/display/KT-LCD3/buttons.c @@ -21,6 +21,7 @@ uint8_t ui8_up_button_state = 0; uint8_t ui8_up_button_state_counter = 0; #define BUTTON_LONG_CLICK_THRESHOLD 120 // 120 -> 1.2 seconds +#define BUTTON_CLICK_THRESHOLD 30 // 30 -> 0.3 seconds buttons_events_t buttons_events = 0; @@ -129,6 +130,7 @@ void buttons_clear_onoff_long_click_event (void) buttons_events &= ~ONOFF_LONG_CLICK; } + uint8_t buttons_get_up_down_click_event (void) { return (buttons_events & UPDOWN_CLICK) ? 1: 0; @@ -139,6 +141,29 @@ void buttons_clear_up_down_click_event (void) buttons_events &= ~UPDOWN_CLICK; } + +uint8_t buttons_get_onoff_up_click_event (void) +{ + return (buttons_events & ONOFFUP_CLICK) ? 1: 0; +} + +void buttons_clear_onoff_up_click_event (void) +{ + buttons_events &= ~ONOFFUP_CLICK; +} + + +uint8_t buttons_get_onoff_down_click_event (void) +{ + return (buttons_events & ONOFFUP_CLICK) ? 1: 0; +} + +void buttons_clear_onoff_down_click_event (void) +{ + buttons_events &= ~ONOFFUP_CLICK; +} + + buttons_events_t buttons_get_events (void) { return buttons_events; @@ -159,63 +184,87 @@ void buttons_clear_all_events (void) void buttons_clock (void) { - // needed if the event is not cleared anywhere else - buttons_clear_onoff_click_long_click_event(); - switch (ui8_onoff_button_state) { case 0: + if (!buttons_get_onoff_click_event() && !buttons_get_onoff_long_click_event() && !buttons_get_onoff_click_long_click_event() && + !buttons_get_onoff_up_click_event() && + !buttons_get_onoff_down_click_event() && buttons_get_onoff_state ()) - { - ui8_onoff_button_state_counter = 0; - ui8_onoff_button_state = 1; - } + { + ui8_onoff_button_state_counter = 0; + ui8_onoff_button_state = 1; + } + break; case 1: + ui8_onoff_button_state_counter++; - // event long click + // long click if (ui8_onoff_button_state_counter > BUTTON_LONG_CLICK_THRESHOLD) { - buttons_set_events(ONOFF_LONG_CLICK); + // onoff and up or down button click + if (ui8_up_button_state == 1) + { + buttons_set_events(ONOFFUP_CLICK); + ui8_up_button_state = 2; + } + else if (ui8_down_button_state == 1) + { + buttons_set_events(ONOFFDOWN_CLICK); + ui8_down_button_state = 2; + } + else + { + buttons_set_events(ONOFF_LONG_CLICK); + } + ui8_onoff_button_state = 2; + break; } - // if button release + // button release if (!buttons_get_onoff_state ()) { - // let's validade if will be a quick click + long click - if (ui8_onoff_button_state_counter <= 30) // 0.3 second + // check for click + long click + if (ui8_onoff_button_state_counter < BUTTON_CLICK_THRESHOLD) { ui8_onoff_button_state_counter = 0; ui8_onoff_button_state = 3; + break; } - // event click else { buttons_set_events(ONOFF_CLICK); ui8_onoff_button_state = 0; + break; } } + break; case 2: + // wait for button release if (!buttons_get_onoff_state ()) { ui8_onoff_button_state = 0; + break; } + break; case 3: + ui8_onoff_button_state_counter++; // on next step, start counting for long click @@ -223,26 +272,31 @@ void buttons_clock (void) { ui8_onoff_button_state_counter = 0; ui8_onoff_button_state = 4; + break; } // event click - if (ui8_onoff_button_state_counter > 40) + if (ui8_onoff_button_state_counter > BUTTON_CLICK_THRESHOLD) { buttons_set_events(ONOFF_CLICK); ui8_onoff_button_state = 0; + break; } + break; case 4: + ui8_onoff_button_state_counter++; // event click, but this time it is: click + long click - if (ui8_onoff_button_state_counter > 100) + if (ui8_onoff_button_state_counter > BUTTON_LONG_CLICK_THRESHOLD) { buttons_set_events(ONOFF_CLICK_LONG_CLICK); ui8_onoff_button_state = 2; + break; } @@ -251,34 +305,42 @@ void buttons_clock (void) { buttons_set_events(ONOFF_CLICK); ui8_onoff_button_state = 0; + break; } + break; default: + ui8_onoff_button_state = 0; + break; } switch (ui8_up_button_state) { case 0: + if (!buttons_get_up_click_event() && !buttons_get_up_long_click_event() && !buttons_get_up_click_long_click_event() && !buttons_get_up_down_click_event() && + !buttons_get_onoff_up_click_event() && buttons_get_up_state()) - { - ui8_up_button_state_counter = 0; - ui8_up_button_state = 1; - } + { + ui8_up_button_state_counter = 0; + ui8_up_button_state = 1; + } + break; case 1: + ui8_up_button_state_counter++; - // event long click - if (ui8_up_button_state_counter++ > BUTTON_LONG_CLICK_THRESHOLD) + // long click + if (ui8_up_button_state_counter > BUTTON_LONG_CLICK_THRESHOLD) { // up and down button click if (ui8_down_button_state == 1) @@ -292,40 +354,46 @@ void buttons_clock (void) } ui8_up_button_state = 2; - ui8_up_button_state_counter = 0; + break; } - // if button release + // button release if (!buttons_get_up_state ()) { - // let's validade if will be a quick click + long click - if (ui8_up_button_state_counter <= 30) // 0.3 second + // check for click + long click + if (ui8_up_button_state_counter <= BUTTON_CLICK_THRESHOLD) { ui8_up_button_state_counter = 0; ui8_up_button_state = 3; + break; } - // event click else { buttons_set_events(UP_CLICK); ui8_up_button_state = 0; + break; } } + break; case 2: + // wait for button release if (!buttons_get_up_state ()) { ui8_up_button_state = 0; + break; } + break; case 3: + ui8_up_button_state_counter++; // on next step, start counting for long click @@ -333,26 +401,31 @@ void buttons_clock (void) { ui8_up_button_state_counter = 0; ui8_up_button_state = 4; + break; } // event click - if (ui8_up_button_state_counter > 30) + if (ui8_up_button_state_counter > BUTTON_CLICK_THRESHOLD) { buttons_set_events(UP_CLICK); ui8_up_button_state = 0; + break; } + break; case 4: + ui8_up_button_state_counter++; // event click, but this time it is: click + long click - if (ui8_up_button_state_counter > 100) + if (ui8_up_button_state_counter > BUTTON_LONG_CLICK_THRESHOLD) { buttons_set_events(UP_CLICK_LONG_CLICK); ui8_up_button_state = 2; + break; } @@ -361,36 +434,44 @@ void buttons_clock (void) { buttons_set_events(UP_CLICK); ui8_up_button_state = 0; + break; } + break; default: + ui8_up_button_state = 0; + break; } switch (ui8_down_button_state) { case 0: + if (!buttons_get_down_click_event() && !buttons_get_down_long_click_event() && !buttons_get_down_click_long_click_event() && !buttons_get_up_down_click_event() && + !buttons_get_onoff_down_click_event() && buttons_get_down_state()) - { - ui8_down_button_state_counter = 0; - ui8_down_button_state = 1; - } + { + ui8_down_button_state_counter = 0; + ui8_down_button_state = 1; + } + break; case 1: + ui8_down_button_state_counter++; - // event long click - if (ui8_down_button_state_counter++ > BUTTON_LONG_CLICK_THRESHOLD) + // long click + if (ui8_down_button_state_counter > BUTTON_LONG_CLICK_THRESHOLD) { - // up and down button click + // up and down long click if (ui8_up_button_state == 1) { buttons_set_events(UPDOWN_CLICK); @@ -402,41 +483,47 @@ void buttons_clock (void) } ui8_down_button_state = 2; - ui8_down_button_state_counter = 0; + break; } - // if button release + // button release if (!buttons_get_down_state ()) { - // let's validade if will be a quick click + long click - if (ui8_down_button_state_counter <= 30) // 0.3 second + // check for click + long click + if (ui8_down_button_state_counter < BUTTON_CLICK_THRESHOLD) { ui8_down_button_state_counter = 0; ui8_down_button_state = 3; + break; } - // event click else { buttons_set_events(DOWN_CLICK); ui8_down_button_state = 0; + break; } } + break; case 2: + // wait for button release if (!buttons_get_down_state ()) { ui8_down_button_state = 0; + break; } + break; case 3: + ui8_down_button_state_counter++; // on next step, start counting for long click @@ -444,26 +531,31 @@ void buttons_clock (void) { ui8_down_button_state_counter = 0; ui8_down_button_state = 4; + break; } // event click - if (ui8_down_button_state_counter > 30) + if (ui8_down_button_state_counter > BUTTON_CLICK_THRESHOLD) { buttons_set_events(DOWN_CLICK); ui8_down_button_state = 0; + break; } + break; case 4: + ui8_down_button_state_counter++; // event click, but this time it is: click + long click - if (ui8_down_button_state_counter > 100) + if (ui8_down_button_state_counter > BUTTON_LONG_CLICK_THRESHOLD) { buttons_set_events(DOWN_CLICK_LONG_CLICK); ui8_down_button_state = 2; + break; } @@ -472,12 +564,16 @@ void buttons_clock (void) { buttons_set_events(DOWN_CLICK); ui8_down_button_state = 0; + break; } + break; default: + ui8_down_button_state = 0; + break; } } diff --git a/src/display/KT-LCD3/buttons.h b/src/display/KT-LCD3/buttons.h index 028807e7..47917e62 100644 --- a/src/display/KT-LCD3/buttons.h +++ b/src/display/KT-LCD3/buttons.h @@ -23,7 +23,9 @@ typedef enum DOWN_CLICK = 64, DOWN_CLICK_LONG_CLICK = 128, DOWN_LONG_CLICK = 256, - UPDOWN_CLICK = 512 + UPDOWN_CLICK = 512, + ONOFFUP_CLICK = 1024, + ONOFFDOWN_CLICK = 2048 } buttons_events_t; uint8_t buttons_get_up_state(void); @@ -33,6 +35,7 @@ uint8_t buttons_get_up_long_click_event(void); void buttons_clear_up_click_event(void); void buttons_clear_up_click_long_click_event(void); void buttons_clear_up_long_click_event(void); + uint8_t buttons_get_down_state(void); uint8_t buttons_get_down_click_event(void); uint8_t buttons_get_down_click_long_click_event(void); @@ -40,6 +43,7 @@ uint8_t buttons_get_down_long_click_event(void); void buttons_clear_down_click_event(void); void buttons_clear_down_click_long_click_event(void); void buttons_clear_down_long_click_event(void); + uint8_t buttons_get_onoff_state(void); uint8_t buttons_get_onoff_click_event(void); uint8_t buttons_get_onoff_click_long_click_event(void); @@ -47,8 +51,14 @@ uint8_t buttons_get_onoff_long_click_event(void); void buttons_clear_onoff_click_event(void); void buttons_clear_onoff_click_long_click_event(void); void buttons_clear_onoff_long_click_event(void); + uint8_t buttons_get_up_down_click_event(void); void buttons_clear_up_down_click_event(void); +uint8_t buttons_get_onoff_up_click_event(void); +void buttons_clear_onoff_up_click_event(void); +uint8_t buttons_get_onoff_down_click_event(void); +void buttons_clear_onoff_down_click_event(void); + void buttons_clock(void); buttons_events_t buttons_get_events(void); void buttons_clear_all_events(void); diff --git a/src/display/KT-LCD3/lcd.c b/src/display/KT-LCD3/lcd.c index 629180c4..95cfd2b8 100644 --- a/src/display/KT-LCD3/lcd.c +++ b/src/display/KT-LCD3/lcd.c @@ -95,8 +95,6 @@ static uint8_t ui8_street_mode_enabled = 0; static uint8_t ui8_lcd_menu = 0; static uint8_t ui8_lcd_menu_config_submenu_state = 0; -static uint8_t ui8_lcd_menu_flash_counter = 0; -static uint16_t ui16_lcd_menu_flash_counter_temperature = 0; static uint8_t ui8_lcd_menu_flash_state; static uint8_t ui8_lcd_menu_flash_state_temperature; static uint8_t ui8_lcd_menu_config_submenu_number = 0; @@ -157,7 +155,6 @@ void walk_assist_state (void); void street_mode (void); void time_measurement (void); void energy_data (void); -uint8_t reset_variable_check (void); // menu functions @@ -178,10 +175,10 @@ void update_menu_flashing_state (void); void submenu_state_controller(uint8_t ui8_state_max_number); void advance_on_subfield (uint8_t* ui8_p_state, uint8_t ui8_state_max_number); void odometer_increase_field_state (void); +uint8_t reset_variable_check (void); // LCD functions -static void automatic_power_off_management (void); void lcd_power_off (uint8_t SaveToEEPROM); void lcd_update (void); void lcd_clear (void); @@ -275,7 +272,7 @@ void lcd_clock (void) update_menu_flashing_state (); - // enter configuration menu: UP + DOWN click event + // enter configuration menu if... if (buttons_get_up_down_click_event () && ui8_lcd_menu == 0) { buttons_clear_all_events (); @@ -283,8 +280,8 @@ void lcd_clock (void) ui8_lcd_menu = 1; } - // enter power menu if enabled and not in street mode: ONOFF + UP click event - if (buttons_get_onoff_state () && buttons_get_up_state () && configuration_variables.ui8_main_screen_power_menu_enabled && !ui8_street_mode_enabled) + // enter quick configure power menu if... + if (buttons_get_onoff_up_click_event () && configuration_variables.ui8_main_screen_power_menu_enabled && !ui8_street_mode_enabled && ui8_lcd_menu == 0) { buttons_clear_all_events (); @@ -311,25 +308,11 @@ void lcd_clock (void) } low_pass_filter_battery_voltage_current_power (); - - // filter using every 500 ms value - if (ui8_lcd_menu_counter_500ms_state) - { - low_pass_filter_pedal_cadence (); - } - - // filter using every 100 ms value - if (ui8_lcd_menu_counter_100ms_state) - { - low_pass_filter_pedal_torque_and_power (); - } - + low_pass_filter_pedal_cadence (); + low_pass_filter_pedal_torque_and_power (); calc_battery_soc (); calc_distance (); - automatic_power_off_management (); lcd_update (); - - // power off system: ONOFF long click event power_off_management (); } @@ -437,15 +420,13 @@ void lcd_execute_menu_config (void) // leave config menu if button_onoff_long_click if (buttons_get_onoff_long_click_event ()) { - buttons_clear_onoff_long_click_event (); + buttons_clear_all_events (); // save the updated variables on EEPROM eeprom_write_variables (); // switch to main screen ui8_lcd_menu = 0; - - return; } // print submenu number only half of the time @@ -1565,9 +1546,6 @@ void lcd_execute_menu_config_power (void) var_number_t lcd_var_number; uint16_t ui16_temp; - // because this click event can happen and will block the detection of button_onoff_long_click_event - buttons_clear_onoff_click_event (); - // enable change of variables ui8_lcd_menu_config_submenu_change_variable_enabled = 1; @@ -1607,16 +1585,39 @@ void lcd_execute_menu_config_power (void) // change to main screen ui8_lcd_menu = 0; - - return; } } void power_off_management (void) { - // power off system + static uint16_t ui16_seconds_since_power_on_offset; + + // power off system if ONOFF long click if (buttons_get_onoff_long_click_event ()) { lcd_power_off (1); } + + // check if automatic power off management is configured to any value + if (configuration_variables.ui8_lcd_power_off_time_minutes != 0) + { + // check if there is system activity + if ((motor_controller_data.ui16_wheel_speed_x10 > 0) || // wheel speed > 0 + (motor_controller_data.ui8_battery_current_x5 > 0) || // battery current > 0 + (motor_controller_data.ui8_braking) || // braking + (buttons_get_events ())) // any button active + { + // reset offset + ui16_seconds_since_power_on_offset = ui16_seconds_since_power_on; + } + else + { + // check if system has been inactive over or equal to the configured threshold time + if (ui16_seconds_since_power_on - ui16_seconds_since_power_on_offset >= (configuration_variables.ui8_lcd_power_off_time_minutes * 60)) + { + // power off system and save variables to EEPROM + lcd_power_off (1); + } + } + } } @@ -1876,10 +1877,13 @@ void calc_battery_soc (void) { uint16_t ui16_fluctuate_battery_voltage_x10; uint32_t ui32_temp; + static uint8_t ui8_update_counter; - // update battery level value every 100 ms -> 10 times per second. This helps to filter the fast changing values - if (ui8_lcd_menu_counter_100ms_state) + if (++ui8_update_counter > 10) // update battery level value every 100 ms -> 10. This helps to filter the fast changing values { + // reset counter + ui8_update_counter = 0; + // calculate battery voltage that takes into consideration current and internal battery pack resistance ui16_fluctuate_battery_voltage_x10 = (uint16_t) ((((uint32_t) configuration_variables.ui16_battery_pack_resistance_x1000) * ((uint32_t) ui16_battery_current_filtered_x5)) / ((uint32_t) 500)); @@ -1969,23 +1973,13 @@ void assist_level_state (void) void lights_state (void) { - // if UP button is click and hold if (buttons_get_up_long_click_event ()) { - // clear button event buttons_clear_up_long_click_event (); // toggle light state and display backlight - if (ui8_lights_state == 0) - { - ui8_lights_state = 1; - motor_controller_data.ui8_lights = 1; - } - else - { - ui8_lights_state = 0; - motor_controller_data.ui8_lights = 0; - } + ui8_lights_state = !ui8_lights_state; + motor_controller_data.ui8_lights = ui8_lights_state; } // set backlight brightness @@ -2078,25 +2072,18 @@ void street_mode (void) } } - if (buttons_get_onoff_state () && buttons_get_down_state ()) + if (buttons_get_onoff_down_click_event ()) { buttons_clear_all_events (); - if (ui8_street_mode_enabled) - { - ui8_street_mode_enabled = 0; - motor_controller_data.ui8_street_mode_enabled = 0; - } - else - { - ui8_street_mode_enabled = 1; - motor_controller_data.ui8_street_mode_enabled = 1; - } + ui8_street_mode_enabled = !ui8_street_mode_enabled; + + motor_controller_data.ui8_street_mode_enabled = ui8_street_mode_enabled; } if (ui8_street_mode_enabled) { - if (++ui8_street_mode_assist_symbol_state_counter > 50) + if (++ui8_street_mode_assist_symbol_state_counter > 40) { ui8_street_mode_assist_symbol_state_counter = 0; @@ -2111,8 +2098,7 @@ void street_mode (void) void brake (void) { - if (motor_controller_data.ui8_braking) { lcd_enable_brake_symbol (1); } - else { lcd_enable_brake_symbol (0); } + lcd_enable_brake_symbol (motor_controller_data.ui8_braking); } @@ -2141,23 +2127,25 @@ uint8_t reset_variable_check (void) // if there is one down_click_long_click_event if (buttons_get_down_click_long_click_event()) { + buttons_clear_down_long_click_event(); + + // start counting to reset variable ui8_odometer_reset_distance_counter_state = 1; + + // reset counter for variable reset + ui16_odometer_reset_distance_counter = 0; } if (ui8_odometer_reset_distance_counter_state) { if (buttons_get_down_state ()) { - // clear the button events + // clear the possible button event (can maybe be removed) buttons_clear_down_click_event(); - buttons_clear_down_long_click_event(); // count time, after limit, reset everything if (++ui16_odometer_reset_distance_counter > 300) { - // reset counter - ui16_odometer_reset_distance_counter = 0; - // reset counter state ui8_odometer_reset_distance_counter_state = 0; @@ -2177,11 +2165,6 @@ uint8_t reset_variable_check (void) ui8_odometer_reset_distance_counter_state = 0; } } - else - { - // reset counter - ui16_odometer_reset_distance_counter = 0; - } // display variable as usual return 0; @@ -3444,50 +3427,57 @@ void low_pass_filter_pedal_torque_and_power (void) { static uint32_t ui32_pedal_torque_accumulated; static uint32_t ui32_pedal_power_accumulated; - - // low pass filter for pedal torque display - ui32_pedal_torque_accumulated -= ui32_pedal_torque_accumulated >> PEDAL_TORQUE_FILTER_COEFFICIENT; - ui32_pedal_torque_accumulated += (uint32_t) motor_controller_data.ui16_pedal_torque_x10 / 10; - ui16_pedal_torque_filtered = ((uint32_t) (ui32_pedal_torque_accumulated >> PEDAL_TORQUE_FILTER_COEFFICIENT)); + static uint8_t ui8_update_counter; - if (ui16_pedal_torque_filtered > 200) - { - ui16_pedal_torque_filtered /= 20; - ui16_pedal_torque_filtered *= 20; - } - else if (ui16_pedal_torque_filtered > 100) + if (++ui8_update_counter > 10) // update every 100 ms -> 10. This helps to filter the fast changing values { - ui16_pedal_torque_filtered /= 10; - ui16_pedal_torque_filtered *= 10; - } - else - { - // do nothing to orginal values - } + // reset counter + ui8_update_counter = 0; + + // low pass filter for pedal torque display + ui32_pedal_torque_accumulated -= ui32_pedal_torque_accumulated >> PEDAL_TORQUE_FILTER_COEFFICIENT; + ui32_pedal_torque_accumulated += (uint32_t) motor_controller_data.ui16_pedal_torque_x10 / 10; + ui16_pedal_torque_filtered = ((uint32_t) (ui32_pedal_torque_accumulated >> PEDAL_TORQUE_FILTER_COEFFICIENT)); - // low pass filter for pedal power display - ui32_pedal_power_accumulated -= ui32_pedal_power_accumulated >> PEDAL_POWER_FILTER_COEFFICIENT; - ui32_pedal_power_accumulated += (uint32_t) motor_controller_data.ui16_pedal_power_x10 / 10; - ui16_pedal_power_filtered = ((uint32_t) (ui32_pedal_power_accumulated >> PEDAL_POWER_FILTER_COEFFICIENT)); + if (ui16_pedal_torque_filtered > 200) + { + ui16_pedal_torque_filtered /= 20; + ui16_pedal_torque_filtered *= 20; + } + else if (ui16_pedal_torque_filtered > 100) + { + ui16_pedal_torque_filtered /= 10; + ui16_pedal_torque_filtered *= 10; + } + else + { + // do nothing to orginal values + } - if (ui16_pedal_power_filtered > 500) - { - ui16_pedal_power_filtered /= 25; - ui16_pedal_power_filtered *= 25; - } - else if (ui16_pedal_power_filtered > 200) - { - ui16_pedal_power_filtered /= 20; - ui16_pedal_power_filtered *= 20; - } - else if (ui16_pedal_power_filtered > 10) - { - ui16_pedal_power_filtered /= 10; - ui16_pedal_power_filtered *= 10; - } - else - { - ui16_pedal_power_filtered = 0; // no point to show less than 10 W + // low pass filter for pedal power display + ui32_pedal_power_accumulated -= ui32_pedal_power_accumulated >> PEDAL_POWER_FILTER_COEFFICIENT; + ui32_pedal_power_accumulated += (uint32_t) motor_controller_data.ui16_pedal_power_x10 / 10; + ui16_pedal_power_filtered = ((uint32_t) (ui32_pedal_power_accumulated >> PEDAL_POWER_FILTER_COEFFICIENT)); + + if (ui16_pedal_power_filtered > 500) + { + ui16_pedal_power_filtered /= 25; + ui16_pedal_power_filtered *= 25; + } + else if (ui16_pedal_power_filtered > 200) + { + ui16_pedal_power_filtered /= 20; + ui16_pedal_power_filtered *= 20; + } + else if (ui16_pedal_power_filtered > 10) + { + ui16_pedal_power_filtered /= 10; + ui16_pedal_power_filtered *= 10; + } + else + { + ui16_pedal_power_filtered = 0; // no point to show less than 10 W + } } } @@ -3495,19 +3485,26 @@ void low_pass_filter_pedal_torque_and_power (void) static void low_pass_filter_pedal_cadence (void) { static uint16_t ui16_pedal_cadence_accumulated; - - // low pass filter - ui16_pedal_cadence_accumulated -= (ui16_pedal_cadence_accumulated >> PEDAL_CADENCE_FILTER_COEFFICIENT); - ui16_pedal_cadence_accumulated += (uint16_t) motor_controller_data.ui8_pedal_cadence; + static uint8_t ui8_update_counter; - // consider the filtered value only for medium and high values of the unfiltered value - if (motor_controller_data.ui8_pedal_cadence > 20) + if (++ui8_update_counter > 50) // update every 500 ms -> 50. This helps to filter the fast changing values { - ui8_pedal_cadence_filtered = (uint8_t) (ui16_pedal_cadence_accumulated >> PEDAL_CADENCE_FILTER_COEFFICIENT); - } - else - { - ui8_pedal_cadence_filtered = motor_controller_data.ui8_pedal_cadence; + // reset counter + ui8_update_counter = 0; + + // low pass filter + ui16_pedal_cadence_accumulated -= (ui16_pedal_cadence_accumulated >> PEDAL_CADENCE_FILTER_COEFFICIENT); + ui16_pedal_cadence_accumulated += (uint16_t) motor_controller_data.ui8_pedal_cadence; + + // consider the filtered value only for medium and high values of the unfiltered value + if (motor_controller_data.ui8_pedal_cadence > 20) + { + ui8_pedal_cadence_filtered = (uint8_t) (ui16_pedal_cadence_accumulated >> PEDAL_CADENCE_FILTER_COEFFICIENT); + } + else + { + ui8_pedal_cadence_filtered = motor_controller_data.ui8_pedal_cadence; + } } } @@ -3533,35 +3530,6 @@ void calc_distance (void) } -static void automatic_power_off_management (void) -{ - static uint16_t ui16_seconds_since_power_on_offset; - - // check if automatic power off management is configured to any value - if (configuration_variables.ui8_lcd_power_off_time_minutes != 0) - { - // check if there is system activity - if ((motor_controller_data.ui16_wheel_speed_x10 > 0) || // wheel speed > 0 - (motor_controller_data.ui8_battery_current_x5 > 0) || // battery current > 0 - (motor_controller_data.ui8_braking) || // braking - (buttons_get_events ())) // any button active - { - // reset offset - ui16_seconds_since_power_on_offset = ui16_seconds_since_power_on; - } - else - { - // check if system has been inactive over or equal to the configured threshold time - if (ui16_seconds_since_power_on - ui16_seconds_since_power_on_offset >= (configuration_variables.ui8_lcd_power_off_time_minutes * 60)) - { - // power off system and save variables to EEPROM - lcd_power_off (1); - } - } - } -} - - struct_configuration_variables* get_configuration_variables (void) { return &configuration_variables; @@ -3601,9 +3569,12 @@ void lcd_set_backlight_intensity (uint8_t ui8_intensity) void update_menu_flashing_state(void) { - // *************************************************************************************************** + static uint8_t ui8_lcd_menu_flash_counter; + static uint16_t ui16_lcd_menu_flash_counter_temperature; - // For flashing on menus + #define LCD_TEMPERATURE_FLASH_OFF_THRESHOLD 20 // 20 -> 200 ms + + // flash on menus if (ui8_lcd_menu_flash_counter == 0) { if (ui8_lcd_menu_flash_state) @@ -3619,71 +3590,29 @@ void update_menu_flashing_state(void) } ui8_lcd_menu_flash_counter--; - // *************************************************************************************************** - - ui8_lcd_menu_counter_100ms_state = 0; - if (++ui8_lcd_menu_counter_100ms > 10) - { - ui8_lcd_menu_counter_100ms = 0; - ui8_lcd_menu_counter_100ms_state = 1; - } - - ui8_lcd_menu_counter_500ms_state = 0; - if (++ui8_lcd_menu_counter_500ms > 50) - { - ui8_lcd_menu_counter_500ms = 0; - ui8_lcd_menu_counter_500ms_state = 1; - } - - // *************************************************************************************************** - - // For flashing the temperature field when the current is being limited due to motor over temperature - // flash only if current is being limited: ui8_temperature_current_limiting_value != 255 - if (motor_controller_data.ui8_temperature_current_limiting_value != 255) + // flash the temperature field when the current is being limited due to motor over temperature + if (motor_controller_data.ui8_temperature_current_limiting_value < 255) // flash only if current is being limited, i.e. below 255 { - if (ui8_lcd_menu_flash_state_temperature == 0) // state 0: disabled + if (ui8_lcd_menu_flash_state_temperature == 0) { - if (ui16_lcd_menu_flash_counter_temperature > 0) - { - ui16_lcd_menu_flash_counter_temperature--; - } - - if (ui16_lcd_menu_flash_counter_temperature == 0) + if (++ui16_lcd_menu_flash_counter_temperature > LCD_TEMPERATURE_FLASH_OFF_THRESHOLD) { - // if motor_controller_data.ui8_temperature_current_limiting_value == 0, flash quicker meaning motor is shutoff - if (motor_controller_data.ui8_temperature_current_limiting_value > 0) - { - ui16_lcd_menu_flash_counter_temperature = 50 + ((uint16_t) motor_controller_data.ui8_temperature_current_limiting_value); - } - else - { - ui16_lcd_menu_flash_counter_temperature = 25; - } - + ui16_lcd_menu_flash_counter_temperature = 0; + ui8_lcd_menu_flash_state_temperature = 1; } } - - if (ui8_lcd_menu_flash_state_temperature == 1) // state 1: enabled + else if (++ui16_lcd_menu_flash_counter_temperature > motor_controller_data.ui8_temperature_current_limiting_value + LCD_TEMPERATURE_FLASH_OFF_THRESHOLD) { - if (ui16_lcd_menu_flash_counter_temperature > 0) - { - ui16_lcd_menu_flash_counter_temperature--; - } - - if (ui16_lcd_menu_flash_counter_temperature == 0) - { - ui16_lcd_menu_flash_counter_temperature = 25; // 0.25 second - ui8_lcd_menu_flash_state_temperature = 0; - } + ui16_lcd_menu_flash_counter_temperature = 0; + + ui8_lcd_menu_flash_state_temperature = 0; } } else { ui8_lcd_menu_flash_state_temperature = 1; } - - // *************************************************************************************************** } diff --git a/src/display/KT-LCD3/lcd.h b/src/display/KT-LCD3/lcd.h index 86eaf7e7..d438d765 100644 --- a/src/display/KT-LCD3/lcd.h +++ b/src/display/KT-LCD3/lcd.h @@ -26,7 +26,6 @@ typedef struct _motor_controller_data uint8_t ui8_duty_cycle; uint8_t ui8_error_states; uint16_t ui16_wheel_speed_x10; - uint8_t ui8_motor_controller_state_2; uint8_t ui8_braking; uint8_t ui8_pedal_cadence; uint8_t ui8_lights; diff --git a/src/display/KT-LCD3/main.c b/src/display/KT-LCD3/main.c index 242a7d76..56186d5e 100644 --- a/src/display/KT-LCD3/main.c +++ b/src/display/KT-LCD3/main.c @@ -67,7 +67,7 @@ int main (void) // because of continue; at the end of each if code block that will stop the while (1) loop there, // the first if block code will have the higher priority over any others ui16_timer3_counter = get_timer3_counter (); - if ((ui16_timer3_counter - ui16_10ms_loop_counter) > 10) // every 10ms + if ((ui16_timer3_counter - ui16_10ms_loop_counter) > 10) // every 10 ms { ui16_10ms_loop_counter = ui16_timer3_counter; diff --git a/src/display/KT-LCD3/main.h b/src/display/KT-LCD3/main.h index 43948d90..dc4d717b 100644 --- a/src/display/KT-LCD3/main.h +++ b/src/display/KT-LCD3/main.h @@ -209,10 +209,10 @@ Measured with a cheap digital hook scale. We found that each torque sensor unit is equal to 0.556 Nm. Using the - scale, it was found that each 1kg was measured as 3 + scale, it was found that each 1 kg was measured as 3 torque sensor units. - Force (Nm) = Kg * 9.18 * 0.17 (arm cranks size) + Force (Nm) = kg * 9.18 * 0.17 (arm cranks size) ---------------------------------------------------------*/ diff --git a/src/display/KT-LCD3/uart.c b/src/display/KT-LCD3/uart.c index 7621eaa1..cbbd12a3 100644 --- a/src/display/KT-LCD3/uart.c +++ b/src/display/KT-LCD3/uart.c @@ -126,10 +126,7 @@ void uart_data_clock (void) p_motor_controller_data->ui16_wheel_speed_x10 = (((uint16_t) ui8_rx_buffer [5]) << 8) + ((uint16_t) ui8_rx_buffer [4]); // brake state - p_motor_controller_data->ui8_motor_controller_state_2 = ui8_rx_buffer[6]; - - // set brake state - p_motor_controller_data->ui8_braking = p_motor_controller_data->ui8_motor_controller_state_2 & 1; + p_motor_controller_data->ui8_braking = ui8_rx_buffer[6] & 1; // throttle value from ADC p_motor_controller_data->ui8_adc_throttle = ui8_rx_buffer[7]; @@ -340,8 +337,8 @@ void uart_data_clock (void) // increment message ID for next package if (++ui8_message_ID > UART_MAX_NUMBER_MESSAGE_ID) { ui8_message_ID = 0; } - // let's wait for 10 packages, seems that first ADC battery voltage is an incorrect value - if (++ui8_uart_received_first_package > 10) { ui8_uart_received_first_package = 10; } + // disregard first packages (seems that first ADC battery voltage is an incorrect value) + if (++ui8_uart_received_first_package > 11) { ui8_uart_received_first_package = 11; } } // enable UART2 receive interrupt as we are now ready to receive a new package @@ -352,7 +349,7 @@ void uart_data_clock (void) uint8_t uart_received_first_package (void) { - return (ui8_uart_received_first_package == 10) ? 1: 0; + return (ui8_uart_received_first_package == 11) ? 1: 0; } From 099218a2d435e9b211cf299a6156e18477bedce2 Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 7 Jun 2019 14:10:32 +0200 Subject: [PATCH 3/5] New button code and bug fixes --- src/display/KT-LCD3/buttons.c | 82 ++------------ src/display/KT-LCD3/buttons.h | 12 --- src/display/KT-LCD3/lcd.c | 197 ++++++++++++---------------------- src/display/KT-LCD3/lcd.h | 21 ++-- src/display/KT-LCD3/main.h | 4 +- 5 files changed, 90 insertions(+), 226 deletions(-) diff --git a/src/display/KT-LCD3/buttons.c b/src/display/KT-LCD3/buttons.c index bdd938aa..10cd8869 100644 --- a/src/display/KT-LCD3/buttons.c +++ b/src/display/KT-LCD3/buttons.c @@ -20,7 +20,7 @@ uint8_t ui8_down_button_state_counter = 0; uint8_t ui8_up_button_state = 0; uint8_t ui8_up_button_state_counter = 0; -#define BUTTON_LONG_CLICK_THRESHOLD 120 // 120 -> 1.2 seconds +#define BUTTON_LONG_CLICK_THRESHOLD 100 // 100 -> 1.0 seconds #define BUTTON_CLICK_THRESHOLD 30 // 30 -> 0.3 seconds buttons_events_t buttons_events = 0; @@ -40,11 +40,6 @@ uint8_t buttons_get_up_long_click_event (void) return (buttons_events & UP_LONG_CLICK) ? 1: 0; } -void buttons_clear_up_click_event (void) -{ - buttons_events &= ~UP_CLICK; -} - uint8_t buttons_get_up_click_long_click_event(void) { return (buttons_events & UP_CLICK_LONG_CLICK) ? 1: 0; @@ -55,11 +50,6 @@ void buttons_clear_up_long_click_event (void) buttons_events &= ~UP_LONG_CLICK; } -void buttons_clear_up_click_long_click_event (void) -{ - buttons_events &= ~UP_CLICK_LONG_CLICK; -} - uint8_t buttons_get_down_state (void) { return GPIO_ReadInputPin(LCD3_BUTTON_DOWN__PORT, LCD3_BUTTON_DOWN__PIN) != 0 ? 0: 1; @@ -75,26 +65,11 @@ uint8_t buttons_get_down_long_click_event (void) return (buttons_events & DOWN_LONG_CLICK) ? 1: 0; } -void buttons_clear_down_click_event (void) -{ - buttons_events &= ~DOWN_CLICK; -} - -void buttons_clear_down_click_long_click_event (void) -{ - buttons_events &= ~DOWN_CLICK_LONG_CLICK; -} - uint8_t buttons_get_down_click_long_click_event (void) { return (buttons_events & DOWN_CLICK_LONG_CLICK) ? 1: 0; } -void buttons_clear_down_long_click_event (void) -{ - buttons_events &= ~DOWN_LONG_CLICK; -} - uint8_t buttons_get_onoff_state (void) { return GPIO_ReadInputPin(LCD3_BUTTON_ONOFF__PORT, LCD3_BUTTON_ONOFF__PIN) != 0 ? 1: 0; @@ -115,55 +90,26 @@ uint8_t buttons_get_onoff_click_long_click_event (void) return (buttons_events & ONOFF_CLICK_LONG_CLICK) ? 1: 0; } -void buttons_clear_onoff_click_event (void) -{ - buttons_events &= ~ONOFF_CLICK; -} - void buttons_clear_onoff_click_long_click_event (void) { buttons_events &= ~ONOFF_CLICK_LONG_CLICK; } -void buttons_clear_onoff_long_click_event (void) -{ - buttons_events &= ~ONOFF_LONG_CLICK; -} - - uint8_t buttons_get_up_down_click_event (void) { return (buttons_events & UPDOWN_CLICK) ? 1: 0; } -void buttons_clear_up_down_click_event (void) -{ - buttons_events &= ~UPDOWN_CLICK; -} - - uint8_t buttons_get_onoff_up_click_event (void) { return (buttons_events & ONOFFUP_CLICK) ? 1: 0; } -void buttons_clear_onoff_up_click_event (void) -{ - buttons_events &= ~ONOFFUP_CLICK; -} - - uint8_t buttons_get_onoff_down_click_event (void) { return (buttons_events & ONOFFUP_CLICK) ? 1: 0; } -void buttons_clear_onoff_down_click_event (void) -{ - buttons_events &= ~ONOFFUP_CLICK; -} - - buttons_events_t buttons_get_events (void) { return buttons_events; @@ -177,23 +123,17 @@ void buttons_set_events (buttons_events_t events) void buttons_clear_all_events (void) { buttons_events = 0; - ui8_onoff_button_state = 0; - ui8_up_button_state = 0; - ui8_down_button_state = 0; } void buttons_clock (void) { + buttons_clear_all_events(); + switch (ui8_onoff_button_state) { case 0: - if (!buttons_get_onoff_click_event() && - !buttons_get_onoff_long_click_event() && - !buttons_get_onoff_click_long_click_event() && - !buttons_get_onoff_up_click_event() && - !buttons_get_onoff_down_click_event() && - buttons_get_onoff_state ()) + if (buttons_get_onoff_state()) { ui8_onoff_button_state_counter = 0; ui8_onoff_button_state = 1; @@ -322,12 +262,7 @@ void buttons_clock (void) { case 0: - if (!buttons_get_up_click_event() && - !buttons_get_up_long_click_event() && - !buttons_get_up_click_long_click_event() && - !buttons_get_up_down_click_event() && - !buttons_get_onoff_up_click_event() && - buttons_get_up_state()) + if (buttons_get_up_state()) { ui8_up_button_state_counter = 0; ui8_up_button_state = 1; @@ -451,12 +386,7 @@ void buttons_clock (void) { case 0: - if (!buttons_get_down_click_event() && - !buttons_get_down_long_click_event() && - !buttons_get_down_click_long_click_event() && - !buttons_get_up_down_click_event() && - !buttons_get_onoff_down_click_event() && - buttons_get_down_state()) + if (buttons_get_down_state()) { ui8_down_button_state_counter = 0; ui8_down_button_state = 1; diff --git a/src/display/KT-LCD3/buttons.h b/src/display/KT-LCD3/buttons.h index 47917e62..c21e64ab 100644 --- a/src/display/KT-LCD3/buttons.h +++ b/src/display/KT-LCD3/buttons.h @@ -32,32 +32,20 @@ uint8_t buttons_get_up_state(void); uint8_t buttons_get_up_click_event(void); uint8_t buttons_get_up_click_long_click_event(void); uint8_t buttons_get_up_long_click_event(void); -void buttons_clear_up_click_event(void); -void buttons_clear_up_click_long_click_event(void); -void buttons_clear_up_long_click_event(void); uint8_t buttons_get_down_state(void); uint8_t buttons_get_down_click_event(void); uint8_t buttons_get_down_click_long_click_event(void); uint8_t buttons_get_down_long_click_event(void); -void buttons_clear_down_click_event(void); -void buttons_clear_down_click_long_click_event(void); -void buttons_clear_down_long_click_event(void); uint8_t buttons_get_onoff_state(void); uint8_t buttons_get_onoff_click_event(void); uint8_t buttons_get_onoff_click_long_click_event(void); uint8_t buttons_get_onoff_long_click_event(void); -void buttons_clear_onoff_click_event(void); -void buttons_clear_onoff_click_long_click_event(void); -void buttons_clear_onoff_long_click_event(void); uint8_t buttons_get_up_down_click_event(void); -void buttons_clear_up_down_click_event(void); uint8_t buttons_get_onoff_up_click_event(void); -void buttons_clear_onoff_up_click_event(void); uint8_t buttons_get_onoff_down_click_event(void); -void buttons_clear_onoff_down_click_event(void); void buttons_clock(void); buttons_events_t buttons_get_events(void); diff --git a/src/display/KT-LCD3/lcd.c b/src/display/KT-LCD3/lcd.c index 95cfd2b8..f9142e7f 100644 --- a/src/display/KT-LCD3/lcd.c +++ b/src/display/KT-LCD3/lcd.c @@ -77,6 +77,7 @@ static struct_motor_controller_data motor_controller_data; static struct_configuration_variables configuration_variables; +// global system variables static uint16_t ui16_battery_voltage_filtered_x10; static uint16_t ui16_battery_current_filtered_x5; static uint16_t ui16_battery_power_filtered_x50; @@ -89,11 +90,13 @@ static uint16_t ui16_battery_voltage_soc_x10; static uint16_t ui16_pedal_torque_filtered; static uint16_t ui16_pedal_power_filtered; static uint8_t ui8_pedal_cadence_filtered; - static uint8_t ui8_lights_state = 0; static uint8_t ui8_street_mode_enabled = 0; +static volatile uint16_t ui16_timer3_counter = 0; + -static uint8_t ui8_lcd_menu = 0; +// menu variables +static uint8_t ui8_lcd_menu = MAIN_MENU; static uint8_t ui8_lcd_menu_config_submenu_state = 0; static uint8_t ui8_lcd_menu_flash_state; static uint8_t ui8_lcd_menu_flash_state_temperature; @@ -101,23 +104,11 @@ static uint8_t ui8_lcd_menu_config_submenu_number = 0; static uint8_t ui8_lcd_menu_config_submenu_active = 0; static uint8_t ui8_lcd_menu_config_submenu_change_variable_enabled = 0; static uint8_t ui8_odometer_sub_field_state; - uint8_t ui8_start_odometer_show_field_number = 0; uint8_t ui8_odometer_show_field_number_counter_0 = 0; uint8_t ui8_odometer_show_field_number_counter_1 = 1; uint8_t ui8_odometer_show_field_number_state = 0; uint8_t ui8_odometer_show_field_number = 0; -uint16_t ui16_odometer_reset_distance_counter = 0; -uint8_t ui8_odometer_reset_distance_counter_state = 1; - -static uint8_t ui8_lcd_menu_counter_100ms = 0; -static uint8_t ui8_lcd_menu_counter_100ms_state = 0; -static uint8_t ui8_lcd_menu_counter_500ms = 0; -static uint8_t ui8_lcd_menu_counter_500ms_state = 0; - -static uint8_t ui8_long_click_started = 0; -static uint8_t ui8_long_click_counter = 0; -static volatile uint16_t ui16_timer3_counter = 0; // time measurement variables @@ -187,7 +178,7 @@ void lcd_set_backlight_intensity (uint8_t ui8_intensity); void lcd_print (uint32_t ui32_number, uint8_t ui8_lcd_field, uint8_t ui8_options); void lcd_configurations_print_number(var_number_t* p_lcd_var_number); void power(void); -void power_off_management (void); +void power_off_timer (void); // LCD symbol functions @@ -267,53 +258,37 @@ void lcd_clock (void) // clear the screen lcd_clear (); - // update LCD when the first communication package is received from the motor controller + // return here until the first communication package is received from the motor controller if (uart_received_first_package () == 0) { return; } - - update_menu_flashing_state (); - - // enter configuration menu if... - if (buttons_get_up_down_click_event () && ui8_lcd_menu == 0) - { - buttons_clear_all_events (); - - ui8_lcd_menu = 1; - } - - // enter quick configure power menu if... - if (buttons_get_onoff_up_click_event () && configuration_variables.ui8_main_screen_power_menu_enabled && !ui8_street_mode_enabled && ui8_lcd_menu == 0) - { - buttons_clear_all_events (); - - ui8_lcd_menu = 2; - } + // LCD menus switch (ui8_lcd_menu) { - case 0: + case MAIN_MENU: lcd_execute_main_screen (); break; - case 1: - lcd_execute_menu_config (); + case POWER_MENU: + lcd_execute_menu_config_power (); break; - case 2: - lcd_execute_menu_config_power (); + case CONFIGURATION_MENU: + lcd_execute_menu_config (); break; default: - ui8_lcd_menu = 0; + ui8_lcd_menu = MAIN_MENU; break; } - + + update_menu_flashing_state (); low_pass_filter_battery_voltage_current_power (); low_pass_filter_pedal_cadence (); low_pass_filter_pedal_torque_and_power (); calc_battery_soc (); calc_distance (); lcd_update (); - power_off_management (); + power_off_timer (); } @@ -331,6 +306,24 @@ void lcd_execute_main_screen (void) time_measurement (); energy_data (); assist_level_state (); + + // enter configuration menu + if (buttons_get_up_down_click_event ()) + { + ui8_lcd_menu = CONFIGURATION_MENU; + } + + // enter power menu + if (buttons_get_onoff_up_click_event () && configuration_variables.ui8_main_screen_power_menu_enabled && !ui8_street_mode_enabled) + { + ui8_lcd_menu = POWER_MENU; + } + + // power off + if (buttons_get_onoff_long_click_event ()) + { + lcd_power_off (1); + } } @@ -387,46 +380,36 @@ void lcd_execute_menu_config (void) } else { - // advance on submenu if button_up_click_event + // advance on submenu if... if (buttons_get_up_click_event ()) { - // clear button event - buttons_clear_up_click_event (); - if (ui8_lcd_menu_config_submenu_number < 9) { ++ui8_lcd_menu_config_submenu_number; } else { ui8_lcd_menu_config_submenu_number = 0; } } - // recede on submenu if button_down_click_event + // recede on submenu if... if (buttons_get_down_click_event ()) { - // clear button event - buttons_clear_down_click_event (); - if (ui8_lcd_menu_config_submenu_number > 0) { --ui8_lcd_menu_config_submenu_number; } else { ui8_lcd_menu_config_submenu_number = 9; } } - // enter submenu if onoff click event + // enter submenu if... if (buttons_get_onoff_click_event ()) { - buttons_clear_onoff_click_event (); - ui8_lcd_menu_config_submenu_active = 1; ui8_config_wh_x10_offset = 1; } - // leave config menu if button_onoff_long_click + // leave config menu if... if (buttons_get_onoff_long_click_event ()) { - buttons_clear_all_events (); - // save the updated variables on EEPROM eeprom_write_variables (); - // switch to main screen - ui8_lcd_menu = 0; + // switch to main menu + ui8_lcd_menu = MAIN_MENU; } // print submenu number only half of the time @@ -1129,7 +1112,7 @@ void lcd_execute_menu_config_main_screen_setup (void) lcd_configurations_print_number(&lcd_var_number); break; - // enable/disable main screen power menu + // enable/disable quick set power menu case 9: lcd_var_number.p_var_number = &configuration_variables.ui8_main_screen_power_menu_enabled; lcd_var_number.ui8_size = 8; @@ -1575,27 +1558,22 @@ void lcd_execute_menu_config_power (void) // leave config power menu with a long onoff click if (buttons_get_onoff_long_click_event ()) { - buttons_clear_all_events (); - // disable change of variables ui8_lcd_menu_config_submenu_change_variable_enabled = 0; // save the updated variables to EEPROM eeprom_write_variables (); - // change to main screen - ui8_lcd_menu = 0; + // change to main menu + ui8_lcd_menu = MAIN_MENU; } } -void power_off_management (void) +void power_off_timer (void) { static uint16_t ui16_seconds_since_power_on_offset; - // power off system if ONOFF long click - if (buttons_get_onoff_long_click_event ()) { lcd_power_off (1); } - // check if automatic power off management is configured to any value if (configuration_variables.ui8_lcd_power_off_time_minutes != 0) { @@ -1624,7 +1602,7 @@ void power_off_management (void) void temperature (void) { // if motor current is being limited due to temperature, force showing temperature!! - if (motor_controller_data.ui8_temperature_current_limiting_value != 255) + if (motor_controller_data.ui8_temperature_current_limiting_value < 255) { if (ui8_lcd_menu_flash_state_temperature) { @@ -1935,9 +1913,6 @@ void assist_level_state (void) // if UP button is clicked if (buttons_get_up_click_event ()) { - // clear button event - buttons_clear_up_click_event (); - // increment assist level and check if is out of bounds if (++configuration_variables.ui8_assist_level > configuration_variables.ui8_number_of_assist_levels) { @@ -1949,9 +1924,6 @@ void assist_level_state (void) // if DOWN button is clicked if (buttons_get_down_click_event ()) { - // clear button event - buttons_clear_down_click_event (); - // check if assist level is out of bounds if (configuration_variables.ui8_assist_level > 0) { @@ -1975,8 +1947,6 @@ void lights_state (void) { if (buttons_get_up_long_click_event ()) { - buttons_clear_up_long_click_event (); - // toggle light state and display backlight ui8_lights_state = !ui8_lights_state; motor_controller_data.ui8_lights = ui8_lights_state; @@ -1995,11 +1965,17 @@ void walk_assist_state (void) { static uint8_t ui8_walk_assist_activated; static uint8_t ui8_cruise_activated; + static uint8_t ui8_long_hold_down_button; #define WALK_ASSIST_CRUISE_THRESHOLD_SPEED_X10 80 // 8.0 km/h if (buttons_get_down_long_click_event ()) { - // if down button is still pressed + ui8_long_hold_down_button = 1; + } + + if (ui8_long_hold_down_button) + { + // if down button is pressed if (buttons_get_down_state ()) { // enable walk assist or cruise function depending on speed and if function is enabled, also check if the other function was activetad first @@ -2036,8 +2012,8 @@ void walk_assist_state (void) ui8_walk_assist_activated = 0; ui8_cruise_activated = 0; - // clear button long down click event - buttons_clear_down_long_click_event (); + // reset button event flag + ui8_long_hold_down_button = 0; } } else @@ -2074,8 +2050,6 @@ void street_mode (void) if (buttons_get_onoff_down_click_event ()) { - buttons_clear_all_events (); - ui8_street_mode_enabled = !ui8_street_mode_enabled; motor_controller_data.ui8_street_mode_enabled = ui8_street_mode_enabled; @@ -2124,11 +2098,12 @@ void odometer_start_show_field_number (void) uint8_t reset_variable_check (void) { + static uint8_t ui8_odometer_reset_distance_counter_state; + static uint16_t ui16_odometer_reset_distance_counter; + // if there is one down_click_long_click_event if (buttons_get_down_click_long_click_event()) { - buttons_clear_down_long_click_event(); - // start counting to reset variable ui8_odometer_reset_distance_counter_state = 1; @@ -2140,9 +2115,6 @@ uint8_t reset_variable_check (void) { if (buttons_get_down_state ()) { - // clear the possible button event (can maybe be removed) - buttons_clear_down_click_event(); - // count time, after limit, reset everything if (++ui16_odometer_reset_distance_counter > 300) { @@ -2176,9 +2148,6 @@ void odometer (void) // if user clicks onoff click event if (buttons_get_onoff_click_event ()) { - // clear button ON/OFF event - buttons_clear_onoff_click_event (); - // increment odometer field state odometer_increase_field_state (); @@ -3572,23 +3541,24 @@ void update_menu_flashing_state(void) static uint8_t ui8_lcd_menu_flash_counter; static uint16_t ui16_lcd_menu_flash_counter_temperature; - #define LCD_TEMPERATURE_FLASH_OFF_THRESHOLD 20 // 20 -> 200 ms + #define LCD_MENU_FLASH_OFF_THRESHOLD 20 + #define LCD_MENU_FLASH_ON_THRESHOLD 80 + #define LCD_TEMPERATURE_FLASH_OFF_THRESHOLD 10 // 10 -> 100 ms // flash on menus - if (ui8_lcd_menu_flash_counter == 0) + if (ui8_lcd_menu_flash_counter-- == 0) { if (ui8_lcd_menu_flash_state) { ui8_lcd_menu_flash_state = 0; - ui8_lcd_menu_flash_counter = 20; + ui8_lcd_menu_flash_counter = LCD_MENU_FLASH_OFF_THRESHOLD; } else { ui8_lcd_menu_flash_state = 1; - ui8_lcd_menu_flash_counter = 80; + ui8_lcd_menu_flash_counter = LCD_MENU_FLASH_ON_THRESHOLD; } } - ui8_lcd_menu_flash_counter--; // flash the temperature field when the current is being limited due to motor over temperature if (motor_controller_data.ui8_temperature_current_limiting_value < 255) // flash only if current is being limited, i.e. below 255 @@ -3620,17 +3590,9 @@ void submenu_state_controller (uint8_t ui8_state_max_number) { if (ui8_lcd_menu_config_submenu_change_variable_enabled) { - // clear onoff click event if it happened - if (buttons_get_onoff_click_event ()) - { - buttons_clear_onoff_click_event (); - } - // stop changing variables if long onoff click if (buttons_get_onoff_long_click_event ()) { - buttons_clear_onoff_long_click_event (); - ui8_lcd_menu_config_submenu_change_variable_enabled = 0; } } @@ -3639,37 +3601,27 @@ void submenu_state_controller (uint8_t ui8_state_max_number) // change variables if onoff click event if (buttons_get_onoff_click_event ()) { - buttons_clear_onoff_click_event (); - ui8_lcd_menu_config_submenu_change_variable_enabled = 1; } else { - // advance on submenus on button_up_click_event + // advance on submenus if (buttons_get_up_click_event ()) { - // clear button event - buttons_clear_up_click_event (); - if (ui8_lcd_menu_config_submenu_state < ui8_state_max_number) { ++ui8_lcd_menu_config_submenu_state; } else { ui8_lcd_menu_config_submenu_state = 0; } } - // recede on submenus on button_down_click_event + // recede on submenus if (buttons_get_down_click_event ()) { - // clear button event - buttons_clear_down_click_event (); - if (ui8_lcd_menu_config_submenu_state > 0) { --ui8_lcd_menu_config_submenu_state; } else { ui8_lcd_menu_config_submenu_state = ui8_state_max_number; } } - // leave config menu with a button_onoff_long_click + // leave config menu if (buttons_get_onoff_long_click_event ()) { - buttons_clear_onoff_long_click_event (); - ui8_lcd_menu_config_submenu_active = 0; ui8_lcd_menu_config_submenu_state = 0; @@ -3684,12 +3636,8 @@ void submenu_state_controller (uint8_t ui8_state_max_number) void advance_on_subfield (uint8_t* ui8_p_state, uint8_t ui8_state_max_number) { - // if click up - click long up event if (buttons_get_up_click_long_click_event ()) { - // clear button event - buttons_clear_up_click_long_click_event (); - if (*ui8_p_state < ui8_state_max_number) { ++*ui8_p_state; } else { *ui8_p_state = 0; } @@ -3723,6 +3671,9 @@ void lcd_power_off (uint8_t SaveToEEPROM) void lcd_configurations_print_number(var_number_t* p_lcd_var_number) { + static uint8_t ui8_long_click_started; + static uint8_t ui8_long_click_counter; + uint8_t *ui8_p_var = 0; uint16_t *ui16_p_var = 0; uint32_t *ui32_p_var = 0; @@ -3804,14 +3755,6 @@ void lcd_configurations_print_number(var_number_t* p_lcd_var_number) else { (*ui32_p_var) = p_lcd_var_number->ui32_min_value; } } } - - // clear button events - buttons_clear_up_click_event(); - buttons_clear_down_click_event(); - buttons_clear_up_click_long_click_event(); - buttons_clear_up_long_click_event(); - buttons_clear_down_click_long_click_event(); - buttons_clear_down_long_click_event(); } if(p_lcd_var_number->ui8_size == 8) diff --git a/src/display/KT-LCD3/lcd.h b/src/display/KT-LCD3/lcd.h index d438d765..13f7e061 100644 --- a/src/display/KT-LCD3/lcd.h +++ b/src/display/KT-LCD3/lcd.h @@ -116,20 +116,23 @@ typedef struct _configuration_variables uint8_t ui8_main_screen_power_menu_enabled; } struct_configuration_variables; +// menu definitions +#define MAIN_MENU 0 +#define POWER_MENU 1 +#define CONFIGURATION_MENU 2 -// LCD RAM has 32*8 bits -#define LCD_FRAME_BUFFER_SIZE 32 +#define LCD_FRAME_BUFFER_SIZE 32 // LCD RAM has 32*8 bits extern uint8_t ui8_lcd_frame_buffer[LCD_FRAME_BUFFER_SIZE]; -#define ASSIST_LEVEL_FIELD 0 -#define ODOMETER_FIELD 1 -#define TEMPERATURE_FIELD 2 -#define WHEEL_SPEED_FIELD 3 -#define BATTERY_POWER_FIELD 4 -#define TIME_SECOND_FIELD 5 -#define TIME_MINUTE_FIELD 6 +#define ASSIST_LEVEL_FIELD 0 +#define ODOMETER_FIELD 1 +#define TEMPERATURE_FIELD 2 +#define WHEEL_SPEED_FIELD 3 +#define BATTERY_POWER_FIELD 4 +#define TIME_SECOND_FIELD 5 +#define TIME_MINUTE_FIELD 6 // each digit needs 7 bits to be defined + 1 digit that can be another symbol like a "point" diff --git a/src/display/KT-LCD3/main.h b/src/display/KT-LCD3/main.h index dc4d717b..9ed09d1a 100644 --- a/src/display/KT-LCD3/main.h +++ b/src/display/KT-LCD3/main.h @@ -49,8 +49,8 @@ // default values for battery parameters #define DEFAULT_VALUE_BATTERY_MAX_CURRENT 16 // 16 amps -#define DEFAULT_VALUE_TARGET_MAX_BATTERY_POWER 0 // e.g. 20 = 20 * 25 = 500, 0 is disabled -#define DEFAULT_VALUE_BATTERY_CELLS_NUMBER 13 // 13 --> 48V +#define DEFAULT_VALUE_TARGET_MAX_BATTERY_POWER 10 // 10 -> 10 * 25 = 250 watts +#define DEFAULT_VALUE_BATTERY_CELLS_NUMBER 13 // 13 -> 48 V #define DEFAULT_VALUE_BATTERY_LOW_VOLTAGE_CUT_OFF_X10_0 134 // 48 V battery, LVC = 39.0 (3.0 * 13): (134 + (1 << 8)) #define DEFAULT_VALUE_BATTERY_LOW_VOLTAGE_CUT_OFF_X10_1 1 #define DEFAULT_VALUE_CONFIG_0 0 // motor type, assistance without pedal rotation, temperature limit enabled, temperature field state From aaf9b1a8afc9b59f55a30f166a04fd20fcf36963 Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 7 Jun 2019 22:34:51 +0200 Subject: [PATCH 4/5] Better button code --- src/display/KT-LCD3/buttons.c | 226 +++++++++++++++------------------- src/display/KT-LCD3/buttons.h | 58 +++------ src/display/KT-LCD3/lcd.c | 128 ++++++++----------- 3 files changed, 173 insertions(+), 239 deletions(-) diff --git a/src/display/KT-LCD3/buttons.c b/src/display/KT-LCD3/buttons.c index 10cd8869..28467e03 100644 --- a/src/display/KT-LCD3/buttons.c +++ b/src/display/KT-LCD3/buttons.c @@ -20,34 +20,28 @@ uint8_t ui8_down_button_state_counter = 0; uint8_t ui8_up_button_state = 0; uint8_t ui8_up_button_state_counter = 0; -#define BUTTON_LONG_CLICK_THRESHOLD 100 // 100 -> 1.0 seconds -#define BUTTON_CLICK_THRESHOLD 30 // 30 -> 0.3 seconds +uint8_t ONOFF_CLICK = 0; +uint8_t ONOFF_LONG_CLICK = 0; +uint8_t ONOFF_CLICK_LONG_CLICK = 0; -buttons_events_t buttons_events = 0; +uint8_t UP_CLICK = 0; +uint8_t UP_LONG_CLICK = 0; +uint8_t UP_CLICK_LONG_CLICK = 0; -uint8_t buttons_get_up_state (void) -{ - return GPIO_ReadInputPin(LCD3_BUTTON_UP__PORT, LCD3_BUTTON_UP__PIN) != 0 ? 0: 1; -} +uint8_t DOWN_CLICK = 0; +uint8_t DOWN_LONG_CLICK = 0; +uint8_t DOWN_CLICK_LONG_CLICK = 0; -uint8_t buttons_get_up_click_event (void) -{ - return (buttons_events & UP_CLICK) ? 1: 0; -} +uint8_t UP_DOWN_LONG_CLICK = 0; +uint8_t ONOFF_UP_LONG_CLICK = 0; +uint8_t ONOFF_DOWN_LONG_CLICK = 0; -uint8_t buttons_get_up_long_click_event (void) -{ - return (buttons_events & UP_LONG_CLICK) ? 1: 0; -} +#define LONG_CLICK_THRESHOLD 100 // 100 -> 1.0 seconds +#define CLICK_THRESHOLD 30 // 30 -> 0.3 seconds -uint8_t buttons_get_up_click_long_click_event(void) -{ - return (buttons_events & UP_CLICK_LONG_CLICK) ? 1: 0; -} - -void buttons_clear_up_long_click_event (void) +uint8_t buttons_get_up_state (void) { - buttons_events &= ~UP_LONG_CLICK; + return GPIO_ReadInputPin(LCD3_BUTTON_UP__PORT, LCD3_BUTTON_UP__PIN) != 0 ? 0: 1; } uint8_t buttons_get_down_state (void) @@ -55,80 +49,55 @@ uint8_t buttons_get_down_state (void) return GPIO_ReadInputPin(LCD3_BUTTON_DOWN__PORT, LCD3_BUTTON_DOWN__PIN) != 0 ? 0: 1; } -uint8_t buttons_get_down_click_event (void) -{ - return (buttons_events & DOWN_CLICK) ? 1: 0; -} - -uint8_t buttons_get_down_long_click_event (void) -{ - return (buttons_events & DOWN_LONG_CLICK) ? 1: 0; -} - -uint8_t buttons_get_down_click_long_click_event (void) -{ - return (buttons_events & DOWN_CLICK_LONG_CLICK) ? 1: 0; -} - uint8_t buttons_get_onoff_state (void) { return GPIO_ReadInputPin(LCD3_BUTTON_ONOFF__PORT, LCD3_BUTTON_ONOFF__PIN) != 0 ? 1: 0; } -uint8_t buttons_get_onoff_click_event (void) -{ - return (buttons_events & ONOFF_CLICK) ? 1: 0; -} - -uint8_t buttons_get_onoff_long_click_event (void) +uint8_t buttons_get_events (void) { - return (buttons_events & ONOFF_LONG_CLICK) ? 1: 0; -} - -uint8_t buttons_get_onoff_click_long_click_event (void) -{ - return (buttons_events & ONOFF_CLICK_LONG_CLICK) ? 1: 0; -} - -void buttons_clear_onoff_click_long_click_event (void) -{ - buttons_events &= ~ONOFF_CLICK_LONG_CLICK; -} - -uint8_t buttons_get_up_down_click_event (void) -{ - return (buttons_events & UPDOWN_CLICK) ? 1: 0; -} - -uint8_t buttons_get_onoff_up_click_event (void) -{ - return (buttons_events & ONOFFUP_CLICK) ? 1: 0; -} - -uint8_t buttons_get_onoff_down_click_event (void) -{ - return (buttons_events & ONOFFUP_CLICK) ? 1: 0; -} - -buttons_events_t buttons_get_events (void) -{ - return buttons_events; -} - -void buttons_set_events (buttons_events_t events) -{ - buttons_events |= events; -} - -void buttons_clear_all_events (void) -{ - buttons_events = 0; + if (ONOFF_CLICK || + ONOFF_LONG_CLICK || + ONOFF_CLICK_LONG_CLICK || + UP_CLICK || + UP_LONG_CLICK || + UP_CLICK_LONG_CLICK || + DOWN_CLICK || + DOWN_LONG_CLICK || + DOWN_CLICK_LONG_CLICK || + UP_DOWN_LONG_CLICK || + ONOFF_UP_LONG_CLICK || + ONOFF_DOWN_LONG_CLICK) + { + return 1; + } + else + { + return 0; + } } void buttons_clock (void) { - buttons_clear_all_events(); + // clear all button events + ONOFF_CLICK = 0; + ONOFF_LONG_CLICK = 0; + ONOFF_CLICK_LONG_CLICK = 0; + + UP_CLICK = 0; + UP_LONG_CLICK = 0; + UP_CLICK_LONG_CLICK = 0; + + DOWN_CLICK = 0; + DOWN_LONG_CLICK = 0; + DOWN_CLICK_LONG_CLICK = 0; + + UP_DOWN_LONG_CLICK = 0; + ONOFF_UP_LONG_CLICK = 0; + ONOFF_DOWN_LONG_CLICK = 0; + + // onoff button switch (ui8_onoff_button_state) { case 0: @@ -146,22 +115,21 @@ void buttons_clock (void) ui8_onoff_button_state_counter++; // long click - if (ui8_onoff_button_state_counter > BUTTON_LONG_CLICK_THRESHOLD) + if (ui8_onoff_button_state_counter > LONG_CLICK_THRESHOLD) { - // onoff and up or down button click if (ui8_up_button_state == 1) { - buttons_set_events(ONOFFUP_CLICK); + ONOFF_UP_LONG_CLICK = 1; ui8_up_button_state = 2; } else if (ui8_down_button_state == 1) { - buttons_set_events(ONOFFDOWN_CLICK); + ONOFF_DOWN_LONG_CLICK = 1; ui8_down_button_state = 2; } else { - buttons_set_events(ONOFF_LONG_CLICK); + ONOFF_LONG_CLICK = 1; } ui8_onoff_button_state = 2; @@ -169,11 +137,11 @@ void buttons_clock (void) break; } - // button release + // button release before long click if (!buttons_get_onoff_state ()) { // check for click + long click - if (ui8_onoff_button_state_counter < BUTTON_CLICK_THRESHOLD) + if (ui8_onoff_button_state_counter < CLICK_THRESHOLD) { ui8_onoff_button_state_counter = 0; ui8_onoff_button_state = 3; @@ -182,7 +150,7 @@ void buttons_clock (void) } else { - buttons_set_events(ONOFF_CLICK); + ONOFF_CLICK = 1; ui8_onoff_button_state = 0; break; @@ -217,9 +185,9 @@ void buttons_clock (void) } // event click - if (ui8_onoff_button_state_counter > BUTTON_CLICK_THRESHOLD) + if (ui8_onoff_button_state_counter > CLICK_THRESHOLD) { - buttons_set_events(ONOFF_CLICK); + ONOFF_CLICK = 1; ui8_onoff_button_state = 0; break; @@ -232,9 +200,9 @@ void buttons_clock (void) ui8_onoff_button_state_counter++; // event click, but this time it is: click + long click - if (ui8_onoff_button_state_counter > BUTTON_LONG_CLICK_THRESHOLD) + if (ui8_onoff_button_state_counter > LONG_CLICK_THRESHOLD) { - buttons_set_events(ONOFF_CLICK_LONG_CLICK); + ONOFF_CLICK_LONG_CLICK = 1; ui8_onoff_button_state = 2; break; @@ -243,7 +211,7 @@ void buttons_clock (void) // button release if (!buttons_get_onoff_state ()) { - buttons_set_events(ONOFF_CLICK); + ONOFF_CLICK = 1; ui8_onoff_button_state = 0; break; @@ -257,7 +225,8 @@ void buttons_clock (void) break; } - + + // up button switch (ui8_up_button_state) { case 0: @@ -275,17 +244,21 @@ void buttons_clock (void) ui8_up_button_state_counter++; // long click - if (ui8_up_button_state_counter > BUTTON_LONG_CLICK_THRESHOLD) + if (ui8_up_button_state_counter > LONG_CLICK_THRESHOLD) { - // up and down button click if (ui8_down_button_state == 1) { - buttons_set_events(UPDOWN_CLICK); + UP_DOWN_LONG_CLICK = 1; ui8_down_button_state = 2; } + else if (ui8_onoff_button_state == 1) + { + ONOFF_UP_LONG_CLICK = 1; + ui8_onoff_button_state = 2; + } else { - buttons_set_events(UP_LONG_CLICK); + UP_LONG_CLICK = 1; } ui8_up_button_state = 2; @@ -293,11 +266,11 @@ void buttons_clock (void) break; } - // button release + // button release before long click if (!buttons_get_up_state ()) { // check for click + long click - if (ui8_up_button_state_counter <= BUTTON_CLICK_THRESHOLD) + if (ui8_up_button_state_counter <= CLICK_THRESHOLD) { ui8_up_button_state_counter = 0; ui8_up_button_state = 3; @@ -306,7 +279,7 @@ void buttons_clock (void) } else { - buttons_set_events(UP_CLICK); + UP_CLICK = 1; ui8_up_button_state = 0; break; @@ -341,9 +314,9 @@ void buttons_clock (void) } // event click - if (ui8_up_button_state_counter > BUTTON_CLICK_THRESHOLD) + if (ui8_up_button_state_counter > CLICK_THRESHOLD) { - buttons_set_events(UP_CLICK); + UP_CLICK = 1; ui8_up_button_state = 0; break; @@ -356,9 +329,9 @@ void buttons_clock (void) ui8_up_button_state_counter++; // event click, but this time it is: click + long click - if (ui8_up_button_state_counter > BUTTON_LONG_CLICK_THRESHOLD) + if (ui8_up_button_state_counter > LONG_CLICK_THRESHOLD) { - buttons_set_events(UP_CLICK_LONG_CLICK); + UP_CLICK_LONG_CLICK = 1; ui8_up_button_state = 2; break; @@ -367,7 +340,7 @@ void buttons_clock (void) // button release if (!buttons_get_up_state ()) { - buttons_set_events(UP_CLICK); + UP_CLICK = 1; ui8_up_button_state = 0; break; @@ -381,7 +354,8 @@ void buttons_clock (void) break; } - + + // down button switch (ui8_down_button_state) { case 0: @@ -399,17 +373,21 @@ void buttons_clock (void) ui8_down_button_state_counter++; // long click - if (ui8_down_button_state_counter > BUTTON_LONG_CLICK_THRESHOLD) + if (ui8_down_button_state_counter > LONG_CLICK_THRESHOLD) { - // up and down long click if (ui8_up_button_state == 1) { - buttons_set_events(UPDOWN_CLICK); + UP_DOWN_LONG_CLICK = 1; ui8_up_button_state = 2; } + else if (ui8_onoff_button_state == 1) + { + ONOFF_DOWN_LONG_CLICK = 1; + ui8_onoff_button_state = 2; + } else { - buttons_set_events(DOWN_LONG_CLICK); + DOWN_LONG_CLICK = 1; } ui8_down_button_state = 2; @@ -418,11 +396,11 @@ void buttons_clock (void) } - // button release + // button release before long click if (!buttons_get_down_state ()) { // check for click + long click - if (ui8_down_button_state_counter < BUTTON_CLICK_THRESHOLD) + if (ui8_down_button_state_counter < CLICK_THRESHOLD) { ui8_down_button_state_counter = 0; ui8_down_button_state = 3; @@ -431,7 +409,7 @@ void buttons_clock (void) } else { - buttons_set_events(DOWN_CLICK); + DOWN_CLICK = 1; ui8_down_button_state = 0; break; @@ -466,9 +444,9 @@ void buttons_clock (void) } // event click - if (ui8_down_button_state_counter > BUTTON_CLICK_THRESHOLD) + if (ui8_down_button_state_counter > CLICK_THRESHOLD) { - buttons_set_events(DOWN_CLICK); + DOWN_CLICK = 1; ui8_down_button_state = 0; break; @@ -481,9 +459,9 @@ void buttons_clock (void) ui8_down_button_state_counter++; // event click, but this time it is: click + long click - if (ui8_down_button_state_counter > BUTTON_LONG_CLICK_THRESHOLD) + if (ui8_down_button_state_counter > LONG_CLICK_THRESHOLD) { - buttons_set_events(DOWN_CLICK_LONG_CLICK); + DOWN_CLICK_LONG_CLICK = 1; ui8_down_button_state = 2; break; @@ -492,7 +470,7 @@ void buttons_clock (void) // button release if (!buttons_get_down_state ()) { - buttons_set_events(DOWN_CLICK); + DOWN_CLICK = 1; ui8_down_button_state = 0; break; diff --git a/src/display/KT-LCD3/buttons.h b/src/display/KT-LCD3/buttons.h index c21e64ab..413aae84 100644 --- a/src/display/KT-LCD3/buttons.h +++ b/src/display/KT-LCD3/buttons.h @@ -12,44 +12,26 @@ #include "main.h" #include "stm8s_gpio.h" -typedef enum -{ - ONOFF_CLICK = 1, - ONOFF_CLICK_LONG_CLICK = 2, - ONOFF_LONG_CLICK = 4, - UP_CLICK = 8, - UP_CLICK_LONG_CLICK = 16, - UP_LONG_CLICK = 32, - DOWN_CLICK = 64, - DOWN_CLICK_LONG_CLICK = 128, - DOWN_LONG_CLICK = 256, - UPDOWN_CLICK = 512, - ONOFFUP_CLICK = 1024, - ONOFFDOWN_CLICK = 2048 -} buttons_events_t; - -uint8_t buttons_get_up_state(void); -uint8_t buttons_get_up_click_event(void); -uint8_t buttons_get_up_click_long_click_event(void); -uint8_t buttons_get_up_long_click_event(void); - -uint8_t buttons_get_down_state(void); -uint8_t buttons_get_down_click_event(void); -uint8_t buttons_get_down_click_long_click_event(void); -uint8_t buttons_get_down_long_click_event(void); - -uint8_t buttons_get_onoff_state(void); -uint8_t buttons_get_onoff_click_event(void); -uint8_t buttons_get_onoff_click_long_click_event(void); -uint8_t buttons_get_onoff_long_click_event(void); - -uint8_t buttons_get_up_down_click_event(void); -uint8_t buttons_get_onoff_up_click_event(void); -uint8_t buttons_get_onoff_down_click_event(void); - +extern uint8_t ONOFF_CLICK; +extern uint8_t ONOFF_LONG_CLICK; +extern uint8_t ONOFF_CLICK_LONG_CLICK; + +extern uint8_t UP_CLICK; +extern uint8_t UP_LONG_CLICK; +extern uint8_t UP_CLICK_LONG_CLICK; + +extern uint8_t DOWN_CLICK; +extern uint8_t DOWN_LONG_CLICK; +extern uint8_t DOWN_CLICK_LONG_CLICK; + +extern uint8_t UP_DOWN_LONG_CLICK; +extern uint8_t ONOFF_UP_LONG_CLICK; +extern uint8_t ONOFF_DOWN_LONG_CLICK; + +uint8_t buttons_get_up_state (void); +uint8_t buttons_get_down_state (void); +uint8_t buttons_get_onoff_state (void); +uint8_t buttons_get_events (void); void buttons_clock(void); -buttons_events_t buttons_get_events(void); -void buttons_clear_all_events(void); -void buttons_set_events(buttons_events_t events); #endif /* _BUTTON_H_ */ diff --git a/src/display/KT-LCD3/lcd.c b/src/display/KT-LCD3/lcd.c index f9142e7f..acfd1dfc 100644 --- a/src/display/KT-LCD3/lcd.c +++ b/src/display/KT-LCD3/lcd.c @@ -105,9 +105,7 @@ static uint8_t ui8_lcd_menu_config_submenu_active = 0; static uint8_t ui8_lcd_menu_config_submenu_change_variable_enabled = 0; static uint8_t ui8_odometer_sub_field_state; uint8_t ui8_start_odometer_show_field_number = 0; -uint8_t ui8_odometer_show_field_number_counter_0 = 0; -uint8_t ui8_odometer_show_field_number_counter_1 = 1; -uint8_t ui8_odometer_show_field_number_state = 0; +uint8_t ui8_odometer_show_field_number_counter = 1; uint8_t ui8_odometer_show_field_number = 0; @@ -307,20 +305,20 @@ void lcd_execute_main_screen (void) energy_data (); assist_level_state (); - // enter configuration menu - if (buttons_get_up_down_click_event ()) + // enter configuration menu if... + if (UP_DOWN_LONG_CLICK) { ui8_lcd_menu = CONFIGURATION_MENU; } - // enter power menu - if (buttons_get_onoff_up_click_event () && configuration_variables.ui8_main_screen_power_menu_enabled && !ui8_street_mode_enabled) + // enter power menu if... + if (ONOFF_UP_LONG_CLICK && configuration_variables.ui8_main_screen_power_menu_enabled && !ui8_street_mode_enabled) { ui8_lcd_menu = POWER_MENU; } - // power off - if (buttons_get_onoff_long_click_event ()) + // power off if... + if (ONOFF_LONG_CLICK) { lcd_power_off (1); } @@ -381,21 +379,21 @@ void lcd_execute_menu_config (void) else { // advance on submenu if... - if (buttons_get_up_click_event ()) + if (UP_CLICK) { if (ui8_lcd_menu_config_submenu_number < 9) { ++ui8_lcd_menu_config_submenu_number; } else { ui8_lcd_menu_config_submenu_number = 0; } } // recede on submenu if... - if (buttons_get_down_click_event ()) + if (DOWN_CLICK) { if (ui8_lcd_menu_config_submenu_number > 0) { --ui8_lcd_menu_config_submenu_number; } else { ui8_lcd_menu_config_submenu_number = 9; } } // enter submenu if... - if (buttons_get_onoff_click_event ()) + if (ONOFF_CLICK) { ui8_lcd_menu_config_submenu_active = 1; @@ -403,7 +401,7 @@ void lcd_execute_menu_config (void) } // leave config menu if... - if (buttons_get_onoff_long_click_event ()) + if (ONOFF_LONG_CLICK) { // save the updated variables on EEPROM eeprom_write_variables (); @@ -1556,7 +1554,7 @@ void lcd_execute_menu_config_power (void) lcd_enable_motor_symbol (1); // leave config power menu with a long onoff click - if (buttons_get_onoff_long_click_event ()) + if (ONOFF_LONG_CLICK) { // disable change of variables ui8_lcd_menu_config_submenu_change_variable_enabled = 0; @@ -1911,7 +1909,7 @@ void power(void) void assist_level_state (void) { // if UP button is clicked - if (buttons_get_up_click_event ()) + if (UP_CLICK) { // increment assist level and check if is out of bounds if (++configuration_variables.ui8_assist_level > configuration_variables.ui8_number_of_assist_levels) @@ -1922,7 +1920,7 @@ void assist_level_state (void) } // if DOWN button is clicked - if (buttons_get_down_click_event ()) + if (DOWN_CLICK) { // check if assist level is out of bounds if (configuration_variables.ui8_assist_level > 0) @@ -1945,7 +1943,7 @@ void assist_level_state (void) void lights_state (void) { - if (buttons_get_up_long_click_event ()) + if (UP_LONG_CLICK) { // toggle light state and display backlight ui8_lights_state = !ui8_lights_state; @@ -1968,7 +1966,7 @@ void walk_assist_state (void) static uint8_t ui8_long_hold_down_button; #define WALK_ASSIST_CRUISE_THRESHOLD_SPEED_X10 80 // 8.0 km/h - if (buttons_get_down_long_click_event ()) + if (DOWN_LONG_CLICK) { ui8_long_hold_down_button = 1; } @@ -2048,7 +2046,7 @@ void street_mode (void) } } - if (buttons_get_onoff_down_click_event ()) + if (ONOFF_DOWN_LONG_CLICK) { ui8_street_mode_enabled = !ui8_street_mode_enabled; @@ -2057,7 +2055,7 @@ void street_mode (void) if (ui8_street_mode_enabled) { - if (++ui8_street_mode_assist_symbol_state_counter > 40) + if (++ui8_street_mode_assist_symbol_state_counter > 45) { ui8_street_mode_assist_symbol_state_counter = 0; @@ -2090,9 +2088,7 @@ void odometer_increase_field_state (void) void odometer_start_show_field_number (void) { ui8_start_odometer_show_field_number = 1; - ui8_odometer_show_field_number_counter_0 = 0; - ui8_odometer_show_field_number_counter_1 = 0; - ui8_odometer_show_field_number_state = 1; + ui8_odometer_show_field_number_counter = 0; } @@ -2101,8 +2097,7 @@ uint8_t reset_variable_check (void) static uint8_t ui8_odometer_reset_distance_counter_state; static uint16_t ui16_odometer_reset_distance_counter; - // if there is one down_click_long_click_event - if (buttons_get_down_click_long_click_event()) + if (DOWN_CLICK_LONG_CLICK) { // start counting to reset variable ui8_odometer_reset_distance_counter_state = 1; @@ -2146,7 +2141,7 @@ uint8_t reset_variable_check (void) void odometer (void) { // if user clicks onoff click event - if (buttons_get_onoff_click_event ()) + if (ONOFF_CLICK) { // increment odometer field state odometer_increase_field_state (); @@ -2180,7 +2175,7 @@ void odometer (void) } // advance sub field on click-up-click-long-up button event - advance_on_subfield (&configuration_variables.ui8_odometer_sub_field_state_0, 3); + advance_on_subfield (&configuration_variables.ui8_odometer_sub_field_state_0, 2); // set for flashing of sub field state number ui8_odometer_sub_field_state = configuration_variables.ui8_odometer_sub_field_state_0; @@ -2292,7 +2287,7 @@ void odometer (void) } // advance sub field on click-up-click-long-up button event - advance_on_subfield (&configuration_variables.ui8_odometer_sub_field_state_1, 2); + advance_on_subfield (&configuration_variables.ui8_odometer_sub_field_state_1, 1); // set for flashing of sub field state number ui8_odometer_sub_field_state = configuration_variables.ui8_odometer_sub_field_state_1; @@ -2325,7 +2320,7 @@ void odometer (void) } // advance sub field on click-up-click-long-up button event - advance_on_subfield (&configuration_variables.ui8_odometer_sub_field_state_2, 2); + advance_on_subfield (&configuration_variables.ui8_odometer_sub_field_state_2, 1); // set for flashing of sub field state number ui8_odometer_sub_field_state = configuration_variables.ui8_odometer_sub_field_state_2; @@ -2359,7 +2354,7 @@ void odometer (void) } // advance sub field on click-up-click-long-up button event - advance_on_subfield (&configuration_variables.ui8_odometer_sub_field_state_3, 3); + advance_on_subfield (&configuration_variables.ui8_odometer_sub_field_state_3, 2); // set for flashing of sub field state number ui8_odometer_sub_field_state = configuration_variables.ui8_odometer_sub_field_state_3; @@ -2397,7 +2392,7 @@ void odometer (void) } // advance sub field on click-up-click-long-up button event - advance_on_subfield (&configuration_variables.ui8_odometer_sub_field_state_4, 2); + advance_on_subfield (&configuration_variables.ui8_odometer_sub_field_state_4, 1); // set for flashing of sub field state number ui8_odometer_sub_field_state = configuration_variables.ui8_odometer_sub_field_state_4; @@ -2463,7 +2458,7 @@ void odometer (void) } // advance sub field on click-up-click-long-up button event - advance_on_subfield (&configuration_variables.ui8_odometer_sub_field_state_5, 2); + advance_on_subfield (&configuration_variables.ui8_odometer_sub_field_state_5, 1); // set for flashing of sub field state number ui8_odometer_sub_field_state = configuration_variables.ui8_odometer_sub_field_state_5; @@ -2538,7 +2533,7 @@ void odometer (void) } // advance sub field on click-up-click-long-up button event - advance_on_subfield (&configuration_variables.ui8_odometer_sub_field_state_6, 3); + advance_on_subfield (&configuration_variables.ui8_odometer_sub_field_state_6, 2); // set for flashing of sub field state number ui8_odometer_sub_field_state = configuration_variables.ui8_odometer_sub_field_state_6; @@ -2671,31 +2666,18 @@ void odometer (void) if (ui8_start_odometer_show_field_number) { - // for flashing - if (++ui8_odometer_show_field_number_counter_0 >= 40) + // limit field number flashing after a certain time + if (++ui8_odometer_show_field_number_counter > 200) // 200 -> 2.0 seconds { - ui8_odometer_show_field_number_counter_0 = 0; - - // time limit for flashing - if (++ui8_odometer_show_field_number_counter_1 >= 5) - { - ui8_odometer_show_field_number_counter_0 = 0; - ui8_odometer_show_field_number_counter_1 = 0; - ui8_odometer_show_field_number_state = 1; - ui8_start_odometer_show_field_number = 0; // this disables the flashing - } - - if (ui8_odometer_show_field_number_state) - { - ui8_odometer_show_field_number_state = 0; - } - else - { - ui8_odometer_show_field_number_state = 1; - } + // reset counter + ui8_odometer_show_field_number_counter = 0; + + // disable the flashing + ui8_start_odometer_show_field_number = 0; } + - if (ui8_odometer_show_field_number_state) + if (ui8_lcd_menu_flash_state) { ui8_odometer_show_field_number = ((configuration_variables.ui8_odometer_field_state + 1) * 10); // add units for show (x10) ui8_odometer_show_field_number += ui8_odometer_sub_field_state; @@ -3541,23 +3523,15 @@ void update_menu_flashing_state(void) static uint8_t ui8_lcd_menu_flash_counter; static uint16_t ui16_lcd_menu_flash_counter_temperature; - #define LCD_MENU_FLASH_OFF_THRESHOLD 20 - #define LCD_MENU_FLASH_ON_THRESHOLD 80 + #define LCD_MENU_FLASH_THRESHOLD 25 #define LCD_TEMPERATURE_FLASH_OFF_THRESHOLD 10 // 10 -> 100 ms // flash on menus - if (ui8_lcd_menu_flash_counter-- == 0) + if (++ui8_lcd_menu_flash_counter > LCD_MENU_FLASH_THRESHOLD) { - if (ui8_lcd_menu_flash_state) - { - ui8_lcd_menu_flash_state = 0; - ui8_lcd_menu_flash_counter = LCD_MENU_FLASH_OFF_THRESHOLD; - } - else - { - ui8_lcd_menu_flash_state = 1; - ui8_lcd_menu_flash_counter = LCD_MENU_FLASH_ON_THRESHOLD; - } + ui8_lcd_menu_flash_counter = 0; + + ui8_lcd_menu_flash_state = !ui8_lcd_menu_flash_state; } // flash the temperature field when the current is being limited due to motor over temperature @@ -3591,7 +3565,7 @@ void submenu_state_controller (uint8_t ui8_state_max_number) if (ui8_lcd_menu_config_submenu_change_variable_enabled) { // stop changing variables if long onoff click - if (buttons_get_onoff_long_click_event ()) + if (ONOFF_LONG_CLICK) { ui8_lcd_menu_config_submenu_change_variable_enabled = 0; } @@ -3599,28 +3573,28 @@ void submenu_state_controller (uint8_t ui8_state_max_number) else { // change variables if onoff click event - if (buttons_get_onoff_click_event ()) + if (ONOFF_CLICK) { ui8_lcd_menu_config_submenu_change_variable_enabled = 1; } else { // advance on submenus - if (buttons_get_up_click_event ()) + if (UP_CLICK) { if (ui8_lcd_menu_config_submenu_state < ui8_state_max_number) { ++ui8_lcd_menu_config_submenu_state; } else { ui8_lcd_menu_config_submenu_state = 0; } } // recede on submenus - if (buttons_get_down_click_event ()) + if (DOWN_CLICK) { if (ui8_lcd_menu_config_submenu_state > 0) { --ui8_lcd_menu_config_submenu_state; } else { ui8_lcd_menu_config_submenu_state = ui8_state_max_number; } } // leave config menu - if (buttons_get_onoff_long_click_event ()) + if (ONOFF_LONG_CLICK) { ui8_lcd_menu_config_submenu_active = 0; ui8_lcd_menu_config_submenu_state = 0; @@ -3636,7 +3610,7 @@ void submenu_state_controller (uint8_t ui8_state_max_number) void advance_on_subfield (uint8_t* ui8_p_state, uint8_t ui8_state_max_number) { - if (buttons_get_up_click_long_click_event ()) + if (UP_CLICK_LONG_CLICK) { if (*ui8_p_state < ui8_state_max_number) { ++*ui8_p_state; } else { *ui8_p_state = 0; } @@ -3696,7 +3670,7 @@ void lcd_configurations_print_number(var_number_t* p_lcd_var_number) if (ui8_lcd_menu_config_submenu_change_variable_enabled) { // if LONG CLICK, keep track of long click so variable is increased automatically 10x every second - if(buttons_get_up_long_click_event() || buttons_get_down_long_click_event()) + if (UP_LONG_CLICK || DOWN_LONG_CLICK) { ui8_long_click_started = 1; } @@ -3717,7 +3691,7 @@ void lcd_configurations_print_number(var_number_t* p_lcd_var_number) } // increase - if(buttons_get_up_click_event() || (buttons_get_up_state() && ui8_long_click_trigger)) + if (UP_CLICK || (buttons_get_up_state() && ui8_long_click_trigger)) { if(p_lcd_var_number->ui8_size == 8) { @@ -3737,7 +3711,7 @@ void lcd_configurations_print_number(var_number_t* p_lcd_var_number) } // decrease - if(buttons_get_down_click_event() || (buttons_get_down_state() && ui8_long_click_trigger)) + if (DOWN_CLICK || (buttons_get_down_state() && ui8_long_click_trigger)) { if(p_lcd_var_number->ui8_size == 8) { From f89ba3a2bdfb05d165590c82c6154e8ec4bf29ee Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 7 Jun 2019 23:39:59 +0200 Subject: [PATCH 5/5] New button code and minor clean up --- src/display/KT-LCD3/lcd.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/display/KT-LCD3/lcd.c b/src/display/KT-LCD3/lcd.c index acfd1dfc..25e39534 100644 --- a/src/display/KT-LCD3/lcd.c +++ b/src/display/KT-LCD3/lcd.c @@ -1908,7 +1908,6 @@ void power(void) void assist_level_state (void) { - // if UP button is clicked if (UP_CLICK) { // increment assist level and check if is out of bounds @@ -1919,7 +1918,6 @@ void assist_level_state (void) } } - // if DOWN button is clicked if (DOWN_CLICK) { // check if assist level is out of bounds @@ -2140,7 +2138,6 @@ uint8_t reset_variable_check (void) void odometer (void) { - // if user clicks onoff click event if (ONOFF_CLICK) { // increment odometer field state @@ -3564,7 +3561,7 @@ void submenu_state_controller (uint8_t ui8_state_max_number) { if (ui8_lcd_menu_config_submenu_change_variable_enabled) { - // stop changing variables if long onoff click + // stop changing variables if... if (ONOFF_LONG_CLICK) { ui8_lcd_menu_config_submenu_change_variable_enabled = 0;