Use enum classes, remove old comment

This commit is contained in:
Riku Isokoski 2021-10-25 13:40:43 +03:00
parent d5dfa80876
commit b19a2a760b
4 changed files with 30 additions and 31 deletions

View File

@ -4,7 +4,7 @@ using namespace Pinetime::Controllers;
void ButtonTimerCallback(TimerHandle_t xTimer) { void ButtonTimerCallback(TimerHandle_t xTimer) {
auto* buttonHandler = static_cast<ButtonHandler*>(pvTimerGetTimerID(xTimer)); auto* buttonHandler = static_cast<ButtonHandler*>(pvTimerGetTimerID(xTimer));
buttonHandler->HandleEvent(ButtonHandler::Timer); buttonHandler->HandleEvent(ButtonHandler::Events::Timer);
} }
void ButtonHandler::Init(Pinetime::System::SystemTask* systemTask) { 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); 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 doubleClickTime = pdMS_TO_TICKS(200);
static constexpr TickType_t longPressTime = pdMS_TO_TICKS(400); static constexpr TickType_t longPressTime = pdMS_TO_TICKS(400);
static constexpr TickType_t longerPressTime = pdMS_TO_TICKS(2000); static constexpr TickType_t longerPressTime = pdMS_TO_TICKS(2000);
@ -24,61 +24,61 @@ void ButtonHandler::HandleEvent(events event) {
systemTask->PushMessage(System::Messages::ReloadIdleTimer); systemTask->PushMessage(System::Messages::ReloadIdleTimer);
} }
if (event == Press) { if (event == Events::Press) {
buttonPressed = true; buttonPressed = true;
} else if (event == Release) { } else if (event == Events::Release) {
releaseTime = xTaskGetTickCount(); releaseTime = xTaskGetTickCount();
buttonPressed = false; buttonPressed = false;
} }
switch (state) { switch (state) {
case Idle: case States::Idle:
if (event == Press) { if (event == Events::Press) {
xTimerChangePeriod(buttonTimer, doubleClickTime, 0); xTimerChangePeriod(buttonTimer, doubleClickTime, 0);
xTimerStart(buttonTimer, 0); xTimerStart(buttonTimer, 0);
state = Pressed; state = States::Pressed;
} }
break; break;
case Pressed: case States::Pressed:
if (event == Press) { if (event == Events::Press) {
if (xTaskGetTickCount() - releaseTime < doubleClickTime) { if (xTaskGetTickCount() - releaseTime < doubleClickTime) {
systemTask->PushMessage(System::Messages::OnButtonDoubleClicked); systemTask->PushMessage(System::Messages::OnButtonDoubleClicked);
xTimerStop(buttonTimer, 0); xTimerStop(buttonTimer, 0);
state = Idle; state = States::Idle;
} }
} else if (event == Release) { } else if (event == Events::Release) {
xTimerChangePeriod(buttonTimer, doubleClickTime, 0); xTimerChangePeriod(buttonTimer, doubleClickTime, 0);
xTimerStart(buttonTimer, 0); xTimerStart(buttonTimer, 0);
} else if (event == Timer) { } else if (event == Events::Timer) {
if (buttonPressed) { if (buttonPressed) {
xTimerChangePeriod(buttonTimer, longPressTime - doubleClickTime, 0); xTimerChangePeriod(buttonTimer, longPressTime - doubleClickTime, 0);
xTimerStart(buttonTimer, 0); xTimerStart(buttonTimer, 0);
state = Holding; state = States::Holding;
} else { } else {
systemTask->PushMessage(System::Messages::OnButtonPushed); systemTask->PushMessage(System::Messages::OnButtonPushed);
state = Idle; state = States::Idle;
} }
} }
break; break;
case Holding: case States::Holding:
if (event == Release) { if (event == Events::Release) {
systemTask->PushMessage(System::Messages::OnButtonPushed); systemTask->PushMessage(System::Messages::OnButtonPushed);
xTimerStop(buttonTimer, 0); xTimerStop(buttonTimer, 0);
state = Idle; state = States::Idle;
} else if (event == Timer) { } else if (event == Events::Timer) {
xTimerChangePeriod(buttonTimer, longerPressTime - longPressTime - doubleClickTime, 0); xTimerChangePeriod(buttonTimer, longerPressTime - longPressTime - doubleClickTime, 0);
xTimerStart(buttonTimer, 0); xTimerStart(buttonTimer, 0);
systemTask->PushMessage(System::Messages::OnButtonLongPressed); systemTask->PushMessage(System::Messages::OnButtonLongPressed);
state = LongHeld; state = States::LongHeld;
} }
break; break;
case LongHeld: case States::LongHeld:
if (event == Release) { if (event == Events::Release) {
xTimerStop(buttonTimer, 0); xTimerStop(buttonTimer, 0);
state = Idle; state = States::Idle;
} else if (event == Timer) { } else if (event == Events::Timer) {
systemTask->PushMessage(System::Messages::OnButtonLongerPressed); systemTask->PushMessage(System::Messages::OnButtonLongerPressed);
state = Idle; state = States::Idle;
} }
break; break;
} }

View File

@ -8,17 +8,17 @@ namespace Pinetime {
namespace Controllers { namespace Controllers {
class ButtonHandler { class ButtonHandler {
public: public:
enum events { Press, Release, Timer }; enum class Events : uint8_t { Press, Release, Timer };
void Init(Pinetime::System::SystemTask* systemTask); void Init(Pinetime::System::SystemTask* systemTask);
void HandleEvent(events event); void HandleEvent(Events event);
private: private:
enum class States : uint8_t { Idle, Pressed, Holding, LongHeld };
Pinetime::System::SystemTask* systemTask = nullptr; Pinetime::System::SystemTask* systemTask = nullptr;
TickType_t releaseTime = 0; TickType_t releaseTime = 0;
TimerHandle_t buttonTimer; TimerHandle_t buttonTimer;
bool buttonPressed = false; bool buttonPressed = false;
enum states { Idle, Pressed, Holding, LongHeld }; States state = States::Idle;
states state = Idle;
}; };
} }
} }

View File

@ -178,7 +178,6 @@ void nrfx_gpiote_evt_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action
xTimerStartFromISR(debounceChargeTimer, &xHigherPriorityTaskWoken); xTimerStartFromISR(debounceChargeTimer, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken); portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
} else if (pin == Pinetime::PinMap::Button) { } else if (pin == Pinetime::PinMap::Button) {
// This activates on button release as well due to bouncing
xTimerStartFromISR(debounceTimer, &xHigherPriorityTaskWoken); xTimerStartFromISR(debounceTimer, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken); portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
} }

View File

@ -357,9 +357,9 @@ void SystemTask::Work() {
break; break;
case Messages::HandleButtonEvent: case Messages::HandleButtonEvent:
if (nrf_gpio_pin_read(Pinetime::PinMap::Button) == 0) { if (nrf_gpio_pin_read(Pinetime::PinMap::Button) == 0) {
buttonHandler.HandleEvent(Pinetime::Controllers::ButtonHandler::Release); buttonHandler.HandleEvent(Pinetime::Controllers::ButtonHandler::Events::Release);
} else { } else {
buttonHandler.HandleEvent(Pinetime::Controllers::ButtonHandler::Press); buttonHandler.HandleEvent(Pinetime::Controllers::ButtonHandler::Events::Press);
} }
break; break;
case Messages::OnDisplayTaskSleeping: case Messages::OnDisplayTaskSleeping: