From d5dfa8087679b644c13e1d420b8ef2fc894b3b51 Mon Sep 17 00:00:00 2001
From: Riku Isokoski <riksu9000@gmail.com>
Date: Mon, 25 Oct 2021 12:53:14 +0300
Subject: [PATCH 1/5] Newer buttonhandler

---
 src/CMakeLists.txt                  |  3 +
 src/buttonhandler/ButtonHandler.cpp | 85 +++++++++++++++++++++++++++++
 src/buttonhandler/ButtonHandler.h   | 24 ++++++++
 src/displayapp/DisplayApp.cpp       | 14 +++++
 src/displayapp/Messages.h           |  3 +
 src/main.cpp                        | 24 ++++----
 src/systemtask/Messages.h           |  7 ++-
 src/systemtask/SystemTask.cpp       | 63 ++++++++++++++-------
 src/systemtask/SystemTask.h         |  7 ++-
 9 files changed, 194 insertions(+), 36 deletions(-)
 create mode 100644 src/buttonhandler/ButtonHandler.cpp
 create mode 100644 src/buttonhandler/ButtonHandler.h

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index a839e080..e727b2b0 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -507,6 +507,7 @@ list(APPEND SOURCE_FILES
         components/heartrate/Ptagc.cpp
         components/heartrate/HeartRateController.cpp
 
+        buttonhandler/ButtonHandler.cpp
         touchhandler/TouchHandler.cpp
         )
 
@@ -567,6 +568,7 @@ list(APPEND RECOVERY_SOURCE_FILES
         components/heartrate/Ptagc.cpp
         components/motor/MotorController.cpp
         components/fs/FS.cpp
+        buttonhandler/ButtonHandler.cpp
         touchhandler/TouchHandler.cpp
         )
 
@@ -681,6 +683,7 @@ set(INCLUDE_FILES
         components/heartrate/Ptagc.h
         components/heartrate/HeartRateController.h
         components/motor/MotorController.h
+        buttonhandler/ButtonHandler.h
         touchhandler/TouchHandler.h
         )
 
