GPIO입력을 이용한 버튼 처리방법 메모
typedef struct _button_data
{
uint8_t button_id;
uint8_t prev_physical_status; // Previous physical button status(PRESSED or RELEASED)
uint8_t prev_logical_status; // Previous logical button status(PRESSED or RELEASED)
uint8_t current_logical_status; // Current logical button status(PRESSED or RELEASED)
uint16_t pressed_count; // It represents how long a button is pressed. It is used for de-bounce filtering.
} button_data;
// 1ms마다 호출되는 함수
void SysTick_Handler(void)
{
:
// Update physical button state
if (read_button_status(button_data.button_id) == pressed)
{
if (button_data.prev_physical_status == RELEASED)
{
button_data.prev_physical_status = PRESSED;
}
button_data.pressed_count++; // Pressed time increased
// Update logical button state
if (button_data.pressed_count > button_data.press_check_time)
{
button_data.current_logical_status = PRESSED;
}
}
else
{
// Update physical and logical button state
if (button_data.prev_physical_status == PRESSED)
{
button_data.prev_physical_status = RELEASED;
button_data.current_logical_status = RELEASED;
}
button_data.pressed_count = 0;
}
:
}
int main(void)
{
if (button_data.prev_logical_status == RELEASED)
{
if (button_data.current_logical_status == PRESSED)
{
// TODO : 버튼이 눌렸을 때 동작
button_data.prev_logical_status = PRESSED;
}
}
else if (button_data.prev_logical_status == PRESSED)
{
if (button_data.current_logical_status == RELEASED)
{
// TODO : 버튼을 뗏을 때 동작
button_data.prev_logical_status = RELEASED;
}
}
}
'programming' 카테고리의 다른 글
같은 이벤트에 대한 View와 ViewModel의 메쏘드 호출 순서 (0) | 2024.09.27 |
---|---|
안드로이드 개발 자료모음 (0) | 2022.02.01 |
C언어 - 구조체 멤버 얼라인먼트 (0) | 2019.09.10 |
Gradle을 이용해 만든 자바 프로그램 실행시 "Could not find or load main class 클래스이름"에러 발생 (0) | 2017.08.04 |
Visual Studio 안드로이드 에뮬레이터 - Unable to determine the host ip address에러 (0) | 2017.05.18 |