From b19a2a760b74f27c8d3db262bf43437f722f74bd Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Mon, 25 Oct 2021 13:40:43 +0300 Subject: [PATCH] Use enum classes, remove old comment --- src/buttonhandler/ButtonHandler.cpp | 48 ++++++++++++++--------------- src/buttonhandler/ButtonHandler.h | 8 ++--- src/main.cpp | 1 - src/systemtask/SystemTask.cpp | 4 +-- 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/buttonhandler/ButtonHandler.cpp b/src/buttonhandler/ButtonHandler.cpp index 997409e5..b6067d27 100644 --- a/src/buttonhandler/ButtonHandler.cpp +++ b/src/buttonhandler/ButtonHandler.cpp @@ -4,7 +4,7 @@ using namespace Pinetime::Controllers; void ButtonTimerCallback(TimerHandle_t xTimer) { auto* buttonHandler = static_cast(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; } diff --git a/src/buttonhandler/ButtonHandler.h b/src/buttonhandler/ButtonHandler.h index 5d5b57e9..b4c36bda 100644 --- a/src/buttonhandler/ButtonHandler.h +++ b/src/buttonhandler/ButtonHandler.h @@ -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; }; } } diff --git a/src/main.cpp b/src/main.cpp index b942fd41..53f78ce8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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); } diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index b7db0b9d..8a4f894e 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -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: