|
36 | 36 | /* USER CODE BEGIN PTD */
|
37 | 37 | #define version "3.0.2"
|
38 | 38 |
|
| 39 | +#define BTN_LONG_PRESS 15 //*50ms (htim16 interval) --> 15 = 750ms |
| 40 | + |
39 | 41 | #define DEBUG
|
40 | 42 |
|
41 | 43 | #ifdef DEBUG
|
@@ -83,6 +85,9 @@ uint8_t SW_ready = 1;
|
83 | 85 | uint8_t SW_1_pressed = 0;
|
84 | 86 | uint8_t SW_2_pressed = 0;
|
85 | 87 | 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; |
86 | 91 |
|
87 | 92 | /* states for runtime switch */
|
88 | 93 | typedef enum {
|
@@ -950,10 +955,13 @@ void get_handle_type(){
|
950 | 955 | }
|
951 | 956 |
|
952 | 957 | /* Interrupts at button press */
|
| 958 | +static uint16_t btnPressed = 0; |
953 | 959 | void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
|
954 | 960 | {
|
955 | 961 | 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; |
956 | 963 | HAL_TIM_Base_Start_IT(&htim16);
|
| 964 | + beep(); |
957 | 965 | SW_ready = 0;
|
958 | 966 | }
|
959 | 967 | }
|
@@ -995,24 +1003,43 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
|
995 | 1003 | }
|
996 | 1004 |
|
997 | 1005 | /* Button Debounce timer (50 ms) */
|
| 1006 | + static uint8_t timerCycles = 0; |
| 1007 | + |
998 | 1008 | 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){ |
1000 | 1011 | SW_ready = 1;
|
1001 | 1012 | 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; |
1002 | 1016 | beep();
|
1003 |
| - HAL_TIM_Base_Stop_IT(&htim16); |
| 1017 | + SW_1_pressed_long = 1; |
| 1018 | + timerCycles = 0; |
1004 | 1019 | }
|
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){ |
1006 | 1021 | SW_ready = 1;
|
1007 | 1022 | 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; |
1008 | 1026 | beep();
|
1009 |
| - HAL_TIM_Base_Stop_IT(&htim16); |
| 1027 | + SW_2_pressed_long = 1; |
| 1028 | + timerCycles = 0; |
1010 | 1029 | }
|
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){ |
1012 | 1031 | SW_ready = 1;
|
1013 | 1032 | 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; |
1014 | 1037 | 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++; |
1016 | 1043 | }
|
1017 | 1044 | }
|
1018 | 1045 | }
|
|
0 commit comments