nt9856x/code/application/sifarsdk/333DE/hal/KeyContrl.cpp

48 lines
1.4 KiB
C++

#include "KeyContrl.h"
#include "Log.h"
KeyContrl::KeyContrl(const SfKeyDefine &key, const SF_KEY_PIN_E &pin, const SF_BOOL &valueMeansKeyUp)
: mKey(key), mPin(pin), mValueMeansKeyUp(valueMeansKeyUp)
{
mEventContext = nullptr;
mLastPinValue = valueMeansKeyUp;
LogInfo("mLastPinValue = %d\n", mLastPinValue);
mKeyMonitor = std::make_shared<VKeyEventMonitor>();
}
KeyContrl::~KeyContrl()
{
}
void KeyContrl::Init(void)
{
LogInfo("KeyContrl::Init pin = %d\n", mPin);
GpioInit(mPin, GPIO_DIR_IN);
}
void KeyContrl::UnInit(void)
{
GpioDeinit(mPin);
mKeyMonitor.reset();
}
void KeyContrl::SetKeyEventMonitor(std::shared_ptr<VKeyEventMonitor> monitor, void *context)
{
mKeyMonitor = monitor;
mEventContext = context;
}
void KeyContrl::CheckKeyValue(void)
{
auto monitor = mKeyMonitor.lock();
if (mKeyMonitor.expired())
{
return;
}
SINT8 pinValue = 0;
GpioGet(mPin, pinValue);
// LogInfo("KeyContrl::CheckKeyValue pin = %d, pinValue = %d\n", mPin, pinValue);
if (pinValue == mValueMeansKeyUp && mValueMeansKeyUp != mLastPinValue)
{
monitor->HalKeyEventHappened(mKey, SfKeyEvent::KEY_EVENT_UN_PRESSED, mEventContext);
}
if (pinValue != mValueMeansKeyUp && mValueMeansKeyUp == mLastPinValue)
{
monitor->HalKeyEventHappened(mKey, SfKeyEvent::KEY_EVENT_PRESSED, mEventContext);
}
mLastPinValue = pinValue;
}