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;
|
|
|
|
|
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-01-25 16:47:52 +01:00
|
|
|
Pinetime::Controllers::MotorController& motorController,
|
2021-02-24 20:40:24 +01:00
|
|
|
Pinetime::Drivers::Hrs3300& heartRateSensor,
|
2021-03-31 19:47:27 +02:00
|
|
|
Pinetime::Drivers::Bma421& motionSensor,
|
2021-04-18 19:28:14 +02:00
|
|
|
Controllers::Settings& settingsController)
|
|
|
|
: spi {spi},
|
|
|
|
lcd {lcd},
|
|
|
|
spiNorFlash {spiNorFlash},
|
|
|
|
twiMaster {twiMaster},
|
|
|
|
touchPanel {touchPanel},
|
|
|
|
lvgl {lvgl},
|
|
|
|
batteryController {batteryController},
|
|
|
|
heartRateController {*this},
|
|
|
|
bleController {bleController},
|
|
|
|
dateTimeController {*this},
|
|
|
|
watchdog {},
|
|
|
|
watchdogView {watchdog},
|
|
|
|
motorController {motorController},
|
|
|
|
heartRateSensor {heartRateSensor},
|
|
|
|
motionSensor {motionSensor},
|
|
|
|
settingsController {settingsController},
|
|
|
|
nimbleController(*this, bleController, dateTimeController, notificationManager, batteryController, spiNorFlash, heartRateController) {
|
2020-10-04 14:13:01 +02:00
|
|
|
systemTasksMsgQueue = xQueueCreate(10, 1);
|
2020-02-23 13:44:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SystemTask::Start() {
|
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
|
|
|
|
2020-05-11 18:50:37 +02:00
|
|
|
spi.Init();
|
|
|
|
spiNorFlash.Init();
|
2020-09-20 14:31:26 +02:00
|
|
|
spiNorFlash.Wakeup();
|
2020-04-19 21:26:09 +02:00
|
|
|
nimbleController.Init();
|
|
|
|
nimbleController.StartAdvertising();
|
2021-01-26 20:31:45 +01:00
|
|
|
brightnessController.Init();
|
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();
|
|
|
|
batteryController.Init();
|
2021-02-05 15:43:20 +01:00
|
|
|
motorController.Init();
|
2021-04-08 20:07:24 +02:00
|
|
|
motionSensor.SoftReset();
|
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();
|
|
|
|
settingsController.Init();
|
2020-02-23 13:44:39 +01:00
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
displayApp = std::make_unique<Pinetime::Applications::DisplayApp>(lcd,
|
|
|
|
lvgl,
|
|
|
|
touchPanel,
|
|
|
|
batteryController,
|
|
|
|
bleController,
|
|
|
|
dateTimeController,
|
|
|
|
watchdogView,
|
|
|
|
*this,
|
|
|
|
notificationManager,
|
|
|
|
heartRateController,
|
|
|
|
settingsController,
|
2021-04-20 13:31:40 +02:00
|
|
|
motorController,
|
2021-04-18 19:28:14 +02:00
|
|
|
motionController);
|
2020-02-23 13:44:39 +01:00
|
|
|
displayApp->Start();
|
|
|
|
|
2021-01-26 20:31:45 +01:00
|
|
|
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-03-22 18:56:26 +01:00
|
|
|
heartRateApp = std::make_unique<Pinetime::Applications::HeartRateTask>(heartRateSensor, heartRateController);
|
2021-01-10 17:57:26 +01:00
|
|
|
heartRateApp->Start();
|
|
|
|
|
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-04-18 19:28:14 +02:00
|
|
|
idleTimer = xTimerCreate("idleTimer", pdMS_TO_TICKS(settingsController.GetScreenTimeOut()), pdFALSE, this, IdleTimerCallback);
|
2020-06-01 20:40:11 +02:00
|
|
|
xTimerStart(idleTimer, 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
|
|
|
|
2021-04-16 17:15:38 +02:00
|
|
|
// call the battery controller or use the MSG in DisplayApp to get the battery status ???
|
|
|
|
// it is necessary to validate which is the most efficient
|
2020-11-03 03:14:28 +01:00
|
|
|
batteryController.Update();
|
2021-04-18 19:28:14 +02:00
|
|
|
// displayApp->PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel);
|
|
|
|
// analyze a more efficient way to do this refreshment
|
|
|
|
// this and the UpdateMotion(); can be called on a timer to be independent of the main process ???
|
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:
|
|
|
|
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:
|
|
|
|
xTimerChangePeriod(idleTimer, pdMS_TO_TICKS(settingsController.GetScreenTimeOut()), 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();
|
|
|
|
twiMaster.Wakeup();
|
|
|
|
|
2021-04-04 14:51:22 +02:00
|
|
|
// Double Tap needs the touch screen to be in normal mode
|
2021-04-18 19:28:14 +02:00
|
|
|
if (settingsController.getWakeUpMode() != 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();
|
|
|
|
xTimerStart(idleTimer, 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-01-26 20:31:45 +01:00
|
|
|
displayApp->PushMessage(Pinetime::Applications::Display::Messages::GoToRunning);
|
|
|
|
displayApp->PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel);
|
2021-01-17 10:39:46 +01:00
|
|
|
heartRateApp->PushMessage(Pinetime::Applications::HeartRateTask::Messages::WakeUp);
|
2020-09-13 21:26:44 +02:00
|
|
|
|
|
|
|
isSleeping = false;
|
|
|
|
isWakingUp = false;
|
2020-06-01 20:40:11 +02:00
|
|
|
break;
|
2021-04-05 16:22:10 +02:00
|
|
|
case Messages::TouchWakeUp: {
|
2021-04-09 21:16:21 +02:00
|
|
|
twiMaster.Wakeup();
|
2021-04-05 16:22:10 +02:00
|
|
|
auto touchInfo = touchPanel.GetTouchInfo();
|
2021-04-09 21:16:21 +02:00
|
|
|
twiMaster.Sleep();
|
2021-04-18 19:28:14 +02:00
|
|
|
if (touchInfo.isTouch and ((touchInfo.gesture == Pinetime::Drivers::Cst816S::Gestures::DoubleTap and
|
|
|
|
settingsController.getWakeUpMode() == Pinetime::Controllers::Settings::WakeUpMode::DoubleTap) or
|
|
|
|
(touchInfo.gesture == Pinetime::Drivers::Cst816S::Gestures::SingleTap and
|
|
|
|
settingsController.getWakeUpMode() == 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-01-26 20:31:45 +01:00
|
|
|
displayApp->PushMessage(Pinetime::Applications::Display::Messages::GoToSleep);
|
2021-01-17 10:39:46 +01:00
|
|
|
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-01-26 20:31:45 +01:00
|
|
|
displayApp->PushMessage(Pinetime::Applications::Display::Messages::UpdateDateTime);
|
2020-03-25 21:23:40 +01:00
|
|
|
break;
|
|
|
|
case Messages::OnNewNotification:
|
2021-04-18 19:28:14 +02:00
|
|
|
if (isSleeping && !isWakingUp)
|
|
|
|
GoToRunning();
|
2021-04-04 04:08:51 +02:00
|
|
|
motorController.SetDuration(35);
|
2021-01-26 20:31:45 +01:00
|
|
|
displayApp->PushMessage(Pinetime::Applications::Display::Messages::NewNotification);
|
2020-03-25 21:23:40 +01: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-01-26 20:31:45 +01:00
|
|
|
displayApp->PushMessage(Pinetime::Applications::Display::Messages::BleFirmwareUpdateStarted);
|
2020-05-02 14:16:57 +02:00
|
|
|
break;
|
|
|
|
case Messages::BleFirmwareUpdateFinished:
|
2020-06-01 20:40:11 +02:00
|
|
|
doNotGoToSleep = false;
|
|
|
|
xTimerStart(idleTimer, 0);
|
2021-04-18 19:28:14 +02:00
|
|
|
if (bleController.State() == Pinetime::Controllers::Ble::FirmwareUpdateStates::Validated)
|
2020-06-01 16:30:24 +02:00
|
|
|
NVIC_SystemReset();
|
2020-05-02 14:16:57 +02:00
|
|
|
break;
|
2020-06-01 20:40:11 +02:00
|
|
|
case Messages::OnTouchEvent:
|
2020-07-04 18:10:30 +02:00
|
|
|
ReloadIdleTimer();
|
2020-06-01 20:40:11 +02:00
|
|
|
break;
|
|
|
|
case Messages::OnButtonEvent:
|
2020-07-04 18:10:30 +02:00
|
|
|
ReloadIdleTimer();
|
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-04-18 19:28:14 +02:00
|
|
|
if (settingsController.getWakeUpMode() != Pinetime::Controllers::Settings::WakeUpMode::DoubleTap) {
|
2021-04-04 14:51:22 +02:00
|
|
|
touchPanel.Sleep();
|
2021-04-04 04:08:51 +02:00
|
|
|
}
|
2020-08-22 17:59:59 +02:00
|
|
|
twiMaster.Sleep();
|
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-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--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-18 19:28:14 +02:00
|
|
|
if (isSleeping && settingsController.getWakeUpMode() != Pinetime::Controllers::Settings::WakeUpMode::RaiseWrist)
|
2021-04-16 20:05:46 +02:00
|
|
|
return;
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
if (isSleeping)
|
2021-03-31 19:47:27 +02:00
|
|
|
twiMaster.Wakeup();
|
2021-04-02 17:33:49 +02:00
|
|
|
|
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-18 19:28:14 +02:00
|
|
|
if (isSleeping)
|
2021-03-31 19:47:27 +02:00
|
|
|
twiMaster.Sleep();
|
|
|
|
|
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-01-26 20:31:45 +01:00
|
|
|
displayApp->PushMessage(Pinetime::Applications::Display::Messages::ButtonPushed);
|
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-01-26 20:31:45 +01:00
|
|
|
displayApp->PushMessage(Pinetime::Applications::Display::Messages::TouchEvent);
|
2021-04-18 19:28:14 +02:00
|
|
|
} else if (!isWakingUp) {
|
|
|
|
if (settingsController.getWakeUpMode() == Pinetime::Controllers::Settings::WakeUpMode::None or
|
|
|
|
settingsController.getWakeUpMode() == Pinetime::Controllers::Settings::WakeUpMode::RaiseWrist)
|
|
|
|
return;
|
2021-04-05 16:22:10 +02:00
|
|
|
PushMessage(Messages::TouchWakeUp);
|
2020-06-01 20:40:11 +02:00
|
|
|
}
|
2020-02-23 13:44:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SystemTask::PushMessage(SystemTask::Messages msg) {
|
2021-04-18 19:28:14 +02:00
|
|
|
if (msg == Messages::GoToSleep) {
|
2020-09-13 21:26:44 +02:00
|
|
|
isGoingToSleep = true;
|
|
|
|
}
|
2020-02-23 13:44:39 +01:00
|
|
|
BaseType_t xHigherPriorityTaskWoken;
|
|
|
|
xHigherPriorityTaskWoken = pdFALSE;
|
2020-10-04 14:13:01 +02:00
|
|
|
xQueueSendFromISR(systemTasksMsgQueue, &msg, &xHigherPriorityTaskWoken);
|
2020-02-23 13:44:39 +01:00
|
|
|
if (xHigherPriorityTaskWoken) {
|
|
|
|
/* Actual macro used here is port specific. */
|
2020-10-04 14:08:48 +02:00
|
|
|
// TODO: should I do something here?
|
2020-02-23 13:44:39 +01:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
|
|
|
void SystemTask::ReloadIdleTimer() const {
|
2021-04-18 19:28:14 +02:00
|
|
|
if (isSleeping || isGoingToSleep)
|
|
|
|
return;
|
2020-07-04 18:10:30 +02:00
|
|
|
xTimerReset(idleTimer, 0);
|
|
|
|
}
|