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;
|
|
|
|
|
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-01-25 15:47:52 +00:00
|
|
|
Pinetime::Controllers::MotorController& motorController,
|
2021-02-24 19:40:24 +00:00
|
|
|
Pinetime::Drivers::Hrs3300& heartRateSensor,
|
2021-03-31 17:47:27 +00:00
|
|
|
Pinetime::Drivers::Bma421& motionSensor,
|
2021-04-18 17:28:14 +00: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 12:13:01 +00:00
|
|
|
systemTasksMsgQueue = xQueueCreate(10, 1);
|
2020-02-23 12:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SystemTask::Start() {
|
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
|
|
|
|
2020-05-11 16:50:37 +00:00
|
|
|
spi.Init();
|
|
|
|
spiNorFlash.Init();
|
2020-09-20 12:31:26 +00:00
|
|
|
spiNorFlash.Wakeup();
|
2020-04-19 19:26:09 +00:00
|
|
|
nimbleController.Init();
|
|
|
|
nimbleController.StartAdvertising();
|
2021-01-26 19:31:45 +00:00
|
|
|
brightnessController.Init();
|
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();
|
|
|
|
batteryController.Init();
|
2021-02-05 14:43:20 +00:00
|
|
|
motorController.Init();
|
2021-04-08 18:07:24 +00:00
|
|
|
motionSensor.SoftReset();
|
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();
|
|
|
|
settingsController.Init();
|
2020-02-23 12:44:39 +00:00
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
displayApp = std::make_unique<Pinetime::Applications::DisplayApp>(lcd,
|
|
|
|
lvgl,
|
|
|
|
touchPanel,
|
|
|
|
batteryController,
|
|
|
|
bleController,
|
|
|
|
dateTimeController,
|
|
|
|
watchdogView,
|
|
|
|
*this,
|
|
|
|
notificationManager,
|
|
|
|
heartRateController,
|
|
|
|
settingsController,
|
2021-04-20 11:31:40 +00:00
|
|
|
motorController,
|
2021-04-18 17:28:14 +00:00
|
|
|
motionController);
|
2020-02-23 12:44:39 +00:00
|
|
|
displayApp->Start();
|
|
|
|
|
2021-01-26 19:31:45 +00:00
|
|
|
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-03-22 17:56:26 +00:00
|
|
|
heartRateApp = std::make_unique<Pinetime::Applications::HeartRateTask>(heartRateSensor, heartRateController);
|
2021-01-10 16:57:26 +00:00
|
|
|
heartRateApp->Start();
|
|
|
|
|
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-04-18 17:28:14 +00:00
|
|
|
idleTimer = xTimerCreate("idleTimer", pdMS_TO_TICKS(settingsController.GetScreenTimeOut()), pdFALSE, this, IdleTimerCallback);
|
2020-06-01 18:40:11 +00:00
|
|
|
xTimerStart(idleTimer, 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
|
|
|
|
2021-04-16 15:15:38 +00: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 02:14:28 +00:00
|
|
|
batteryController.Update();
|
2021-04-18 17:28:14 +00: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 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:
|
|
|
|
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:
|
|
|
|
xTimerChangePeriod(idleTimer, pdMS_TO_TICKS(settingsController.GetScreenTimeOut()), 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-04-18 17:28:14 +00:00
|
|
|
if (settingsController.getWakeUpMode() != 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();
|
|
|
|
xTimerStart(idleTimer, 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-01-26 19:31:45 +00:00
|
|
|
displayApp->PushMessage(Pinetime::Applications::Display::Messages::GoToRunning);
|
|
|
|
displayApp->PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel);
|
2021-01-17 09:39:46 +00:00
|
|
|
heartRateApp->PushMessage(Pinetime::Applications::HeartRateTask::Messages::WakeUp);
|
2020-09-13 19:26:44 +00:00
|
|
|
|
|
|
|
isSleeping = false;
|
|
|
|
isWakingUp = 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
|
|
|
|
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 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-01-26 19:31:45 +00:00
|
|
|
displayApp->PushMessage(Pinetime::Applications::Display::Messages::GoToSleep);
|
2021-01-17 09:39:46 +00:00
|
|
|
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-01-26 19:31:45 +00:00
|
|
|
displayApp->PushMessage(Pinetime::Applications::Display::Messages::UpdateDateTime);
|
2020-03-25 20:23:40 +00:00
|
|
|
break;
|
|
|
|
case Messages::OnNewNotification:
|
2021-04-18 17:28:14 +00:00
|
|
|
if (isSleeping && !isWakingUp)
|
|
|
|
GoToRunning();
|
2021-04-04 02:08:51 +00:00
|
|
|
motorController.SetDuration(35);
|
2021-01-26 19:31:45 +00:00
|
|
|
displayApp->PushMessage(Pinetime::Applications::Display::Messages::NewNotification);
|
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-01-26 19:31:45 +00:00
|
|
|
displayApp->PushMessage(Pinetime::Applications::Display::Messages::BleFirmwareUpdateStarted);
|
2020-05-02 12:16:57 +00:00
|
|
|
break;
|
|
|
|
case Messages::BleFirmwareUpdateFinished:
|
2020-06-01 18:40:11 +00:00
|
|
|
doNotGoToSleep = false;
|
|
|
|
xTimerStart(idleTimer, 0);
|
2021-04-18 17:28:14 +00:00
|
|
|
if (bleController.State() == Pinetime::Controllers::Ble::FirmwareUpdateStates::Validated)
|
2020-06-01 14:30:24 +00:00
|
|
|
NVIC_SystemReset();
|
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();
|
2020-06-01 18:40:11 +00:00
|
|
|
break;
|
|
|
|
case Messages::OnButtonEvent:
|
2020-07-04 16:10:30 +00:00
|
|
|
ReloadIdleTimer();
|
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-04-18 17:28:14 +00:00
|
|
|
if (settingsController.getWakeUpMode() != 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-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--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-18 17:28:14 +00:00
|
|
|
if (isSleeping && settingsController.getWakeUpMode() != 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-01-26 19:31:45 +00:00
|
|
|
displayApp->PushMessage(Pinetime::Applications::Display::Messages::ButtonPushed);
|
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-01-26 19:31:45 +00:00
|
|
|
displayApp->PushMessage(Pinetime::Applications::Display::Messages::TouchEvent);
|
2021-04-18 17:28:14 +00:00
|
|
|
} else if (!isWakingUp) {
|
|
|
|
if (settingsController.getWakeUpMode() == Pinetime::Controllers::Settings::WakeUpMode::None or
|
|
|
|
settingsController.getWakeUpMode() == Pinetime::Controllers::Settings::WakeUpMode::RaiseWrist)
|
|
|
|
return;
|
2021-04-05 14:22:10 +00:00
|
|
|
PushMessage(Messages::TouchWakeUp);
|
2020-06-01 18:40:11 +00:00
|
|
|
}
|
2020-02-23 12:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SystemTask::PushMessage(SystemTask::Messages msg) {
|
2021-04-18 17:28:14 +00:00
|
|
|
if (msg == Messages::GoToSleep) {
|
2020-09-13 19:26:44 +00:00
|
|
|
isGoingToSleep = true;
|
|
|
|
}
|
2020-02-23 12:44:39 +00:00
|
|
|
BaseType_t xHigherPriorityTaskWoken;
|
|
|
|
xHigherPriorityTaskWoken = pdFALSE;
|
2020-10-04 12:13:01 +00:00
|
|
|
xQueueSendFromISR(systemTasksMsgQueue, &msg, &xHigherPriorityTaskWoken);
|
2020-02-23 12:44:39 +00:00
|
|
|
if (xHigherPriorityTaskWoken) {
|
|
|
|
/* Actual macro used here is port specific. */
|
2020-10-04 12:08:48 +00:00
|
|
|
// TODO: should I do something here?
|
2020-02-23 12:44:39 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
|
|
|
void SystemTask::ReloadIdleTimer() const {
|
2021-04-18 17:28:14 +00:00
|
|
|
if (isSleeping || isGoingToSleep)
|
|
|
|
return;
|
2020-07-04 16:10:30 +00:00
|
|
|
xTimerReset(idleTimer, 0);
|
|
|
|
}
|