diff --git a/src/components/datetime/DateTimeController.cpp b/src/components/datetime/DateTimeController.cpp index 59982477..9c1f2684 100644 --- a/src/components/datetime/DateTimeController.cpp +++ b/src/components/datetime/DateTimeController.cpp @@ -1,9 +1,14 @@ #include "DateTimeController.h" #include #include +#include using namespace Pinetime::Controllers; +DateTime::DateTime(System::SystemTask& systemTask) : systemTask{systemTask} { + +} + void DateTime::SetTime(uint16_t year, uint8_t month, uint8_t day, uint8_t dayOfWeek, uint8_t hour, uint8_t minute, uint8_t second, uint32_t systickCounter) { @@ -62,6 +67,14 @@ void DateTime::UpdateTime(uint32_t systickCounter) { hour = time.hours().count(); minute = time.minutes().count(); second = time.seconds().count(); + + // Notify new day to SystemTask + if(hour == 0 and not isMidnightAlreadyNotified) { + isMidnightAlreadyNotified = true; + systemTask.PushMessage(System::SystemTask::Messages::OnNewDay); + } else if (hour != 0) { + isMidnightAlreadyNotified = false; + } } const char *DateTime::MonthShortToString() { diff --git a/src/components/datetime/DateTimeController.h b/src/components/datetime/DateTimeController.h index 16bb59c9..fb0969d1 100644 --- a/src/components/datetime/DateTimeController.h +++ b/src/components/datetime/DateTimeController.h @@ -4,12 +4,17 @@ #include namespace Pinetime { + namespace System { + class SystemTask; + } namespace Controllers { class DateTime { public: enum class Days : uint8_t {Unknown, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}; enum class Months : uint8_t {Unknown, January, February, March, April, May, June, July, August, September, October, November, December}; + DateTime(System::SystemTask& systemTask); + void SetTime(uint16_t year, uint8_t month, uint8_t day, uint8_t dayOfWeek, uint8_t hour, uint8_t minute, uint8_t second, uint32_t systickCounter); void UpdateTime(uint32_t systickCounter); uint16_t Year() const { return year; } @@ -31,6 +36,7 @@ namespace Pinetime { std::chrono::time_point CurrentDateTime() const { return currentDateTime; } std::chrono::seconds Uptime() const { return uptime; } private: + System::SystemTask& systemTask; uint16_t year = 0; Months month = Months::Unknown; uint8_t day = 0; @@ -43,6 +49,8 @@ namespace Pinetime { std::chrono::time_point currentDateTime; std::chrono::seconds uptime {0}; + bool isMidnightAlreadyNotified = false; + static char const *DaysString[]; static char const *DaysStringShort[]; static char const *DaysStringLow[]; diff --git a/src/drivers/Bma421.cpp b/src/drivers/Bma421.cpp index 4e33ef8b..10d3e5e8 100644 --- a/src/drivers/Bma421.cpp +++ b/src/drivers/Bma421.cpp @@ -105,3 +105,7 @@ Bma421::Values Bma421::Process() { bool Bma421::IsOk() const { return isOk; } + +void Bma421::ResetStepCounter() { + bma423_reset_step_counter(&bma); +} diff --git a/src/drivers/Bma421.h b/src/drivers/Bma421.h index e2e24f0d..d36d1db5 100644 --- a/src/drivers/Bma421.h +++ b/src/drivers/Bma421.h @@ -19,8 +19,8 @@ namespace Pinetime { Bma421& operator=(Bma421&&) = delete; void Init(); - void Reset(); Values Process(); + void ResetStepCounter(); void Read(uint8_t registerAddress, uint8_t *buffer, size_t size); void Write(uint8_t registerAddress, const uint8_t *data, size_t size); @@ -28,6 +28,8 @@ namespace Pinetime { bool IsOk() const; private: + void Reset(); + TwiMaster& twiMaster; uint8_t deviceAddress = 0x18; struct bma4_dev bma; diff --git a/src/main.cpp b/src/main.cpp index f1d1bcf7..6cca9c8b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -107,7 +107,6 @@ Pinetime::Drivers::Hrs3300 heartRateSensor {twiMaster, heartRateSensorTwiAddress TimerHandle_t debounceTimer; Pinetime::Controllers::Battery batteryController; Pinetime::Controllers::Ble bleController; -Pinetime::Controllers::DateTime dateTimeController; void ble_manager_set_ble_connection_callback(void (*connection)()); void ble_manager_set_ble_disconnection_callback(void (*disconnection)()); static constexpr uint8_t pinTouchIrq = 28; @@ -259,7 +258,7 @@ int main(void) { debounceTimer = xTimerCreate ("debounceTimer", 200, pdFALSE, (void *) 0, DebounceTimerCallback); systemTask = std::make_unique(spi, lcd, spiNorFlash, twiMaster, touchPanel, lvgl, batteryController, bleController, - dateTimeController, motorController, heartRateSensor, motionSensor, settingsController); + motorController, heartRateSensor, motionSensor, settingsController); systemTask->Start(); nimble_port_init(); diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index d2f38adb..4605ba95 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -40,7 +40,6 @@ SystemTask::SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, Drivers::TwiMaster& twiMaster, Drivers::Cst816S &touchPanel, Components::LittleVgl &lvgl, Controllers::Battery &batteryController, Controllers::Ble &bleController, - Controllers::DateTime &dateTimeController, Pinetime::Controllers::MotorController& motorController, Pinetime::Drivers::Hrs3300& heartRateSensor, Pinetime::Drivers::Bma421& motionSensor, @@ -48,7 +47,7 @@ SystemTask::SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, spi{spi}, lcd{lcd}, spiNorFlash{spiNorFlash}, twiMaster{twiMaster}, touchPanel{touchPanel}, lvgl{lvgl}, batteryController{batteryController}, heartRateController{*this}, - bleController{bleController}, dateTimeController{dateTimeController}, + bleController{bleController}, dateTimeController{*this}, watchdog{}, watchdogView{watchdog}, motorController{motorController}, heartRateSensor{heartRateSensor}, motionSensor{motionSensor}, settingsController{settingsController}, @@ -210,6 +209,11 @@ void SystemTask::Work() { isSleeping = true; isGoingToSleep = false; break; + 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; default: break; } } @@ -239,6 +243,12 @@ void SystemTask::UpdateMotion() { if(isSleeping) twiMaster.Wakeup(); + + if(stepCounterMustBeReset) { + motionSensor.ResetStepCounter(); + stepCounterMustBeReset = false; + } + auto motionValues = motionSensor.Process(); if(isSleeping) twiMaster.Sleep(); diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index 2b70d19a..bb2a2868 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -40,7 +40,8 @@ namespace Pinetime { class SystemTask { public: enum class Messages {GoToSleep, GoToRunning, OnNewTime, OnNewNotification, OnNewCall, BleConnected, - BleFirmwareUpdateStarted, BleFirmwareUpdateFinished, OnTouchEvent, OnButtonEvent, OnDisplayTaskSleeping + BleFirmwareUpdateStarted, BleFirmwareUpdateFinished, OnTouchEvent, OnButtonEvent, OnDisplayTaskSleeping, + OnNewDay }; SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, @@ -48,7 +49,6 @@ namespace Pinetime { Drivers::TwiMaster& twiMaster, Drivers::Cst816S &touchPanel, Components::LittleVgl &lvgl, Controllers::Battery &batteryController, Controllers::Ble &bleController, - Controllers::DateTime &dateTimeController, Pinetime::Controllers::MotorController& motorController, Pinetime::Drivers::Hrs3300& heartRateSensor, Pinetime::Drivers::Bma421& motionSensor, @@ -80,7 +80,7 @@ namespace Pinetime { std::unique_ptr heartRateApp; Pinetime::Controllers::Ble& bleController; - Pinetime::Controllers::DateTime& dateTimeController; + Pinetime::Controllers::DateTime dateTimeController; QueueHandle_t systemTasksMsgQueue; std::atomic isSleeping{false}; std::atomic isGoingToSleep{false}; @@ -115,6 +115,7 @@ namespace Pinetime { void GoToRunning(); void UpdateMotion(); + bool stepCounterMustBeReset = false; #if configUSE_TRACE_FACILITY == 1 SystemMonitor monitor;