2021-10-25 05:53:14 -04:00
|
|
|
#include "ButtonHandler.h"
|
|
|
|
|
|
|
|
using namespace Pinetime::Controllers;
|
|
|
|
|
|
|
|
void ButtonTimerCallback(TimerHandle_t xTimer) {
|
|
|
|
auto* buttonHandler = static_cast<ButtonHandler*>(pvTimerGetTimerID(xTimer));
|
2021-10-25 06:40:43 -04:00
|
|
|
buttonHandler->HandleEvent(ButtonHandler::Events::Timer);
|
2021-10-25 05:53:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonHandler::Init(Pinetime::System::SystemTask* systemTask) {
|
|
|
|
this->systemTask = systemTask;
|
|
|
|
buttonTimer = xTimerCreate("buttonTimer", 0, pdFALSE, this, ButtonTimerCallback);
|
|
|
|
}
|
|
|
|
|
2021-10-25 06:40:43 -04:00
|
|
|
void ButtonHandler::HandleEvent(Events event) {
|
2021-10-25 05:53:14 -04:00
|
|
|
static constexpr TickType_t doubleClickTime = pdMS_TO_TICKS(200);
|
|
|
|
static constexpr TickType_t longPressTime = pdMS_TO_TICKS(400);
|
|
|
|
static constexpr TickType_t longerPressTime = pdMS_TO_TICKS(2000);
|
|
|
|
|
|
|
|
if (systemTask->IsSleeping()) {
|
|
|
|
// This is for faster wakeup, sacrificing special longpress and doubleclick handling while sleeping
|
|
|
|
systemTask->PushMessage(System::Messages::GoToRunning);
|
|
|
|
} else {
|
|
|
|
systemTask->PushMessage(System::Messages::ReloadIdleTimer);
|
|
|
|
}
|
|
|
|
|
2021-10-25 06:40:43 -04:00
|
|
|
if (event == Events::Press) {
|
2021-10-25 05:53:14 -04:00
|
|
|
buttonPressed = true;
|
2021-10-25 06:40:43 -04:00
|
|
|
} else if (event == Events::Release) {
|
2021-10-25 05:53:14 -04:00
|
|
|
releaseTime = xTaskGetTickCount();
|
|
|
|
buttonPressed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (state) {
|
2021-10-25 06:40:43 -04:00
|
|
|
case States::Idle:
|
|
|
|
if (event == Events::Press) {
|
2021-10-25 05:53:14 -04:00
|
|
|
xTimerChangePeriod(buttonTimer, doubleClickTime, 0);
|
|
|
|
xTimerStart(buttonTimer, 0);
|
2021-10-25 06:40:43 -04:00
|
|
|
state = States::Pressed;
|
2021-10-25 05:53:14 -04:00
|
|
|
}
|
|
|
|
break;
|
2021-10-25 06:40:43 -04:00
|
|
|
case States::Pressed:
|
|
|
|
if (event == Events::Press) {
|
2021-10-25 05:53:14 -04:00
|
|
|
if (xTaskGetTickCount() - releaseTime < doubleClickTime) {
|
|
|
|
systemTask->PushMessage(System::Messages::OnButtonDoubleClicked);
|
|
|
|
xTimerStop(buttonTimer, 0);
|
2021-10-25 06:40:43 -04:00
|
|
|
state = States::Idle;
|
2021-10-25 05:53:14 -04:00
|
|
|
}
|
2021-10-25 06:40:43 -04:00
|
|
|
} else if (event == Events::Release) {
|
2021-10-25 05:53:14 -04:00
|
|
|
xTimerChangePeriod(buttonTimer, doubleClickTime, 0);
|
|
|
|
xTimerStart(buttonTimer, 0);
|
2021-10-25 06:40:43 -04:00
|
|
|
} else if (event == Events::Timer) {
|
2021-10-25 05:53:14 -04:00
|
|
|
if (buttonPressed) {
|
|
|
|
xTimerChangePeriod(buttonTimer, longPressTime - doubleClickTime, 0);
|
|
|
|
xTimerStart(buttonTimer, 0);
|
2021-10-25 06:40:43 -04:00
|
|
|
state = States::Holding;
|
2021-10-25 05:53:14 -04:00
|
|
|
} else {
|
|
|
|
systemTask->PushMessage(System::Messages::OnButtonPushed);
|
2021-10-25 06:40:43 -04:00
|
|
|
state = States::Idle;
|
2021-10-25 05:53:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2021-10-25 06:40:43 -04:00
|
|
|
case States::Holding:
|
|
|
|
if (event == Events::Release) {
|
2021-10-25 05:53:14 -04:00
|
|
|
systemTask->PushMessage(System::Messages::OnButtonPushed);
|
|
|
|
xTimerStop(buttonTimer, 0);
|
2021-10-25 06:40:43 -04:00
|
|
|
state = States::Idle;
|
|
|
|
} else if (event == Events::Timer) {
|
2021-10-25 05:53:14 -04:00
|
|
|
xTimerChangePeriod(buttonTimer, longerPressTime - longPressTime - doubleClickTime, 0);
|
|
|
|
xTimerStart(buttonTimer, 0);
|
|
|
|
systemTask->PushMessage(System::Messages::OnButtonLongPressed);
|
2021-10-25 06:40:43 -04:00
|
|
|
state = States::LongHeld;
|
2021-10-25 05:53:14 -04:00
|
|
|
}
|
|
|
|
break;
|
2021-10-25 06:40:43 -04:00
|
|
|
case States::LongHeld:
|
|
|
|
if (event == Events::Release) {
|
2021-10-25 05:53:14 -04:00
|
|
|
xTimerStop(buttonTimer, 0);
|
2021-10-25 06:40:43 -04:00
|
|
|
state = States::Idle;
|
|
|
|
} else if (event == Events::Timer) {
|
2021-10-25 05:53:14 -04:00
|
|
|
systemTask->PushMessage(System::Messages::OnButtonLongerPressed);
|
2021-10-25 06:40:43 -04:00
|
|
|
state = States::Idle;
|
2021-10-25 05:53:14 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|