#include "includes.h"

void Key_Open()
{
}

// 处理按键
void Key_Handler(void) 
{
	static uint32_t key_tick = 0;
	uint32_t tick = GetTickCount();
	uint8_t g_KeyVal = KEY_INVALID;
	
	// 保存键值
	uint8_t pe5 = LL_GPIO_IsInputPinSet(GPIOE, GPIO_PIN_5);

	// 判断是否为低电平，排除干扰
	if(LL_GPIO_IsInputPinSet(GPIOE, GPIO_PIN_5))
		return;

	// 防抖动，300ms以内的连续按键丢弃
	if((tick >= key_tick && tick - key_tick < 300) || (tick < key_tick && 0xFFFFFFFFul - key_tick + 1 + tick < 300))
		return;

	if(pe5 == 0)
	{
		//printf("\n----------- Key pe5 pressed -----------\n");
		g_KeyVal = KEY_ALARMOFF;
	}
	
	if(g_KeyVal != KEY_INVALID)
	{
		key_tick = GetTickCount();
		
		// 解除报警
		if(g_KeyVal == KEY_ALARMOFF && IS_ALARM_ON())
			KZ_ALARM_OFF();
	}	
}
