Refactor into defined states

This commit is contained in:
mark9064 2024-08-25 22:57:24 +01:00 committed by JF
parent 97ba39988b
commit f032847ae1
6 changed files with 52 additions and 38 deletions

View File

@ -220,7 +220,9 @@ void DisplayApp::Refresh() {
TickType_t queueTimeout;
switch (state) {
case States::Idle:
if (settingsController.GetAlwaysOnDisplay()) {
queueTimeout = portMAX_DELAY;
break;
case States::AOD:
if (!currentScreen->IsRunning()) {
LoadPreviousScreen();
}
@ -238,10 +240,7 @@ void DisplayApp::Refresh() {
alwaysOnTickCount += 1;
queueTimeout = CalculateSleepTime();
}
};
}
} else {
queueTimeout = portMAX_DELAY;
}
break;
case States::Running:
@ -284,6 +283,7 @@ void DisplayApp::Refresh() {
if (xQueueReceive(msgQueue, &msg, queueTimeout) == pdTRUE) {
switch (msg) {
case Messages::GoToSleep:
case Messages::GoToAOD:
if (state != States::Running) {
break;
}
@ -292,7 +292,7 @@ void DisplayApp::Refresh() {
vTaskDelay(100);
}
// Turn brightness down (or set to AlwaysOn mode)
if (settingsController.GetAlwaysOnDisplay()) {
if (msg == Messages::GoToAOD) {
brightnessController.Set(Controllers::BrightnessController::Levels::AlwaysOn);
} else {
brightnessController.Set(Controllers::BrightnessController::Levels::Off);
@ -305,17 +305,18 @@ void DisplayApp::Refresh() {
while (!lv_task_handler()) {
};
}
// Turn LCD display off (or set to low power for AlwaysOn mode)
if (settingsController.GetAlwaysOnDisplay()) {
if (msg == Messages::GoToAOD) {
lcd.LowPowerOn();
// Record idle entry time
alwaysOnTickCount = 0;
alwaysOnStartTime = xTaskGetTickCount();
PushMessageToSystemTask(Pinetime::System::Messages::OnDisplayTaskAOD);
state = States::AOD;
} else {
lcd.Sleep();
}
PushMessageToSystemTask(Pinetime::System::Messages::OnDisplayTaskSleeping);
state = States::Idle;
}
break;
case Messages::NotifyDeviceActivity:
lv_disp_trig_activity(nullptr);
@ -324,7 +325,7 @@ void DisplayApp::Refresh() {
if (state == States::Running) {
break;
}
if (settingsController.GetAlwaysOnDisplay()) {
if (state == States::AOD) {
lcd.LowPowerOff();
} else {
lcd.Wakeup();

View File

@ -49,7 +49,7 @@ namespace Pinetime {
namespace Applications {
class DisplayApp {
public:
enum class States { Idle, Running };
enum class States { Idle, Running, AOD };
enum class FullRefreshDirections { None, Up, Down, Left, Right, LeftAnim, RightAnim };
DisplayApp(Drivers::St7789& lcd,

View File

@ -6,6 +6,7 @@ namespace Pinetime {
namespace Display {
enum class Messages : uint8_t {
GoToSleep,
GoToAOD,
GoToRunning,
UpdateBleConnection,
TouchEvent,

View File

@ -17,6 +17,7 @@ namespace Pinetime {
HandleButtonEvent,
HandleButtonTimerEvent,
OnDisplayTaskSleeping,
OnDisplayTaskAOD,
EnableSleeping,
DisableSleeping,
OnNewDay,

View File

@ -284,9 +284,10 @@ void SystemTask::Work() {
HandleButtonAction(action);
} break;
case Messages::OnDisplayTaskSleeping:
case Messages::OnDisplayTaskAOD:
// The state was set to GoingToSleep when GoToSleep() was called
// If the state is no longer GoingToSleep, we have since transitioned back to Running
// In this case absorb the OnDisplayTaskSleeping
// In this case absorb the OnDisplayTaskSleeping/AOD
// as DisplayApp is about to receive GoToRunning
if (state != SystemTaskState::GoingToSleep) {
break;
@ -298,7 +299,7 @@ void SystemTask::Work() {
}
// Must keep SPI awake when still updating the display for always on
if (!settingsController.GetAlwaysOnDisplay()) {
if (msg == Messages::OnDisplayTaskSleeping) {
spi.Sleep();
}
@ -307,7 +308,11 @@ void SystemTask::Work() {
touchPanel.Sleep();
}
if (msg == Messages::OnDisplayTaskSleeping) {
state = SystemTaskState::Sleeping;
} else {
state = SystemTaskState::AODSleeping;
}
break;
case Messages::OnNewDay:
// We might be sleeping (with TWI device disabled.
@ -381,8 +386,8 @@ void SystemTask::GoToRunning() {
if (state == SystemTaskState::Running) {
return;
}
// SPI doesn't go to sleep for always on mode
if (!settingsController.GetAlwaysOnDisplay()) {
// SPI only switched off when entering Sleeping, not AOD or GoingToSleep
if (state == SystemTaskState::Sleeping) {
spi.Wakeup();
}
@ -411,14 +416,20 @@ void SystemTask::GoToSleep() {
return;
}
NRF_LOG_INFO("[systemtask] Going to sleep");
if (settingsController.GetAlwaysOnDisplay()) {
displayApp.PushMessage(Pinetime::Applications::Display::Messages::GoToAOD);
} else {
displayApp.PushMessage(Pinetime::Applications::Display::Messages::GoToSleep);
}
heartRateApp.PushMessage(Pinetime::Applications::HeartRateTask::Messages::GoToSleep);
state = SystemTaskState::GoingToSleep;
};
void SystemTask::UpdateMotion() {
if (IsSleeping() && !(settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::RaiseWrist) ||
// Only consider disabling motion updates specifically in the Sleeping state
// AOD needs motion on to show up to date step counts
if (state == SystemTaskState::Sleeping && !(settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::RaiseWrist) ||
settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::Shake) ||
motionController.GetService()->IsMotionNotificationSubscribed())) {
return;

View File

@ -52,7 +52,7 @@ namespace Pinetime {
namespace System {
class SystemTask {
public:
enum class SystemTaskState { Sleeping, Running, GoingToSleep };
enum class SystemTaskState { Sleeping, Running, GoingToSleep, AODSleeping };
SystemTask(Drivers::SpiMaster& spi,
Pinetime::Drivers::SpiNorFlash& spiNorFlash,
Drivers::TwiMaster& twiMaster,