2020-02-23 12:44:39 +00:00
|
|
|
#include "SystemTask.h"
|
2020-11-15 14:05:51 +00:00
|
|
|
#define min // workaround: nimble's min/max macros conflict with libstdc++
|
|
|
|
#define max
|
2020-04-19 18:44:59 +00:00
|
|
|
#include <host/ble_gap.h>
|
2020-11-15 14:05:51 +00:00
|
|
|
#include <host/ble_gatt.h>
|
|
|
|
#include <host/ble_hs_adv.h>
|
2020-04-19 18:44:59 +00:00
|
|
|
#include <host/util/util.h>
|
2020-11-15 14:05:51 +00: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 16:57:26 +00:00
|
|
|
#include "drivers/Hrs3300.h"
|
2020-10-04 12:11:21 +00:00
|
|
|
#include "main.h"
|
2020-04-19 19:26:09 +00:00
|
|
|
|
2021-03-16 12:43:50 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2020-02-23 12:44:39 +00:00
|
|
|
using namespace Pinetime::System;
|
|
|
|
|
2021-06-10 19:20:27 +00:00
|
|
|
namespace {
|
|
|
|
static inline bool in_isr(void) {
|
|
|
|
return (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) != 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-24 18:29:10 +00:00
|
|
|
void DimTimerCallback(TimerHandle_t xTimer) {
|
|
|
|
|
|
|
|
NRF_LOG_INFO("DimTimerCallback");
|
|
|
|
auto sysTask = static_cast<SystemTask*>(pvTimerGetTimerID(xTimer));
|
|
|
|
sysTask->OnDim();
|
|
|
|
}
|
|
|
|
|
2020-06-01 18:40:11 +00:00
|
|
|
void IdleTimerCallback(TimerHandle_t xTimer) {
|
2020-06-13 15:33:49 +00:00
|
|
|
|
|
|
|
NRF_LOG_INFO("IdleTimerCallback");
|
2021-04-18 17:28:14 +00:00
|
|
|
auto sysTask = static_cast<SystemTask*>(pvTimerGetTimerID(xTimer));
|
2020-06-01 18:40:11 +00:00
|
|
|
sysTask->OnIdle();
|
|
|
|
}
|
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
SystemTask::SystemTask(Drivers::SpiMaster& spi,
|
|
|
|
Drivers::St7789& lcd,
|
2020-07-19 18:30:44 +00:00
|
|
|
Pinetime::Drivers::SpiNorFlash& spiNorFlash,
|
2021-04-18 17:28:14 +00:00
|
|
|
Drivers::TwiMaster& twiMaster,
|
|
|
|
Drivers::Cst816S& touchPanel,
|
|
|
|
Components::LittleVgl& lvgl,
|
|
|
|
Controllers::Battery& batteryController,
|
|
|
|
Controllers::Ble& bleController,
|
2021-06-06 13:56:03 +00:00
|
|
|
Controllers::DateTime& dateTimeController,
|
|
|
|
Controllers::TimerController& timerController,
|
|
|
|
Drivers::Watchdog& watchdog,
|
|
|
|
Pinetime::Controllers::NotificationManager& notificationManager,
|
2021-01-25 15:47:52 +00:00
|
|
|
Pinetime::Controllers::MotorController& motorController,
|
2021-02-24 19:40:24 +00:00
|
|
|
Pinetime::Drivers::Hrs3300& heartRateSensor,
|
2021-06-06 13:56:03 +00:00
|
|
|
Pinetime::Controllers::MotionController& motionController,
|
2021-03-31 17:47:27 +00:00
|
|
|
Pinetime::Drivers::Bma421& motionSensor,
|
2021-06-06 13:56:03 +00:00
|
|
|
Controllers::Settings& settingsController,
|
|
|
|
Pinetime::Controllers::HeartRateController& heartRateController,
|
|
|
|
Pinetime::Applications::DisplayApp& displayApp,
|
2021-07-11 13:06:06 +00:00
|
|
|
Pinetime::Applications::HeartRateTask& heartRateApp,
|
|
|
|
Pinetime::Controllers::FS& fs)
|
2021-04-18 17:28:14 +00:00
|
|
|
: spi {spi},
|
|
|
|
lcd {lcd},
|
|
|
|
spiNorFlash {spiNorFlash},
|
|
|
|
twiMaster {twiMaster},
|
|
|
|
touchPanel {touchPanel},
|
|
|
|
lvgl {lvgl},
|
|
|
|
batteryController {batteryController},
|
|
|
|
bleController {bleController},
|
2021-06-06 13:56:03 +00:00
|
|
|
dateTimeController {dateTimeController},
|
|
|
|
timerController {timerController},
|
|
|
|
watchdog {watchdog},
|
|
|
|
notificationManager{notificationManager},
|
2021-04-18 17:28:14 +00:00
|
|
|
motorController {motorController},
|
|
|
|
heartRateSensor {heartRateSensor},
|
|
|
|
motionSensor {motionSensor},
|
|
|
|
settingsController {settingsController},
|
2021-06-06 13:56:03 +00:00
|
|
|
heartRateController{heartRateController},
|
|
|
|
motionController{motionController},
|
|
|
|
displayApp{displayApp},
|
2021-07-11 13:06:06 +00:00
|
|
|
heartRateApp(heartRateApp),
|
|
|
|
fs{fs},
|
2021-04-18 17:28:14 +00:00
|
|
|
nimbleController(*this, bleController, dateTimeController, notificationManager, batteryController, spiNorFlash, heartRateController) {
|
2021-06-12 08:58:28 +00:00
|
|
|
|
2020-02-23 12:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SystemTask::Start() {
|
2021-06-12 08:58:28 +00:00
|
|
|
systemTasksMsgQueue = xQueueCreate(10, 1);
|
2020-02-23 15:14:03 +00:00
|
|
|
if (pdPASS != xTaskCreate(SystemTask::Process, "MAIN", 350, this, 0, &taskHandle))
|
2020-02-23 12:44:39 +00:00
|
|
|
APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
|
|
|
|
}
|
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
void SystemTask::Process(void* instance) {
|
|
|
|
auto* app = static_cast<SystemTask*>(instance);
|
2020-10-02 18:45:21 +00:00
|
|
|
NRF_LOG_INFO("systemtask task started!");
|
2020-02-23 12:44:39 +00:00
|
|
|
app->Work();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SystemTask::Work() {
|
2020-06-27 14:11:42 +00:00
|
|
|
watchdog.Setup(7);
|
|
|
|
watchdog.Start();
|
2020-02-23 20:09:11 +00:00
|
|
|
NRF_LOG_INFO("Last reset reason : %s", Pinetime::Drivers::Watchdog::ResetReasonToString(watchdog.ResetReason()));
|
2020-02-23 12:44:39 +00:00
|
|
|
APP_GPIOTE_INIT(2);
|
2020-04-19 18:44:59 +00:00
|
|
|
|
2021-05-20 18:43:54 +00:00
|
|
|
app_timer_init();
|
2021-07-24 18:29:10 +00:00
|
|
|
|
2020-05-11 16:50:37 +00:00
|
|
|
spi.Init();
|
|
|
|
spiNorFlash.Init();
|
2020-09-20 12:31:26 +00:00
|
|
|
spiNorFlash.Wakeup();
|
2021-07-11 13:06:06 +00:00
|
|
|
|
|
|
|
fs.Init();
|
|
|
|
|
2020-04-19 19:26:09 +00:00
|
|
|
nimbleController.Init();
|
|
|
|
nimbleController.StartAdvertising();
|
2020-02-23 12:44:39 +00:00
|
|
|
lcd.Init();
|
2020-05-11 16:50:37 +00:00
|
|
|
|
2020-07-19 18:30:44 +00:00
|
|
|
twiMaster.Init();
|
2020-02-23 12:44:39 +00:00
|
|
|
touchPanel.Init();
|
2021-06-06 13:56:03 +00:00
|
|
|
dateTimeController.Register(this);
|
2020-02-23 12:44:39 +00:00
|
|
|
batteryController.Init();
|
2021-02-05 14:43:20 +00:00
|
|
|
motorController.Init();
|
2021-04-08 18:07:24 +00:00
|
|
|
motionSensor.SoftReset();
|
2021-06-06 13:56:03 +00:00
|
|
|
timerController.Register(this);
|
2021-05-20 18:43:54 +00:00
|
|
|
timerController.Init();
|
2021-02-14 13:19:30 +00:00
|
|
|
|
2021-04-08 18:07:24 +00:00
|
|
|
// Reset the TWI device because the motion sensor chip most probably crashed it...
|
|
|
|
twiMaster.Sleep();
|
|
|
|
twiMaster.Init();
|
2021-02-24 19:40:24 +00:00
|
|
|
|
2021-04-08 18:07:24 +00:00
|
|
|
motionSensor.Init();
|
2021-06-19 18:27:59 +00:00
|
|
|
motionController.Init(motionSensor.DeviceType());
|
2021-04-08 18:07:24 +00:00
|
|
|
settingsController.Init();
|
2020-02-23 12:44:39 +00:00
|
|
|
|
2021-06-06 13:56:03 +00:00
|
|
|
displayApp.Register(this);
|
|
|
|
displayApp.Start();
|
|
|
|
|
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel);
|
2020-02-23 12:44:39 +00:00
|
|
|
|
2021-01-10 16:57:26 +00:00
|
|
|
heartRateSensor.Init();
|
|
|
|
heartRateSensor.Disable();
|
2021-06-06 13:56:03 +00:00
|
|
|
heartRateApp.Start();
|
2021-01-10 16:57:26 +00:00
|
|
|
|
2021-04-18 17:28:14 +00: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 12:44:39 +00: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 17:28:14 +00: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 12:44:39 +00:00
|
|
|
|
|
|
|
nrfx_gpiote_in_init(pinButton, &pinConfig, nrfx_gpiote_evt_handler);
|
|
|
|
|
2021-04-18 17:28:14 +00: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 12:44:39 +00:00
|
|
|
|
|
|
|
pinConfig.skip_gpio_setup = true;
|
|
|
|
pinConfig.hi_accuracy = false;
|
|
|
|
pinConfig.is_watcher = false;
|
2021-04-18 17:28:14 +00: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 12:44:39 +00:00
|
|
|
|
|
|
|
nrfx_gpiote_in_init(pinTouchIrq, &pinConfig, nrfx_gpiote_evt_handler);
|
|
|
|
|
2021-05-16 19:13:22 +00: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 07:52:31 +00: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 18:29:10 +00: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 12:44:39 +00:00
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
// Suppress endless loop diagnostic
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma ide diagnostic ignored "EndlessLoop"
|
|
|
|
while (true) {
|
2021-03-31 17:47:27 +00:00
|
|
|
UpdateMotion();
|
|
|
|
|
2020-02-23 12:44:39 +00:00
|
|
|
uint8_t msg;
|
2021-03-31 17:47:27 +00:00
|
|
|
if (xQueueReceive(systemTasksMsgQueue, &msg, 100)) {
|
2021-04-18 17:28:14 +00:00
|
|
|
|
2020-11-03 02:14:28 +00:00
|
|
|
batteryController.Update();
|
2021-05-16 19:13:22 +00: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 15:15:38 +00:00
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
Messages message = static_cast<Messages>(msg);
|
|
|
|
switch (message) {
|
2021-04-04 02:08:51 +00:00
|
|
|
case Messages::EnableSleeping:
|
2021-07-22 19:57:45 +00: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 17:28:14 +00:00
|
|
|
break;
|
2021-04-04 02:08:51 +00:00
|
|
|
case Messages::DisableSleeping:
|
|
|
|
doNotGoToSleep = true;
|
2021-04-18 17:28:14 +00:00
|
|
|
break;
|
2021-04-04 02:08:51 +00:00
|
|
|
case Messages::UpdateTimeOut:
|
2021-07-24 18:29:10 +00:00
|
|
|
xTimerChangePeriod(dimTimer, pdMS_TO_TICKS(settingsController.GetScreenTimeOut() - 2000), 0);
|
2021-04-18 17:28:14 +00:00
|
|
|
break;
|
2020-06-01 18:40:11 +00:00
|
|
|
case Messages::GoToRunning:
|
2020-09-13 19:26:44 +00:00
|
|
|
spi.Wakeup();
|
|
|
|
twiMaster.Wakeup();
|
|
|
|
|
2021-04-04 12:51:22 +00:00
|
|
|
// Double Tap needs the touch screen to be in normal mode
|
2021-07-14 18:51:51 +00:00
|
|
|
if (!settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) {
|
2021-04-04 12:51:22 +00:00
|
|
|
touchPanel.Wakeup();
|
|
|
|
}
|
2020-09-13 19:26:44 +00:00
|
|
|
|
2020-11-03 04:13:31 +00:00
|
|
|
nimbleController.StartAdvertising();
|
2021-07-24 18:29:10 +00:00
|
|
|
xTimerStart(dimTimer, 0);
|
2021-04-18 17:28:14 +00:00
|
|
|
spiNorFlash.Wakeup();
|
2020-11-03 04:13:31 +00:00
|
|
|
lcd.Wakeup();
|
2020-09-13 19:26:44 +00:00
|
|
|
|
2021-06-06 13:56:03 +00: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 19:26:44 +00:00
|
|
|
|
|
|
|
isSleeping = false;
|
|
|
|
isWakingUp = false;
|
2021-07-24 18:29:10 +00:00
|
|
|
isDimmed = false;
|
2020-06-01 18:40:11 +00:00
|
|
|
break;
|
2021-04-05 14:22:10 +00:00
|
|
|
case Messages::TouchWakeUp: {
|
2021-04-09 19:16:21 +00:00
|
|
|
twiMaster.Wakeup();
|
2021-04-05 14:22:10 +00:00
|
|
|
auto touchInfo = touchPanel.GetTouchInfo();
|
2021-04-09 19:16:21 +00:00
|
|
|
twiMaster.Sleep();
|
2021-04-18 17:28:14 +00:00
|
|
|
if (touchInfo.isTouch and ((touchInfo.gesture == Pinetime::Drivers::Cst816S::Gestures::DoubleTap and
|
2021-07-14 18:51:51 +00:00
|
|
|
settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) or
|
2021-04-18 17:28:14 +00:00
|
|
|
(touchInfo.gesture == Pinetime::Drivers::Cst816S::Gestures::SingleTap and
|
2021-07-14 18:51:51 +00:00
|
|
|
settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::SingleTap)))) {
|
2021-04-05 14:22:10 +00:00
|
|
|
GoToRunning();
|
|
|
|
}
|
|
|
|
} break;
|
2020-02-23 12:44:39 +00:00
|
|
|
case Messages::GoToSleep:
|
2020-09-13 19:26:44 +00:00
|
|
|
isGoingToSleep = true;
|
2020-10-02 18:45:21 +00:00
|
|
|
NRF_LOG_INFO("[systemtask] Going to sleep");
|
2020-06-13 15:33:49 +00:00
|
|
|
xTimerStop(idleTimer, 0);
|
2021-07-24 18:29:10 +00:00
|
|
|
xTimerStop(dimTimer, 0);
|
2021-06-06 13:56:03 +00:00
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::GoToSleep);
|
|
|
|
heartRateApp.PushMessage(Pinetime::Applications::HeartRateTask::Messages::GoToSleep);
|
2020-06-01 18:40:11 +00:00
|
|
|
break;
|
2020-03-25 20:23:40 +00:00
|
|
|
case Messages::OnNewTime:
|
2020-07-04 16:10:30 +00:00
|
|
|
ReloadIdleTimer();
|
2021-06-06 13:56:03 +00:00
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::UpdateDateTime);
|
2020-03-25 20:23:40 +00:00
|
|
|
break;
|
|
|
|
case Messages::OnNewNotification:
|
2021-05-20 18:43:54 +00:00
|
|
|
if (isSleeping && !isWakingUp) {
|
2021-04-18 17:28:14 +00:00
|
|
|
GoToRunning();
|
2021-05-20 18:43:54 +00:00
|
|
|
}
|
2021-06-06 13:56:03 +00:00
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::NewNotification);
|
2021-08-01 10:05:48 +00:00
|
|
|
break;
|
2021-05-20 18:43:54 +00:00
|
|
|
case Messages::OnTimerDone:
|
|
|
|
if (isSleeping && !isWakingUp) {
|
|
|
|
GoToRunning();
|
|
|
|
}
|
2021-08-01 10:05:48 +00:00
|
|
|
motorController.RunForDuration(35);
|
2021-06-06 13:56:03 +00:00
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::TimerDone);
|
2020-03-25 20:23:40 +00:00
|
|
|
break;
|
2020-05-01 19:58:31 +00:00
|
|
|
case Messages::BleConnected:
|
2020-07-04 16:10:30 +00:00
|
|
|
ReloadIdleTimer();
|
2020-05-01 19:58:31 +00:00
|
|
|
isBleDiscoveryTimerRunning = true;
|
|
|
|
bleDiscoveryTimer = 5;
|
|
|
|
break;
|
2020-05-02 12:16:57 +00:00
|
|
|
case Messages::BleFirmwareUpdateStarted:
|
2020-06-01 18:40:11 +00:00
|
|
|
doNotGoToSleep = true;
|
2021-04-18 17:28:14 +00:00
|
|
|
if (isSleeping && !isWakingUp)
|
|
|
|
GoToRunning();
|
2021-06-06 13:56:03 +00:00
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::BleFirmwareUpdateStarted);
|
2020-05-02 12:16:57 +00:00
|
|
|
break;
|
|
|
|
case Messages::BleFirmwareUpdateFinished:
|
2021-07-22 19:57:45 +00:00
|
|
|
if (bleController.State() == Pinetime::Controllers::Ble::FirmwareUpdateStates::Validated) {
|
2020-06-01 14:30:24 +00:00
|
|
|
NVIC_SystemReset();
|
2021-07-22 19:57:45 +00:00
|
|
|
}
|
2020-06-01 18:40:11 +00:00
|
|
|
doNotGoToSleep = false;
|
2021-07-24 18:29:10 +00:00
|
|
|
xTimerStart(dimTimer, 0);
|
2020-05-02 12:16:57 +00:00
|
|
|
break;
|
2020-06-01 18:40:11 +00:00
|
|
|
case Messages::OnTouchEvent:
|
2020-07-04 16:10:30 +00:00
|
|
|
ReloadIdleTimer();
|
2021-07-24 18:29:10 +00:00
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::TouchEvent);
|
2020-06-01 18:40:11 +00:00
|
|
|
break;
|
|
|
|
case Messages::OnButtonEvent:
|
2020-07-04 16:10:30 +00:00
|
|
|
ReloadIdleTimer();
|
2021-07-24 18:29:10 +00:00
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::ButtonPushed);
|
2020-06-01 18:40:11 +00:00
|
|
|
break;
|
2020-08-22 15:59:59 +00:00
|
|
|
case Messages::OnDisplayTaskSleeping:
|
2021-04-18 17:28:14 +00:00
|
|
|
if (BootloaderVersion::IsValid()) {
|
2020-10-27 18:38:45 +00: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 15:59:59 +00:00
|
|
|
lcd.Sleep();
|
|
|
|
spi.Sleep();
|
2021-04-04 02:08:51 +00:00
|
|
|
|
|
|
|
// Double Tap needs the touch screen to be in normal mode
|
2021-07-14 18:51:51 +00:00
|
|
|
if (!settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) {
|
2021-04-04 12:51:22 +00:00
|
|
|
touchPanel.Sleep();
|
2021-04-04 02:08:51 +00:00
|
|
|
}
|
2020-08-22 15:59:59 +00:00
|
|
|
twiMaster.Sleep();
|
2021-04-18 17:28:14 +00:00
|
|
|
|
2020-09-13 19:26:44 +00:00
|
|
|
isSleeping = true;
|
|
|
|
isGoingToSleep = false;
|
2020-08-22 15:59:59 +00:00
|
|
|
break;
|
2021-04-02 15:33:49 +00: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 19:13:22 +00:00
|
|
|
case Messages::OnChargingEvent:
|
2021-08-01 10:05:48 +00:00
|
|
|
motorController.RunForDuration(15);
|
2021-05-16 19:13:22 +00:00
|
|
|
// Battery level is updated on every message - there's no need to do anything
|
|
|
|
break;
|
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
default:
|
|
|
|
break;
|
2020-02-23 12:44:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-01 19:58:31 +00:00
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
if (isBleDiscoveryTimerRunning) {
|
|
|
|
if (bleDiscoveryTimer == 0) {
|
2020-05-01 19:58:31 +00: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 14:55:06 +00:00
|
|
|
if (xTaskGetTickCount() - batteryNotificationTick > batteryNotificationPeriod) {
|
|
|
|
nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining());
|
|
|
|
batteryNotificationTick = xTaskGetTickCount();
|
|
|
|
}
|
|
|
|
|
2020-07-02 19:38:52 +00:00
|
|
|
monitor.Process();
|
2020-11-19 20:44:57 +00:00
|
|
|
uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG);
|
2020-11-19 20:50:39 +00:00
|
|
|
dateTimeController.UpdateTime(systick_counter);
|
2021-04-18 17:28:14 +00:00
|
|
|
if (!nrf_gpio_pin_read(pinButton))
|
2020-02-23 20:09:11 +00:00
|
|
|
watchdog.Kick();
|
2020-02-23 12:44:39 +00:00
|
|
|
}
|
2021-04-18 17:28:14 +00:00
|
|
|
// Clear diagnostic suppression
|
|
|
|
#pragma clang diagnostic pop
|
2020-02-23 12:44:39 +00:00
|
|
|
}
|
2021-03-31 17:47:27 +00:00
|
|
|
void SystemTask::UpdateMotion() {
|
2021-04-18 17:28:14 +00:00
|
|
|
if (isGoingToSleep or isWakingUp)
|
|
|
|
return;
|
2021-03-31 17:47:27 +00:00
|
|
|
|
2021-07-14 18:51:51 +00:00
|
|
|
if (isSleeping && !settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::RaiseWrist))
|
2021-04-16 18:05:46 +00:00
|
|
|
return;
|
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
if (isSleeping)
|
2021-03-31 17:47:27 +00:00
|
|
|
twiMaster.Wakeup();
|
2021-04-02 15:33:49 +00:00
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
if (stepCounterMustBeReset) {
|
2021-04-02 15:33:49 +00:00
|
|
|
motionSensor.ResetStepCounter();
|
|
|
|
stepCounterMustBeReset = false;
|
|
|
|
}
|
|
|
|
|
2021-03-31 17:47:27 +00:00
|
|
|
auto motionValues = motionSensor.Process();
|
2021-04-18 17:28:14 +00:00
|
|
|
if (isSleeping)
|
2021-03-31 17:47:27 +00:00
|
|
|
twiMaster.Sleep();
|
|
|
|
|
2021-04-02 14:56:14 +00:00
|
|
|
motionController.IsSensorOk(motionSensor.IsOk());
|
2021-04-18 17:28:14 +00:00
|
|
|
motionController.Update(motionValues.x, motionValues.y, motionValues.z, motionValues.steps);
|
2021-03-31 17:47:27 +00:00
|
|
|
if (motionController.ShouldWakeUp(isSleeping)) {
|
|
|
|
GoToRunning();
|
|
|
|
}
|
|
|
|
}
|
2020-02-23 12:44:39 +00:00
|
|
|
|
|
|
|
void SystemTask::OnButtonPushed() {
|
2021-04-18 17:28:14 +00:00
|
|
|
if (isGoingToSleep)
|
|
|
|
return;
|
|
|
|
if (!isSleeping) {
|
2020-10-02 18:45:21 +00:00
|
|
|
NRF_LOG_INFO("[systemtask] Button pushed");
|
2020-06-01 18:40:11 +00:00
|
|
|
PushMessage(Messages::OnButtonEvent);
|
2021-04-18 17:28:14 +00:00
|
|
|
} else {
|
|
|
|
if (!isWakingUp) {
|
2020-10-02 18:45:21 +00:00
|
|
|
NRF_LOG_INFO("[systemtask] Button pushed, waking up");
|
2020-09-13 19:26:44 +00:00
|
|
|
GoToRunning();
|
|
|
|
}
|
2020-02-23 12:44:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 18:40:11 +00:00
|
|
|
void SystemTask::GoToRunning() {
|
2021-04-18 17:28:14 +00:00
|
|
|
if (isGoingToSleep or (not isSleeping) or isWakingUp)
|
|
|
|
return;
|
2020-09-13 19:26:44 +00:00
|
|
|
isWakingUp = true;
|
2020-06-01 18:40:11 +00:00
|
|
|
PushMessage(Messages::GoToRunning);
|
|
|
|
}
|
|
|
|
|
2020-02-23 12:44:39 +00:00
|
|
|
void SystemTask::OnTouchEvent() {
|
2021-04-18 17:28:14 +00:00
|
|
|
if (isGoingToSleep)
|
|
|
|
return;
|
|
|
|
if (!isSleeping) {
|
2020-06-01 18:40:11 +00:00
|
|
|
PushMessage(Messages::OnTouchEvent);
|
2021-04-18 17:28:14 +00:00
|
|
|
} else if (!isWakingUp) {
|
2021-07-14 18:51:51 +00:00
|
|
|
if (settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::SingleTap) or
|
|
|
|
settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) {
|
|
|
|
PushMessage(Messages::TouchWakeUp);
|
|
|
|
}
|
2020-06-01 18:40:11 +00:00
|
|
|
}
|
2020-02-23 12:44:39 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 13:56:03 +00:00
|
|
|
void SystemTask::PushMessage(System::Messages msg) {
|
2021-04-18 17:28:14 +00:00
|
|
|
if (msg == Messages::GoToSleep) {
|
2020-09-13 19:26:44 +00:00
|
|
|
isGoingToSleep = true;
|
|
|
|
}
|
2021-06-10 19:20:27 +00:00
|
|
|
|
|
|
|
if(in_isr()) {
|
|
|
|
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 12:44:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-01 18:40:11 +00:00
|
|
|
|
2021-07-24 18:29:10 +00: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 18:40:11 +00:00
|
|
|
void SystemTask::OnIdle() {
|
2021-04-18 17:28:14 +00:00
|
|
|
if (doNotGoToSleep)
|
|
|
|
return;
|
2020-06-01 18:40:11 +00:00
|
|
|
NRF_LOG_INFO("Idle timeout -> Going to sleep")
|
|
|
|
PushMessage(Messages::GoToSleep);
|
|
|
|
}
|
2020-07-04 16:10:30 +00:00
|
|
|
|
2021-07-24 18:29:10 +00:00
|
|
|
void SystemTask::ReloadIdleTimer() {
|
2021-04-18 17:28:14 +00:00
|
|
|
if (isSleeping || isGoingToSleep)
|
|
|
|
return;
|
2021-07-24 18:29:10 +00:00
|
|
|
if (isDimmed) {
|
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::RestoreBrightness);
|
|
|
|
isDimmed = false;
|
|
|
|
}
|
|
|
|
xTimerReset(dimTimer, 0);
|
|
|
|
xTimerStop(idleTimer, 0);
|
2020-07-04 16:10:30 +00:00
|
|
|
}
|