Merge pull request #782 from Riksu9000/newer_buttonhandler

Newer ButtonHandler
This commit is contained in:
JF 2021-11-06 10:55:37 +01:00 committed by GitHub
commit 4a5b5f954f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 211 additions and 33 deletions

View File

@ -507,6 +507,7 @@ list(APPEND SOURCE_FILES
components/heartrate/Ptagc.cpp components/heartrate/Ptagc.cpp
components/heartrate/HeartRateController.cpp components/heartrate/HeartRateController.cpp
buttonhandler/ButtonHandler.cpp
touchhandler/TouchHandler.cpp touchhandler/TouchHandler.cpp
) )
@ -567,6 +568,7 @@ list(APPEND RECOVERY_SOURCE_FILES
components/heartrate/Ptagc.cpp components/heartrate/Ptagc.cpp
components/motor/MotorController.cpp components/motor/MotorController.cpp
components/fs/FS.cpp components/fs/FS.cpp
buttonhandler/ButtonHandler.cpp
touchhandler/TouchHandler.cpp touchhandler/TouchHandler.cpp
) )
@ -681,6 +683,7 @@ set(INCLUDE_FILES
components/heartrate/Ptagc.h components/heartrate/Ptagc.h
components/heartrate/HeartRateController.h components/heartrate/HeartRateController.h
components/motor/MotorController.h components/motor/MotorController.h
buttonhandler/ButtonHandler.h
touchhandler/TouchHandler.h touchhandler/TouchHandler.h
) )

View File

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

View File

@ -0,0 +1,78 @@
#include "ButtonHandler.h"
using namespace Pinetime::Controllers;
void ButtonTimerCallback(TimerHandle_t xTimer) {
auto* sysTask = static_cast<Pinetime::System::SystemTask*>(pvTimerGetTimerID(xTimer));
sysTask->PushMessage(Pinetime::System::Messages::HandleButtonTimerEvent);
}
void ButtonHandler::Init(Pinetime::System::SystemTask* systemTask) {
buttonTimer = xTimerCreate("buttonTimer", 0, pdFALSE, systemTask, ButtonTimerCallback);
}
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 (event == Events::Press) {
buttonPressed = true;
} else if (event == Events::Release) {
releaseTime = xTaskGetTickCount();
buttonPressed = false;
}
switch (state) {
case States::Idle:
if (event == Events::Press) {
xTimerChangePeriod(buttonTimer, doubleClickTime, 0);
xTimerStart(buttonTimer, 0);
state = States::Pressed;
}
break;
case States::Pressed:
if (event == Events::Press) {
if (xTaskGetTickCount() - releaseTime < doubleClickTime) {
xTimerStop(buttonTimer, 0);
state = States::Idle;
return ButtonActions::DoubleClick;
}
} else if (event == Events::Release) {
xTimerChangePeriod(buttonTimer, doubleClickTime, 0);
xTimerStart(buttonTimer, 0);
} else if (event == Events::Timer) {
if (buttonPressed) {
xTimerChangePeriod(buttonTimer, longPressTime - doubleClickTime, 0);
xTimerStart(buttonTimer, 0);
state = States::Holding;
} else {
state = States::Idle;
return ButtonActions::Click;
}
}
break;
case States::Holding:
if (event == Events::Release) {
xTimerStop(buttonTimer, 0);
state = States::Idle;
return ButtonActions::Click;
} else if (event == Events::Timer) {
xTimerChangePeriod(buttonTimer, longerPressTime - longPressTime - doubleClickTime, 0);
xTimerStart(buttonTimer, 0);
state = States::LongHeld;
return ButtonActions::LongPress;
}
break;
case States::LongHeld:
if (event == Events::Release) {
xTimerStop(buttonTimer, 0);
state = States::Idle;
} else if (event == Events::Timer) {
state = States::Idle;
return ButtonActions::LongerPress;
}
break;
}
return ButtonActions::None;
}

View File

@ -0,0 +1,24 @@
#pragma once
#include "ButtonActions.h"
#include "systemtask/SystemTask.h"
#include <FreeRTOS.h>
#include <timers.h>
namespace Pinetime {
namespace Controllers {
class ButtonHandler {
public:
enum class Events : uint8_t { Press, Release, Timer };
void Init(Pinetime::System::SystemTask* systemTask);
ButtonActions HandleEvent(Events event);
private:
enum class States : uint8_t { Idle, Pressed, Holding, LongHeld };
TickType_t releaseTime = 0;
TimerHandle_t buttonTimer;
bool buttonPressed = false;
States state = States::Idle;
};
}
}

