button: add basic button functionalities

- Provided a way to set handler for each button one-press/long-press
- Debouncing with RC filter and Schmitt trigger
This commit is contained in:
Dien-Nhung Nguyen-Phu
2024-06-02 18:27:36 +07:00
parent f6d84a1ead
commit 799d8e43b5
4 changed files with 173 additions and 16 deletions

23
src/button.h Normal file
View File

@@ -0,0 +1,23 @@
#ifndef __BUTTON_H__
#define __BUTTON_H__
#include "CH58x_common.h"
enum keys {
KEY1 = 0,
KEY2,
KEY_INDEX,
};
#define KEY2_PIN (GPIO_Pin_22) // PB
#define KEY1_PIN (GPIO_Pin_1) // PA
#define isPressed(key) ((key) ? \
!GPIOB_ReadPortPin(KEY2_PIN) : \
GPIOA_ReadPortPin(KEY1_PIN))
void btn_onOnePress(int key, void (*handler)(void));
void btn_onLongPress(int key, void (*handler)(void));
void btn_init();
#endif /* __BUTTON_H__ */