diff --git a/src/buttonhandler/ButtonHandler.cpp b/src/buttonhandler/ButtonHandler.cpp
new file mode 100644
index 00000000..997409e5
--- /dev/null
+++ b/src/buttonhandler/ButtonHandler.cpp
@@ -0,0 +1,85 @@
+#include "ButtonHandler.h"
+
+using namespace Pinetime::Controllers;
+
+void ButtonTimerCallback(TimerHandle_t xTimer) {
+  auto* buttonHandler = static_cast<ButtonHandler*>(pvTimerGetTimerID(xTimer));
+  buttonHandler->HandleEvent(ButtonHandler::Timer);
+}
+
+void ButtonHandler::Init(Pinetime::System::SystemTask* systemTask) {
+  this->systemTask = systemTask;
+  buttonTimer = xTimerCreate("buttonTimer", 0, pdFALSE, this, ButtonTimerCallback);
+}
+
+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);
+
+  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 == Press) {
+    buttonPressed = true;
+  } else if (event == Release) {
+    releaseTime = xTaskGetTickCount();
+    buttonPressed = false;
+  }
+
+  switch (state) {
+    case Idle:
+      if (event == Press) {
+        xTimerChangePeriod(buttonTimer, doubleClickTime, 0);
+        xTimerStart(buttonTimer, 0);
+        state = Pressed;
+      }
+      break;
+    case Pressed:
+      if (event == Press) {
+        if (xTaskGetTickCount() - releaseTime < doubleClickTime) {
+          systemTask->PushMessage(System::Messages::OnButtonDoubleClicked);
+          xTimerStop(buttonTimer, 0);
+          state = Idle;
+        }
+      } else if (event == Release) {
+        xTimerChangePeriod(buttonTimer, doubleClickTime, 0);
+        xTimerStart(buttonTimer, 0);
+      } else if (event == Timer) {
+        if (buttonPressed) {
+          xTimerChangePeriod(buttonTimer, longPressTime - doubleClickTime, 0);
+          xTimerStart(buttonTimer, 0);
+          state = Holding;
+        } else {
+          systemTask->PushMessage(System::Messages::OnButtonPushed);
+          state = Idle;
+        }
+      }
+      break;
+    case Holding:
+      if (event == Release) {
+        systemTask->PushMessage(System::Messages::OnButtonPushed);
+        xTimerStop(buttonTimer, 0);
+        state = Idle;
+      } else if (event == Timer) {
+        xTimerChangePeriod(buttonTimer, longerPressTime - longPressTime - doubleClickTime, 0);
+        xTimerStart(buttonTimer, 0);
+        systemTask->PushMessage(System::Messages::OnButtonLongPressed);
+        state = LongHeld;
+      }
+      break;
+    case LongHeld:
+      if (event == Release) {
+        xTimerStop(buttonTimer, 0);
+        state = Idle;
+      } else if (event == Timer) {
+        systemTask->PushMessage(System::Messages::OnButtonLongerPressed);
+        state = Idle;
+      }
+      break;
+  }
+}
diff --git a/src/buttonhandler/ButtonHandler.h b/src/buttonhandler/ButtonHandler.h
new file mode 100644
index 00000000..5d5b57e9
--- /dev/null
+++ b/src/buttonhandler/ButtonHandler.h
@@ -0,0 +1,24 @@
+#pragma once
+
+#include "systemtask/SystemTask.h"
+#include <FreeRTOS.h>
+#include <timers.h>
+
+namespace Pinetime {
+  namespace Controllers {
+    class ButtonHandler {
+    public:
+      enum events { Press, Release, Timer };
+      void Init(Pinetime::System::SystemTask* systemTask);
+      void HandleEvent(events event);
+
+    private:
+      Pinetime::System::SystemTask* systemTask = nullptr;
+      TickType_t releaseTime = 0;
+      TimerHandle_t buttonTimer;
+      bool buttonPressed = false;
+      enum states { Idle, Pressed, Holding, LongHeld };
+      states state = Idle;
+    };
+  }
+}
diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp
index abe5851e..13ee0045 100644
--- a/src/displayapp/DisplayApp.cpp
+++ b/src/displayapp/DisplayApp.cpp
@@ -260,6 +260,20 @@ void DisplayApp::Refresh() {
           }
         }
         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:
         LoadApp(Apps::FirmwareUpdate, DisplayApp::FullRefreshDirections::Down);