View File

@ -260,6 +260,20 @@ void DisplayApp::Refresh() {
} }
} }
break; break;
case Messages::ButtonLongPressed:
if (currentApp != Apps::Clock) {
LoadApp(Apps::Clock, DisplayApp::FullRefreshDirections::Down);
}
break;
case Messages::ButtonLongerPressed:
// Create reboot app and open it instead
LoadApp(Apps::SysInfo, DisplayApp::FullRefreshDirections::Up);
break;
case Messages::ButtonDoubleClicked:
if (currentApp != Apps::Notifications && currentApp != Apps::NotificationsPreview) {
LoadApp(Apps::Notifications, DisplayApp::FullRefreshDirections::Down);
}
break;
case Messages::BleFirmwareUpdateStarted: case Messages::BleFirmwareUpdateStarted:
LoadApp(Apps::FirmwareUpdate, DisplayApp::FullRefreshDirections::Down); LoadApp(Apps::FirmwareUpdate, DisplayApp::FullRefreshDirections::Down);

View File

@ -9,6 +9,9 @@ namespace Pinetime {
UpdateBleConnection, UpdateBleConnection,
TouchEvent, TouchEvent,
ButtonPushed, ButtonPushed,
ButtonLongPressed,
ButtonLongerPressed,
ButtonDoubleClicked,
NewNotification, NewNotification,
TimerDone, TimerDone,
BleFirmwareUpdateStarted, BleFirmwareUpdateStarted,

View File

@ -47,6 +47,7 @@
#include "systemtask/SystemTask.h" #include "systemtask/SystemTask.h"
#include "drivers/PinMap.h" #include "drivers/PinMap.h"
#include "touchhandler/TouchHandler.h" #include "touchhandler/TouchHandler.h"
#include "buttonhandler/ButtonHandler.h"
#if NRF_LOG_ENABLED #if NRF_LOG_ENABLED
#include "logging/NrfLogger.h" #include "logging/NrfLogger.h"
@ -96,8 +97,6 @@ TimerHandle_t debounceTimer;
TimerHandle_t debounceChargeTimer; TimerHandle_t debounceChargeTimer;
Pinetime::Controllers::Battery batteryController; Pinetime::Controllers::Battery batteryController;
Pinetime::Controllers::Ble bleController; Pinetime::Controllers::Ble bleController;
static constexpr uint8_t pinTouchIrq = Pinetime::PinMap::Cst816sIrq;
static constexpr uint8_t pinPowerPresentIrq = Pinetime::PinMap::PowerPresent;
Pinetime::Controllers::HeartRateController heartRateController; Pinetime::Controllers::HeartRateController heartRateController;
Pinetime::Applications::HeartRateTask heartRateApp(heartRateSensor, heartRateController); Pinetime::Applications::HeartRateTask heartRateApp(heartRateSensor, heartRateController);
@ -110,6 +109,7 @@ Pinetime::Controllers::MotionController motionController;
Pinetime::Controllers::TimerController timerController; Pinetime::Controllers::TimerController timerController;
Pinetime::Controllers::AlarmController alarmController {dateTimeController}; Pinetime::Controllers::AlarmController alarmController {dateTimeController};
Pinetime::Controllers::TouchHandler touchHandler(touchPanel, lvgl); Pinetime::Controllers::TouchHandler touchHandler(touchPanel, lvgl);
Pinetime::Controllers::ButtonHandler buttonHandler;
Pinetime::Controllers::FS fs {spiNorFlash}; Pinetime::Controllers::FS fs {spiNorFlash};
Pinetime::Controllers::Settings settingsController {fs}; Pinetime::Controllers::Settings settingsController {fs};
@ -153,7 +153,8 @@ Pinetime::System::SystemTask systemTask(spi,
displayApp, displayApp,
heartRateApp, heartRateApp,
fs, fs,
touchHandler); touchHandler,
buttonHandler);
/* Variable Declarations for variables in noinit SRAM /* Variable Declarations for variables in noinit SRAM
Increment NoInit_MagicValue upon adding variables to this area Increment NoInit_MagicValue upon adding variables to this area
@ -176,11 +177,10 @@ void nrfx_gpiote_evt_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action
if (pin == Pinetime::PinMap::PowerPresent and action == NRF_GPIOTE_POLARITY_TOGGLE) { if (pin == Pinetime::PinMap::PowerPresent and action == NRF_GPIOTE_POLARITY_TOGGLE) {
xTimerStartFromISR(debounceChargeTimer, &xHigherPriorityTaskWoken); xTimerStartFromISR(debounceChargeTimer, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken); portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
return; } else if (pin == Pinetime::PinMap::Button) {
}
xTimerStartFromISR(debounceTimer, &xHigherPriorityTaskWoken); xTimerStartFromISR(debounceTimer, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken); portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
} }
void DebounceTimerChargeCallback(TimerHandle_t xTimer) { void DebounceTimerChargeCallback(TimerHandle_t xTimer) {
@ -188,9 +188,8 @@ void DebounceTimerChargeCallback(TimerHandle_t xTimer) {
systemTask.PushMessage(Pinetime::System::Messages::OnChargingEvent); systemTask.PushMessage(Pinetime::System::Messages::OnChargingEvent);
} }
void DebounceTimerCallback(TimerHandle_t xTimer) { void DebounceTimerCallback(TimerHandle_t /*unused*/) {
xTimerStop(xTimer, 0); systemTask.PushMessage(Pinetime::System::Messages::HandleButtonEvent);
systemTask.OnButtonPushed();
} }
void SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQHandler(void) { void SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQHandler(void) {
@ -319,8 +318,8 @@ int main(void) {
} }
nrf_gpio_cfg_default(Pinetime::PinMap::TwiScl); nrf_gpio_cfg_default(Pinetime::PinMap::TwiScl);
debounceTimer = xTimerCreate("debounceTimer", 200, pdFALSE, (void*) 0, DebounceTimerCallback); debounceTimer = xTimerCreate("debounceTimer", 10, pdFALSE, nullptr, DebounceTimerCallback);
debounceChargeTimer = xTimerCreate("debounceTimerCharge", 200, pdFALSE, (void*) 0, DebounceTimerChargeCallback); debounceChargeTimer = xTimerCreate("debounceTimerCharge", 200, pdFALSE, nullptr, DebounceTimerChargeCallback);
// retrieve version stored by bootloader // retrieve version stored by bootloader
Pinetime::BootloaderVersion::SetVersion(NRF_TIMER2->CC[0]); Pinetime::BootloaderVersion::SetVersion(NRF_TIMER2->CC[0]);

View File

@ -15,7 +15,8 @@ namespace Pinetime {
BleFirmwareUpdateStarted, BleFirmwareUpdateStarted,
BleFirmwareUpdateFinished, BleFirmwareUpdateFinished,
OnTouchEvent, OnTouchEvent,
OnButtonEvent, HandleButtonEvent,
HandleButtonTimerEvent,
OnDisplayTaskSleeping, OnDisplayTaskSleeping,
EnableSleeping, EnableSleeping,
DisableSleeping, DisableSleeping,

View File

@ -25,7 +25,6 @@
#include "main.h" #include "main.h"
#include "BootErrors.h" #include "BootErrors.h"
#include <memory> #include <memory>
using namespace Pinetime::System; using namespace Pinetime::System;
@ -77,7 +76,8 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi,
Pinetime::Applications::DisplayApp& displayApp, Pinetime::Applications::DisplayApp& displayApp,
Pinetime::Applications::HeartRateTask& heartRateApp, Pinetime::Applications::HeartRateTask& heartRateApp,
Pinetime::Controllers::FS& fs, Pinetime::Controllers::FS& fs,
Pinetime::Controllers::TouchHandler& touchHandler) Pinetime::Controllers::TouchHandler& touchHandler,
Pinetime::Controllers::ButtonHandler& buttonHandler)
: spi {spi}, : spi {spi},
lcd {lcd}, lcd {lcd},
spiNorFlash {spiNorFlash}, spiNorFlash {spiNorFlash},
@ -101,8 +101,15 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi,
heartRateApp(heartRateApp), heartRateApp(heartRateApp),
fs {fs}, fs {fs},
touchHandler {touchHandler}, touchHandler {touchHandler},
nimbleController(*this, bleController, dateTimeController, notificationManager, buttonHandler {buttonHandler},
batteryController, spiNorFlash, heartRateController, motionController) { nimbleController(*this,
bleController,
dateTimeController,
notificationManager,
batteryController,
spiNorFlash,
heartRateController,
motionController) {
} }
void SystemTask::Start() { void SystemTask::Start() {
@ -163,6 +170,8 @@ void SystemTask::Work() {
heartRateSensor.Disable(); heartRateSensor.Disable();
heartRateApp.Start(); heartRateApp.Start();
buttonHandler.Init(this);
// Button // Button
nrf_gpio_cfg_output(15); nrf_gpio_cfg_output(15);
nrf_gpio_pin_set(15); nrf_gpio_pin_set(15);
@ -326,10 +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::OnButtonEvent: case Messages::HandleButtonEvent: {
ReloadIdleTimer(); Controllers::ButtonActions action;
displayApp.PushMessage(Pinetime::Applications::Display::Messages::ButtonPushed); if (nrf_gpio_pin_read(Pinetime::PinMap::Button) == 0) {
action = buttonHandler.HandleEvent(Controllers::ButtonHandler::Events::Release);
} else {
action = buttonHandler.HandleEvent(Controllers::ButtonHandler::Events::Press);
// This is for faster wakeup, sacrificing special longpress and doubleclick handling while sleeping
if (IsSleeping()) {
fastWakeUpDone = true;
GoToRunning();
break; 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
@ -414,18 +438,36 @@ void SystemTask::UpdateMotion() {
} }
} }
void SystemTask::OnButtonPushed() { void SystemTask::HandleButtonAction(Controllers::ButtonActions action) {
if (isGoingToSleep) if (IsSleeping()) {
return; return;
if (!isSleeping) {
NRF_LOG_INFO("[systemtask] Button pushed");
PushMessage(Messages::OnButtonEvent);
} else {
if (!isWakingUp) {
NRF_LOG_INFO("[systemtask] Button pushed, waking up");
GoToRunning();
} }
ReloadIdleTimer();
using Actions = Controllers::ButtonActions;
switch (action) {
case Actions::Click:
// If the first action after fast wakeup is a click, it should be ignored.
if (!fastWakeUpDone && !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:
return;
}
fastWakeUpDone = false;
} }
void SystemTask::GoToRunning() { void SystemTask::GoToRunning() {

View File

@ -20,6 +20,8 @@
#include "components/alarm/AlarmController.h" #include "components/alarm/AlarmController.h"
#include "components/fs/FS.h" #include "components/fs/FS.h"
#include "touchhandler/TouchHandler.h" #include "touchhandler/TouchHandler.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"
@ -45,6 +47,7 @@ namespace Pinetime {
} }
namespace Controllers { namespace Controllers {
class TouchHandler; class TouchHandler;
class ButtonHandler;
} }
namespace System { namespace System {
class SystemTask { class SystemTask {
@ -71,12 +74,12 @@ namespace Pinetime {
Pinetime::Applications::DisplayApp& displayApp, Pinetime::Applications::DisplayApp& displayApp,
Pinetime::Applications::HeartRateTask& heartRateApp, Pinetime::Applications::HeartRateTask& heartRateApp,
Pinetime::Controllers::FS& fs, Pinetime::Controllers::FS& fs,
Pinetime::Controllers::TouchHandler& touchHandler); Pinetime::Controllers::TouchHandler& touchHandler,
Pinetime::Controllers::ButtonHandler& buttonHandler);
void Start(); void Start();
void PushMessage(Messages msg); void PushMessage(Messages msg);
void OnButtonPushed();
void OnTouchEvent(); void OnTouchEvent();
void OnIdle(); void OnIdle();
@ -123,6 +126,7 @@ namespace Pinetime {
Pinetime::Applications::HeartRateTask& heartRateApp; Pinetime::Applications::HeartRateTask& heartRateApp;
Pinetime::Controllers::FS& fs; Pinetime::Controllers::FS& fs;
Pinetime::Controllers::TouchHandler& touchHandler; Pinetime::Controllers::TouchHandler& touchHandler;
Pinetime::Controllers::ButtonHandler& buttonHandler;
Pinetime::Controllers::NimbleController nimbleController; Pinetime::Controllers::NimbleController nimbleController;
static void Process(void* instance); static void Process(void* instance);
@ -135,6 +139,9 @@ namespace Pinetime {
TimerHandle_t measureBatteryTimer; TimerHandle_t measureBatteryTimer;
bool doNotGoToSleep = false; bool doNotGoToSleep = false;
void HandleButtonAction(Controllers::ButtonActions action);
bool fastWakeUpDone = false;
void GoToRunning(); void GoToRunning();
void UpdateMotion(); void UpdateMotion();
bool stepCounterMustBeReset = false; bool stepCounterMustBeReset = false;