#ifndef INC_BUTTON_H_
#define INC_BUTTON_H_

#include "main.h"
#include "stdbool.h"

typedef struct {
	GPIO_TypeDef		*port;
	uint16_t			pinNumber;
	GPIO_PinState		onState;
} BUTTON_CONTROL;

bool buttonGetPressed(uint8_t num);

#endif /* INC_BUTTON_H_ */
// button.c
#include "button.h"

// Button Port, Pin 정보, Pull-Up이므로 0에서 ON
BUTTON_CONTROL button[1] = {
		{GPIOC, GPIO_PIN_4, 0}
};

// Button이 눌리면 Debounce 처리해서 True 반환
bool buttonGetPressed(uint8_t num) {
	static uint32_t prevTime = 0xffffffff;		// 초기값 최대치 설정, 처음부터 Debounce

	if(prevTime == 0xffffffff) {
		prevTime = HAL_GetTick();				// prevTime 초기화 작업
	}

	bool ret = false;

	if(HAL_GPIO_ReadPin(button[num].port, button[num].pinNumber) == button[num].onState) {
		uint32_t currTime = HAL_GetTick();

		if(currTime - prevTime >= 300) {		// Debounce Code
			if(HAL_GPIO_ReadPin(button[num].port, button[num].pinNumber) == button[num].onState) {
				ret = true;
			}
			prevTime = currTime;
		}
	}

	return ret;
}