2020-02-23 13:44:39 +01:00
|
|
|
#include "SystemTask.h"
|
2020-11-15 15:05:51 +01:00
|
|
|
#define min // workaround: nimble's min/max macros conflict with libstdc++
|
|
|
|
#define max
|
2020-04-19 20:44:59 +02:00
|
|
|
#include <host/ble_gap.h>
|
2020-11-15 15:05:51 +01:00
|
|
|
#include <host/ble_gatt.h>
|
|
|
|
#include <host/ble_hs_adv.h>
|
2020-04-19 20:44:59 +02:00
|
|
|
#include <host/util/util.h>
|
2020-11-15 15:05:51 +01:00
|
|
|
#include <nimble/hci_common.h>
|
|
|
|
#undef max
|
|
|
|
#undef min
|
|
|
|
#include <hal/nrf_rtc.h>
|
|
|
|
#include <libraries/gpiote/app_gpiote.h>
|
|
|
|
#include <libraries/log/nrf_log.h>
|
|
|
|
|
|
|
|
#include "BootloaderVersion.h"
|
|
|
|
#include "components/ble/BleController.h"
|
|
|
|
#include "drivers/Cst816s.h"
|
|
|
|
#include "drivers/St7789.h"
|
|
|
|
#include "drivers/InternalFlash.h"
|
|
|
|
#include "drivers/SpiMaster.h"
|
|
|
|
#include "drivers/SpiNorFlash.h"
|
|
|
|
#include "drivers/TwiMaster.h"
|
2021-01-10 17:57:26 +01:00
|
|
|
#include "drivers/Hrs3300.h"
|
2020-10-04 14:11:21 +02:00
|
|
|
#include "main.h"
|
2020-04-19 21:26:09 +02:00
|
|
|
|
2021-03-16 13:43:50 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2020-02-23 13:44:39 +01:00
|
|
|
using namespace Pinetime::System;
|
|
|
|
|
2021-06-10 21:20:27 +02:00
|
|
|
namespace {
|
|
|
|
static inline bool in_isr(void) {
|
|
|
|
return (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) != 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-24 20:29:10 +02:00
|
|
|
void DimTimerCallback(TimerHandle_t xTimer) {
|
|
|
|
|
|
|
|
NRF_LOG_INFO("DimTimerCallback");
|
|
|
|
auto sysTask = static_cast<SystemTask*>(pvTimerGetTimerID(xTimer));
|
|
|
|
sysTask->OnDim();
|
|
|
|
}
|
|
|
|
|
2020-06-01 20:40:11 +02:00
|
|
|
void IdleTimerCallback(TimerHandle_t xTimer) {
|
2020-06-13 17:33:49 +02:00
|
|
|
|
|
|
|
NRF_LOG_INFO("IdleTimerCallback");
|
2021-04-18 19:28:14 +02:00
|
|
|
auto sysTask = static_cast<SystemTask*>(pvTimerGetTimerID(xTimer));
|
2020-06-01 20:40:11 +02:00
|
|
|
sysTask->OnIdle();
|
|
|
|
}
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
SystemTask::SystemTask(Drivers::SpiMaster& spi,
|
|
|
|
Drivers::St7789& lcd,
|
2020-07-19 20:30:44 +02:00
|
|
|
Pinetime::Drivers::SpiNorFlash& spiNorFlash,
|
2021-04-18 19:28:14 +02:00
|
|
|
Drivers::TwiMaster& twiMaster,
|
|
|
|
Drivers::Cst816S& touchPanel,
|
|
|
|
Components::LittleVgl& lvgl,
|
|
|
|
Controllers::Battery& batteryController,
|
|
|
|
Controllers::Ble& bleController,
|
2021-06-06 15:56:03 +02:00
|
|
|
Controllers::DateTime& dateTimeController,
|
|
|
|
Controllers::TimerController& timerController,
|
|
|
|
Drivers::Watchdog& watchdog,
|
|
|
|
Pinetime::Controllers::NotificationManager& notificationManager,
|
2021-01-25 16:47:52 +01:00
|
|
|
Pinetime::Controllers::MotorController& motorController,
|
2021-02-24 20:40:24 +01:00
|
|
|
Pinetime::Drivers::Hrs3300& heartRateSensor,
|
2021-06-06 15:56:03 +02:00
|
|
|
Pinetime::Controllers::MotionController& motionController,
|
2021-03-31 19:47:27 +02:00
|
|
|
Pinetime::Drivers::Bma421& motionSensor,
|
2021-06-06 15:56:03 +02:00
|
|
|
Controllers::Settings& settingsController,
|
|
|
|
Pinetime::Controllers::HeartRateController& heartRateController,
|
|
|
|
Pinetime::Applications::DisplayApp& displayApp,
|
2021-07-11 15:06:06 +02:00
|
|
|
Pinetime::Applications::HeartRateTask& heartRateApp,
|
2021-07-16 00:49:20 +02:00
|
|
|
Pinetime::Controllers::FS& fs,
|
|
|
|
Pinetime::Controllers::TouchHandler& touchHandler)
|
2021-04-18 19:28:14 +02:00
|
|
|
: spi {spi},
|
|
|
|
lcd {lcd},
|
|
|
|
spiNorFlash {spiNorFlash},
|
|
|
|
twiMaster {twiMaster},
|
|
|
|
touchPanel {touchPanel},
|
|
|
|
lvgl {lvgl},
|
|
|
|
batteryController {batteryController},
|
|
|
|
bleController {bleController},
|
2021-06-06 15:56:03 +02:00
|
|
|
dateTimeController {dateTimeController},
|
|
|
|
timerController {timerController},
|
|
|
|
watchdog {watchdog},
|
2021-07-16 00:49:20 +02:00
|
|
|
notificationManager {notificationManager},
|
2021-04-18 19:28:14 +02:00
|
|
|
motorController {motorController},
|
|
|
|
heartRateSensor {heartRateSensor},
|
|
|
|
motionSensor {motionSensor},
|
|
|
|
settingsController {settingsController},
|
2021-07-16 00:49:20 +02:00
|
|
|
heartRateController {heartRateController},
|
|
|
|
motionController {motionController},
|
|
|
|
displayApp {displayApp},
|
2021-07-11 15:06:06 +02:00
|
|
|
heartRateApp(heartRateApp),
|
2021-07-16 00:49:20 +02:00
|
|
|
fs {fs},
|
|
|
|
touchHandler {touchHandler},
|
2021-07-11 15:06:06 +02:00
|
|
|
nimbleController(*this, bleController, dateTimeController, notificationManager, batteryController, spiNorFlash, heartRateController) {
|
2020-02-23 13:44:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SystemTask::Start() {
|
2021-06-12 10:58:28 +02:00
|
|
|
systemTasksMsgQueue = xQueueCreate(10, 1);
|
2020-02-23 16:14:03 +01:00
|
|
|
if (pdPASS != xTaskCreate(SystemTask::Process, "MAIN", 350, this, 0, &taskHandle))
|
2020-02-23 13:44:39 +01:00
|
|
|
APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
|
|
|
|
}
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
void SystemTask::Process(void* instance) {
|
|
|
|
auto* app = static_cast<SystemTask*>(instance);
|
2020-10-02 20:45:21 +02:00
|
|
|
NRF_LOG_INFO("systemtask task started!");
|
2020-02-23 13:44:39 +01:00
|
|
|
app->Work();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SystemTask::Work() {
|
2020-06-27 16:11:42 +02:00
|
|
|
watchdog.Setup(7);
|
|
|
|
watchdog.Start();
|
2020-02-23 21:09:11 +01:00
|
|
|
NRF_LOG_INFO("Last reset reason : %s", Pinetime::Drivers::Watchdog::ResetReasonToString(watchdog.ResetReason()));
|
2020-02-23 13:44:39 +01:00
|
|
|
APP_GPIOTE_INIT(2);
|
2020-04-19 20:44:59 +02:00
|
|
|
|
2021-05-20 20:43:54 +02:00
|
|
|
app_timer_init();
|
2021-07-16 00:49:20 +02:00
|
|
|
|
2020-05-11 18:50:37 +02:00
|
|
|
spi.Init();
|
|
|
|
spiNorFlash.Init();
|
2020-09-20 14:31:26 +02:00
|
|
|
spiNorFlash.Wakeup();
|
2021-07-16 00:49:20 +02:00
|
|
|
|
2021-07-11 15:06:06 +02:00
|
|
|
fs.Init();
|
|
|
|
|
2020-04-19 21:26:09 +02:00
|
|
|
nimbleController.Init();
|
|
|
|
nimbleController.StartAdvertising();
|
2020-02-23 13:44:39 +01:00
|
|
|
lcd.Init();
|
2020-05-11 18:50:37 +02:00
|
|
|
|
2020-07-19 20:30:44 +02:00
|
|
|
twiMaster.Init();
|
2020-02-23 13:44:39 +01:00
|
|
|
touchPanel.Init();
|
2021-06-06 15:56:03 +02:00
|
|
|
dateTimeController.Register(this);
|
2020-02-23 13:44:39 +01:00
|
|
|
batteryController.Init();
|
2021-02-05 15:43:20 +01:00
|
|
|
motorController.Init();
|
2021-04-08 20:07:24 +02:00
|
|
|
motionSensor.SoftReset();
|
2021-06-06 15:56:03 +02:00
|
|
|
timerController.Register(this);
|
2021-05-20 20:43:54 +02:00
|
|
|
timerController.Init();
|
2021-02-14 14:19:30 +01:00
|
|
|
|
2021-04-08 20:07:24 +02:00
|
|
|
// Reset the TWI device because the motion sensor chip most probably crashed it...
|
|
|
|
twiMaster.Sleep();
|
|
|
|
twiMaster.Init();
|
2021-02-24 20:40:24 +01:00
|
|
|
|
2021-04-08 20:07:24 +02:00
|
|
|
motionSensor.Init();
|
2021-06-19 20:27:59 +02:00
|
|
|
motionController.Init(motionSensor.DeviceType());
|
2021-04-08 20:07:24 +02:00
|
|
|
settingsController.Init();
|
2020-02-23 13:44:39 +01:00
|
|
|
|
2021-06-06 15:56:03 +02:00
|
|
|
displayApp.Register(this);
|
|
|
|
displayApp.Start();
|
|
|
|
|
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel);
|
2020-02-23 13:44:39 +01:00
|
|
|
|
2021-01-10 17:57:26 +01:00
|
|
|
heartRateSensor.Init();
|
|
|
|
heartRateSensor.Disable();
|
2021-06-06 15:56:03 +02:00
|
|
|
heartRateApp.Start();
|
2021-01-10 17:57:26 +01:00
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
nrf_gpio_cfg_sense_input(pinButton, (nrf_gpio_pin_pull_t) GPIO_PIN_CNF_PULL_Pulldown, (nrf_gpio_pin_sense_t) GPIO_PIN_CNF_SENSE_High);
|
2020-02-23 13:44:39 +01:00
|
|
|
nrf_gpio_cfg_output(15);
|
|
|
|
nrf_gpio_pin_set(15);
|
|
|
|
|
|
|
|
nrfx_gpiote_in_config_t pinConfig;
|
|
|
|
pinConfig.skip_gpio_setup = true;
|
|
|
|
pinConfig.hi_accuracy = false;
|
|
|
|
pinConfig.is_watcher = false;
|
2021-04-18 19:28:14 +02:00
|
|
|
pinConfig.sense = (nrf_gpiote_polarity_t) NRF_GPIOTE_POLARITY_HITOLO;
|
|
|
|
pinConfig.pull = (nrf_gpio_pin_pull_t) GPIO_PIN_CNF_PULL_Pulldown;
|
2020-02-23 13:44:39 +01:00
|
|
|
|
|
|
|
nrfx_gpiote_in_init(pinButton, &pinConfig, nrfx_gpiote_evt_handler);
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
nrf_gpio_cfg_sense_input(pinTouchIrq, (nrf_gpio_pin_pull_t) GPIO_PIN_CNF_PULL_Pullup, (nrf_gpio_pin_sense_t) GPIO_PIN_CNF_SENSE_Low);
|
2020-02-23 13:44:39 +01:00
|
|
|
|
|
|
|
pinConfig.skip_gpio_setup = true;
|
|
|
|
pinConfig.hi_accuracy = false;
|
|
|
|
pinConfig.is_watcher = false;
|
2021-04-18 19:28:14 +02:00
|
|
|
pinConfig.sense = (nrf_gpiote_polarity_t) NRF_GPIOTE_POLARITY_HITOLO;
|
|
|
|
pinConfig.pull = (nrf_gpio_pin_pull_t) GPIO_PIN_CNF_PULL_Pullup;
|
2020-02-23 13:44:39 +01:00
|
|
|
|
|
|
|
nrfx_gpiote_in_init(pinTouchIrq, &pinConfig, nrfx_gpiote_evt_handler);
|
|
|
|
|
2021-05-16 21:13:22 +02:00
|
|
|
pinConfig.sense = NRF_GPIOTE_POLARITY_TOGGLE;
|
|
|
|
pinConfig.pull = NRF_GPIO_PIN_NOPULL;
|
|
|
|
pinConfig.is_watcher = false;
|
|
|
|
pinConfig.hi_accuracy = false;
|
|
|
|
pinConfig.skip_gpio_setup = true;
|
|
|
|
nrfx_gpiote_in_init(pinPowerPresentIrq, &pinConfig, nrfx_gpiote_evt_handler);
|
|
|
|
|
2021-05-17 09:52:31 +02:00
|
|
|
if (nrf_gpio_pin_read(pinPowerPresentIrq)) {
|
|
|
|
nrf_gpio_cfg_sense_input(pinPowerPresentIrq, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_SENSE_LOW);
|
|
|
|
} else {
|
|
|
|
nrf_gpio_cfg_sense_input(pinPowerPresentIrq, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_SENSE_HIGH);
|
|
|
|
}
|
|
|
|
|
2021-07-24 20:29:10 +02:00
|
|
|
idleTimer = xTimerCreate("idleTimer", pdMS_TO_TICKS(2000), pdFALSE, this, IdleTimerCallback);
|
|
|
|
dimTimer = xTimerCreate("dimTimer", pdMS_TO_TICKS(settingsController.GetScreenTimeOut() - 2000), pdFALSE, this, DimTimerCallback);
|
|
|
|
xTimerStart(dimTimer, 0);
|
2020-02-23 13:44:39 +01:00
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
// Suppress endless loop diagnostic
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma ide diagnostic ignored "EndlessLoop"
|
|
|
|
while (true) {
|
2021-03-31 19:47:27 +02:00
|
|
|
UpdateMotion();
|
|
|
|
|
2020-02-23 13:44:39 +01:00
|
|
|
uint8_t msg;
|
2021-03-31 19:47:27 +02:00
|
|
|
if (xQueueReceive(systemTasksMsgQueue, &msg, 100)) {
|
2021-04-18 19:28:14 +02:00
|
|
|
|
2020-11-03 03:14:28 +01:00
|
|
|
batteryController.Update();
|
2021-05-16 21:13:22 +02:00
|
|
|
// the battery does not emit events when changing charge levels, so we piggyback
|
|
|
|
// on any system event to read and update the current values
|
2021-04-16 17:15:38 +02:00
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
Messages message = static_cast<Messages>(msg);
|
|
|
|
switch (message) {
|
2021-04-04 04:08:51 +02:00
|
|
|
case Messages::EnableSleeping:
|
2021-07-22 21:57:45 +02:00
|
|
|
// Make sure that exiting an app doesn't enable sleeping,
|
|
|
|
// if the exiting was caused by a firmware update
|
|
|
|
if (!bleController.IsFirmwareUpdating()) {
|
|
|
|
doNotGoToSleep = false;
|
|
|
|
}
|
2021-04-18 19:28:14 +02:00
|
|
|
break;
|
2021-04-04 04:08:51 +02:00
|
|
|
case Messages::DisableSleeping:
|
|
|
|
doNotGoToSleep = true;
|
2021-04-18 19:28:14 +02:00
|
|
|
break;
|
2021-04-04 04:08:51 +02:00
|
|
|
case Messages::UpdateTimeOut:
|
2021-07-24 20:29:10 +02:00
|
|
|
xTimerChangePeriod(dimTimer, pdMS_TO_TICKS(settingsController.GetScreenTimeOut() - 2000), 0);
|
2021-04-18 19:28:14 +02:00
|
|
|
break;
|
2020-06-01 20:40:11 +02:00
|
|
|
case Messages::GoToRunning:
|
2020-09-13 21:26:44 +02:00
|
|
|
spi.Wakeup();
|
|
|
|
|
2021-04-04 14:51:22 +02:00
|
|
|
// Double Tap needs the touch screen to be in normal mode
|
2021-07-14 20:51:51 +02:00
|
|
|
if (!settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) {
|
2021-04-04 14:51:22 +02:00
|
|
|
touchPanel.Wakeup();
|
|
|
|
}
|
2020-09-13 21:26:44 +02:00
|
|
|
|
2020-11-03 05:13:31 +01:00
|
|
|
nimbleController.StartAdvertising();
|
2021-07-24 20:29:10 +02:00
|
|
|
xTimerStart(dimTimer, 0);
|
2021-04-18 19:28:14 +02:00
|
|
|
spiNorFlash.Wakeup();
|
2020-11-03 05:13:31 +01:00
|
|
|
lcd.Wakeup();
|
2020-09-13 21:26:44 +02:00
|
|
|
|
2021-06-06 15:56:03 +02:00
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::GoToRunning);
|
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel);
|
|
|
|
heartRateApp.PushMessage(Pinetime::Applications::HeartRateTask::Messages::WakeUp);
|
2020-09-13 21:26:44 +02:00
|
|
|
|
|
|
|
isSleeping = false;
|
|
|
|
isWakingUp = false;
|
2021-07-24 20:29:10 +02:00
|
|
|
isDimmed = false;
|
2020-06-01 20:40:11 +02:00
|
|
|
break;
|
2021-04-05 16:22:10 +02:00
|
|
|
case Messages::TouchWakeUp: {
|
|
|
|
auto touchInfo = touchPanel.GetTouchInfo();
|
2021-04-18 19:28:14 +02:00
|
|
|
if (touchInfo.isTouch and ((touchInfo.gesture == Pinetime::Drivers::Cst816S::Gestures::DoubleTap and
|
2021-07-14 20:51:51 +02:00
|
|
|
settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) or
|
2021-04-18 19:28:14 +02:00
|
|
|
(touchInfo.gesture == Pinetime::Drivers::Cst816S::Gestures::SingleTap and
|
2021-07-14 20:51:51 +02:00
|
|
|
settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::SingleTap)))) {
|
2021-04-05 16:22:10 +02:00
|
|
|
GoToRunning();
|
|
|
|
}
|
|
|
|
} break;
|
2020-02-23 13:44:39 +01:00
|
|
|
case Messages::GoToSleep:
|
2020-09-13 21:26:44 +02:00
|
|
|
isGoingToSleep = true;
|
2020-10-02 20:45:21 +02:00
|
|
|
NRF_LOG_INFO("[systemtask] Going to sleep");
|
2020-06-13 17:33:49 +02:00
|
|
|
xTimerStop(idleTimer, 0);
|
2021-07-24 20:29:10 +02:00
|
|
|
xTimerStop(dimTimer, 0);
|
2021-06-06 15:56:03 +02:00
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::GoToSleep);
|
|
|
|
heartRateApp.PushMessage(Pinetime::Applications::HeartRateTask::Messages::GoToSleep);
|
2020-06-01 20:40:11 +02:00
|
|
|
break;
|
2020-03-25 21:23:40 +01:00
|
|
|
case Messages::OnNewTime:
|
2020-07-04 18:10:30 +02:00
|
|
|
ReloadIdleTimer();
|
2021-06-06 15:56:03 +02:00
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::UpdateDateTime);
|
2020-03-25 21:23:40 +01:00
|
|
|
break;
|
|
|
|
case Messages::OnNewNotification:
|
2021-05-20 20:43:54 +02:00
|
|
|
if (isSleeping && !isWakingUp) {
|
2021-04-18 19:28:14 +02:00
|
|
|
GoToRunning();
|
2021-05-20 20:43:54 +02:00
|
|
|
}
|
2021-06-06 15:56:03 +02:00
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::NewNotification);
|
2020-03-25 21:23:40 +01:00
|
|
|
break;
|
2021-05-20 20:43:54 +02:00
|
|
|
case Messages::OnTimerDone:
|
|
|
|
if (isSleeping && !isWakingUp) {
|
|
|
|
GoToRunning();
|
|
|
|
}
|
2021-08-01 12:05:48 +02:00
|
|
|
motorController.RunForDuration(35);
|
2021-06-06 15:56:03 +02:00
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::TimerDone);
|
2021-05-20 20:43:54 +02:00
|
|
|
break;
|
2020-05-01 21:58:31 +02:00
|
|
|
case Messages::BleConnected:
|
2020-07-04 18:10:30 +02:00
|
|
|
ReloadIdleTimer();
|
2020-05-01 21:58:31 +02:00
|
|
|
isBleDiscoveryTimerRunning = true;
|
|
|
|
bleDiscoveryTimer = 5;
|
|
|
|
break;
|
2020-05-02 14:16:57 +02:00
|
|
|
case Messages::BleFirmwareUpdateStarted:
|
2020-06-01 20:40:11 +02:00
|
|
|
doNotGoToSleep = true;
|
2021-04-18 19:28:14 +02:00
|
|
|
if (isSleeping && !isWakingUp)
|
|
|
|
GoToRunning();
|
2021-06-06 15:56:03 +02:00
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::BleFirmwareUpdateStarted);
|
2020-05-02 14:16:57 +02:00
|
|
|
break;
|
|
|
|
case Messages::BleFirmwareUpdateFinished:
|
2021-07-22 21:57:45 +02:00
|
|
|
if (bleController.State() == Pinetime::Controllers::Ble::FirmwareUpdateStates::Validated) {
|
|
|
|
NVIC_SystemReset();
|
|
|
|
}
|
2020-06-01 20:40:11 +02:00
|
|
|
doNotGoToSleep = false;
|
2021-07-24 20:29:10 +02:00
|
|
|
xTimerStart(dimTimer, 0);
|
2020-05-02 14:16:57 +02:00
|
|
|
break;
|
2020-06-01 20:40:11 +02:00
|
|
|
case Messages::OnTouchEvent:
|
2021-08-10 21:03:34 +02:00
|
|
|
if (touchHandler.GetNewTouchInfo()) {
|
|
|
|
touchHandler.UpdateLvglTouchPoint();
|
|
|
|
}
|
2020-07-04 18:10:30 +02:00
|
|
|
ReloadIdleTimer();
|
2021-08-10 21:18:21 +02:00
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::TouchEvent);
|
2020-06-01 20:40:11 +02:00
|
|
|
break;
|
|
|
|
case Messages::OnButtonEvent:
|
2020-07-04 18:10:30 +02:00
|
|
|
ReloadIdleTimer();
|
2021-07-24 20:29:10 +02:00
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::ButtonPushed);
|
2020-06-01 20:40:11 +02:00
|
|
|
break;
|
2020-08-22 17:59:59 +02:00
|
|
|
case Messages::OnDisplayTaskSleeping:
|
2021-04-18 19:28:14 +02:00
|
|
|
if (BootloaderVersion::IsValid()) {
|
2020-10-27 19:38:45 +01:00
|
|
|
// First versions of the bootloader do not expose their version and cannot initialize the SPI NOR FLASH
|
|
|
|
// if it's in sleep mode. Avoid bricked device by disabling sleep mode on these versions.
|
|
|
|
spiNorFlash.Sleep();
|
|
|
|
}
|
2020-08-22 17:59:59 +02:00
|
|
|
lcd.Sleep();
|
|
|
|
spi.Sleep();
|
2021-04-04 04:08:51 +02:00
|
|
|
|
|
|
|
// Double Tap needs the touch screen to be in normal mode
|
2021-07-14 20:51:51 +02:00
|
|
|
if (!settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) {
|
2021-04-04 14:51:22 +02:00
|
|
|
touchPanel.Sleep();
|
2021-04-04 04:08:51 +02:00
|
|
|
}
|
2021-04-18 19:28:14 +02:00
|
|
|
|
2020-09-13 21:26:44 +02:00
|
|
|
isSleeping = true;
|
|
|
|
isGoingToSleep = false;
|
2020-08-22 17:59:59 +02:00
|
|
|
break;
|
2021-04-02 17:33:49 +02:00
|
|
|
case Messages::OnNewDay:
|
|
|
|
// We might be sleeping (with TWI device disabled.
|
|
|
|
// Remember we'll have to reset the counter next time we're awake
|
|
|
|
stepCounterMustBeReset = true;
|
|
|
|
break;
|
2021-05-16 21:13:22 +02:00
|
|
|
case Messages::OnChargingEvent:
|
2021-08-01 12:05:48 +02:00
|
|
|
motorController.RunForDuration(15);
|
2021-07-16 00:49:20 +02:00
|
|
|
// Battery level is updated on every message - there's no need to do anything
|
2021-05-16 21:13:22 +02:00
|
|
|
break;
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
default:
|
|
|
|
break;
|
2020-02-23 13:44:39 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-01 21:58:31 +02:00
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
if (isBleDiscoveryTimerRunning) {
|
|
|
|
if (bleDiscoveryTimer == 0) {
|
2020-05-01 21:58:31 +02:00
|
|
|
isBleDiscoveryTimerRunning = false;
|
|
|
|
// Services discovery is deffered from 3 seconds to avoid the conflicts between the host communicating with the
|
|
|
|
// tharget and vice-versa. I'm not sure if this is the right way to handle this...
|
|
|
|
nimbleController.StartDiscovery();
|
|
|
|
} else {
|
|
|
|
bleDiscoveryTimer--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-11 16:55:06 +02:00
|
|
|
if (xTaskGetTickCount() - batteryNotificationTick > batteryNotificationPeriod) {
|
|
|
|
nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining());
|
|
|
|
batteryNotificationTick = xTaskGetTickCount();
|
|
|
|
}
|
|
|
|
|
2020-07-02 21:38:52 +02:00
|
|
|
monitor.Process();
|
2020-11-19 21:44:57 +01:00
|
|
|
uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG);
|
2020-11-19 21:50:39 +01:00
|
|
|
dateTimeController.UpdateTime(systick_counter);
|
2021-04-18 19:28:14 +02:00
|
|
|
if (!nrf_gpio_pin_read(pinButton))
|
2020-02-23 21:09:11 +01:00
|
|
|
watchdog.Kick();
|
2020-02-23 13:44:39 +01:00
|
|
|
}
|
2021-04-18 19:28:14 +02:00
|
|
|
// Clear diagnostic suppression
|
|
|
|
#pragma clang diagnostic pop
|
2020-02-23 13:44:39 +01:00
|
|
|
}
|
2021-03-31 19:47:27 +02:00
|
|
|
void SystemTask::UpdateMotion() {
|
2021-04-18 19:28:14 +02:00
|
|
|
if (isGoingToSleep or isWakingUp)
|
|
|
|
return;
|
2021-03-31 19:47:27 +02:00
|
|
|
|
2021-07-14 20:51:51 +02:00
|
|
|
if (isSleeping && !settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::RaiseWrist))
|
2021-04-16 20:05:46 +02:00
|
|
|
return;
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
if (stepCounterMustBeReset) {
|
2021-04-02 17:33:49 +02:00
|
|
|
motionSensor.ResetStepCounter();
|
|
|
|
stepCounterMustBeReset = false;
|
|
|
|
}
|
|
|
|
|
2021-03-31 19:47:27 +02:00
|
|
|
auto motionValues = motionSensor.Process();
|
|
|
|
|
2021-04-02 16:56:14 +02:00
|
|
|
motionController.IsSensorOk(motionSensor.IsOk());
|
2021-04-18 19:28:14 +02:00
|
|
|
motionController.Update(motionValues.x, motionValues.y, motionValues.z, motionValues.steps);
|
2021-03-31 19:47:27 +02:00
|
|
|
if (motionController.ShouldWakeUp(isSleeping)) {
|
|
|
|
GoToRunning();
|
|
|
|
}
|
|
|
|
}
|
2020-02-23 13:44:39 +01:00
|
|
|
|
|
|
|
void SystemTask::OnButtonPushed() {
|
2021-04-18 19:28:14 +02:00
|
|
|
if (isGoingToSleep)
|
|
|
|
return;
|
|
|
|
if (!isSleeping) {
|
2020-10-02 20:45:21 +02:00
|
|
|
NRF_LOG_INFO("[systemtask] Button pushed");
|
2020-06-01 20:40:11 +02:00
|
|
|
PushMessage(Messages::OnButtonEvent);
|
2021-04-18 19:28:14 +02:00
|
|
|
} else {
|
|
|
|
if (!isWakingUp) {
|
2020-10-02 20:45:21 +02:00
|
|
|
NRF_LOG_INFO("[systemtask] Button pushed, waking up");
|
2020-09-13 21:26:44 +02:00
|
|
|
GoToRunning();
|
|
|
|
}
|
2020-02-23 13:44:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 20:40:11 +02:00
|
|
|
void SystemTask::GoToRunning() {
|
2021-04-18 19:28:14 +02:00
|
|
|
if (isGoingToSleep or (not isSleeping) or isWakingUp)
|
|
|
|
return;
|
2020-09-13 21:26:44 +02:00
|
|
|
isWakingUp = true;
|
2020-06-01 20:40:11 +02:00
|
|
|
PushMessage(Messages::GoToRunning);
|
|
|
|
}
|
|
|
|
|
2020-02-23 13:44:39 +01:00
|
|
|
void SystemTask::OnTouchEvent() {
|
2021-04-18 19:28:14 +02:00
|
|
|
if (isGoingToSleep)
|
|
|
|
return;
|
|
|
|
if (!isSleeping) {
|
2020-06-01 20:40:11 +02:00
|
|
|
PushMessage(Messages::OnTouchEvent);
|
2021-04-18 19:28:14 +02:00
|
|
|
} else if (!isWakingUp) {
|
2021-07-14 20:51:51 +02:00
|
|
|
if (settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::SingleTap) or
|
|
|
|
settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) {
|
|
|
|
PushMessage(Messages::TouchWakeUp);
|
|
|
|
}
|
2020-06-01 20:40:11 +02:00
|
|
|
}
|
2020-02-23 13:44:39 +01:00
|
|
|
}
|
|
|
|
|
2021-06-06 15:56:03 +02:00
|
|
|
void SystemTask::PushMessage(System::Messages msg) {
|
2021-04-18 19:28:14 +02:00
|
|
|
if (msg == Messages::GoToSleep) {
|
2020-09-13 21:26:44 +02:00
|
|
|
isGoingToSleep = true;
|
|
|
|
}
|
2021-06-10 21:20:27 +02:00
|
|
|
|
2021-07-16 00:49:20 +02:00
|
|
|
if (in_isr()) {
|
2021-06-10 21:20:27 +02:00
|
|
|
BaseType_t xHigherPriorityTaskWoken;
|
|
|
|
xHigherPriorityTaskWoken = pdFALSE;
|
|
|
|
xQueueSendFromISR(systemTasksMsgQueue, &msg, &xHigherPriorityTaskWoken);
|
|
|
|
if (xHigherPriorityTaskWoken) {
|
|
|
|
/* Actual macro used here is port specific. */
|
|
|
|
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
xQueueSend(systemTasksMsgQueue, &msg, portMAX_DELAY);
|
2020-02-23 13:44:39 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-01 20:40:11 +02:00
|
|
|
|
2021-07-24 20:29:10 +02:00
|
|
|
void SystemTask::OnDim() {
|
|
|
|
if (doNotGoToSleep)
|
|
|
|
return;
|
|
|
|
NRF_LOG_INFO("Dim timeout -> Dim screen")
|
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::DimScreen);
|
|
|
|
xTimerStart(idleTimer, 0);
|
|
|
|
isDimmed = true;
|
|
|
|
}
|
|
|
|
|
2020-06-01 20:40:11 +02:00
|
|
|
void SystemTask::OnIdle() {
|
2021-04-18 19:28:14 +02:00
|
|
|
if (doNotGoToSleep)
|
|
|
|
return;
|
2020-06-01 20:40:11 +02:00
|
|
|
NRF_LOG_INFO("Idle timeout -> Going to sleep")
|
|
|
|
PushMessage(Messages::GoToSleep);
|
|
|
|
}
|
2020-07-04 18:10:30 +02:00
|
|
|
|
2021-07-24 20:29:10 +02:00
|
|
|
void SystemTask::ReloadIdleTimer() {
|
2021-04-18 19:28:14 +02:00
|
|
|
if (isSleeping || isGoingToSleep)
|
|
|
|
return;
|
2021-07-24 20:29:10 +02:00
|
|
|
if (isDimmed) {
|
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::RestoreBrightness);
|
|
|
|
isDimmed = false;
|
|
|
|
}
|
|
|
|
xTimerReset(dimTimer, 0);
|
|
|
|
xTimerStop(idleTimer, 0);
|
2020-07-04 18:10:30 +02:00
|
|
|
}
|