Skip to content

Commit f032847

Browse files
mark9064JF002
authored andcommitted
Refactor into defined states
1 parent 97ba399 commit f032847

6 files changed

Lines changed: 52 additions & 38 deletions

File tree

src/displayapp/DisplayApp.cpp

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -220,28 +220,27 @@ void DisplayApp::Refresh() {
220220
TickType_t queueTimeout;
221221
switch (state) {
222222
case States::Idle:
223-
if (settingsController.GetAlwaysOnDisplay()) {
224-
if (!currentScreen->IsRunning()) {
225-
LoadPreviousScreen();
226-
}
227-
// Check we've slept long enough
228-
// Might not be true if the loop received an event
229-
// If not true, then wait that amount of time
230-
queueTimeout = CalculateSleepTime();
231-
if (queueTimeout == 0) {
232-
// Only advance the tick count when LVGL is done
233-
// Otherwise keep running the task handler while it still has things to draw
234-
// Note: under high graphics load, LVGL will always have more work to do
235-
if (lv_task_handler() > 0) {
236-
// Drop frames that we've missed if drawing/event handling took way longer than expected
237-
while (queueTimeout == 0) {
238-
alwaysOnTickCount += 1;
239-
queueTimeout = CalculateSleepTime();
240-
}
241-
};
223+
queueTimeout = portMAX_DELAY;
224+
break;
225+
case States::AOD:
226+
if (!currentScreen->IsRunning()) {
227+
LoadPreviousScreen();
228+
}
229+
// Check we've slept long enough
230+
// Might not be true if the loop received an event
231+
// If not true, then wait that amount of time
232+
queueTimeout = CalculateSleepTime();
233+
if (queueTimeout == 0) {
234+
// Only advance the tick count when LVGL is done
235+
// Otherwise keep running the task handler while it still has things to draw
236+
// Note: under high graphics load, LVGL will always have more work to do
237+
if (lv_task_handler() > 0) {
238+
// Drop frames that we've missed if drawing/event handling took way longer than expected
239+
while (queueTimeout == 0) {
240+
alwaysOnTickCount += 1;
241+
queueTimeout = CalculateSleepTime();
242+
}
242243
}
243-
} else {
244-
queueTimeout = portMAX_DELAY;
245244
}
246245
break;
247246
case States::Running:
@@ -284,6 +283,7 @@ void DisplayApp::Refresh() {
284283
if (xQueueReceive(msgQueue, &msg, queueTimeout) == pdTRUE) {
285284
switch (msg) {
286285
case Messages::GoToSleep:
286+
case Messages::GoToAOD:
287287
if (state != States::Running) {
288288
break;
289289
}
@@ -292,7 +292,7 @@ void DisplayApp::Refresh() {
292292
vTaskDelay(100);
293293
}
294294
// Turn brightness down (or set to AlwaysOn mode)
295-
if (settingsController.GetAlwaysOnDisplay()) {
295+
if (msg == Messages::GoToAOD) {
296296
brightnessController.Set(Controllers::BrightnessController::Levels::AlwaysOn);
297297
} else {
298298
brightnessController.Set(Controllers::BrightnessController::Levels::Off);
@@ -305,17 +305,18 @@ void DisplayApp::Refresh() {
305305
while (!lv_task_handler()) {
306306
};
307307
}
308-
// Turn LCD display off (or set to low power for AlwaysOn mode)
309-
if (settingsController.GetAlwaysOnDisplay()) {
308+
if (msg == Messages::GoToAOD) {
310309
lcd.LowPowerOn();
311310
// Record idle entry time
312311
alwaysOnTickCount = 0;
313312
alwaysOnStartTime = xTaskGetTickCount();
313+
PushMessageToSystemTask(Pinetime::System::Messages::OnDisplayTaskAOD);
314+
state = States::AOD;
314315
} else {
315316
lcd.Sleep();
317+
PushMessageToSystemTask(Pinetime::System::Messages::OnDisplayTaskSleeping);
318+
state = States::Idle;
316319
}
317-
PushMessageToSystemTask(Pinetime::System::Messages::OnDisplayTaskSleeping);
318-
state = States::Idle;
319320
break;
320321
case Messages::NotifyDeviceActivity:
321322
lv_disp_trig_activity(nullptr);
@@ -324,7 +325,7 @@ void DisplayApp::Refresh() {
324325
if (state == States::Running) {
325326
break;
326327
}
327-
if (settingsController.GetAlwaysOnDisplay()) {
328+
if (state == States::AOD) {
328329
lcd.LowPowerOff();
329330
} else {
330331
lcd.Wakeup();

src/displayapp/DisplayApp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace Pinetime {
4949
namespace Applications {
5050
class DisplayApp {
5151
public:
52-
enum class States { Idle, Running };
52+
enum class States { Idle, Running, AOD };
5353
enum class FullRefreshDirections { None, Up, Down, Left, Right, LeftAnim, RightAnim };
5454

5555
DisplayApp(Drivers::St7789& lcd,

src/displayapp/Messages.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace Pinetime {
66
namespace Display {
77
enum class Messages : uint8_t {
88
GoToSleep,
9+
GoToAOD,
910
GoToRunning,
1011
UpdateBleConnection,
1112
TouchEvent,

src/systemtask/Messages.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace Pinetime {
1717
HandleButtonEvent,
1818
HandleButtonTimerEvent,
1919
OnDisplayTaskSleeping,
20+
OnDisplayTaskAOD,
2021
EnableSleeping,
2122
DisableSleeping,
2223
OnNewDay,

src/systemtask/SystemTask.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,10 @@ void SystemTask::Work() {
284284
HandleButtonAction(action);
285285
} break;
286286
case Messages::OnDisplayTaskSleeping:
287+
case Messages::OnDisplayTaskAOD:
287288
// The state was set to GoingToSleep when GoToSleep() was called
288289
// If the state is no longer GoingToSleep, we have since transitioned back to Running
289-
// In this case absorb the OnDisplayTaskSleeping
290+
// In this case absorb the OnDisplayTaskSleeping/AOD
290291
// as DisplayApp is about to receive GoToRunning
291292
if (state != SystemTaskState::GoingToSleep) {
292293
break;
@@ -298,7 +299,7 @@ void SystemTask::Work() {
298299
}
299300

300301
// Must keep SPI awake when still updating the display for always on
301-
if (!settingsController.GetAlwaysOnDisplay()) {
302+
if (msg == Messages::OnDisplayTaskSleeping) {
302303
spi.Sleep();
303304
}
304305

@@ -307,7 +308,11 @@ void SystemTask::Work() {
307308
touchPanel.Sleep();
308309
}
309310

310-
state = SystemTaskState::Sleeping;
311+
if (msg == Messages::OnDisplayTaskSleeping) {
312+
state = SystemTaskState::Sleeping;
313+
} else {
314+
state = SystemTaskState::AODSleeping;
315+
}
311316
break;
312317
case Messages::OnNewDay:
313318
// We might be sleeping (with TWI device disabled.
@@ -381,8 +386,8 @@ void SystemTask::GoToRunning() {
381386
if (state == SystemTaskState::Running) {
382387
return;
383388
}
384-
// SPI doesn't go to sleep for always on mode
385-
if (!settingsController.GetAlwaysOnDisplay()) {
389+
// SPI only switched off when entering Sleeping, not AOD or GoingToSleep
390+
if (state == SystemTaskState::Sleeping) {
386391
spi.Wakeup();
387392
}
388393

@@ -411,16 +416,22 @@ void SystemTask::GoToSleep() {
411416
return;
412417
}
413418
NRF_LOG_INFO("[systemtask] Going to sleep");
414-
displayApp.PushMessage(Pinetime::Applications::Display::Messages::GoToSleep);
419+
if (settingsController.GetAlwaysOnDisplay()) {
420+
displayApp.PushMessage(Pinetime::Applications::Display::Messages::GoToAOD);
421+
} else {
422+
displayApp.PushMessage(Pinetime::Applications::Display::Messages::GoToSleep);
423+
}
415424
heartRateApp.PushMessage(Pinetime::Applications::HeartRateTask::Messages::GoToSleep);
416425

417426
state = SystemTaskState::GoingToSleep;
418427
};
419428

420429
void SystemTask::UpdateMotion() {
421-
if (IsSleeping() && !(settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::RaiseWrist) ||
422-
settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::Shake) ||
423-
motionController.GetService()->IsMotionNotificationSubscribed())) {
430+
// Only consider disabling motion updates specifically in the Sleeping state
431+
// AOD needs motion on to show up to date step counts
432+
if (state == SystemTaskState::Sleeping && !(settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::RaiseWrist) ||
433+
settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::Shake) ||
434+
motionController.GetService()->IsMotionNotificationSubscribed())) {
424435
return;
425436
}
426437

src/systemtask/SystemTask.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespace Pinetime {
5252
namespace System {
5353
class SystemTask {
5454
public:
55-
enum class SystemTaskState { Sleeping, Running, GoingToSleep };
55+
enum class SystemTaskState { Sleeping, Running, GoingToSleep, AODSleeping };
5656
SystemTask(Drivers::SpiMaster& spi,
5757
Pinetime::Drivers::SpiNorFlash& spiNorFlash,
5858
Drivers::TwiMaster& twiMaster,

0 commit comments

Comments
 (0)