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

View File

@ -1,5 +1,6 @@
#pragma once
#include "ButtonActions.h"
#include "systemtask/SystemTask.h"
#include <FreeRTOS.h>
#include <timers.h>
@ -10,11 +11,10 @@ namespace Pinetime {
public:
enum class Events : uint8_t { Press, Release, Timer };
void Init(Pinetime::System::SystemTask* systemTask);
void HandleEvent(Events event);
ButtonActions 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;

View File

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

View File

@ -335,33 +335,25 @@ void SystemTask::Work() {
ReloadIdleTimer();
displayApp.PushMessage(Pinetime::Applications::Display::Messages::TouchEvent);
break;
case Messages::OnButtonPushed:
if (!isSleeping && !isGoingToSleep) {
displayApp.PushMessage(Pinetime::Applications::Display::Messages::ButtonPushed);
}
case Messages::HandleButtonEvent: {
// This is for faster wakeup, sacrificing special longpress and doubleclick handling while sleeping
if (IsSleeping()) {
GoToRunning();
break;
case Messages::OnButtonLongPressed:
if (!isSleeping) {
displayApp.PushMessage(Pinetime::Applications::Display::Messages::ButtonLongPressed);
}
break;
case Messages::OnButtonLongerPressed:
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:
Controllers::ButtonActions action;
if (nrf_gpio_pin_read(Pinetime::PinMap::Button) == 0) {
buttonHandler.HandleEvent(Pinetime::Controllers::ButtonHandler::Events::Release);
action = buttonHandler.HandleEvent(Controllers::ButtonHandler::Events::Release);
} 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:
if (BootloaderVersion::IsValid()) {
// 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:
nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining());
break;
case Messages::ReloadIdleTimer:
ReloadIdleTimer();
break;
default:
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() {
if (isGoingToSleep or (not isSleeping) or isWakingUp)
return;

View File

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