From 20ac7e8df38836d0a72c378b900b99c827c996c2 Mon Sep 17 00:00:00 2001 From: KaffeinatedKat Date: Fri, 29 Sep 2023 21:00:07 -0600 Subject: [PATCH 01/16] feat: always on display --- src/components/settings/Settings.h | 15 ++++++++++++++- src/displayapp/DisplayApp.cpp | 6 +++++- .../screens/settings/SettingDisplay.cpp | 14 ++++++++++++-- src/displayapp/screens/settings/SettingDisplay.h | 2 +- src/systemtask/SystemTask.cpp | 15 +++++++++++---- 5 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/components/settings/Settings.h b/src/components/settings/Settings.h index 06312077..d75cd678 100644 --- a/src/components/settings/Settings.h +++ b/src/components/settings/Settings.h @@ -214,6 +214,17 @@ namespace Pinetime { return settings.screenTimeOut; }; + void SetAlwaysOnDisplay(bool state) { + if (state != settings.alwaysOnDisplay) { + settingsChanged = true; + } + settings.alwaysOnDisplay = state; + }; + + bool GetAlwaysOnDisplay() const { + return settings.alwaysOnDisplay; + }; + void SetShakeThreshold(uint16_t thresh) { if (settings.shakeWakeThreshold != thresh) { settings.shakeWakeThreshold = thresh; @@ -286,13 +297,15 @@ namespace Pinetime { private: Pinetime::Controllers::FS& fs; - static constexpr uint32_t settingsVersion = 0x0007; + static constexpr uint32_t settingsVersion = 0x0008; struct SettingsData { uint32_t version = settingsVersion; uint32_t stepsGoal = 10000; uint32_t screenTimeOut = 15000; + bool alwaysOnDisplay = false; + ClockType clockType = ClockType::H24; WeatherFormat weatherFormat = WeatherFormat::Metric; Notification notificationStatus = Notification::On; diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index 3fd34b3a..c7fb62ab 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -203,7 +203,11 @@ void DisplayApp::Refresh() { TickType_t queueTimeout; switch (state) { case States::Idle: - queueTimeout = portMAX_DELAY; + if (settingsController.GetAlwaysOnDisplay()) { + queueTimeout = lv_task_handler(); + } else { + queueTimeout = portMAX_DELAY; + } break; case States::Running: if (!currentScreen->IsRunning()) { diff --git a/src/displayapp/screens/settings/SettingDisplay.cpp b/src/displayapp/screens/settings/SettingDisplay.cpp index bd533e67..760f1e9e 100644 --- a/src/displayapp/screens/settings/SettingDisplay.cpp +++ b/src/displayapp/screens/settings/SettingDisplay.cpp @@ -15,7 +15,7 @@ namespace { } } -constexpr std::array SettingDisplay::options; +constexpr std::array SettingDisplay::options; SettingDisplay::SettingDisplay(Pinetime::Applications::DisplayApp* app, Pinetime::Controllers::Settings& settingsController) : app {app}, settingsController {settingsController} { @@ -46,7 +46,11 @@ SettingDisplay::SettingDisplay(Pinetime::Applications::DisplayApp* app, Pinetime char buffer[4]; for (unsigned int i = 0; i < options.size(); i++) { cbOption[i] = lv_checkbox_create(container1, nullptr); - snprintf(buffer, sizeof(buffer), "%2" PRIu16 "s", options[i] / 1000); + if (options[i] == 0) { + sprintf(buffer, "%s", "Always On"); + } else { + sprintf(buffer, "%2ds", options[i] / 1000); + } lv_checkbox_set_text(cbOption[i], buffer); cbOption[i]->user_data = this; lv_obj_set_event_cb(cbOption[i], event_handler); @@ -64,6 +68,12 @@ SettingDisplay::~SettingDisplay() { } void SettingDisplay::UpdateSelected(lv_obj_t* object, lv_event_t event) { + if (settingsController.GetScreenTimeOut() == 0) { + settingsController.SetAlwaysOnDisplay(true); + } else { + settingsController.SetAlwaysOnDisplay(false); + } + if (event == LV_EVENT_CLICKED) { for (unsigned int i = 0; i < options.size(); i++) { if (object == cbOption[i]) { diff --git a/src/displayapp/screens/settings/SettingDisplay.h b/src/displayapp/screens/settings/SettingDisplay.h index 64212c02..a4370463 100644 --- a/src/displayapp/screens/settings/SettingDisplay.h +++ b/src/displayapp/screens/settings/SettingDisplay.h @@ -21,7 +21,7 @@ namespace Pinetime { private: DisplayApp* app; - static constexpr std::array options = {5000, 7000, 10000, 15000, 20000, 30000}; + static constexpr std::array options = {5000, 7000, 10000, 15000, 20000, 30000, 0}; Controllers::Settings& settingsController; lv_obj_t* cbOption[options.size()]; diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index a56c2591..fb7493aa 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -198,7 +198,10 @@ void SystemTask::Work() { doNotGoToSleep = true; break; case Messages::GoToRunning: - spi.Wakeup(); + // SPI doesn't go to sleep for always on mode + if (!settingsController.GetAlwaysOnDisplay()) { + spi.Wakeup(); + } // Double Tap needs the touch screen to be in normal mode if (!settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) { @@ -231,7 +234,7 @@ void SystemTask::Work() { break; } case Messages::GoToSleep: - if (doNotGoToSleep) { + if (doNotGoToSleep or settingsController.GetAlwaysOnDisplay()) { break; } state = SystemTaskState::GoingToSleep; // Already set in PushMessage() @@ -323,7 +326,11 @@ void SystemTask::Work() { // if it's in sleep mode. Avoid bricked device by disabling sleep mode on these versions. spiNorFlash.Sleep(); } - spi.Sleep(); + + // Must keep SPI awake when still updating the display for always on + if (!settingsController.GetAlwaysOnDisplay()) { + spi.Sleep(); + } // Double Tap needs the touch screen to be in normal mode if (!settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) { @@ -503,7 +510,7 @@ void SystemTask::OnTouchEvent() { } void SystemTask::PushMessage(System::Messages msg) { - if (msg == Messages::GoToSleep && !doNotGoToSleep) { + if (msg == Messages::GoToSleep && !doNotGoToSleep && !settingsController.GetAlwaysOnDisplay()) { state = SystemTaskState::GoingToSleep; } From 3dca742b6566859aee89e1b943ae2ea5fc0eaa95 Mon Sep 17 00:00:00 2001 From: mark9064 <30447455+mark9064@users.noreply.github.com> Date: Mon, 22 Jan 2024 23:37:52 +0000 Subject: [PATCH 02/16] aod: PPI/RTC-based backlight brightness --- .../brightness/BrightnessController.cpp | 124 ++++++++++++++++-- .../brightness/BrightnessController.h | 24 +++- src/displayapp/DisplayApp.cpp | 10 +- src/systemtask/SystemTask.cpp | 4 +- 4 files changed, 146 insertions(+), 16 deletions(-) diff --git a/src/components/brightness/BrightnessController.cpp b/src/components/brightness/BrightnessController.cpp index 0392158c..4d1eba6a 100644 --- a/src/components/brightness/BrightnessController.cpp +++ b/src/components/brightness/BrightnessController.cpp @@ -2,38 +2,138 @@ #include #include "displayapp/screens/Symbols.h" #include "drivers/PinMap.h" +#include using namespace Pinetime::Controllers; +namespace { + // reinterpret_cast is not constexpr so this is the best we can do + static NRF_RTC_Type* const RTC = reinterpret_cast(NRF_RTC2_BASE); +} + void BrightnessController::Init() { nrf_gpio_cfg_output(PinMap::LcdBacklightLow); nrf_gpio_cfg_output(PinMap::LcdBacklightMedium); nrf_gpio_cfg_output(PinMap::LcdBacklightHigh); + + nrf_gpio_pin_clear(PinMap::LcdBacklightLow); + nrf_gpio_pin_clear(PinMap::LcdBacklightMedium); + nrf_gpio_pin_clear(PinMap::LcdBacklightHigh); + + static_assert(timerFrequency == 32768, "Change the prescaler below"); + RTC->PRESCALER = 0; + // CC1 switches the backlight on (pin transitions from high to low) and resets the counter to 0 + RTC->CC[1] = timerPeriod; + // Enable compare events for CC0,CC1 + RTC->EVTEN = 0b0000'0000'0000'0011'0000'0000'0000'0000; + // Disable all interrupts + RTC->INTENCLR = 0b0000'0000'0000'1111'0000'0000'0000'0011; Set(level); } +void BrightnessController::ApplyBrightness(uint16_t rawBrightness) { + // The classic off, low, medium, high brightnesses are at {0, timerPeriod, timerPeriod*2, timerPeriod*3} + // These brightness levels do not use PWM: they only set/clear the corresponding pins + // Any brightness level between the above levels is achieved with efficient RTC based PWM on the next pin up + // E.g 2.5*timerPeriod corresponds to medium brightness with 50% PWM on the high pin + // Note: Raw brightness does not necessarily correspond to a linear perceived brightness + + uint8_t pin; + if (rawBrightness > 2 * timerPeriod) { + rawBrightness -= 2 * timerPeriod; + pin = PinMap::LcdBacklightHigh; + } else if (rawBrightness > timerPeriod) { + rawBrightness -= timerPeriod; + pin = PinMap::LcdBacklightMedium; + } else { + pin = PinMap::LcdBacklightLow; + } + if (rawBrightness == timerPeriod || rawBrightness == 0) { + if (lastPin != UNSET) { + RTC->TASKS_STOP = 1; + nrf_delay_us(rtcStopTime); + nrf_ppi_channel_disable(ppiBacklightOff); + nrf_ppi_channel_disable(ppiBacklightOn); + nrfx_gpiote_out_uninit(lastPin); + nrf_gpio_cfg_output(lastPin); + } + lastPin = UNSET; + if (rawBrightness == 0) { + nrf_gpio_pin_set(pin); + } else { + nrf_gpio_pin_clear(pin); + } + } else { + // If the pin on which we are doing PWM is changing + // Disable old PWM channel (if exists) and set up new one + if (lastPin != pin) { + if (lastPin != UNSET) { + RTC->TASKS_STOP = 1; + nrf_delay_us(rtcStopTime); + nrf_ppi_channel_disable(ppiBacklightOff); + nrf_ppi_channel_disable(ppiBacklightOn); + nrfx_gpiote_out_uninit(lastPin); + nrf_gpio_cfg_output(lastPin); + } + nrfx_gpiote_out_config_t gpioteCfg = {.action = NRF_GPIOTE_POLARITY_TOGGLE, + .init_state = NRF_GPIOTE_INITIAL_VALUE_LOW, + .task_pin = true}; + APP_ERROR_CHECK(nrfx_gpiote_out_init(pin, &gpioteCfg)); + nrfx_gpiote_out_task_enable(pin); + nrf_ppi_channel_endpoint_setup(ppiBacklightOff, + reinterpret_cast(&RTC->EVENTS_COMPARE[0]), + nrfx_gpiote_out_task_addr_get(pin)); + nrf_ppi_channel_endpoint_setup(ppiBacklightOn, + reinterpret_cast(&RTC->EVENTS_COMPARE[1]), + nrfx_gpiote_out_task_addr_get(pin)); + nrf_ppi_fork_endpoint_setup(ppiBacklightOn, reinterpret_cast(&RTC->TASKS_CLEAR)); + nrf_ppi_channel_enable(ppiBacklightOff); + nrf_ppi_channel_enable(ppiBacklightOn); + } else { + // If the pin used for PWM isn't changing, we only need to set the pin state to the initial value (low) + RTC->TASKS_STOP = 1; + nrf_delay_us(rtcStopTime); + // Due to errata 20,179 and the intricacies of RTC timing, keep it simple: override the pin state + nrfx_gpiote_out_task_force(pin, false); + } + // CC0 switches the backlight off (pin transitions from low to high) + RTC->CC[0] = rawBrightness; + RTC->TASKS_CLEAR = 1; + RTC->TASKS_START = 1; + lastPin = pin; + } + switch (pin) { + case PinMap::LcdBacklightHigh: + nrf_gpio_pin_clear(PinMap::LcdBacklightLow); + nrf_gpio_pin_clear(PinMap::LcdBacklightMedium); + break; + case PinMap::LcdBacklightMedium: + nrf_gpio_pin_clear(PinMap::LcdBacklightLow); + nrf_gpio_pin_set(PinMap::LcdBacklightHigh); + break; + case PinMap::LcdBacklightLow: + nrf_gpio_pin_set(PinMap::LcdBacklightMedium); + nrf_gpio_pin_set(PinMap::LcdBacklightHigh); + } +} + void BrightnessController::Set(BrightnessController::Levels level) { this->level = level; switch (level) { default: case Levels::High: - nrf_gpio_pin_clear(PinMap::LcdBacklightLow); - nrf_gpio_pin_clear(PinMap::LcdBacklightMedium); - nrf_gpio_pin_clear(PinMap::LcdBacklightHigh); + ApplyBrightness(3 * timerPeriod); break; case Levels::Medium: - nrf_gpio_pin_clear(PinMap::LcdBacklightLow); - nrf_gpio_pin_clear(PinMap::LcdBacklightMedium); - nrf_gpio_pin_set(PinMap::LcdBacklightHigh); + ApplyBrightness(2 * timerPeriod); break; case Levels::Low: - nrf_gpio_pin_clear(PinMap::LcdBacklightLow); - nrf_gpio_pin_set(PinMap::LcdBacklightMedium); - nrf_gpio_pin_set(PinMap::LcdBacklightHigh); + ApplyBrightness(timerPeriod); + break; + case Levels::AlwaysOn: + ApplyBrightness(timerPeriod / 10); break; case Levels::Off: - nrf_gpio_pin_set(PinMap::LcdBacklightLow); - nrf_gpio_pin_set(PinMap::LcdBacklightMedium); - nrf_gpio_pin_set(PinMap::LcdBacklightHigh); + ApplyBrightness(0); break; } } diff --git a/src/components/brightness/BrightnessController.h b/src/components/brightness/BrightnessController.h index 7f86759a..650749a8 100644 --- a/src/components/brightness/BrightnessController.h +++ b/src/components/brightness/BrightnessController.h @@ -2,11 +2,14 @@ #include +#include "nrf_ppi.h" +#include "nrfx_gpiote.h" + namespace Pinetime { namespace Controllers { class BrightnessController { public: - enum class Levels { Off, Low, Medium, High }; + enum class Levels { Off, AlwaysOn, Low, Medium, High }; void Init(); void Set(Levels level); @@ -20,6 +23,25 @@ namespace Pinetime { private: Levels level = Levels::High; + static constexpr uint8_t UNSET = UINT8_MAX; + uint8_t lastPin = UNSET; + // Maximum time (μs) it takes for the RTC to fully stop + static constexpr uint8_t rtcStopTime = 46; + // Frequency of timer used for PWM (Hz) + static constexpr uint16_t timerFrequency = 32768; + // Backlight PWM frequency (Hz) + static constexpr uint16_t pwmFreq = 1000; + // Wraparound point in timer ticks + // Defines the number of brightness levels between each pin + static constexpr uint16_t timerPeriod = timerFrequency / pwmFreq; + // Warning: nimble reserves some PPIs + // https://github.com/InfiniTimeOrg/InfiniTime/blob/034d83fe6baf1ab3875a34f8cee387e24410a824/src/libs/mynewt-nimble/nimble/drivers/nrf52/src/ble_phy.c#L53 + // SpiMaster uses PPI 0 for an erratum workaround + // Channel 1, 2 should be free to use + static constexpr nrf_ppi_channel_t ppiBacklightOn = NRF_PPI_CHANNEL1; + static constexpr nrf_ppi_channel_t ppiBacklightOff = NRF_PPI_CHANNEL2; + + void ApplyBrightness(uint16_t val); }; } } diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index c7fb62ab..5e68ef23 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -242,11 +242,17 @@ void DisplayApp::Refresh() { RestoreBrightness(); break; case Messages::GoToSleep: - while (brightnessController.Level() != Controllers::BrightnessController::Levels::Off) { + while (brightnessController.Level() != Controllers::BrightnessController::Levels::Low) { brightnessController.Lower(); vTaskDelay(100); } - lcd.Sleep(); + // Don't actually turn off the display for AlwaysOn mode + if (settingsController.GetAlwaysOnDisplay()) { + brightnessController.Set(Controllers::BrightnessController::Levels::AlwaysOn); + } else { + brightnessController.Set(Controllers::BrightnessController::Levels::Off); + lcd.Sleep(); + } PushMessageToSystemTask(Pinetime::System::Messages::OnDisplayTaskSleeping); state = States::Idle; break; diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index fb7493aa..0dea5f98 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -102,7 +102,9 @@ void SystemTask::Work() { watchdog.Setup(7, Drivers::Watchdog::SleepBehaviour::Run, Drivers::Watchdog::HaltBehaviour::Pause); watchdog.Start(); NRF_LOG_INFO("Last reset reason : %s", Pinetime::Drivers::ResetReasonToString(watchdog.GetResetReason())); - APP_GPIOTE_INIT(2); + if (!nrfx_gpiote_is_init()) { + nrfx_gpiote_init(); + } spi.Init(); spiNorFlash.Init(); From 85a2181b648d4219fef269e562bbfc1a4f4c9436 Mon Sep 17 00:00:00 2001 From: John Crawford Date: Sun, 1 Oct 2023 10:38:45 -0600 Subject: [PATCH 03/16] aod: integrate with display timeout --- .../screens/settings/SettingDisplay.cpp | 37 ++++++++++++------- .../screens/settings/SettingDisplay.h | 4 +- src/systemtask/SystemTask.cpp | 4 +- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/displayapp/screens/settings/SettingDisplay.cpp b/src/displayapp/screens/settings/SettingDisplay.cpp index 760f1e9e..12d0f561 100644 --- a/src/displayapp/screens/settings/SettingDisplay.cpp +++ b/src/displayapp/screens/settings/SettingDisplay.cpp @@ -9,13 +9,20 @@ using namespace Pinetime::Applications::Screens; namespace { - void event_handler(lv_obj_t* obj, lv_event_t event) { + void TimeoutEventHandler(lv_obj_t* obj, lv_event_t event) { auto* screen = static_cast(obj->user_data); screen->UpdateSelected(obj, event); } + + void AlwaysOnEventHandler(lv_obj_t* obj, lv_event_t event) { + if (event == LV_EVENT_VALUE_CHANGED) { + auto* screen = static_cast(obj->user_data); + screen->ToggleAlwaysOn(); + } + } } -constexpr std::array SettingDisplay::options; +constexpr std::array SettingDisplay::options; SettingDisplay::SettingDisplay(Pinetime::Applications::DisplayApp* app, Pinetime::Controllers::Settings& settingsController) : app {app}, settingsController {settingsController} { @@ -46,20 +53,23 @@ SettingDisplay::SettingDisplay(Pinetime::Applications::DisplayApp* app, Pinetime char buffer[4]; for (unsigned int i = 0; i < options.size(); i++) { cbOption[i] = lv_checkbox_create(container1, nullptr); - if (options[i] == 0) { - sprintf(buffer, "%s", "Always On"); - } else { - sprintf(buffer, "%2ds", options[i] / 1000); - } + snprintf(buffer, sizeof(buffer), "%2" PRIu16 "s", options[i] / 1000); lv_checkbox_set_text(cbOption[i], buffer); cbOption[i]->user_data = this; - lv_obj_set_event_cb(cbOption[i], event_handler); + lv_obj_set_event_cb(cbOption[i], TimeoutEventHandler); SetRadioButtonStyle(cbOption[i]); if (settingsController.GetScreenTimeOut() == options[i]) { lv_checkbox_set_checked(cbOption[i], true); } } + + alwaysOnCheckbox = lv_checkbox_create(container1, nullptr); + lv_checkbox_set_text(alwaysOnCheckbox, "Always On"); + lv_checkbox_set_checked(alwaysOnCheckbox, settingsController.GetAlwaysOnDisplay()); + lv_obj_add_state(alwaysOnCheckbox, LV_STATE_DEFAULT); + alwaysOnCheckbox->user_data = this; + lv_obj_set_event_cb(alwaysOnCheckbox, AlwaysOnEventHandler); } SettingDisplay::~SettingDisplay() { @@ -67,13 +77,12 @@ SettingDisplay::~SettingDisplay() { settingsController.SaveSettings(); } -void SettingDisplay::UpdateSelected(lv_obj_t* object, lv_event_t event) { - if (settingsController.GetScreenTimeOut() == 0) { - settingsController.SetAlwaysOnDisplay(true); - } else { - settingsController.SetAlwaysOnDisplay(false); - } +void SettingDisplay::ToggleAlwaysOn() { + settingsController.SetAlwaysOnDisplay(!settingsController.GetAlwaysOnDisplay()); + lv_checkbox_set_checked(alwaysOnCheckbox, settingsController.GetAlwaysOnDisplay()); +} +void SettingDisplay::UpdateSelected(lv_obj_t* object, lv_event_t event) { if (event == LV_EVENT_CLICKED) { for (unsigned int i = 0; i < options.size(); i++) { if (object == cbOption[i]) { diff --git a/src/displayapp/screens/settings/SettingDisplay.h b/src/displayapp/screens/settings/SettingDisplay.h index a4370463..b6d147c8 100644 --- a/src/displayapp/screens/settings/SettingDisplay.h +++ b/src/displayapp/screens/settings/SettingDisplay.h @@ -18,13 +18,15 @@ namespace Pinetime { ~SettingDisplay() override; void UpdateSelected(lv_obj_t* object, lv_event_t event); + void ToggleAlwaysOn(); private: DisplayApp* app; - static constexpr std::array options = {5000, 7000, 10000, 15000, 20000, 30000, 0}; + static constexpr std::array options = {5000, 7000, 10000, 15000, 20000, 30000}; Controllers::Settings& settingsController; lv_obj_t* cbOption[options.size()]; + lv_obj_t* alwaysOnCheckbox; }; } } diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 0dea5f98..5bd71c38 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -236,7 +236,7 @@ void SystemTask::Work() { break; } case Messages::GoToSleep: - if (doNotGoToSleep or settingsController.GetAlwaysOnDisplay()) { + if (doNotGoToSleep) { break; } state = SystemTaskState::GoingToSleep; // Already set in PushMessage() @@ -512,7 +512,7 @@ void SystemTask::OnTouchEvent() { } void SystemTask::PushMessage(System::Messages msg) { - if (msg == Messages::GoToSleep && !doNotGoToSleep && !settingsController.GetAlwaysOnDisplay()) { + if (msg == Messages::GoToSleep && !doNotGoToSleep) { state = SystemTaskState::GoingToSleep; } From e884b053d32d4a7c3b4464e07edaddfbb334ec27 Mon Sep 17 00:00:00 2001 From: John Crawford Date: Tue, 3 Oct 2023 18:50:36 -0600 Subject: [PATCH 04/16] aod: disable while in notification sleep --- src/components/settings/Settings.h | 39 +++++++++++++++++-- .../screens/settings/SettingDisplay.cpp | 6 +-- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/components/settings/Settings.h b/src/components/settings/Settings.h index d75cd678..1ab67095 100644 --- a/src/components/settings/Settings.h +++ b/src/components/settings/Settings.h @@ -196,6 +196,14 @@ namespace Pinetime { if (status != settings.notificationStatus) { settingsChanged = true; } + // Disable always on screen while sleep mode is enabled + if (settings.alwaysOnDisplay.enabled) { + if (status == Notification::Sleep) { + settings.alwaysOnDisplay.state = false; + } else { + settings.alwaysOnDisplay.state = true; + } + } settings.notificationStatus = status; }; @@ -215,16 +223,32 @@ namespace Pinetime { }; void SetAlwaysOnDisplay(bool state) { - if (state != settings.alwaysOnDisplay) { + if (state != settings.alwaysOnDisplay.state) { settingsChanged = true; } - settings.alwaysOnDisplay = state; + settings.alwaysOnDisplay.state = state; }; bool GetAlwaysOnDisplay() const { - return settings.alwaysOnDisplay; + return settings.alwaysOnDisplay.state; }; + void SetAlwaysOnDisplaySetting(bool state) { + if (state != settings.alwaysOnDisplay.enabled) { + settingsChanged = true; + } + settings.alwaysOnDisplay.enabled = state; + + // Don't enable always on if we are currently in notification sleep + if (GetNotificationStatus() != Notification::Sleep) { + SetAlwaysOnDisplay(state); + } + } + + bool GetAlwaysOnDisplaySetting() const { + return settings.alwaysOnDisplay.enabled; + } + void SetShakeThreshold(uint16_t thresh) { if (settings.shakeWakeThreshold != thresh) { settings.shakeWakeThreshold = thresh; @@ -299,12 +323,19 @@ namespace Pinetime { static constexpr uint32_t settingsVersion = 0x0008; + // To enable disabling it during notification sleep, differentiate between + // the setting being on, and the setting being set by the user + struct alwaysOnDisplayData { + bool enabled = false; + bool state = false; + }; + struct SettingsData { uint32_t version = settingsVersion; uint32_t stepsGoal = 10000; uint32_t screenTimeOut = 15000; - bool alwaysOnDisplay = false; + alwaysOnDisplayData alwaysOnDisplay; ClockType clockType = ClockType::H24; WeatherFormat weatherFormat = WeatherFormat::Metric; diff --git a/src/displayapp/screens/settings/SettingDisplay.cpp b/src/displayapp/screens/settings/SettingDisplay.cpp index 12d0f561..57a64d7f 100644 --- a/src/displayapp/screens/settings/SettingDisplay.cpp +++ b/src/displayapp/screens/settings/SettingDisplay.cpp @@ -66,7 +66,7 @@ SettingDisplay::SettingDisplay(Pinetime::Applications::DisplayApp* app, Pinetime alwaysOnCheckbox = lv_checkbox_create(container1, nullptr); lv_checkbox_set_text(alwaysOnCheckbox, "Always On"); - lv_checkbox_set_checked(alwaysOnCheckbox, settingsController.GetAlwaysOnDisplay()); + lv_checkbox_set_checked(alwaysOnCheckbox, settingsController.GetAlwaysOnDisplaySetting()); lv_obj_add_state(alwaysOnCheckbox, LV_STATE_DEFAULT); alwaysOnCheckbox->user_data = this; lv_obj_set_event_cb(alwaysOnCheckbox, AlwaysOnEventHandler); @@ -78,8 +78,8 @@ SettingDisplay::~SettingDisplay() { } void SettingDisplay::ToggleAlwaysOn() { - settingsController.SetAlwaysOnDisplay(!settingsController.GetAlwaysOnDisplay()); - lv_checkbox_set_checked(alwaysOnCheckbox, settingsController.GetAlwaysOnDisplay()); + settingsController.SetAlwaysOnDisplaySetting(!settingsController.GetAlwaysOnDisplaySetting()); + lv_checkbox_set_checked(alwaysOnCheckbox, settingsController.GetAlwaysOnDisplaySetting()); } void SettingDisplay::UpdateSelected(lv_obj_t* object, lv_event_t event) { From 5385f7e275a0b3ca83d8a7cae959b02700ef153a Mon Sep 17 00:00:00 2001 From: John Crawford Date: Sat, 14 Oct 2023 10:16:49 -0600 Subject: [PATCH 05/16] aod: switch to 8 colors when always on --- src/displayapp/DisplayApp.cpp | 7 ++++++- src/drivers/St7789.cpp | 18 ++++++++++++++++++ src/drivers/St7789.h | 6 ++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index 5e68ef23..1a579cb1 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -249,6 +249,7 @@ void DisplayApp::Refresh() { // Don't actually turn off the display for AlwaysOn mode if (settingsController.GetAlwaysOnDisplay()) { brightnessController.Set(Controllers::BrightnessController::Levels::AlwaysOn); + lcd.LowPowerOn(); } else { brightnessController.Set(Controllers::BrightnessController::Levels::Off); lcd.Sleep(); @@ -257,7 +258,11 @@ void DisplayApp::Refresh() { state = States::Idle; break; case Messages::GoToRunning: - lcd.Wakeup(); + if (settingsController.GetAlwaysOnDisplay()) { + lcd.LowPowerOff(); + } else { + lcd.Wakeup(); + } lv_disp_trig_activity(nullptr); ApplyBrightness(); state = States::Running; diff --git a/src/drivers/St7789.cpp b/src/drivers/St7789.cpp index c22f2199..274e2b62 100644 --- a/src/drivers/St7789.cpp +++ b/src/drivers/St7789.cpp @@ -127,6 +127,14 @@ void St7789::NormalModeOn() { WriteCommand(static_cast(Commands::NormalModeOn)); } +void St7789::IdleModeOn() { + WriteCommand(static_cast(Commands::IdleModeOn)); +} + +void St7789::IdleModeOff() { + WriteCommand(static_cast(Commands::IdleModeOff)); +} + void St7789::DisplayOn() { WriteCommand(static_cast(Commands::DisplayOn)); } @@ -198,6 +206,16 @@ void St7789::HardwareReset() { vTaskDelay(pdMS_TO_TICKS(125)); } +void St7789::LowPowerOn() { + IdleModeOn(); + NRF_LOG_INFO("[LCD] Low power mode"); +} + +void St7789::LowPowerOff() { + IdleModeOff(); + NRF_LOG_INFO("[LCD] Normal power mode"); +} + void St7789::Sleep() { SleepIn(); nrf_gpio_cfg_default(pinDataCommand); diff --git a/src/drivers/St7789.h b/src/drivers/St7789.h index 844e0180..ccc951ff 100644 --- a/src/drivers/St7789.h +++ b/src/drivers/St7789.h @@ -24,6 +24,8 @@ namespace Pinetime { void DrawBuffer(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint8_t* data, size_t size); + void LowPowerOn(); + void LowPowerOff(); void Sleep(); void Wakeup(); @@ -45,6 +47,8 @@ namespace Pinetime { void DisplayInversionOn(); void NormalModeOn(); void WriteToRam(const uint8_t* data, size_t size); + void IdleModeOn(); + void IdleModeOff(); void DisplayOn(); void DisplayOff(); @@ -68,6 +72,8 @@ namespace Pinetime { MemoryDataAccessControl = 0x36, VerticalScrollDefinition = 0x33, VerticalScrollStartAddress = 0x37, + IdleModeOff = 0x38, + IdleModeOn = 0x39, PixelFormat = 0x3a, VdvSet = 0xc4, }; From 0960d670010852f294e8ba19a6d92eb93e537421 Mon Sep 17 00:00:00 2001 From: John Crawford Date: Sat, 14 Oct 2023 15:19:50 -0600 Subject: [PATCH 06/16] aod: lower refresh rate when always on --- src/drivers/St7789.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/drivers/St7789.h | 5 +++++ 2 files changed, 44 insertions(+) diff --git a/src/drivers/St7789.cpp b/src/drivers/St7789.cpp index 274e2b62..035d61c9 100644 --- a/src/drivers/St7789.cpp +++ b/src/drivers/St7789.cpp @@ -16,6 +16,7 @@ void St7789::Init() { nrf_gpio_pin_set(pinReset); HardwareReset(); SoftwareReset(); + Command2Enable(); SleepOut(); PixelFormat(); MemoryDataAccessControl(); @@ -63,6 +64,17 @@ void St7789::SoftwareReset() { vTaskDelay(pdMS_TO_TICKS(125)); } +void St7789::Command2Enable() { + WriteCommand(static_cast(Commands::Command2Enable)); + constexpr uint8_t args[] = { + 0x5a, // Constant + 0x69, // Constant + 0x02, // Constant + 0x01, // Enable + }; + WriteData(args, sizeof(args)); +} + void St7789::SleepOut() { if (!sleepIn) { return; @@ -135,6 +147,31 @@ void St7789::IdleModeOff() { WriteCommand(static_cast(Commands::IdleModeOff)); } +void St7789::FrameRateLow() { + WriteCommand(static_cast(Commands::FrameRate)); + // Enable frame rate control for partial/idle mode, 8x frame divider + // According to the datasheet, these controls should apply only to partial/idle mode + // However they appear to apply to normal mode, so we have to enable/disable + // every time we enter/exit always on + // In testing this divider appears to actually be 16x? + constexpr uint8_t args[] = { + 0x13, // Enable frame rate control for partial/idle mode, 8x frame divider + 0x1f, // Idle mode frame rate (lowest possible) + 0x1f, // Partial mode frame rate (lowest possible, unused) + }; + WriteData(args, sizeof(args)); +} + +void St7789::FrameRateNormal() { + WriteCommand(static_cast(Commands::FrameRate)); + constexpr uint8_t args[] = { + 0x00, // Disable frame rate control and divider + 0x0f, // Idle mode frame rate (normal) + 0x0f, // Partial mode frame rate (normal, unused) + }; + WriteData(args, sizeof(args)); +} + void St7789::DisplayOn() { WriteCommand(static_cast(Commands::DisplayOn)); } @@ -208,11 +245,13 @@ void St7789::HardwareReset() { void St7789::LowPowerOn() { IdleModeOn(); + FrameRateLow(); NRF_LOG_INFO("[LCD] Low power mode"); } void St7789::LowPowerOff() { IdleModeOff(); + FrameRateNormal(); NRF_LOG_INFO("[LCD] Normal power mode"); } diff --git a/src/drivers/St7789.h b/src/drivers/St7789.h index ccc951ff..e249e0b0 100644 --- a/src/drivers/St7789.h +++ b/src/drivers/St7789.h @@ -39,6 +39,7 @@ namespace Pinetime { void HardwareReset(); void SoftwareReset(); + void Command2Enable(); void SleepOut(); void EnsureSleepOutPostDelay(); void SleepIn(); @@ -49,6 +50,8 @@ namespace Pinetime { void WriteToRam(const uint8_t* data, size_t size); void IdleModeOn(); void IdleModeOff(); + void FrameRateNormal(); + void FrameRateLow(); void DisplayOn(); void DisplayOff(); @@ -75,7 +78,9 @@ namespace Pinetime { IdleModeOff = 0x38, IdleModeOn = 0x39, PixelFormat = 0x3a, + FrameRate = 0xb3, VdvSet = 0xc4, + Command2Enable = 0xdf, }; void WriteData(uint8_t data); void WriteData(const uint8_t* data, size_t size); From 947c4f5067615fea540e2859c0c0127f2a3ce495 Mon Sep 17 00:00:00 2001 From: mark9064 <30447455+mark9064@users.noreply.github.com> Date: Sat, 14 Oct 2023 16:02:23 -0600 Subject: [PATCH 07/16] aod: fix brightness getting stuck high --- src/displayapp/DisplayApp.cpp | 6 +++--- src/displayapp/Messages.h | 2 +- src/systemtask/SystemTask.cpp | 10 +++++----- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index 1a579cb1..6fda99db 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -238,9 +238,6 @@ void DisplayApp::Refresh() { case Messages::DimScreen: DimScreen(); break; - case Messages::RestoreBrightness: - RestoreBrightness(); - break; case Messages::GoToSleep: while (brightnessController.Level() != Controllers::BrightnessController::Levels::Low) { brightnessController.Lower(); @@ -257,6 +254,9 @@ void DisplayApp::Refresh() { PushMessageToSystemTask(Pinetime::System::Messages::OnDisplayTaskSleeping); state = States::Idle; break; + case Messages::NotifyDeviceActivity: + lv_disp_trig_activity(nullptr); + break; case Messages::GoToRunning: if (settingsController.GetAlwaysOnDisplay()) { lcd.LowPowerOff(); diff --git a/src/displayapp/Messages.h b/src/displayapp/Messages.h index dada3088..1418f6be 100644 --- a/src/displayapp/Messages.h +++ b/src/displayapp/Messages.h @@ -18,7 +18,7 @@ namespace Pinetime { TimerDone, BleFirmwareUpdateStarted, DimScreen, - RestoreBrightness, + NotifyDeviceActivity, ShowPairingKey, AlarmTriggered, Chime, diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 5bd71c38..211e19ec 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -194,7 +194,7 @@ void SystemTask::Work() { if (!bleController.IsFirmwareUpdating()) { doNotGoToSleep = false; } - displayApp.PushMessage(Pinetime::Applications::Display::Messages::RestoreBrightness); + displayApp.PushMessage(Pinetime::Applications::Display::Messages::NotifyDeviceActivity); break; case Messages::DisableSleeping: doNotGoToSleep = true; @@ -245,7 +245,7 @@ void SystemTask::Work() { heartRateApp.PushMessage(Pinetime::Applications::HeartRateTask::Messages::GoToSleep); break; case Messages::OnNewTime: - displayApp.PushMessage(Pinetime::Applications::Display::Messages::RestoreBrightness); + displayApp.PushMessage(Pinetime::Applications::Display::Messages::NotifyDeviceActivity); displayApp.PushMessage(Pinetime::Applications::Display::Messages::UpdateDateTime); if (alarmController.State() == Controllers::AlarmController::AlarmState::Set) { alarmController.ScheduleAlarm(); @@ -256,7 +256,7 @@ void SystemTask::Work() { if (state == SystemTaskState::Sleeping) { GoToRunning(); } else { - displayApp.PushMessage(Pinetime::Applications::Display::Messages::RestoreBrightness); + displayApp.PushMessage(Pinetime::Applications::Display::Messages::NotifyDeviceActivity); } displayApp.PushMessage(Pinetime::Applications::Display::Messages::NewNotification); } @@ -268,7 +268,7 @@ void SystemTask::Work() { displayApp.PushMessage(Pinetime::Applications::Display::Messages::AlarmTriggered); break; case Messages::BleConnected: - displayApp.PushMessage(Pinetime::Applications::Display::Messages::RestoreBrightness); + displayApp.PushMessage(Pinetime::Applications::Display::Messages::NotifyDeviceActivity); isBleDiscoveryTimerRunning = true; bleDiscoveryTimer = 5; break; @@ -466,7 +466,7 @@ void SystemTask::HandleButtonAction(Controllers::ButtonActions action) { return; } - displayApp.PushMessage(Pinetime::Applications::Display::Messages::RestoreBrightness); + displayApp.PushMessage(Pinetime::Applications::Display::Messages::NotifyDeviceActivity); using Actions = Controllers::ButtonActions; From bf69e0dcc515b85ea034b99414e433a7233ad25e Mon Sep 17 00:00:00 2001 From: mark9064 <30447455+mark9064@users.noreply.github.com> Date: Wed, 25 Oct 2023 02:42:14 +0000 Subject: [PATCH 08/16] aod: fix flashlight brightness restore --- src/displayapp/screens/FlashLight.cpp | 2 ++ src/displayapp/screens/FlashLight.h | 1 + 2 files changed, 3 insertions(+) diff --git a/src/displayapp/screens/FlashLight.cpp b/src/displayapp/screens/FlashLight.cpp index 1b7cf39c..f169fac1 100644 --- a/src/displayapp/screens/FlashLight.cpp +++ b/src/displayapp/screens/FlashLight.cpp @@ -17,6 +17,7 @@ namespace { FlashLight::FlashLight(System::SystemTask& systemTask, Controllers::BrightnessController& brightnessController) : systemTask {systemTask}, brightnessController {brightnessController} { + previousBrightnessLevel = brightnessController.Level(); brightnessController.Set(Controllers::BrightnessController::Levels::Low); flashLight = lv_label_create(lv_scr_act(), nullptr); @@ -52,6 +53,7 @@ FlashLight::FlashLight(System::SystemTask& systemTask, Controllers::BrightnessCo FlashLight::~FlashLight() { lv_obj_clean(lv_scr_act()); lv_obj_set_style_local_bg_color(lv_scr_act(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK); + brightnessController.Set(previousBrightnessLevel); systemTask.PushMessage(Pinetime::System::Messages::EnableSleeping); } diff --git a/src/displayapp/screens/FlashLight.h b/src/displayapp/screens/FlashLight.h index 2b710ed5..c5404e93 100644 --- a/src/displayapp/screens/FlashLight.h +++ b/src/displayapp/screens/FlashLight.h @@ -27,6 +27,7 @@ namespace Pinetime { Controllers::BrightnessController& brightnessController; Controllers::BrightnessController::Levels brightnessLevel = Controllers::BrightnessController::Levels::High; + Controllers::BrightnessController::Levels previousBrightnessLevel; lv_obj_t* flashLight; lv_obj_t* backgroundAction; From 0bcd7e000960d18db6d26813185fe0e2695bdfe6 Mon Sep 17 00:00:00 2001 From: John Crawford Date: Tue, 17 Oct 2023 08:19:01 -0600 Subject: [PATCH 09/16] aod: lower voltage going to the display --- src/drivers/St7789.cpp | 14 ++++++++++++++ src/drivers/St7789.h | 3 +++ 2 files changed, 17 insertions(+) diff --git a/src/drivers/St7789.cpp b/src/drivers/St7789.cpp index 035d61c9..48b65acb 100644 --- a/src/drivers/St7789.cpp +++ b/src/drivers/St7789.cpp @@ -27,6 +27,7 @@ void St7789::Init() { #endif NormalModeOn(); SetVdv(); + PowerControl(); DisplayOn(); } @@ -176,6 +177,19 @@ void St7789::DisplayOn() { WriteCommand(static_cast(Commands::DisplayOn)); } +void St7789::PowerControl() { + WriteCommand(static_cast(Commands::PowerControl1)); + constexpr uint8_t args[] = { + 0xa4, // Constant + 0x00, // Lowest possible voltages + }; + WriteData(args, sizeof(args)); + + WriteCommand(static_cast(Commands::PowerControl2)); + // Lowest possible boost circuit clocks + WriteData(0xb3); +} + void St7789::SetAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { WriteCommand(static_cast(Commands::ColumnAddressSet)); uint8_t colArgs[] = { diff --git a/src/drivers/St7789.h b/src/drivers/St7789.h index e249e0b0..68e9f058 100644 --- a/src/drivers/St7789.h +++ b/src/drivers/St7789.h @@ -54,6 +54,7 @@ namespace Pinetime { void FrameRateLow(); void DisplayOn(); void DisplayOff(); + void PowerControl(); void SetAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1); void SetVdv(); @@ -81,6 +82,8 @@ namespace Pinetime { FrameRate = 0xb3, VdvSet = 0xc4, Command2Enable = 0xdf, + PowerControl1 = 0xd0, + PowerControl2 = 0xe8, }; void WriteData(uint8_t data); void WriteData(const uint8_t* data, size_t size); From da9ab4a7b44b3daed233d876f50c245ee4ee4229 Mon Sep 17 00:00:00 2001 From: mark9064 <30447455+mark9064@users.noreply.github.com> Date: Mon, 23 Oct 2023 22:22:46 +0100 Subject: [PATCH 10/16] aod: lower lcd voltage --- src/drivers/St7789.cpp | 7 +++++++ src/drivers/St7789.h | 2 ++ 2 files changed, 9 insertions(+) diff --git a/src/drivers/St7789.cpp b/src/drivers/St7789.cpp index 48b65acb..cdfa6a34 100644 --- a/src/drivers/St7789.cpp +++ b/src/drivers/St7789.cpp @@ -28,6 +28,7 @@ void St7789::Init() { NormalModeOn(); SetVdv(); PowerControl(); + GateControl(); DisplayOn(); } @@ -190,6 +191,12 @@ void St7789::PowerControl() { WriteData(0xb3); } +void St7789::GateControl() { + WriteCommand(static_cast(Commands::GateControl)); + // Lowest possible VGL/VGH + WriteData(0x00); +} + void St7789::SetAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { WriteCommand(static_cast(Commands::ColumnAddressSet)); uint8_t colArgs[] = { diff --git a/src/drivers/St7789.h b/src/drivers/St7789.h index 68e9f058..96d16b93 100644 --- a/src/drivers/St7789.h +++ b/src/drivers/St7789.h @@ -55,6 +55,7 @@ namespace Pinetime { void DisplayOn(); void DisplayOff(); void PowerControl(); + void GateControl(); void SetAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1); void SetVdv(); @@ -84,6 +85,7 @@ namespace Pinetime { Command2Enable = 0xdf, PowerControl1 = 0xd0, PowerControl2 = 0xe8, + GateControl = 0xb7, }; void WriteData(uint8_t data); void WriteData(const uint8_t* data, size_t size); From ef88e8165c3d8475da2d7dcae78fd1b2ac7ff34d Mon Sep 17 00:00:00 2001 From: mark9064 <30447455+mark9064@users.noreply.github.com> Date: Thu, 25 Jan 2024 22:05:41 +0000 Subject: [PATCH 11/16] aod: porch control: 2Hz idle + 75Hz on --- src/drivers/St7789.cpp | 42 +++++++++++++++++++++++++++++++----------- src/drivers/St7789.h | 10 +++++++--- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/drivers/St7789.cpp b/src/drivers/St7789.cpp index cdfa6a34..0df19b45 100644 --- a/src/drivers/St7789.cpp +++ b/src/drivers/St7789.cpp @@ -25,6 +25,9 @@ void St7789::Init() { #ifndef DRIVER_DISPLAY_MIRROR DisplayInversionOn(); #endif + PorchSet(); + FrameRateNormalSet(); + IdleFrameRateOff(); NormalModeOn(); SetVdv(); PowerControl(); @@ -149,27 +152,44 @@ void St7789::IdleModeOff() { WriteCommand(static_cast(Commands::IdleModeOff)); } -void St7789::FrameRateLow() { - WriteCommand(static_cast(Commands::FrameRate)); - // Enable frame rate control for partial/idle mode, 8x frame divider +void St7789::PorchSet() { + WriteCommand(static_cast(Commands::Porch)); + constexpr uint8_t args[] = { + 0x02, // Normal mode front porch + 0x03, // Normal mode back porch + 0x01, // Porch control enable + 0xed, // Idle mode front:back porch + 0xed, // Partial mode front:back porch (partial mode unused but set anyway) + }; + WriteData(args, sizeof(args)); +} + +void St7789::FrameRateNormalSet() { + WriteCommand(static_cast(Commands::FrameRateNormal)); + // Note that the datasheet table is imprecise - see formula below table + WriteData(0x0a); +} + +void St7789::IdleFrameRateOn() { + WriteCommand(static_cast(Commands::FrameRateIdle)); // According to the datasheet, these controls should apply only to partial/idle mode // However they appear to apply to normal mode, so we have to enable/disable // every time we enter/exit always on // In testing this divider appears to actually be 16x? constexpr uint8_t args[] = { 0x13, // Enable frame rate control for partial/idle mode, 8x frame divider - 0x1f, // Idle mode frame rate (lowest possible) - 0x1f, // Partial mode frame rate (lowest possible, unused) + 0x1e, // Idle mode frame rate + 0x1e, // Partial mode frame rate (unused) }; WriteData(args, sizeof(args)); } -void St7789::FrameRateNormal() { - WriteCommand(static_cast(Commands::FrameRate)); +void St7789::IdleFrameRateOff() { + WriteCommand(static_cast(Commands::FrameRateIdle)); constexpr uint8_t args[] = { 0x00, // Disable frame rate control and divider - 0x0f, // Idle mode frame rate (normal) - 0x0f, // Partial mode frame rate (normal, unused) + 0x0a, // Idle mode frame rate (normal) + 0x0a, // Partial mode frame rate (normal, unused) }; WriteData(args, sizeof(args)); } @@ -266,13 +286,13 @@ void St7789::HardwareReset() { void St7789::LowPowerOn() { IdleModeOn(); - FrameRateLow(); + IdleFrameRateOn(); NRF_LOG_INFO("[LCD] Low power mode"); } void St7789::LowPowerOff() { IdleModeOff(); - FrameRateNormal(); + IdleFrameRateOff(); NRF_LOG_INFO("[LCD] Normal power mode"); } diff --git a/src/drivers/St7789.h b/src/drivers/St7789.h index 96d16b93..9c778905 100644 --- a/src/drivers/St7789.h +++ b/src/drivers/St7789.h @@ -50,12 +50,14 @@ namespace Pinetime { void WriteToRam(const uint8_t* data, size_t size); void IdleModeOn(); void IdleModeOff(); - void FrameRateNormal(); - void FrameRateLow(); + void FrameRateNormalSet(); + void IdleFrameRateOff(); + void IdleFrameRateOn(); void DisplayOn(); void DisplayOff(); void PowerControl(); void GateControl(); + void PorchSet(); void SetAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1); void SetVdv(); @@ -80,12 +82,14 @@ namespace Pinetime { IdleModeOff = 0x38, IdleModeOn = 0x39, PixelFormat = 0x3a, - FrameRate = 0xb3, + FrameRateIdle = 0xb3, + FrameRateNormal = 0xc6, VdvSet = 0xc4, Command2Enable = 0xdf, PowerControl1 = 0xd0, PowerControl2 = 0xe8, GateControl = 0xb7, + Porch = 0xb2, }; void WriteData(uint8_t data); void WriteData(const uint8_t* data, size_t size); From 2bb611db8e5be5731d848edef351f780c676deae Mon Sep 17 00:00:00 2001 From: mark9064 <30447455+mark9064@users.noreply.github.com> Date: Thu, 25 Jan 2024 22:28:53 +0000 Subject: [PATCH 12/16] aod: constant frequency idle frames --- src/displayapp/DisplayApp.cpp | 49 ++++++++++++++++++++++++++++++++++- src/displayapp/DisplayApp.h | 7 +++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index 6fda99db..88ce085f 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -154,6 +154,36 @@ void DisplayApp::InitHw() { lcd.Init(); } +TickType_t DisplayApp::CalculateSleepTime() { + TickType_t ticksElapsed = xTaskGetTickCount() - alwaysOnStartTime; + // Divide both the numerator and denominator by 8 to increase the number of ticks (frames) before the overflow tick is reached + TickType_t elapsedTarget = ROUNDED_DIV((configTICK_RATE_HZ / 8) * alwaysOnTickCount * alwaysOnRefreshPeriod, 1000 / 8); + // ROUNDED_DIV overflows when numerator + (denominator floordiv 2) > uint32 max + // in this case around 9 hours + constexpr TickType_t overflowTick = (UINT32_MAX - (1000 / 16)) / ((configTICK_RATE_HZ / 8) * alwaysOnRefreshPeriod); + + // Assumptions + + // Tick rate is multiple of 8 + // Needed for division trick above + static_assert(configTICK_RATE_HZ % 8 == 0); + + // Local tick count must always wraparound before the system tick count does + // As a static assert we can use 64 bit ints and therefore dodge overflows + // Always on overflow time (ms) < system tick overflow time (ms) + static_assert((uint64_t) overflowTick * (uint64_t) alwaysOnRefreshPeriod < (uint64_t) UINT32_MAX * 1000ULL / configTICK_RATE_HZ); + + if (alwaysOnTickCount == overflowTick) { + alwaysOnTickCount = 0; + alwaysOnStartTime = xTaskGetTickCount(); + } + if (elapsedTarget > ticksElapsed) { + return elapsedTarget - ticksElapsed; + } else { + return 0; + } +} + void DisplayApp::Refresh() { auto LoadPreviousScreen = [this]() { FullRefreshDirections returnDirection; @@ -204,7 +234,21 @@ void DisplayApp::Refresh() { switch (state) { case States::Idle: if (settingsController.GetAlwaysOnDisplay()) { - queueTimeout = lv_task_handler(); + if (!currentScreen->IsRunning()) { + LoadPreviousScreen(); + } + // Check we've slept long enough + // Might not be true if the loop received an event + // If not true, then wait that amount of time + queueTimeout = CalculateSleepTime(); + if (queueTimeout == 0) { + lv_task_handler(); + // Drop frames that we've missed if the loop took way longer than expected to execute + while (queueTimeout == 0) { + alwaysOnTickCount += 1; + queueTimeout = CalculateSleepTime(); + } + } } else { queueTimeout = portMAX_DELAY; } @@ -247,6 +291,9 @@ void DisplayApp::Refresh() { if (settingsController.GetAlwaysOnDisplay()) { brightnessController.Set(Controllers::BrightnessController::Levels::AlwaysOn); lcd.LowPowerOn(); + // Record idle entry time + alwaysOnTickCount = 0; + alwaysOnStartTime = xTaskGetTickCount(); } else { brightnessController.Set(Controllers::BrightnessController::Levels::Off); lcd.Sleep(); diff --git a/src/displayapp/DisplayApp.h b/src/displayapp/DisplayApp.h index 96bce4dd..356e490f 100644 --- a/src/displayapp/DisplayApp.h +++ b/src/displayapp/DisplayApp.h @@ -135,6 +135,13 @@ namespace Pinetime { Utility::StaticStack appStackDirections; bool isDimmed = false; + + TickType_t CalculateSleepTime(); + TickType_t alwaysOnTickCount; + TickType_t alwaysOnStartTime; + // If this is to be changed, make sure the actual always on refresh rate is changed + // by configuring the LCD refresh timings + static constexpr uint32_t alwaysOnRefreshPeriod = 500; }; } } From 3e8accde6969737183eeb14c4b73761f8932197f Mon Sep 17 00:00:00 2001 From: mark9064 <30447455+mark9064@users.noreply.github.com> Date: Mon, 1 Apr 2024 00:21:13 +0100 Subject: [PATCH 13/16] aod: run LVGL task handler until all work finished --- src/displayapp/DisplayApp.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index 88ce085f..bc9a57bc 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -242,7 +242,9 @@ void DisplayApp::Refresh() { // If not true, then wait that amount of time queueTimeout = CalculateSleepTime(); if (queueTimeout == 0) { - lv_task_handler(); + // Keep running the task handler if it still has things to draw + while (!lv_task_handler()) { + }; // Drop frames that we've missed if the loop took way longer than expected to execute while (queueTimeout == 0) { alwaysOnTickCount += 1; From a407902b0600ca0b2b26c91a4597c856d16e4b26 Mon Sep 17 00:00:00 2001 From: mark9064 <30447455+mark9064@users.noreply.github.com> Date: Tue, 23 Apr 2024 00:16:19 +0100 Subject: [PATCH 14/16] aod: avoid spinning DisplayApp under high LVGL load --- src/displayapp/DisplayApp.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index bc9a57bc..f5a92117 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -242,14 +242,16 @@ void DisplayApp::Refresh() { // If not true, then wait that amount of time queueTimeout = CalculateSleepTime(); if (queueTimeout == 0) { - // Keep running the task handler if it still has things to draw - while (!lv_task_handler()) { + // Only advance the tick count when LVGL is done + // Otherwise keep running the task handler while it still has things to draw + // Note: under high graphics load, LVGL will always have more work to do + if (lv_task_handler() > 0) { + // Drop frames that we've missed if drawing/event handling took way longer than expected + while (queueTimeout == 0) { + alwaysOnTickCount += 1; + queueTimeout = CalculateSleepTime(); + } }; - // Drop frames that we've missed if the loop took way longer than expected to execute - while (queueTimeout == 0) { - alwaysOnTickCount += 1; - queueTimeout = CalculateSleepTime(); - } } } else { queueTimeout = portMAX_DELAY; From 53dc9dafe7c4b3a9be89724df2226715d6626651 Mon Sep 17 00:00:00 2001 From: mark9064 <30447455+mark9064@users.noreply.github.com> Date: Sun, 21 Jul 2024 15:52:35 +0100 Subject: [PATCH 15/16] aod: simplify AOD disablement based on notification status --- src/components/settings/Settings.h | 37 ++++-------------------------- 1 file changed, 5 insertions(+), 32 deletions(-) diff --git a/src/components/settings/Settings.h b/src/components/settings/Settings.h index 1ab67095..602de3a5 100644 --- a/src/components/settings/Settings.h +++ b/src/components/settings/Settings.h @@ -196,14 +196,6 @@ namespace Pinetime { if (status != settings.notificationStatus) { settingsChanged = true; } - // Disable always on screen while sleep mode is enabled - if (settings.alwaysOnDisplay.enabled) { - if (status == Notification::Sleep) { - settings.alwaysOnDisplay.state = false; - } else { - settings.alwaysOnDisplay.state = true; - } - } settings.notificationStatus = status; }; @@ -222,31 +214,19 @@ namespace Pinetime { return settings.screenTimeOut; }; - void SetAlwaysOnDisplay(bool state) { - if (state != settings.alwaysOnDisplay.state) { - settingsChanged = true; - } - settings.alwaysOnDisplay.state = state; - }; - bool GetAlwaysOnDisplay() const { - return settings.alwaysOnDisplay.state; + return settings.alwaysOnDisplay && GetNotificationStatus() != Notification::Sleep; }; void SetAlwaysOnDisplaySetting(bool state) { - if (state != settings.alwaysOnDisplay.enabled) { + if (state != settings.alwaysOnDisplay) { settingsChanged = true; } - settings.alwaysOnDisplay.enabled = state; - - // Don't enable always on if we are currently in notification sleep - if (GetNotificationStatus() != Notification::Sleep) { - SetAlwaysOnDisplay(state); - } + settings.alwaysOnDisplay = state; } bool GetAlwaysOnDisplaySetting() const { - return settings.alwaysOnDisplay.enabled; + return settings.alwaysOnDisplay; } void SetShakeThreshold(uint16_t thresh) { @@ -323,19 +303,12 @@ namespace Pinetime { static constexpr uint32_t settingsVersion = 0x0008; - // To enable disabling it during notification sleep, differentiate between - // the setting being on, and the setting being set by the user - struct alwaysOnDisplayData { - bool enabled = false; - bool state = false; - }; - struct SettingsData { uint32_t version = settingsVersion; uint32_t stepsGoal = 10000; uint32_t screenTimeOut = 15000; - alwaysOnDisplayData alwaysOnDisplay; + bool alwaysOnDisplay = false; ClockType clockType = ClockType::H24; WeatherFormat weatherFormat = WeatherFormat::Metric; From 3a0d673df420411a4db3a2acf57d04436446430c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Milants?= Date: Tue, 23 Jul 2024 19:27:19 +0200 Subject: [PATCH 16/16] Display the SPI flash JEDEC IDs in SystemInformation. This is needed since a new memory chip will be used in future batches of PineTimes. --- src/displayapp/DisplayApp.cpp | 7 +++++-- src/displayapp/DisplayApp.h | 4 +++- src/displayapp/DisplayAppRecovery.cpp | 3 ++- src/displayapp/DisplayAppRecovery.h | 4 +++- src/displayapp/screens/SystemInfo.cpp | 11 +++++++++-- src/displayapp/screens/SystemInfo.h | 4 +++- src/drivers/SpiNorFlash.cpp | 10 +++++++--- src/drivers/SpiNorFlash.h | 5 ++++- src/main.cpp | 3 ++- 9 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index f5a92117..394a3239 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -81,7 +81,8 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd, Pinetime::Controllers::AlarmController& alarmController, Pinetime::Controllers::BrightnessController& brightnessController, Pinetime::Controllers::TouchHandler& touchHandler, - Pinetime::Controllers::FS& filesystem) + Pinetime::Controllers::FS& filesystem, + Pinetime::Drivers::SpiNorFlash& spiNorFlash) : lcd {lcd}, touchPanel {touchPanel}, batteryController {batteryController}, @@ -97,6 +98,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd, brightnessController {brightnessController}, touchHandler {touchHandler}, filesystem {filesystem}, + spiNorFlash {spiNorFlash}, lvgl {lcd, filesystem}, timer(this, TimerCallback), controllers {batteryController, @@ -601,7 +603,8 @@ void DisplayApp::LoadScreen(Apps app, DisplayApp::FullRefreshDirections directio bleController, watchdog, motionController, - touchPanel); + touchPanel, + spiNorFlash); break; case Apps::FlashLight: currentScreen = std::make_unique(*systemTask, brightnessController); diff --git a/src/displayapp/DisplayApp.h b/src/displayapp/DisplayApp.h index 356e490f..d443b8b2 100644 --- a/src/displayapp/DisplayApp.h +++ b/src/displayapp/DisplayApp.h @@ -66,7 +66,8 @@ namespace Pinetime { Pinetime::Controllers::AlarmController& alarmController, Pinetime::Controllers::BrightnessController& brightnessController, Pinetime::Controllers::TouchHandler& touchHandler, - Pinetime::Controllers::FS& filesystem); + Pinetime::Controllers::FS& filesystem, + Pinetime::Drivers::SpiNorFlash& spiNorFlash); void Start(System::BootErrors error); void PushMessage(Display::Messages msg); @@ -96,6 +97,7 @@ namespace Pinetime { Pinetime::Controllers::BrightnessController& brightnessController; Pinetime::Controllers::TouchHandler& touchHandler; Pinetime::Controllers::FS& filesystem; + Pinetime::Drivers::SpiNorFlash& spiNorFlash; Pinetime::Controllers::FirmwareValidator validator; Pinetime::Components::LittleVgl lvgl; diff --git a/src/displayapp/DisplayAppRecovery.cpp b/src/displayapp/DisplayAppRecovery.cpp index 28892723..bcb8db0e 100644 --- a/src/displayapp/DisplayAppRecovery.cpp +++ b/src/displayapp/DisplayAppRecovery.cpp @@ -24,7 +24,8 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd, Pinetime::Controllers::AlarmController& /*alarmController*/, Pinetime::Controllers::BrightnessController& /*brightnessController*/, Pinetime::Controllers::TouchHandler& /*touchHandler*/, - Pinetime::Controllers::FS& /*filesystem*/) + Pinetime::Controllers::FS& /*filesystem*/, + Pinetime::Drivers::SpiNorFlash& /*spiNorFlash*/) : lcd {lcd}, bleController {bleController} { } diff --git a/src/displayapp/DisplayAppRecovery.h b/src/displayapp/DisplayAppRecovery.h index c1bf6243..162ff257 100644 --- a/src/displayapp/DisplayAppRecovery.h +++ b/src/displayapp/DisplayAppRecovery.h @@ -18,6 +18,7 @@ namespace Pinetime { class St7789; class Cst816S; class Watchdog; + class SpiNorFlash; } namespace Controllers { @@ -59,7 +60,8 @@ namespace Pinetime { Pinetime::Controllers::AlarmController& alarmController, Pinetime::Controllers::BrightnessController& brightnessController, Pinetime::Controllers::TouchHandler& touchHandler, - Pinetime::Controllers::FS& filesystem); + Pinetime::Controllers::FS& filesystem, + Pinetime::Drivers::SpiNorFlash& spiNorFlash); void Start(); void Start(Pinetime::System::BootErrors) { diff --git a/src/displayapp/screens/SystemInfo.cpp b/src/displayapp/screens/SystemInfo.cpp index 18fb7ad2..dd39f88a 100644 --- a/src/displayapp/screens/SystemInfo.cpp +++ b/src/displayapp/screens/SystemInfo.cpp @@ -38,7 +38,8 @@ SystemInfo::SystemInfo(Pinetime::Applications::DisplayApp* app, const Pinetime::Controllers::Ble& bleController, const Pinetime::Drivers::Watchdog& watchdog, Pinetime::Controllers::MotionController& motionController, - const Pinetime::Drivers::Cst816S& touchPanel) + const Pinetime::Drivers::Cst816S& touchPanel, + const Pinetime::Drivers::SpiNorFlash& spiNorFlash) : app {app}, dateTimeController {dateTimeController}, batteryController {batteryController}, @@ -47,6 +48,7 @@ SystemInfo::SystemInfo(Pinetime::Applications::DisplayApp* app, watchdog {watchdog}, motionController {motionController}, touchPanel {touchPanel}, + spiNorFlash {spiNorFlash}, screens {app, 0, {[this]() -> std::unique_ptr { @@ -186,10 +188,12 @@ std::unique_ptr SystemInfo::CreateScreen3() { lv_obj_t* label = lv_label_create(lv_scr_act(), nullptr); lv_label_set_recolor(label, true); const auto& bleAddr = bleController.Address(); + auto spiFlashId = spiNorFlash.GetIdentification(); lv_label_set_text_fmt(label, "#808080 BLE MAC#\n" - " %02x:%02x:%02x:%02x:%02x:%02x" + " %02x:%02x:%02x:%02x:%02x:%02x\n" "\n" + "#808080 SPI Flash# %02x-%02x-%02x\n" "\n" "#808080 Memory heap#\n" " #808080 Free# %d\n" @@ -202,6 +206,9 @@ std::unique_ptr SystemInfo::CreateScreen3() { bleAddr[2], bleAddr[1], bleAddr[0], + spiFlashId.manufacturer, + spiFlashId.type, + spiFlashId.density, xPortGetFreeHeapSize(), xPortGetMinimumEverFreeHeapSize(), mallocFailedCount, diff --git a/src/displayapp/screens/SystemInfo.h b/src/displayapp/screens/SystemInfo.h index 199af51e..3129c463 100644 --- a/src/displayapp/screens/SystemInfo.h +++ b/src/displayapp/screens/SystemInfo.h @@ -29,7 +29,8 @@ namespace Pinetime { const Pinetime::Controllers::Ble& bleController, const Pinetime::Drivers::Watchdog& watchdog, Pinetime::Controllers::MotionController& motionController, - const Pinetime::Drivers::Cst816S& touchPanel); + const Pinetime::Drivers::Cst816S& touchPanel, + const Pinetime::Drivers::SpiNorFlash& spiNorFlash); ~SystemInfo() override; bool OnTouchEvent(TouchEvents event) override; @@ -42,6 +43,7 @@ namespace Pinetime { const Pinetime::Drivers::Watchdog& watchdog; Pinetime::Controllers::MotionController& motionController; const Pinetime::Drivers::Cst816S& touchPanel; + const Pinetime::Drivers::SpiNorFlash& spiNorFlash; ScreenList<5> screens; diff --git a/src/drivers/SpiNorFlash.cpp b/src/drivers/SpiNorFlash.cpp index 56a8aabd..3bc45cca 100644 --- a/src/drivers/SpiNorFlash.cpp +++ b/src/drivers/SpiNorFlash.cpp @@ -10,7 +10,7 @@ SpiNorFlash::SpiNorFlash(Spi& spi) : spi {spi} { } void SpiNorFlash::Init() { - device_id = ReadIdentificaion(); + device_id = ReadIdentification(); NRF_LOG_INFO("[SpiNorFlash] Manufacturer : %d, Memory type : %d, memory density : %d", device_id.manufacturer, device_id.type, @@ -32,7 +32,7 @@ void SpiNorFlash::Wakeup() { uint8_t cmd[cmdSize] = {static_cast(Commands::ReleaseFromDeepPowerDown), 0x01, 0x02, 0x03}; uint8_t id = 0; spi.Read(reinterpret_cast(&cmd), cmdSize, &id, 1); - auto devId = device_id = ReadIdentificaion(); + auto devId = device_id = ReadIdentification(); if (devId.type != device_id.type) { NRF_LOG_INFO("[SpiNorFlash] ID on Wakeup: Failed"); } else { @@ -41,7 +41,7 @@ void SpiNorFlash::Wakeup() { NRF_LOG_INFO("[SpiNorFlash] Wakeup") } -SpiNorFlash::Identification SpiNorFlash::ReadIdentificaion() { +SpiNorFlash::Identification SpiNorFlash::ReadIdentification() { auto cmd = static_cast(Commands::ReadIdentification); Identification identification; spi.Read(&cmd, 1, reinterpret_cast(&identification), sizeof(Identification)); @@ -145,3 +145,7 @@ void SpiNorFlash::Write(uint32_t address, const uint8_t* buffer, size_t size) { len -= toWrite; } } + +SpiNorFlash::Identification SpiNorFlash::GetIdentification() const { + return device_id; +} diff --git a/src/drivers/SpiNorFlash.h b/src/drivers/SpiNorFlash.h index 8a063fea..028f92b4 100644 --- a/src/drivers/SpiNorFlash.h +++ b/src/drivers/SpiNorFlash.h @@ -20,7 +20,6 @@ namespace Pinetime { uint8_t density = 0; }; - Identification ReadIdentificaion(); uint8_t ReadStatusRegister(); bool WriteInProgress(); bool WriteEnabled(); @@ -33,6 +32,8 @@ namespace Pinetime { bool ProgramFailed(); bool EraseFailed(); + Identification GetIdentification() const; + void Init(); void Uninit(); @@ -40,6 +41,8 @@ namespace Pinetime { void Wakeup(); private: + Identification ReadIdentification(); + enum class Commands : uint8_t { PageProgram = 0x02, Read = 0x03, diff --git a/src/main.cpp b/src/main.cpp index ee6a6d3d..ab50fa74 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -123,7 +123,8 @@ Pinetime::Applications::DisplayApp displayApp(lcd, alarmController, brightnessController, touchHandler, - fs); + fs, + spiNorFlash); Pinetime::System::SystemTask systemTask(spi, spiNorFlash,