Skip to content

Commit 7423256

Browse files
committed
added feature to detect long button presses
1 parent 6e92eb4 commit 7423256

File tree

1 file changed

+33
-6
lines changed
  • AxxSolder_firmware/Core/Src

1 file changed

+33
-6
lines changed

AxxSolder_firmware/Core/Src/main.c

+33-6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
/* USER CODE BEGIN PTD */
3737
#define version "3.0.2"
3838

39+
#define BTN_LONG_PRESS 15 //*50ms (htim16 interval) --> 15 = 750ms
40+
3941
#define DEBUG
4042

4143
#ifdef DEBUG
@@ -83,6 +85,9 @@ uint8_t SW_ready = 1;
8385
uint8_t SW_1_pressed = 0;
8486
uint8_t SW_2_pressed = 0;
8587
uint8_t SW_3_pressed = 0;
88+
uint8_t SW_1_pressed_long = 0;
89+
uint8_t SW_2_pressed_long = 0;
90+
uint8_t SW_3_pressed_long = 0;
8691

8792
/* states for runtime switch */
8893
typedef enum {
@@ -950,10 +955,13 @@ void get_handle_type(){
950955
}
951956

952957
/* Interrupts at button press */
958+
static uint16_t btnPressed = 0;
953959
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
954960
{
955961
if(((GPIO_Pin == SW_1_Pin) || (GPIO_Pin == SW_2_Pin) || (GPIO_Pin == SW_3_Pin)) && (SW_ready == 1)){ //A button is pressed
962+
btnPressed = GPIO_Pin;
956963
HAL_TIM_Base_Start_IT(&htim16);
964+
beep();
957965
SW_ready = 0;
958966
}
959967
}
@@ -995,24 +1003,43 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
9951003
}
9961004

9971005
/* Button Debounce timer (50 ms) */
1006+
static uint8_t timerCycles = 0;
1007+
9981008
if ((htim == &htim16 && SW_ready == 0)){
999-
if(HAL_GPIO_ReadPin(SW_1_GPIO_Port, SW_1_Pin) == GPIO_PIN_SET){
1009+
HAL_TIM_Base_Stop_IT(&htim16);
1010+
if(btnPressed == SW_1_Pin && HAL_GPIO_ReadPin(SW_1_GPIO_Port, SW_1_Pin) == GPIO_PIN_RESET){
10001011
SW_ready = 1;
10011012
SW_1_pressed = 1;
1013+
timerCycles = 0;
1014+
}else if(timerCycles > BTN_LONG_PRESS && btnPressed == SW_1_Pin && HAL_GPIO_ReadPin(SW_1_GPIO_Port, SW_1_Pin) == GPIO_PIN_SET){
1015+
SW_ready = 1;
10021016
beep();
1003-
HAL_TIM_Base_Stop_IT(&htim16);
1017+
SW_1_pressed_long = 1;
1018+
timerCycles = 0;
10041019
}
1005-
else if(HAL_GPIO_ReadPin(SW_2_GPIO_Port, SW_2_Pin) == GPIO_PIN_SET){
1020+
else if(btnPressed == SW_2_Pin && HAL_GPIO_ReadPin(SW_2_GPIO_Port, SW_2_Pin) == GPIO_PIN_RESET){
10061021
SW_ready = 1;
10071022
SW_2_pressed = 1;
1023+
timerCycles = 0;
1024+
}else if(timerCycles > BTN_LONG_PRESS && btnPressed == SW_2_Pin && HAL_GPIO_ReadPin(SW_2_GPIO_Port, SW_2_Pin) == GPIO_PIN_SET){
1025+
SW_ready = 1;
10081026
beep();
1009-
HAL_TIM_Base_Stop_IT(&htim16);
1027+
SW_2_pressed_long = 1;
1028+
timerCycles = 0;
10101029
}
1011-
else if(HAL_GPIO_ReadPin(SW_3_GPIO_Port, SW_3_Pin) == GPIO_PIN_SET){
1030+
else if(btnPressed == SW_3_Pin && HAL_GPIO_ReadPin(SW_3_GPIO_Port, SW_3_Pin) == GPIO_PIN_RESET){
10121031
SW_ready = 1;
10131032
SW_3_pressed = 1;
1033+
timerCycles = 0;
1034+
}
1035+
else if(timerCycles > BTN_LONG_PRESS && btnPressed == SW_3_Pin && HAL_GPIO_ReadPin(SW_3_GPIO_Port, SW_3_Pin) == GPIO_PIN_SET){
1036+
SW_ready = 1;
10141037
beep();
1015-
HAL_TIM_Base_Stop_IT(&htim16);
1038+
SW_3_pressed_long = 1;
1039+
timerCycles = 0;
1040+
}else{
1041+
HAL_TIM_Base_Start_IT(&htim16);
1042+
timerCycles++;
10161043
}
10171044
}
10181045
}

0 commit comments

Comments
 (0)