Return button action instead of pushing messages

This commit is contained in:
Riku Isokoski 2021-10-25 16:57:29 +03:00
parent b19a2a760b
commit 351c60a131
6 changed files with 66 additions and 50 deletions

View File

@ -0,0 +1,7 @@
#pragma once
namespace Pinetime {
namespace Controllers {
enum class ButtonActions { None, Click, DoubleClick, LongPress, LongerPress };
}
}

View File

@ -3,27 +3,19 @@
using namespace Pinetime::Controllers; using namespace Pinetime::Controllers;
void ButtonTimerCallback(TimerHandle_t xTimer) { void ButtonTimerCallback(TimerHandle_t xTimer) {
auto* buttonHandler = static_cast<ButtonHandler*>(pvTimerGetTimerID(xTimer)); auto* sysTask = static_cast<Pinetime::System::SystemTask*>(pvTimerGetTimerID(xTimer));
buttonHandler->HandleEvent(ButtonHandler::Events::Timer); sysTask->PushMessage(Pinetime::System::Messages::HandleButtonTimerEvent);
} }
void ButtonHandler::Init(Pinetime::System::SystemTask* systemTask) { void ButtonHandler::Init(Pinetime::System::SystemTask* systemTask) {
this->systemTask = systemTask; buttonTimer = xTimerCreate("buttonTimer", 0, pdFALSE, systemTask, ButtonTimerCallback);
buttonTimer = xTimerCreate("buttonTimer", 0, pdFALSE, this, ButtonTimerCallback);
} }
void ButtonHandler::HandleEvent(Events event) { ButtonActions 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);
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);
}
if (event == Events::Press) { if (event == Events::Press) {
buttonPressed = true; buttonPressed = true;
} else if (event == Events::Release) { } else if (event == Events::Release) {
@ -42,9 +34,9 @@ void ButtonHandler::HandleEvent(Events event) {
case States::Pressed: case States::Pressed:
if (event == Events::Press) { if (event == Events::Press) {
if (xTaskGetTickCount() - releaseTime < doubleClickTime) { if (xTaskGetTickCount() - releaseTime < doubleClickTime) {
systemTask->PushMessage(System::Messages::OnButtonDoubleClicked);
xTimerStop(buttonTimer, 0); xTimerStop(buttonTimer, 0);
state = States::Idle; state = States::Idle;
return ButtonActions::DoubleClick;
} }
} else if (event == Events::Release) { } else if (event == Events::Release) {
xTimerChangePeriod(buttonTimer, doubleClickTime, 0); xTimerChangePeriod(buttonTimer, doubleClickTime, 0);
@ -55,21 +47,21 @@ void ButtonHandler::HandleEvent(Events event) {
xTimerStart(buttonTimer, 0); xTimerStart(buttonTimer, 0);
state = States::Holding; state = States::Holding;
} else { } else {
systemTask->PushMessage(System::Messages::OnButtonPushed);
state = States::Idle; state = States::Idle;
return ButtonActions::Click;
} }
} }
break; break;
case States::Holding: case States::Holding:
if (event == Events::Release) { if (event == Events::Release) {
systemTask->PushMessage(System::Messages::OnButtonPushed);
xTimerStop(buttonTimer, 0); xTimerStop(buttonTimer, 0);
state = States::Idle; state = States::Idle;
return ButtonActions::Click;
} else if (event == Events::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);
state = States::LongHeld; state = States::LongHeld;
return ButtonActions::LongPress;
} }
break; break;
case States::LongHeld: case States::LongHeld:
@ -77,9 +69,10 @@ void ButtonHandler::HandleEvent(Events event) {
xTimerStop(buttonTimer, 0); xTimerStop(buttonTimer, 0);
state = States::Idle; state = States::Idle;
} else if (event == Events::Timer) { } else if (event == Events::Timer) {
systemTask->PushMessage(System::Messages::OnButtonLongerPressed);
state = States::Idle; state = States::Idle;
return ButtonActions::LongerPress;
} }
break; break;
} }
return ButtonActions::None;
} }

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "ButtonActions.h"
#include "systemtask/SystemTask.h" #include "systemtask/SystemTask.h"
#include <FreeRTOS.h> #include <FreeRTOS.h>
#include <timers.h> #include <timers.h>
@ -10,11 +11,10 @@ namespace Pinetime {
public: public:
enum class Events : uint8_t { 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); ButtonActions HandleEvent(Events event);
private: private:
enum class States : uint8_t { Idle, Pressed, Holding, LongHeld }; enum class States : uint8_t { Idle, Pressed, Holding, LongHeld };
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;

View File

@ -15,17 +15,13 @@ namespace Pinetime {
BleFirmwareUpdateStarted, BleFirmwareUpdateStarted,
BleFirmwareUpdateFinished, BleFirmwareUpdateFinished,
OnTouchEvent, OnTouchEvent,
OnButtonPushed,
OnButtonLongPressed,
OnButtonLongerPressed,
OnButtonDoubleClicked,
HandleButtonEvent, HandleButtonEvent,
HandleButtonTimerEvent,
OnDisplayTaskSleeping, OnDisplayTaskSleeping,
EnableSleeping, EnableSleeping,
DisableSleeping, DisableSleeping,
OnNewDay, OnNewDay,
OnChargingEvent, OnChargingEvent,
ReloadIdleTimer,
SetOffAlarm, SetOffAlarm,
StopRinging, StopRinging,
MeasureBatteryTimerExpired, MeasureBatteryTimerExpired,

View File

@ -335,33 +335,25 @@ void SystemTask::Work() {
ReloadIdleTimer(); ReloadIdleTimer();
displayApp.PushMessage(Pinetime::Applications::Display::Messages::TouchEvent); displayApp.PushMessage(Pinetime::Applications::Display::Messages::TouchEvent);
break; break;
case Messages::OnButtonPushed: case Messages::HandleButtonEvent: {
if (!isSleeping && !isGoingToSleep) { // This is for faster wakeup, sacrificing special longpress and doubleclick handling while sleeping
displayApp.PushMessage(Pinetime::Applications::Display::Messages::ButtonPushed); if (IsSleeping()) {
} GoToRunning();
break; break;
case Messages::OnButtonLongPressed:
if (!isSleeping) {
displayApp.PushMessage(Pinetime::Applications::Display::Messages::ButtonLongPressed);
} }
break;
case Messages::OnButtonLongerPressed: Controllers::ButtonActions action;
if (!isSleeping) {
displayApp.PushMessage(Pinetime::Applications::Display::Messages::ButtonLongerPressed);
}
break;
case Messages::OnButtonDoubleClicked:
if (!isSleeping) {
displayApp.PushMessage(Pinetime::Applications::Display::Messages::ButtonDoubleClicked);
}
break;
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::Events::Release); action = buttonHandler.HandleEvent(Controllers::ButtonHandler::Events::Release);
} else { } else {
buttonHandler.HandleEvent(Pinetime::Controllers::ButtonHandler::Events::Press); action = buttonHandler.HandleEvent(Controllers::ButtonHandler::Events::Press);
} }
break; HandleButtonAction(action);
} break;
case Messages::HandleButtonTimerEvent: {
auto action = buttonHandler.HandleEvent(Controllers::ButtonHandler::Events::Timer);
HandleButtonAction(action);
} break;
case Messages::OnDisplayTaskSleeping: case Messages::OnDisplayTaskSleeping:
if (BootloaderVersion::IsValid()) { if (BootloaderVersion::IsValid()) {
// First versions of the bootloader do not expose their version and cannot initialize the SPI NOR FLASH // First versions of the bootloader do not expose their version and cannot initialize the SPI NOR FLASH
@ -398,9 +390,6 @@ void SystemTask::Work() {
case Messages::BatteryPercentageUpdated: case Messages::BatteryPercentageUpdated:
nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining()); nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining());
break; break;
case Messages::ReloadIdleTimer:
ReloadIdleTimer();
break;
default: default:
break; break;
@ -449,6 +438,35 @@ void SystemTask::UpdateMotion() {
} }
} }
void SystemTask::HandleButtonAction(Controllers::ButtonActions action) {
if (IsSleeping()) {
return;
}
ReloadIdleTimer();
using Actions = Controllers::ButtonActions;
switch (action) {
case Actions::Click:
if (!isGoingToSleep) {
displayApp.PushMessage(Applications::Display::Messages::ButtonPushed);
}
break;
case Actions::DoubleClick:
displayApp.PushMessage(Applications::Display::Messages::ButtonDoubleClicked);
break;
case Actions::LongPress:
displayApp.PushMessage(Applications::Display::Messages::ButtonLongPressed);
break;
case Actions::LongerPress:
displayApp.PushMessage(Applications::Display::Messages::ButtonLongerPressed);
break;
default:
break;
}
}
void SystemTask::GoToRunning() { void SystemTask::GoToRunning() {
if (isGoingToSleep or (not isSleeping) or isWakingUp) if (isGoingToSleep or (not isSleeping) or isWakingUp)
return; return;

View File

@ -21,6 +21,7 @@
#include "components/fs/FS.h" #include "components/fs/FS.h"
#include "touchhandler/TouchHandler.h" #include "touchhandler/TouchHandler.h"
#include "buttonhandler/ButtonHandler.h" #include "buttonhandler/ButtonHandler.h"
#include "buttonhandler/ButtonActions.h"
#ifdef PINETIME_IS_RECOVERY #ifdef PINETIME_IS_RECOVERY
#include "displayapp/DisplayAppRecovery.h" #include "displayapp/DisplayAppRecovery.h"
@ -138,6 +139,7 @@ namespace Pinetime {
TimerHandle_t measureBatteryTimer; TimerHandle_t measureBatteryTimer;
bool doNotGoToSleep = false; bool doNotGoToSleep = false;
void HandleButtonAction(Controllers::ButtonActions action);
void GoToRunning(); void GoToRunning();
void UpdateMotion(); void UpdateMotion();
bool stepCounterMustBeReset = false; bool stepCounterMustBeReset = false;