Skip to content

Commit 310ea81

Browse files
committed
inactivity: Use LVGL inactivity timers
Replace custom FreeRTOS inactivity timers with LVGL inactivity timers. DisplayApp: Trigger display activity on timer done. inactivity: Add additional checks The backlight could be turned on by RestoreBrightness() on ble connect event. inactivity: Trigger activity on screen switch A notification timing out could put the watch to sleep immediately. While this could be ideal behaviour, it was caused by delay in processing the EnableSleeping event and pushing RestoreBrightness to DisplayApp.
1 parent 11ade64 commit 310ea81

7 files changed

Lines changed: 52 additions & 76 deletions

File tree

src/displayapp/DisplayApp.cpp

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,29 @@ void DisplayApp::Refresh() {
155155
LoadScreen(returnAppStack.Pop(), returnDirection);
156156
};
157157

158+
auto DimScreen = [this]() {
159+
if (brightnessController.Level() != Controllers::BrightnessController::Levels::Off) {
160+
isDimmed = true;
161+
brightnessController.Set(Controllers::BrightnessController::Levels::Low);
162+
}
163+
};
164+
165+
auto RestoreBrightness = [this]() {
166+
if (brightnessController.Level() != Controllers::BrightnessController::Levels::Off) {
167+
isDimmed = false;
168+
lv_disp_trig_activity(nullptr);
169+
ApplyBrightness();
170+
}
171+
};
172+
173+
auto IsPastDimTime = [this]() -> bool {
174+
return lv_disp_get_inactive_time(nullptr) >= pdMS_TO_TICKS(settingsController.GetScreenTimeOut() - 2000);
175+
};
176+
177+
auto IsPastSleepTime = [this]() -> bool {
178+
return lv_disp_get_inactive_time(nullptr) >= pdMS_TO_TICKS(settingsController.GetScreenTimeOut());
179+
};
180+
158181
TickType_t queueTimeout;
159182
switch (state) {
160183
case States::Idle:
@@ -165,6 +188,18 @@ void DisplayApp::Refresh() {
165188
LoadPreviousScreen();
166189
}
167190
queueTimeout = lv_task_handler();
191+
192+
if (!systemTask->IsSleepDisabled() && IsPastDimTime()) {
193+
if (!isDimmed) {
194+
DimScreen();
195+
}
196+
if (IsPastSleepTime()) {
197+
systemTask->PushMessage(System::Messages::GoToSleep);
198+
state = States::Idle;
199+
}
200+
} else if (isDimmed) {
201+
RestoreBrightness();
202+
}
168203
break;
169204
default:
170205
queueTimeout = portMAX_DELAY;
@@ -175,10 +210,10 @@ void DisplayApp::Refresh() {
175210
if (xQueueReceive(msgQueue, &msg, queueTimeout) == pdTRUE) {
176211
switch (msg) {
177212
case Messages::DimScreen:
178-
brightnessController.Set(Controllers::BrightnessController::Levels::Low);
213+
DimScreen();
179214
break;
180215
case Messages::RestoreBrightness:
181-
ApplyBrightness();
216+
RestoreBrightness();
182217
break;
183218
case Messages::GoToSleep:
184219
while (brightnessController.Level() != Controllers::BrightnessController::Levels::Off) {
@@ -191,12 +226,10 @@ void DisplayApp::Refresh() {
191226
break;
192227
case Messages::GoToRunning:
193228
lcd.Wakeup();
229+
lv_disp_trig_activity(nullptr);
194230
ApplyBrightness();
195231
state = States::Running;
196232
break;
197-
case Messages::UpdateTimeOut:
198-
PushMessageToSystemTask(System::Messages::UpdateTimeOut);
199-
break;
200233
case Messages::UpdateBleConnection:
201234
// clockScreen.SetBleConnectionState(bleController.IsConnected() ? Screens::Clock::BleConnectionStates::Connected :
202235
// Screens::Clock::BleConnectionStates::NotConnected);
@@ -206,6 +239,7 @@ void DisplayApp::Refresh() {
206239
break;
207240
case Messages::TimerDone:
208241
if (currentApp == Apps::Timer) {
242+
lv_disp_trig_activity(nullptr);
209243
auto* timer = static_cast<Screens::Timer*>(currentScreen.get());
210244
timer->Reset();
211245
} else {
@@ -319,6 +353,7 @@ void DisplayApp::Refresh() {
319353
motorController.RunForDuration(35);
320354
break;
321355
case Messages::OnChargingEvent:
356+
RestoreBrightness();
322357
motorController.RunForDuration(15);
323358
break;
324359
}
@@ -352,7 +387,7 @@ void DisplayApp::LoadNewScreen(Apps app, DisplayApp::FullRefreshDirections direc
352387

353388
void DisplayApp::LoadScreen(Apps app, DisplayApp::FullRefreshDirections direction) {
354389
lvgl.CancelTap();
355-
ApplyBrightness();
390+
lv_disp_trig_activity(nullptr);
356391
motorController.StopRinging();
357392

358393
currentScreen.reset(nullptr);

src/displayapp/DisplayApp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ namespace Pinetime {
128128
static constexpr size_t returnAppStackSize = 10;
129129
StaticStack<Apps, returnAppStackSize> returnAppStack;
130130
StaticStack<FullRefreshDirections, returnAppStackSize> appStackDirections;
131+
132+
bool isDimmed = false;
131133
};
132134
}
133135
}

src/displayapp/Messages.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ namespace Pinetime {
1717
NewNotification,
1818
TimerDone,
1919
BleFirmwareUpdateStarted,
20-
UpdateTimeOut,
2120
DimScreen,
2221
RestoreBrightness,
2322
ShowPairingKey,

src/displayapp/screens/settings/SettingDisplay.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ void SettingDisplay::UpdateSelected(lv_obj_t* object, lv_event_t event) {
6969
if (object == cbOption[i]) {
7070
lv_checkbox_set_checked(cbOption[i], true);
7171
settingsController.SetScreenTimeOut(options[i]);
72-
app->PushMessage(Applications::Display::Messages::UpdateTimeOut);
7372
} else {
7473
lv_checkbox_set_checked(cbOption[i], false);
7574
}

src/systemtask/Messages.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ namespace Pinetime {
1212
OnTimerDone,
1313
OnNewCall,
1414
BleConnected,
15-
UpdateTimeOut,
1615
BleFirmwareUpdateStarted,
1716
BleFirmwareUpdateFinished,
1817
OnTouchEvent,

src/systemtask/SystemTask.cpp

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,6 @@ namespace {
2727
}
2828
}
2929

30-
void DimTimerCallback(TimerHandle_t xTimer) {
31-
32-
NRF_LOG_INFO("DimTimerCallback");
33-
auto* sysTask = static_cast<SystemTask*>(pvTimerGetTimerID(xTimer));
34-
sysTask->OnDim();
35-
}
36-
37-
void IdleTimerCallback(TimerHandle_t xTimer) {
38-
39-
NRF_LOG_INFO("IdleTimerCallback");
40-
auto* sysTask = static_cast<SystemTask*>(pvTimerGetTimerID(xTimer));
41-
sysTask->OnIdle();
42-
}
43-
4430
void MeasureBatteryTimerCallback(TimerHandle_t xTimer) {
4531
auto* sysTask = static_cast<SystemTask*>(pvTimerGetTimerID(xTimer));
4632
sysTask->PushMessage(Pinetime::System::Messages::MeasureBatteryTimerExpired);
@@ -189,10 +175,7 @@ void SystemTask::Work() {
189175

190176
batteryController.MeasureVoltage();
191177

192-
idleTimer = xTimerCreate("idleTimer", pdMS_TO_TICKS(2000), pdFALSE, this, IdleTimerCallback);
193-
dimTimer = xTimerCreate("dimTimer", pdMS_TO_TICKS(settingsController.GetScreenTimeOut() - 2000), pdFALSE, this, DimTimerCallback);
194178
measureBatteryTimer = xTimerCreate("measureBattery", batteryMeasurementPeriod, pdTRUE, this, MeasureBatteryTimerCallback);
195-
xTimerStart(dimTimer, 0);
196179
xTimerStart(measureBatteryTimer, portMAX_DELAY);
197180

198181
#pragma clang diagnostic push
@@ -209,14 +192,11 @@ void SystemTask::Work() {
209192
if (!bleController.IsFirmwareUpdating()) {
210193
doNotGoToSleep = false;
211194
}
212-
ReloadIdleTimer();
195+
displayApp.PushMessage(Pinetime::Applications::Display::Messages::RestoreBrightness);
213196
break;
214197
case Messages::DisableSleeping:
215198
doNotGoToSleep = true;
216199
break;
217-
case Messages::UpdateTimeOut:
218-
xTimerChangePeriod(dimTimer, pdMS_TO_TICKS(settingsController.GetScreenTimeOut() - 2000), 0);
219-
break;
220200
case Messages::GoToRunning:
221201
spi.Wakeup();
222202

@@ -225,7 +205,6 @@ void SystemTask::Work() {
225205
touchPanel.Wakeup();
226206
}
227207

228-
xTimerStart(dimTimer, 0);
229208
spiNorFlash.Wakeup();
230209

231210
displayApp.PushMessage(Pinetime::Applications::Display::Messages::GoToRunning);
@@ -236,7 +215,6 @@ void SystemTask::Work() {
236215
}
237216

238217
state = SystemTaskState::Running;
239-
isDimmed = false;
240218
break;
241219
case Messages::TouchWakeUp: {
242220
if (touchHandler.ProcessTouchInfo(touchPanel.GetTouchInfo())) {
@@ -258,13 +236,11 @@ void SystemTask::Work() {
258236
}
259237
state = SystemTaskState::GoingToSleep; // Already set in PushMessage()
260238
NRF_LOG_INFO("[systemtask] Going to sleep");
261-
xTimerStop(idleTimer, 0);
262-
xTimerStop(dimTimer, 0);
263239
displayApp.PushMessage(Pinetime::Applications::Display::Messages::GoToSleep);
264240
heartRateApp.PushMessage(Pinetime::Applications::HeartRateTask::Messages::GoToSleep);
265241
break;
266242
case Messages::OnNewTime:
267-
ReloadIdleTimer();
243+
displayApp.PushMessage(Pinetime::Applications::Display::Messages::RestoreBrightness);
268244
displayApp.PushMessage(Pinetime::Applications::Display::Messages::UpdateDateTime);
269245
if (alarmController.State() == Controllers::AlarmController::AlarmState::Set) {
270246
alarmController.ScheduleAlarm();
@@ -275,7 +251,7 @@ void SystemTask::Work() {
275251
if (state == SystemTaskState::Sleeping) {
276252
GoToRunning();
277253
} else {
278-
ReloadIdleTimer();
254+
displayApp.PushMessage(Pinetime::Applications::Display::Messages::RestoreBrightness);
279255
}
280256
displayApp.PushMessage(Pinetime::Applications::Display::Messages::NewNotification);
281257
}
@@ -293,7 +269,7 @@ void SystemTask::Work() {
293269
displayApp.PushMessage(Pinetime::Applications::Display::Messages::AlarmTriggered);
294270
break;
295271
case Messages::BleConnected:
296-
ReloadIdleTimer();
272+
displayApp.PushMessage(Pinetime::Applications::Display::Messages::RestoreBrightness);
297273
isBleDiscoveryTimerRunning = true;
298274
bleDiscoveryTimer = 5;
299275
break;
@@ -309,7 +285,6 @@ void SystemTask::Work() {
309285
NVIC_SystemReset();
310286
}
311287
doNotGoToSleep = false;
312-
xTimerStart(dimTimer, 0);
313288
break;
314289
case Messages::StartFileTransfer:
315290
NRF_LOG_INFO("[systemtask] FS Started");
@@ -322,12 +297,10 @@ void SystemTask::Work() {
322297
case Messages::StopFileTransfer:
323298
NRF_LOG_INFO("[systemtask] FS Stopped");
324299
doNotGoToSleep = false;
325-
xTimerStart(dimTimer, 0);
326300
// TODO add intent of fs access icon or something
327301
break;
328302
case Messages::OnTouchEvent:
329303
if (touchHandler.ProcessTouchInfo(touchPanel.GetTouchInfo())) {
330-
ReloadIdleTimer();
331304
displayApp.PushMessage(Pinetime::Applications::Display::Messages::TouchEvent);
332305
}
333306
break;
@@ -395,7 +368,6 @@ void SystemTask::Work() {
395368
case Messages::OnChargingEvent:
396369
batteryController.ReadPowerState();
397370
displayApp.PushMessage(Applications::Display::Messages::OnChargingEvent);
398-
ReloadIdleTimer();
399371
if (state == SystemTaskState::Sleeping) {
400372
GoToRunning();
401373
}
@@ -481,7 +453,7 @@ void SystemTask::HandleButtonAction(Controllers::ButtonActions action) {
481453
return;
482454
}
483455

484-
ReloadIdleTimer();
456+
displayApp.PushMessage(Pinetime::Applications::Display::Messages::RestoreBrightness);
485457

486458
using Actions = Controllers::ButtonActions;
487459

@@ -542,33 +514,3 @@ void SystemTask::PushMessage(System::Messages msg) {
542514
xQueueSend(systemTasksMsgQueue, &msg, portMAX_DELAY);
543515
}
544516
}
545-
546-
void SystemTask::OnDim() {
547-
if (doNotGoToSleep) {
548-
return;
549-
}
550-
NRF_LOG_INFO("Dim timeout -> Dim screen")
551-
displayApp.PushMessage(Pinetime::Applications::Display::Messages::DimScreen);
552-
xTimerStart(idleTimer, 0);
553-
isDimmed = true;
554-
}
555-
556-
void SystemTask::OnIdle() {
557-
if (doNotGoToSleep) {
558-
return;
559-
}
560-
NRF_LOG_INFO("Idle timeout -> Going to sleep")
561-
PushMessage(Messages::GoToSleep);
562-
}
563-
564-
void SystemTask::ReloadIdleTimer() {
565-
if (state != SystemTaskState::Running) {
566-
return;
567-
}
568-
if (isDimmed) {
569-
displayApp.PushMessage(Pinetime::Applications::Display::Messages::RestoreBrightness);
570-
isDimmed = false;
571-
}
572-
xTimerReset(dimTimer, 0);
573-
xTimerStop(idleTimer, 0);
574-
}

src/systemtask/SystemTask.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ namespace Pinetime {
8484
void OnIdle();
8585
void OnDim();
8686

87+
bool IsSleepDisabled() {
88+
return doNotGoToSleep;
89+
}
90+
8791
Pinetime::Controllers::NimbleController& nimble() {
8892
return nimbleController;
8993
};
@@ -123,14 +127,10 @@ namespace Pinetime {
123127

124128
static void Process(void* instance);
125129
void Work();
126-
void ReloadIdleTimer();
127130
bool isBleDiscoveryTimerRunning = false;
128131
uint8_t bleDiscoveryTimer = 0;
129-
TimerHandle_t dimTimer;
130-
TimerHandle_t idleTimer;
131132
TimerHandle_t measureBatteryTimer;
132133
bool doNotGoToSleep = false;
133-
bool isDimmed = false;
134134
SystemTaskState state = SystemTaskState::Running;
135135

136136
void HandleButtonAction(Controllers::ButtonActions action);

0 commit comments

Comments
 (0)