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"
|
2021-08-03 20:32:23 +02:00
|
|
|
#include "drivers/PinMap.h"
|
2020-10-04 14:11:21 +02:00
|
|
|
#include "main.h"
|
2021-08-18 14:23:30 +02:00
|
|
|
#include "BootErrors.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-08-14 20:18:11 +02:00
|
|
|
void MeasureBatteryTimerCallback(TimerHandle_t xTimer) {
|
|
|
|
auto* sysTask = static_cast<SystemTask*>(pvTimerGetTimerID(xTimer));
|
|
|
|
sysTask->PushMessage(Pinetime::System::Messages::MeasureBatteryTimerExpired);
|
|
|
|
}
|
|
|
|
|
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,
|
2021-09-11 00:40:13 +02:00
|
|
|
Controllers::AlarmController& alarmController,
|
2021-06-06 15:56:03 +02:00
|
|
|
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,
|
2021-10-25 11:53:14 +02:00
|
|
|
Pinetime::Controllers::TouchHandler& touchHandler,
|
|
|
|
Pinetime::Controllers::ButtonHandler& buttonHandler)
|
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},
|
2021-09-11 00:40:13 +02:00
|
|
|
alarmController {alarmController},
|
2021-06-06 15:56:03 +02:00
|
|
|
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-10-25 11:53:14 +02:00
|
|
|
buttonHandler {buttonHandler},
|
|
|
|
nimbleController(*this,
|
|
|
|
bleController,
|
|
|
|
dateTimeController,
|
|
|
|
notificationManager,
|
|
|
|
batteryController,
|
|
|
|
spiNorFlash,
|
|
|
|
heartRateController,
|
|
|
|
motionController) {
|
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() {
|
2021-08-18 14:23:30 +02:00
|
|
|
BootErrors bootError = BootErrors::None;
|
|
|
|
|
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();
|
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();
|
2021-11-07 18:00:34 +01:00
|
|
|
/*
|
|
|
|
* TODO We disable this warning message until we ensure it won't be displayed
|
|
|
|
* on legitimate PineTime equipped with a compatible touch controller.
|
|
|
|
* (some users reported false positive). See https://github.com/InfiniTimeOrg/InfiniTime/issues/763
|
2021-08-18 14:23:30 +02:00
|
|
|
if (!touchPanel.Init()) {
|
|
|
|
bootError = BootErrors::TouchController;
|
|
|
|
}
|
2021-11-07 18:00:34 +01:00
|
|
|
*/
|
2021-11-07 20:13:22 +01:00
|
|
|
touchPanel.Init();
|
2021-06-06 15:56:03 +02:00
|
|
|
dateTimeController.Register(this);
|
2021-08-14 20:18:11 +02:00
|
|
|
batteryController.Register(this);
|
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-09-13 21:26:28 +02:00
|
|
|
alarmController.Init(this);
|
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);
|
2021-08-18 14:23:30 +02:00
|
|
|
displayApp.Start(bootError);
|
2021-06-06 15:56:03 +02: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-10-25 11:53:14 +02:00
|
|
|
buttonHandler.Init(this);
|
|
|
|
|
2021-10-03 16:47:01 +02:00
|
|
|
// Button
|
2020-02-23 13:44:39 +01:00
|
|
|
nrf_gpio_cfg_output(15);
|
|
|
|
nrf_gpio_pin_set(15);
|
|
|
|
|
|
|
|
nrfx_gpiote_in_config_t pinConfig;
|
2021-10-03 16:47:01 +02:00
|
|
|
pinConfig.skip_gpio_setup = false;
|
2020-02-23 13:44:39 +01:00
|
|
|
pinConfig.hi_accuracy = false;
|
|
|
|
pinConfig.is_watcher = false;
|
2021-10-03 16:47:01 +02:00
|
|
|
pinConfig.sense = (nrf_gpiote_polarity_t) NRF_GPIOTE_POLARITY_TOGGLE;
|
2021-04-18 19:28:14 +02:00
|
|
|
pinConfig.pull = (nrf_gpio_pin_pull_t) GPIO_PIN_CNF_PULL_Pulldown;
|
2020-02-23 13:44:39 +01:00
|
|
|
|
2021-08-03 20:32:23 +02:00
|
|
|
nrfx_gpiote_in_init(PinMap::Button, &pinConfig, nrfx_gpiote_evt_handler);
|
2021-10-03 16:47:01 +02:00
|
|
|
nrfx_gpiote_in_event_enable(PinMap::Button, true);
|
2020-02-23 13:44:39 +01:00
|
|
|
|
2021-10-03 16:47:01 +02:00
|
|
|
// Touchscreen
|
2021-08-03 20:32:23 +02:00
|
|
|
nrf_gpio_cfg_sense_input(PinMap::Cst816sIrq, (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
|
|
|
|
2021-08-03 20:32:23 +02:00
|
|
|
nrfx_gpiote_in_init(PinMap::Cst816sIrq, &pinConfig, nrfx_gpiote_evt_handler);
|
2020-02-23 13:44:39 +01:00
|
|
|
|
2021-10-03 16:47:01 +02:00
|
|
|
// Power present
|
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;
|
2021-10-03 16:47:01 +02:00
|
|
|
pinConfig.skip_gpio_setup = false;
|
2021-08-03 20:32:23 +02:00
|
|
|
nrfx_gpiote_in_init(PinMap::PowerPresent, &pinConfig, nrfx_gpiote_evt_handler);
|
2021-10-03 16:47:01 +02:00
|
|
|
nrfx_gpiote_in_event_enable(PinMap::PowerPresent, true);
|
2021-05-16 21:13:22 +02:00
|
|
|
|
2021-10-09 12:39:27 +02:00
|
|
|
batteryController.MeasureVoltage();
|
|
|
|
|
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);
|
2021-08-14 20:18:11 +02:00
|
|
|
measureBatteryTimer = xTimerCreate("measureBattery", batteryMeasurementPeriod, pdTRUE, this, MeasureBatteryTimerCallback);
|
2021-07-24 20:29:10 +02:00
|
|
|
xTimerStart(dimTimer, 0);
|
2021-08-14 20:18:11 +02:00
|
|
|
xTimerStart(measureBatteryTimer, portMAX_DELAY);
|
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
|
|
|
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
|
|
|
|
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);
|
|
|
|
heartRateApp.PushMessage(Pinetime::Applications::HeartRateTask::Messages::WakeUp);
|
2020-09-13 21:26:44 +02:00
|
|
|
|
2021-09-05 22:53:20 +02:00
|
|
|
if (!bleController.IsConnected())
|
|
|
|
nimbleController.RestartFastAdv();
|
|
|
|
|
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: {
|
2021-09-07 21:38:48 +02:00
|
|
|
if (touchHandler.GetNewTouchInfo()) {
|
2021-08-28 21:30:31 +02:00
|
|
|
auto gesture = touchHandler.GestureGet();
|
2021-09-07 21:38:48 +02:00
|
|
|
if (gesture != Pinetime::Drivers::Cst816S::Gestures::None and
|
|
|
|
((gesture == Pinetime::Drivers::Cst816S::Gestures::DoubleTap and
|
|
|
|
settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) or
|
|
|
|
(gesture == Pinetime::Drivers::Cst816S::Gestures::SingleTap and
|
|
|
|
settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::SingleTap)))) {
|
2021-08-28 21:30:31 +02:00
|
|
|
GoToRunning();
|
|
|
|
}
|
2021-04-05 16:22:10 +02:00
|
|
|
}
|
|
|
|
} 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-09-12 10:08:25 +02:00
|
|
|
if (settingsController.GetNotificationStatus() == Pinetime::Controllers::Settings::Notification::ON) {
|
|
|
|
if (isSleeping && !isWakingUp) {
|
|
|
|
GoToRunning();
|
2021-09-07 21:38:48 +02:00
|
|
|
} else {
|
|
|
|
ReloadIdleTimer();
|
2021-09-12 10:08:25 +02:00
|
|
|
}
|
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::NewNotification);
|
2021-05-20 20:43:54 +02:00
|
|
|
}
|
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;
|
2021-09-11 00:40:13 +02:00
|
|
|
case Messages::SetOffAlarm:
|
|
|
|
if (isSleeping && !isWakingUp) {
|
|
|
|
GoToRunning();
|
|
|
|
}
|
2021-09-16 21:38:31 +02:00
|
|
|
motorController.StartRinging();
|
2021-09-11 00:40:13 +02:00
|
|
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::AlarmTriggered);
|
|
|
|
break;
|
|
|
|
case Messages::StopRinging:
|
|
|
|
motorController.StopRinging();
|
|
|
|
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;
|
2021-10-25 15:57:29 +02:00
|
|
|
case Messages::HandleButtonEvent: {
|
|
|
|
Controllers::ButtonActions action;
|
2021-10-25 11:53:14 +02:00
|
|
|
if (nrf_gpio_pin_read(Pinetime::PinMap::Button) == 0) {
|
2021-10-25 15:57:29 +02:00
|
|
|
action = buttonHandler.HandleEvent(Controllers::ButtonHandler::Events::Release);
|
2021-10-25 11:53:14 +02:00
|
|
|
} else {
|
2021-10-25 16:45:48 +02:00
|
|
|
action = buttonHandler.HandleEvent(Controllers::ButtonHandler::Events::Press);
|
|
|
|
// This is for faster wakeup, sacrificing special longpress and doubleclick handling while sleeping
|
2021-10-25 16:13:02 +02:00
|
|
|
if (IsSleeping()) {
|
2021-10-25 16:45:48 +02:00
|
|
|
fastWakeUpDone = true;
|
2021-10-25 16:13:02 +02:00
|
|
|
GoToRunning();
|
|
|
|
break;
|
|
|
|
}
|
2021-10-25 11:53:14 +02:00
|
|
|
}
|
2021-10-25 15:57:29 +02:00
|
|
|
HandleButtonAction(action);
|
|
|
|
} break;
|
|
|
|
case Messages::HandleButtonTimerEvent: {
|
|
|
|
auto action = buttonHandler.HandleEvent(Controllers::ButtonHandler::Events::Timer);
|
|
|
|
HandleButtonAction(action);
|
|
|
|
} 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-10-09 12:39:27 +02:00
|
|
|
batteryController.ReadPowerState();
|
2021-08-01 12:05:48 +02:00
|
|
|
motorController.RunForDuration(15);
|
2021-10-08 17:01:27 +02:00
|
|
|
ReloadIdleTimer();
|
2021-10-08 16:53:49 +02:00
|
|
|
if (isSleeping && !isWakingUp) {
|
|
|
|
GoToRunning();
|
|
|
|
}
|
2021-08-14 20:18:11 +02:00
|
|
|
break;
|
|
|
|
case Messages::MeasureBatteryTimerExpired:
|
2021-10-09 12:39:27 +02:00
|
|
|
batteryController.MeasureVoltage();
|
2021-08-14 20:18:11 +02:00
|
|
|
break;
|
2021-10-04 00:41:38 +02:00
|
|
|
case Messages::BatteryPercentageUpdated:
|
|
|
|
nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining());
|
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
|
2021-09-13 20:02:53 +02:00
|
|
|
// target and vice-versa. I'm not sure if this is the right way to handle this...
|
2020-05-01 21:58:31 +02:00
|
|
|
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-08-19 02:51:12 +02:00
|
|
|
NoInit_BackUpTime = dateTimeController.CurrentDateTime();
|
2021-08-03 20:32:23 +02:00
|
|
|
if (!nrf_gpio_pin_read(PinMap::Button))
|
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
|
|
|
|
2021-10-25 15:57:29 +02:00
|
|
|
void SystemTask::HandleButtonAction(Controllers::ButtonActions action) {
|
|
|
|
if (IsSleeping()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReloadIdleTimer();
|
|
|
|
|
|
|
|
using Actions = Controllers::ButtonActions;
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case Actions::Click:
|
2021-10-25 16:45:48 +02:00
|
|
|
// If the first action after fast wakeup is a click, it should be ignored.
|
|
|
|
if (!fastWakeUpDone && !isGoingToSleep) {
|
2021-10-25 15:57:29 +02:00
|
|
|
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:
|
2021-10-25 16:45:48 +02:00
|
|
|
return;
|
2021-10-25 15:57:29 +02:00
|
|
|
}
|
2021-10-25 16:45:48 +02:00
|
|
|
|
|
|
|
fastWakeUpDone = false;
|
2021-10-25 15:57:29 +02: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
|
|
|
}
|