GeekTrack/2.Firmware/simulator/LVGL.Simulator/Win32HAL/Event/ButtonEvent.cpp

100 lines
2.8 KiB
C++
Raw Normal View History

2022-09-18 18:40:49 +08:00
#include "ButtonEvent.h"
#ifdef _WIN32
#include<time.h>
#define ButtonEvent_Millis clock()
#else
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#define ButtonEvent_Millis xTaskGetTickCount()
#endif
/**
* @brief
* @param NoPressState:
* @param LongPressTimeMs_Set:
* @param LongPressTimeRepeatMs_Set:
* @retval
*/
ButtonEvent::ButtonEvent(
bool NoPressState,
uint16_t LongPressTimeMs_Set,
uint16_t LongPressTimeRepeatMs_Set
)
{
Button_NoPressState = NoPressState;
LongPressTimeMs = LongPressTimeMs_Set;
LongPressTimeRepeatMs = LongPressTimeRepeatMs_Set;
LastLongPress_TimePoint = LastClick_TimePoint = LastPress_TimePoint = 0;
IS_LongPressed = IS_Pressed = false;
CallbackFunc = 0;
}
/**
* @brief
* @param Function:
* @retval
*/
void ButtonEvent::EventAttach(ButtonEvent_FuncCallBack_t function)
{
CallbackFunc = function;
}
/**
* @brief 10ms
* @param NowState:
* @retval
*/
void ButtonEvent::EventMonitor(bool NowState)
{
if(!CallbackFunc)
return;
if (!IS_Pressed && (NowState != Button_NoPressState))
{
IsPressed = IS_Pressed = true;
Button_NowState = Press;
LastPress_TimePoint = ButtonEvent_Millis;
CallbackFunc(this, EVENT_ButtonPress);
CallbackFunc(this, EVENT_ButtonChange);
}
else if (IS_Pressed && ButtonEvent_Millis - LastPress_TimePoint >= LongPressTimeMs && (NowState != Button_NoPressState))
{
Button_NowState = LongPress;
if(!IS_LongPressed)
{
CallbackFunc(this, EVENT_ButtonLongPressed);
LastLongPress_TimePoint = ButtonEvent_Millis;
IsLongPressed = IS_LongPressed = true;
}
else if(ButtonEvent_Millis - LastLongPress_TimePoint >= LongPressTimeRepeatMs)
{
LastLongPress_TimePoint = ButtonEvent_Millis;
CallbackFunc(this, EVENT_ButtonLongPressRepeat);
}
}
else if (IS_Pressed && (NowState == Button_NoPressState))
{
IS_LongPressed = IS_Pressed = false;
IsClicked = true;
Button_NowState = NoPress;
LastClick_TimePoint = ButtonEvent_Millis;
if(ButtonEvent_Millis - LastPress_TimePoint < LongPressTimeMs)
{
CallbackFunc(this, EVENT_ButtonClick);
}
CallbackFunc(this, EVENT_ButtonRelease);
CallbackFunc(this, EVENT_ButtonChange);
}
if(IS_Pressed && ButtonEvent_Millis - LastClick_TimePoint < 200)
{
Click_Cnt++;
CallbackFunc(this, EVENT_ButtonDoubleClick);
}
}