diff --git a/src/displayapp/Messages.h b/src/displayapp/Messages.h
index d48b646f..ab0a0608 100644
--- a/src/displayapp/Messages.h
+++ b/src/displayapp/Messages.h
@@ -9,6 +9,9 @@ namespace Pinetime {
         UpdateBleConnection,
         TouchEvent,
         ButtonPushed,
+        ButtonLongPressed,
+        ButtonLongerPressed,
+        ButtonDoubleClicked,
         NewNotification,
         TimerDone,
         BleFirmwareUpdateStarted,
diff --git a/src/main.cpp b/src/main.cpp
index fc772355..b942fd41 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -47,6 +47,7 @@
 #include "systemtask/SystemTask.h"
 #include "drivers/PinMap.h"
 #include "touchhandler/TouchHandler.h"
+#include "buttonhandler/ButtonHandler.h"
 
 #if NRF_LOG_ENABLED
   #include "logging/NrfLogger.h"
@@ -96,8 +97,6 @@ TimerHandle_t debounceTimer;
 TimerHandle_t debounceChargeTimer;
 Pinetime::Controllers::Battery batteryController;
 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::Applications::HeartRateTask heartRateApp(heartRateSensor, heartRateController);
@@ -110,6 +109,7 @@ Pinetime::Controllers::MotionController motionController;
 Pinetime::Controllers::TimerController timerController;
 Pinetime::Controllers::AlarmController alarmController {dateTimeController};
 Pinetime::Controllers::TouchHandler touchHandler(touchPanel, lvgl);
+Pinetime::Controllers::ButtonHandler buttonHandler;
 
 Pinetime::Controllers::FS fs {spiNorFlash};
 Pinetime::Controllers::Settings settingsController {fs};
@@ -153,7 +153,8 @@ Pinetime::System::SystemTask systemTask(spi,
                                         displayApp,
                                         heartRateApp,
                                         fs,
-                                        touchHandler);
+                                        touchHandler,
+                                        buttonHandler);
 
 /* Variable Declarations for variables in noinit SRAM 
    Increment NoInit_MagicValue upon adding variables to this area
@@ -176,11 +177,11 @@ 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) {
     xTimerStartFromISR(debounceChargeTimer, &xHigherPriorityTaskWoken);
     portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
-    return;
+  } else if (pin == Pinetime::PinMap::Button) {
+    // This activates on button release as well due to bouncing
+    xTimerStartFromISR(debounceTimer, &xHigherPriorityTaskWoken);
+    portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
   }
-
-  xTimerStartFromISR(debounceTimer, &xHigherPriorityTaskWoken);
-  portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
 }
 
 void DebounceTimerChargeCallback(TimerHandle_t xTimer) {
@@ -188,9 +189,8 @@ void DebounceTimerChargeCallback(TimerHandle_t xTimer) {
   systemTask.PushMessage(Pinetime::System::Messages::OnChargingEvent);
 }
 
-void DebounceTimerCallback(TimerHandle_t xTimer) {
-  xTimerStop(xTimer, 0);
-  systemTask.OnButtonPushed();
+void DebounceTimerCallback(TimerHandle_t /*unused*/) {
+  systemTask.PushMessage(Pinetime::System::Messages::HandleButtonEvent);
 }
 
 void SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQHandler(void) {
@@ -319,8 +319,8 @@ int main(void) {
   }
   nrf_gpio_cfg_default(Pinetime::PinMap::TwiScl);
 
-  debounceTimer = xTimerCreate("debounceTimer", 200, pdFALSE, (void*) 0, DebounceTimerCallback);
-  debounceChargeTimer = xTimerCreate("debounceTimerCharge", 200, pdFALSE, (void*) 0, DebounceTimerChargeCallback);
+  debounceTimer = xTimerCreate("debounceTimer", 10, pdFALSE, nullptr, DebounceTimerCallback);
+  debounceChargeTimer = xTimerCreate("debounceTimerCharge", 200, pdFALSE, nullptr, DebounceTimerChargeCallback);
 
   // retrieve version stored by bootloader
   Pinetime::BootloaderVersion::SetVersion(NRF_TIMER2->CC[0]);
diff --git a/src/systemtask/Messages.h b/src/systemtask/Messages.h
index 5aa218d2..b0bdbf31 100644
--- a/src/systemtask/Messages.h
+++ b/src/systemtask/Messages.h
@@ -15,12 +15,17 @@ namespace Pinetime {
         BleFirmwareUpdateStarted,
         BleFirmwareUpdateFinished,
         OnTouchEvent,
-        OnButtonEvent,
+        OnButtonPushed,
+        OnButtonLongPressed,
+        OnButtonLongerPressed,
+        OnButtonDoubleClicked,
+        HandleButtonEvent,
         OnDisplayTaskSleeping,
         EnableSleeping,
         DisableSleeping,
         OnNewDay,
         OnChargingEvent,
+        ReloadIdleTimer,
         SetOffAlarm,
         StopRinging,
         MeasureBatteryTimerExpired,
diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp
index e0a5907a..b7db0b9d 100644
--- a/src/systemtask/SystemTask.cpp
+++ b/src/systemtask/SystemTask.cpp
@@ -25,7 +25,6 @@
 #include "main.h"
 #include "BootErrors.h"
 
-
 #include <memory>
 
 using namespace Pinetime::System;
@@ -77,7 +76,8 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi,
                        Pinetime::Applications::DisplayApp& displayApp,
                        Pinetime::Applications::HeartRateTask& heartRateApp,
                        Pinetime::Controllers::FS& fs,
-                       Pinetime::Controllers::TouchHandler& touchHandler)
+                       Pinetime::Controllers::TouchHandler& touchHandler,
+                       Pinetime::Controllers::ButtonHandler& buttonHandler)
   : spi {spi},
     lcd {lcd},
     spiNorFlash {spiNorFlash},
