47 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			47 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| #pragma once
 | |
| 
 | |
| #define BTN_DEFAULT_DEBOUNCE_MS 20
 | |
| #define BTN_DEFAULT_LONGPRESSED_MS 1000
 | |
| #define BTN_DEFAULT_REPEAT_MS 200
 | |
| 
 | |
| // States for state machine
 | |
| typedef enum
 | |
| {
 | |
|     IDLE = 0,
 | |
|     DEBOUNCE,
 | |
|     PRESSED,
 | |
|     LONGPRESSED,
 | |
|     REPEAT,
 | |
|     MAX_STATE
 | |
| } ButtonState_t;
 | |
| 
 | |
| // Struct for button
 | |
| typedef struct ButtonKey ButtonKey_t;
 | |
| typedef void (*buttonPressed_t)(ButtonKey_t *key);
 | |
| typedef void (*buttonLongPressed_t)(ButtonKey_t *key);
 | |
| typedef void (*buttonRepeat_t)(ButtonKey_t *key);
 | |
| 
 | |
| struct ButtonKey
 | |
| {
 | |
|     uint8_t instance;    // Button name/number
 | |
|     ButtonState_t state; // Button current state
 | |
| 
 | |
|     GPIO_TypeDef *gpio_port; // GPIO Port for a button
 | |
|     uint16_t gpio_pin;       // GPIO Pin for a button
 | |
|     GPIO_PinState pushed_state;
 | |
| 
 | |
|     uint32_t last_tick;          // Last remembered time before steps
 | |
|     uint32_t timer_debounce;     // Fixed, settable time for debounce timer
 | |
|     uint32_t timer_long_pressed; // Fixed, adjustable time for long press timer
 | |
|     uint32_t timer_repeat_delay; // Fixed, adjustable interval time
 | |
| 
 | |
|     buttonPressed_t buttonReleased;        // A callback for button released
 | |
|     buttonPressed_t buttonPressed;         // A callback for button pressed
 | |
|     buttonLongPressed_t buttonLongPressed; // A callback for long pressed
 | |
|     buttonRepeat_t buttonRepeat;           // A callback for repeat
 | |
| };
 | |
| 
 | |
| // Public functions
 | |
| 
 | |
| void buttonHandler(ButtonKey_t *key);
 |