Use enum classes, remove old comment
This commit is contained in:
parent
d5dfa80876
commit
b19a2a760b
|
@ -4,7 +4,7 @@ using namespace Pinetime::Controllers;
|
|||
|
||||
void ButtonTimerCallback(TimerHandle_t xTimer) {
|
||||
auto* buttonHandler = static_cast<ButtonHandler*>(pvTimerGetTimerID(xTimer));
|
||||
buttonHandler->HandleEvent(ButtonHandler::Timer);
|
||||
buttonHandler->HandleEvent(ButtonHandler::Events::Timer);
|
||||
}
|
||||
|
||||
void ButtonHandler::Init(Pinetime::System::SystemTask* systemTask) {
|
||||
|
@ -12,7 +12,7 @@ void ButtonHandler::Init(Pinetime::System::SystemTask* systemTask) {
|
|||
buttonTimer = xTimerCreate("buttonTimer", 0, pdFALSE, this, ButtonTimerCallback);
|
||||
}
|
||||
|
||||
void ButtonHandler::HandleEvent(events event) {
|
||||
void ButtonHandler::HandleEvent(Events event) {
|
||||
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);
|
||||
|
@ -24,61 +24,61 @@ void ButtonHandler::HandleEvent(events event) {
|
|||
systemTask->PushMessage(System::Messages::ReloadIdleTimer);
|
||||
}
|
||||
|
||||
if (event == Press) {
|
||||
if (event == Events::Press) {
|
||||
buttonPressed = true;
|
||||
} else if (event == Release) {
|
||||
} else if (event == Events::Release) {
|
||||
releaseTime = xTaskGetTickCount();
|
||||
buttonPressed = false;
|
||||
}
|
||||
|
||||
switch (state) {
|
||||
case Idle:
|
||||
if (event == Press) {
|
||||
case States::Idle:
|
||||
if (event == Events::Press) {
|
||||
xTimerChangePeriod(buttonTimer, doubleClickTime, 0);
|
||||
xTimerStart(buttonTimer, 0);
|
||||
state = Pressed;
|
||||
state = States::Pressed;
|
||||
}
|
||||
break;
|
||||
case Pressed:
|
||||
if (event == Press) {
|
||||
case States::Pressed:
|
||||
if (event == Events::Press) {
|
||||
if (xTaskGetTickCount() - releaseTime < doubleClickTime) {
|
||||
systemTask->PushMessage(System::Messages::OnButtonDoubleClicked);
|
||||
xTimerStop(buttonTimer, 0);
|
||||
state = Idle;
|
||||
state = States::Idle;
|
||||
}
|
||||
} else if (event == Release) {
|
||||
} else if (event == Events::Release) {
|
||||
xTimerChangePeriod(buttonTimer, doubleClickTime, 0);
|
||||
xTimerStart(buttonTimer, 0);
|
||||
} else if (event == Timer) {
|
||||
} else if (event == Events::Timer) {
|
||||
if (buttonPressed) {
|
||||
xTimerChangePeriod(buttonTimer, longPressTime - doubleClickTime, 0);
|
||||
xTimerStart(buttonTimer, 0);
|
||||
state = Holding;
|
||||
state = States::Holding;
|
||||
} else {
|
||||
systemTask->PushMessage(System::Messages::OnButtonPushed);
|
||||
state = Idle;
|
||||
state = States::Idle;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Holding:
|
||||
if (event == Release) {
|
||||
case States::Holding:
|
||||
if (event == Events::Release) {
|
||||
systemTask->PushMessage(System::Messages::OnButtonPushed);
|
||||
xTimerStop(buttonTimer, 0);
|
||||
state = Idle;
|
||||
} else if (event == Timer) {
|
||||
state = States::Idle;
|
||||
} else if (event == Events::Timer) {
|
||||
xTimerChangePeriod(buttonTimer, longerPressTime - longPressTime - doubleClickTime, 0);
|
||||
xTimerStart(buttonTimer, 0);
|
||||
systemTask->PushMessage(System::Messages::OnButtonLongPressed);
|
||||
state = LongHeld;
|
||||
state = States::LongHeld;
|
||||
}
|
||||
break;
|
||||
case LongHeld:
|
||||
if (event == Release) {
|
||||
case States::LongHeld:
|
||||
if (event == Events::Release) {
|
||||
xTimerStop(buttonTimer, 0);
|
||||
state = Idle;
|
||||
} else if (event == Timer) {
|
||||
state = States::Idle;
|
||||
} else if (event == Events::Timer) {
|
||||
systemTask->PushMessage(System::Messages::OnButtonLongerPressed);
|
||||
state = Idle;
|
||||
state = States::Idle;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -8,17 +8,17 @@ namespace Pinetime {
|
|||
namespace Controllers {
|
||||
class ButtonHandler {
|
||||
public:
|
||||
enum events { Press, Release, Timer };
|
||||
enum class Events : uint8_t { Press, Release, Timer };
|
||||
void Init(Pinetime::System::SystemTask* systemTask);
|
||||
void HandleEvent(events event);
|
||||
void HandleEvent(Events event);
|
||||
|
||||
private:
|
||||
enum class States : uint8_t { Idle, Pressed, Holding, LongHeld };
|
||||
Pinetime::System::SystemTask* systemTask = nullptr;
|
||||
TickType_t releaseTime = 0;
|
||||
TimerHandle_t buttonTimer;
|
||||
bool buttonPressed = false;
|
||||
enum states { Idle, Pressed, Holding, LongHeld };
|
||||
states state = Idle;
|
||||
States state = States::Idle;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -178,7 +178,6 @@ void nrfx_gpiote_evt_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action
|
|||
xTimerStartFromISR(debounceChargeTimer, &xHigherPriorityTaskWoken);
|
||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||
} else if (pin == Pinetime::PinMap::Button) {
|
||||
// This activates on button release as well due to bouncing
|
||||
xTimerStartFromISR(debounceTimer, &xHigherPriorityTaskWoken);
|
||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||
}
|
||||
|
|
|
@ -357,9 +357,9 @@ void SystemTask::Work() {
|
|||
break;
|
||||
case Messages::HandleButtonEvent:
|
||||
if (nrf_gpio_pin_read(Pinetime::PinMap::Button) == 0) {
|
||||
buttonHandler.HandleEvent(Pinetime::Controllers::ButtonHandler::Release);
|
||||
buttonHandler.HandleEvent(Pinetime::Controllers::ButtonHandler::Events::Release);
|
||||
} else {
|
||||
buttonHandler.HandleEvent(Pinetime::Controllers::ButtonHandler::Press);
|
||||
buttonHandler.HandleEvent(Pinetime::Controllers::ButtonHandler::Events::Press);
|
||||
}
|
||||
break;
|
||||
case Messages::OnDisplayTaskSleeping:
|
||||
|
|
Loading…
Reference in a new issue