@@ -101,8 +101,15 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi,
     heartRateApp(heartRateApp),
     fs {fs},
     touchHandler {touchHandler},
-    nimbleController(*this, bleController, dateTimeController, notificationManager,
-                     batteryController, spiNorFlash, heartRateController, motionController) {
+    buttonHandler {buttonHandler},
+    nimbleController(*this,
+                     bleController,
+                     dateTimeController,
+                     notificationManager,
+                     batteryController,
+                     spiNorFlash,
+                     heartRateController,
+                     motionController) {
 }
 
 void SystemTask::Start() {
@@ -163,6 +170,8 @@ void SystemTask::Work() {
   heartRateSensor.Disable();
   heartRateApp.Start();
 
+  buttonHandler.Init(this);
+
   // Button
   nrf_gpio_cfg_output(15);
   nrf_gpio_pin_set(15);
@@ -326,9 +335,32 @@ void SystemTask::Work() {
           ReloadIdleTimer();
           displayApp.PushMessage(Pinetime::Applications::Display::Messages::TouchEvent);
           break;
-        case Messages::OnButtonEvent:
-          ReloadIdleTimer();
-          displayApp.PushMessage(Pinetime::Applications::Display::Messages::ButtonPushed);
+        case Messages::OnButtonPushed:
+          if (!isSleeping && !isGoingToSleep) {
+            displayApp.PushMessage(Pinetime::Applications::Display::Messages::ButtonPushed);
+          }
+          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:
+          if (nrf_gpio_pin_read(Pinetime::PinMap::Button) == 0) {
+            buttonHandler.HandleEvent(Pinetime::Controllers::ButtonHandler::Release);
+          } else {
+            buttonHandler.HandleEvent(Pinetime::Controllers::ButtonHandler::Press);
+          }
           break;
         case Messages::OnDisplayTaskSleeping:
           if (BootloaderVersion::IsValid()) {
@@ -366,6 +398,9 @@ void SystemTask::Work() {
         case Messages::BatteryPercentageUpdated:
           nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining());
           break;
+        case Messages::ReloadIdleTimer:
+          ReloadIdleTimer();
+          break;
 
         default:
           break;
@@ -414,20 +449,6 @@ void SystemTask::UpdateMotion() {
   }
 }
 
-void SystemTask::OnButtonPushed() {
-  if (isGoingToSleep)
-    return;
-  if (!isSleeping) {
-    NRF_LOG_INFO("[systemtask] Button pushed");
-    PushMessage(Messages::OnButtonEvent);
-  } else {
-    if (!isWakingUp) {
-      NRF_LOG_INFO("[systemtask] Button pushed, waking up");
-      GoToRunning();
-    }
-  }
-}
-
 void SystemTask::GoToRunning() {
   if (isGoingToSleep or (not isSleeping) or isWakingUp)
     return;
diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h
index 879c1be8..9967f75c 100644
--- a/src/systemtask/SystemTask.h
+++ b/src/systemtask/SystemTask.h
@@ -20,6 +20,7 @@
 #include "components/alarm/AlarmController.h"
 #include "components/fs/FS.h"
 #include "touchhandler/TouchHandler.h"
+#include "buttonhandler/ButtonHandler.h"
 
 #ifdef PINETIME_IS_RECOVERY
   #include "displayapp/DisplayAppRecovery.h"
@@ -45,6 +46,7 @@ namespace Pinetime {
   }
   namespace Controllers {
     class TouchHandler;
+    class ButtonHandler;
   }
   namespace System {
     class SystemTask {
@@ -71,12 +73,12 @@ namespace Pinetime {
                  Pinetime::Applications::DisplayApp& displayApp,
                  Pinetime::Applications::HeartRateTask& heartRateApp,
                  Pinetime::Controllers::FS& fs,
-                 Pinetime::Controllers::TouchHandler& touchHandler);
+                 Pinetime::Controllers::TouchHandler& touchHandler,
+                 Pinetime::Controllers::ButtonHandler& buttonHandler);
 
       void Start();
       void PushMessage(Messages msg);
 
-      void OnButtonPushed();
       void OnTouchEvent();
 
       void OnIdle();
@@ -123,6 +125,7 @@ namespace Pinetime {
       Pinetime::Applications::HeartRateTask& heartRateApp;
       Pinetime::Controllers::FS& fs;
       Pinetime::Controllers::TouchHandler& touchHandler;
+      Pinetime::Controllers::ButtonHandler& buttonHandler;
       Pinetime::Controllers::NimbleController nimbleController;
 
       static void Process(void* instance);

From b19a2a760b74f27c8d3db262bf43437f722f74bd Mon Sep 17 00:00:00 2001
From: Riku Isokoski <riksu9000@gmail.com>
Date: Mon, 25 Oct 2021 13:40:43 +0300
Subject: [PATCH 2/5] 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<ButtonHandler*>(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:

From 351c60a13167c05dfdbb0516f84077a4cd6adeec Mon Sep 17 00:00:00 2001
From: Riku Isokoski <riksu9000@gmail.com>
Date: Mon, 25 Oct 2021 16:57:29 +0300
Subject: [PATCH 3/5] Return button action instead of pushing messages

---
 src/buttonhandler/ButtonActions.h   |  7 +++
 src/buttonhandler/ButtonHandler.cpp | 27 +++++------
 src/buttonhandler/ButtonHandler.h   |  4 +-
 src/systemtask/Messages.h           |  6 +--
 src/systemtask/SystemTask.cpp       | 70 ++++++++++++++++++-----------
 src/systemtask/SystemTask.h         |  2 +
 6 files changed, 66 insertions(+), 50 deletions(-)
 create mode 100644 src/buttonhandler/ButtonActions.h

diff --git a/src/buttonhandler/ButtonActions.h b/src/buttonhandler/ButtonActions.h
new file mode 100644
index 00000000..21be441b
--- /dev/null
+++ b/src/buttonhandler/ButtonActions.h
@@ -0,0 +1,7 @@
+#pragma once
+
+namespace Pinetime {
+  namespace Controllers {
+    enum class ButtonActions { None, Click, DoubleClick, LongPress, LongerPress };
+  }
+}
diff --git a/src/buttonhandler/ButtonHandler.cpp b/src/buttonhandler/ButtonHandler.cpp
index b6067d27..91e8bbd0 100644
--- a/src/buttonhandler/ButtonHandler.cpp
+++ b/src/buttonhandler/ButtonHandler.cpp
@@ -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;
 }
diff --git a/src/buttonhandler/ButtonHandler.h b/src/buttonhandler/ButtonHandler.h
index b4c36bda..44b20f19 100644
--- a/src/buttonhandler/ButtonHandler.h
+++ b/src/buttonhandler/ButtonHandler.h
@@ -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;
diff --git a/src/systemtask/Messages.h b/src/systemtask/Messages.h
index b0bdbf31..b7142704 100644
--- a/src/systemtask/Messages.h
+++ b/src/systemtask/Messages.h
@@ -15,17 +15,13 @@ namespace Pinetime {
         BleFirmwareUpdateStarted,
         BleFirmwareUpdateFinished,
         OnTouchEvent,
-        OnButtonPushed,
-        OnButtonLongPressed,
-        OnButtonLongerPressed,
-        OnButtonDoubleClicked,
         HandleButtonEvent,
+        HandleButtonTimerEvent,
         OnDisplayTaskSleeping,
         EnableSleeping,
         DisableSleeping,
         OnNewDay,
         OnChargingEvent,
-        ReloadIdleTimer,
         SetOffAlarm,
         StopRinging,
         MeasureBatteryTimerExpired,
diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp
index 8a4f894e..85cefb6f 100644
--- a/src/systemtask/SystemTask.cpp
+++ b/src/systemtask/SystemTask.cpp
@@ -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;
           }
-          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;
diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h
index 9967f75c..d6045e9c 100644
--- a/src/systemtask/SystemTask.h
+++ b/src/systemtask/SystemTask.h
@@ -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;

From 887c409b135bb2f21f2fb5ae70a4d8831049d14d Mon Sep 17 00:00:00 2001
From: Riku Isokoski <riksu9000@gmail.com>
Date: Mon, 25 Oct 2021 17:13:02 +0300
Subject: [PATCH 4/5] Only wake up on press. Fixes issue with longer press and
 sleep

---
 src/systemtask/SystemTask.cpp | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp
index 85cefb6f..51dbc3e3 100644
--- a/src/systemtask/SystemTask.cpp
+++ b/src/systemtask/SystemTask.cpp
@@ -337,15 +337,14 @@ void SystemTask::Work() {
           break;
         case Messages::HandleButtonEvent: {
           // This is for faster wakeup, sacrificing special longpress and doubleclick handling while sleeping
-          if (IsSleeping()) {
-            GoToRunning();
-            break;
-          }
-
           Controllers::ButtonActions action;
           if (nrf_gpio_pin_read(Pinetime::PinMap::Button) == 0) {
             action = buttonHandler.HandleEvent(Controllers::ButtonHandler::Events::Release);
           } else {
+            if (IsSleeping()) {
+              GoToRunning();
+              break;
+            }
             action = buttonHandler.HandleEvent(Controllers::ButtonHandler::Events::Press);
           }
           HandleButtonAction(action);

From 60a717b1a2272e61dfc4d297998da1c7672a8316 Mon Sep 17 00:00:00 2001
From: Riku Isokoski <riksu9000@gmail.com>
Date: Mon, 25 Oct 2021 17:45:48 +0300
Subject: [PATCH 5/5] Make it so special actions can be input while sleeping,
 like in #480

---
 src/systemtask/SystemTask.cpp | 12 ++++++++----
 src/systemtask/SystemTask.h   |  2 ++
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp
index 51dbc3e3..0a3f9951 100644
--- a/src/systemtask/SystemTask.cpp
+++ b/src/systemtask/SystemTask.cpp
@@ -336,16 +336,17 @@ void SystemTask::Work() {
           displayApp.PushMessage(Pinetime::Applications::Display::Messages::TouchEvent);
           break;
         case Messages::HandleButtonEvent: {
-          // This is for faster wakeup, sacrificing special longpress and doubleclick handling while sleeping
           Controllers::ButtonActions action;
           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;
             }
-            action = buttonHandler.HandleEvent(Controllers::ButtonHandler::Events::Press);
           }
           HandleButtonAction(action);
         } break;
@@ -448,7 +449,8 @@ void SystemTask::HandleButtonAction(Controllers::ButtonActions action) {
 
   switch (action) {
     case Actions::Click:
-      if (!isGoingToSleep) {
+      // If the first action after fast wakeup is a click, it should be ignored.
+      if (!fastWakeUpDone && !isGoingToSleep) {
         displayApp.PushMessage(Applications::Display::Messages::ButtonPushed);
       }
       break;
@@ -462,8 +464,10 @@ void SystemTask::HandleButtonAction(Controllers::ButtonActions action) {
       displayApp.PushMessage(Applications::Display::Messages::ButtonLongerPressed);
       break;
     default:
-      break;
+      return;
   }
+
+  fastWakeUpDone = false;
 }
 
 void SystemTask::GoToRunning() {
diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h
index d6045e9c..412878b1 100644
--- a/src/systemtask/SystemTask.h
+++ b/src/systemtask/SystemTask.h
@@ -140,6 +140,8 @@ namespace Pinetime {
       bool doNotGoToSleep = false;
 
       void HandleButtonAction(Controllers::ButtonActions action);
+      bool fastWakeUpDone = false;
+
       void GoToRunning();
       void UpdateMotion();
       bool stepCounterMustBeReset = false;