Skip to content

Commit

Permalink
Refactor doNotGoToSleep to a wakelock counter
Browse files Browse the repository at this point in the history
  • Loading branch information
mark9064 committed Sep 21, 2024
1 parent c3d0590 commit 992e555
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 41 deletions.
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ list(APPEND SOURCE_FILES

systemtask/SystemTask.cpp
systemtask/SystemMonitor.cpp
systemtask/WakeLock.cpp
drivers/TwiMaster.cpp

heartratetask/HeartRateTask.cpp
Expand Down Expand Up @@ -542,6 +543,7 @@ list(APPEND RECOVERY_SOURCE_FILES

systemtask/SystemTask.cpp
systemtask/SystemMonitor.cpp
systemtask/WakeLock.cpp
drivers/TwiMaster.cpp
components/rle/RleDecoder.cpp
components/heartrate/HeartRateController.cpp
Expand Down Expand Up @@ -660,6 +662,7 @@ set(INCLUDE_FILES
displayapp/InfiniTimeTheme.h
systemtask/SystemTask.h
systemtask/SystemMonitor.h
systemtask/WakeLock.h
displayapp/screens/Symbols.h
drivers/TwiMaster.h
heartratetask/HeartRateTask.h
Expand Down
6 changes: 3 additions & 3 deletions src/displayapp/screens/Alarm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Alarm::Alarm(Controllers::AlarmController& alarmController,
Controllers::Settings::ClockType clockType,
System::SystemTask& systemTask,
Controllers::MotorController& motorController)
: alarmController {alarmController}, systemTask {systemTask}, motorController {motorController} {
: alarmController {alarmController}, wakeLock(systemTask), motorController {motorController} {

hourCounter.Create();
lv_obj_align(hourCounter.GetObject(), nullptr, LV_ALIGN_IN_TOP_LEFT, 0, 0);
Expand Down Expand Up @@ -205,7 +205,7 @@ void Alarm::SetAlerting() {
lv_obj_set_hidden(btnStop, false);
taskStopAlarm = lv_task_create(StopAlarmTaskCallback, pdMS_TO_TICKS(60 * 1000), LV_TASK_PRIO_MID, this);
motorController.StartRinging();
systemTask.PushMessage(System::Messages::DisableSleeping);
wakeLock.Lock();
}

void Alarm::StopAlerting() {
Expand All @@ -216,7 +216,7 @@ void Alarm::StopAlerting() {
lv_task_del(taskStopAlarm);
taskStopAlarm = nullptr;
}
systemTask.PushMessage(System::Messages::EnableSleeping);
wakeLock.Release();
lv_obj_set_hidden(enableSwitch, false);
lv_obj_set_hidden(btnStop, true);
}
Expand Down
3 changes: 2 additions & 1 deletion src/displayapp/screens/Alarm.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "displayapp/screens/Screen.h"
#include "displayapp/widgets/Counter.h"
#include "displayapp/Controllers.h"
#include "systemtask/WakeLock.h"
#include "Symbols.h"

namespace Pinetime {
Expand All @@ -43,7 +44,7 @@ namespace Pinetime {

private:
Controllers::AlarmController& alarmController;
System::SystemTask& systemTask;
System::WakeLock wakeLock;
Controllers::MotorController& motorController;

lv_obj_t *btnStop, *txtStop, *btnRecur, *txtRecur, *btnInfo, *enableSwitch;
Expand Down
5 changes: 2 additions & 3 deletions src/displayapp/screens/FlashLight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace {
}

FlashLight::FlashLight(System::SystemTask& systemTask, Controllers::BrightnessController& brightnessController)
: systemTask {systemTask}, brightnessController {brightnessController} {
: wakeLock(systemTask), brightnessController {brightnessController} {

previousBrightnessLevel = brightnessController.Level();
brightnessController.Set(Controllers::BrightnessController::Levels::Low);
Expand Down Expand Up @@ -47,14 +47,13 @@ FlashLight::FlashLight(System::SystemTask& systemTask, Controllers::BrightnessCo
backgroundAction->user_data = this;
lv_obj_set_event_cb(backgroundAction, EventHandler);

systemTask.PushMessage(Pinetime::System::Messages::DisableSleeping);
wakeLock.Lock();
}

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);
}

void FlashLight::SetColors() {
Expand Down
3 changes: 2 additions & 1 deletion src/displayapp/screens/FlashLight.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "displayapp/screens/Screen.h"
#include "components/brightness/BrightnessController.h"
#include "systemtask/SystemTask.h"
#include "systemtask/WakeLock.h"
#include <cstdint>
#include <lvgl/lvgl.h>

Expand All @@ -23,7 +24,7 @@ namespace Pinetime {
void SetIndicators();
void SetColors();

Pinetime::System::SystemTask& systemTask;
Pinetime::System::WakeLock wakeLock;
Controllers::BrightnessController& brightnessController;

Controllers::BrightnessController::Levels brightnessLevel = Controllers::BrightnessController::Levels::High;
Expand Down
9 changes: 4 additions & 5 deletions src/displayapp/screens/HeartRate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace {
}

HeartRate::HeartRate(Controllers::HeartRateController& heartRateController, System::SystemTask& systemTask)
: heartRateController {heartRateController}, systemTask {systemTask} {
: heartRateController {heartRateController}, wakeLock(systemTask) {
bool isHrRunning = heartRateController.State() != Controllers::HeartRateController::States::Stopped;
label_hr = lv_label_create(lv_scr_act(), nullptr);

Expand Down Expand Up @@ -63,7 +63,7 @@ HeartRate::HeartRate(Controllers::HeartRateController& heartRateController, Syst
label_startStop = lv_label_create(btn_startStop, nullptr);
UpdateStartStopButton(isHrRunning);
if (isHrRunning) {
systemTask.PushMessage(Pinetime::System::Messages::DisableSleeping);
wakeLock.Lock();
}

taskRefresh = lv_task_create(RefreshTaskCallback, 100, LV_TASK_PRIO_MID, this);
Expand All @@ -72,7 +72,6 @@ HeartRate::HeartRate(Controllers::HeartRateController& heartRateController, Syst
HeartRate::~HeartRate() {
lv_task_del(taskRefresh);
lv_obj_clean(lv_scr_act());
systemTask.PushMessage(Pinetime::System::Messages::EnableSleeping);
}

void HeartRate::Refresh() {
Expand Down Expand Up @@ -101,12 +100,12 @@ void HeartRate::OnStartStopEvent(lv_event_t event) {
if (heartRateController.State() == Controllers::HeartRateController::States::Stopped) {
heartRateController.Start();
UpdateStartStopButton(heartRateController.State() != Controllers::HeartRateController::States::Stopped);
systemTask.PushMessage(Pinetime::System::Messages::DisableSleeping);
wakeLock.Lock();
lv_obj_set_style_local_text_color(label_hr, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::highlight);
} else {
heartRateController.Stop();
UpdateStartStopButton(heartRateController.State() != Controllers::HeartRateController::States::Stopped);
systemTask.PushMessage(Pinetime::System::Messages::EnableSleeping);
wakeLock.Release();
lv_obj_set_style_local_text_color(label_hr, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::lightGray);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/displayapp/screens/HeartRate.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <chrono>
#include "displayapp/screens/Screen.h"
#include "systemtask/SystemTask.h"
#include "systemtask/WakeLock.h"
#include "Symbols.h"
#include <lvgl/src/lv_core/lv_style.h>
#include <lvgl/src/lv_core/lv_obj.h>
Expand All @@ -27,7 +28,7 @@ namespace Pinetime {

private:
Controllers::HeartRateController& heartRateController;
Pinetime::System::SystemTask& systemTask;
Pinetime::System::WakeLock wakeLock;
void UpdateStartStopButton(bool isRunning);
lv_obj_t* label_hr;
lv_obj_t* label_bpm;
Expand Down
7 changes: 3 additions & 4 deletions src/displayapp/screens/Metronome.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace {
}

Metronome::Metronome(Controllers::MotorController& motorController, System::SystemTask& systemTask)
: motorController {motorController}, systemTask {systemTask} {
: motorController {motorController}, wakeLock(systemTask) {

bpmArc = lv_arc_create(lv_scr_act(), nullptr);
bpmArc->user_data = this;
Expand Down Expand Up @@ -72,7 +72,6 @@ Metronome::Metronome(Controllers::MotorController& motorController, System::Syst

Metronome::~Metronome() {
lv_task_del(taskRefresh);
systemTask.PushMessage(System::Messages::EnableSleeping);
lv_obj_clean(lv_scr_act());
}

Expand Down Expand Up @@ -128,12 +127,12 @@ void Metronome::OnEvent(lv_obj_t* obj, lv_event_t event) {
metronomeStarted = !metronomeStarted;
if (metronomeStarted) {
lv_label_set_text_static(lblPlayPause, Symbols::pause);
systemTask.PushMessage(System::Messages::DisableSleeping);
wakeLock.Lock();
startTime = xTaskGetTickCount();
counter = 1;
} else {
lv_label_set_text_static(lblPlayPause, Symbols::play);
systemTask.PushMessage(System::Messages::EnableSleeping);
wakeLock.Release();
}
}
break;
Expand Down
3 changes: 2 additions & 1 deletion src/displayapp/screens/Metronome.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "systemtask/SystemTask.h"
#include "systemtask/WakeLock.h"
#include "components/motor/MotorController.h"
#include "displayapp/screens/Screen.h"
#include "Symbols.h"
Expand All @@ -21,7 +22,7 @@ namespace Pinetime {
TickType_t startTime = 0;
TickType_t tappedTime = 0;
Controllers::MotorController& motorController;
System::SystemTask& systemTask;
System::WakeLock wakeLock;
int16_t bpm = 120;
uint8_t bpb = 4;
uint8_t counter = 1;
Expand Down
7 changes: 3 additions & 4 deletions src/displayapp/screens/Notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Notifications::Notifications(DisplayApp* app,
notificationManager {notificationManager},
alertNotificationService {alertNotificationService},
motorController {motorController},
systemTask {systemTask},
wakeLock(systemTask),
mode {mode} {

notificationManager.ClearNewNotificationFlag();
Expand All @@ -40,7 +40,7 @@ Notifications::Notifications(DisplayApp* app,
validDisplay = false;
}
if (mode == Modes::Preview) {
systemTask.PushMessage(System::Messages::DisableSleeping);
wakeLock.Lock();
if (notification.category == Controllers::NotificationManager::Categories::IncomingCall) {
motorController.StartRinging();
} else {
Expand All @@ -65,7 +65,6 @@ Notifications::~Notifications() {
lv_task_del(taskRefresh);
// make sure we stop any vibrations before exiting
motorController.StopRinging();
systemTask.PushMessage(System::Messages::EnableSleeping);
lv_obj_clean(lv_scr_act());
}

Expand Down Expand Up @@ -120,7 +119,7 @@ void Notifications::Refresh() {
}

void Notifications::OnPreviewInteraction() {
systemTask.PushMessage(System::Messages::EnableSleeping);
wakeLock.Release();
motorController.StopRinging();
if (timeoutLine != nullptr) {
lv_obj_del(timeoutLine);
Expand Down
3 changes: 2 additions & 1 deletion src/displayapp/screens/Notifications.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "components/ble/NotificationManager.h"
#include "components/motor/MotorController.h"
#include "systemtask/SystemTask.h"
#include "systemtask/WakeLock.h"

namespace Pinetime {
namespace Controllers {
Expand Down Expand Up @@ -73,7 +74,7 @@ namespace Pinetime {
Pinetime::Controllers::NotificationManager& notificationManager;
Pinetime::Controllers::AlertNotificationService& alertNotificationService;
Pinetime::Controllers::MotorController& motorController;
System::SystemTask& systemTask;
System::WakeLock wakeLock;
Modes mode = Modes::Normal;
std::unique_ptr<NotificationItem> currentItem;
Pinetime::Controllers::NotificationManager::Notification::Id currentId;
Expand Down
7 changes: 3 additions & 4 deletions src/displayapp/screens/StopWatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace {
constexpr TickType_t blinkInterval = pdMS_TO_TICKS(1000);
}

StopWatch::StopWatch(System::SystemTask& systemTask) : systemTask {systemTask} {
StopWatch::StopWatch(System::SystemTask& systemTask) : wakeLock(systemTask) {
static constexpr uint8_t btnWidth = 115;
static constexpr uint8_t btnHeight = 80;
btnPlayPause = lv_btn_create(lv_scr_act(), nullptr);
Expand Down Expand Up @@ -79,7 +79,6 @@ StopWatch::StopWatch(System::SystemTask& systemTask) : systemTask {systemTask} {

StopWatch::~StopWatch() {
lv_task_del(taskRefresh);
systemTask.PushMessage(Pinetime::System::Messages::EnableSleeping);
lv_obj_clean(lv_scr_act());
}

Expand Down Expand Up @@ -135,7 +134,7 @@ void StopWatch::Start() {
SetInterfaceRunning();
startTime = xTaskGetTickCount();
currentState = States::Running;
systemTask.PushMessage(Pinetime::System::Messages::DisableSleeping);
wakeLock.Lock();
}

void StopWatch::Pause() {
Expand All @@ -145,7 +144,7 @@ void StopWatch::Pause() {
oldTimeElapsed = laps[lapsDone];
blinkTime = xTaskGetTickCount() + blinkInterval;
currentState = States::Halted;
systemTask.PushMessage(Pinetime::System::Messages::EnableSleeping);
wakeLock.Release();
}

void StopWatch::Refresh() {
Expand Down
3 changes: 2 additions & 1 deletion src/displayapp/screens/StopWatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "portmacro_cmsis.h"

#include "systemtask/SystemTask.h"
#include "systemtask/WakeLock.h"
#include "displayapp/apps/Apps.h"
#include "displayapp/Controllers.h"
#include "Symbols.h"
Expand Down Expand Up @@ -43,7 +44,7 @@ namespace Pinetime {
void Start();
void Pause();

Pinetime::System::SystemTask& systemTask;
Pinetime::System::WakeLock wakeLock;
States currentState = States::Init;
TickType_t startTime;
TickType_t oldTimeElapsed = 0;
Expand Down
16 changes: 6 additions & 10 deletions src/systemtask/SystemTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,11 @@ void SystemTask::Work() {
if (xQueueReceive(systemTasksMsgQueue, &msg, 100) == pdTRUE) {
switch (msg) {
case Messages::EnableSleeping:
// Make sure that exiting an app doesn't enable sleeping,
// if the exiting was caused by a firmware update
if (!bleController.IsFirmwareUpdating()) {
doNotGoToSleep = false;
}
wakeLocksHeld--;
break;
case Messages::DisableSleeping:
GoToRunning();
doNotGoToSleep = true;
wakeLocksHeld++;
break;
case Messages::GoToRunning:
GoToRunning();
Expand Down Expand Up @@ -243,24 +239,24 @@ void SystemTask::Work() {
break;
case Messages::BleFirmwareUpdateStarted:
GoToRunning();
doNotGoToSleep = true;
wakeLocksHeld++;
displayApp.PushMessage(Pinetime::Applications::Display::Messages::BleFirmwareUpdateStarted);
break;
case Messages::BleFirmwareUpdateFinished:
if (bleController.State() == Pinetime::Controllers::Ble::FirmwareUpdateStates::Validated) {
NVIC_SystemReset();
}
doNotGoToSleep = false;
wakeLocksHeld--;
break;
case Messages::StartFileTransfer:
NRF_LOG_INFO("[systemtask] FS Started");
GoToRunning();
doNotGoToSleep = true;
wakeLocksHeld++;
// TODO add intent of fs access icon or something
break;
case Messages::StopFileTransfer:
NRF_LOG_INFO("[systemtask] FS Stopped");
doNotGoToSleep = false;
wakeLocksHeld--;
// TODO add intent of fs access icon or something
break;
case Messages::OnTouchEvent:
Expand Down
4 changes: 2 additions & 2 deletions src/systemtask/SystemTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace Pinetime {
void OnTouchEvent();

bool IsSleepDisabled() {
return doNotGoToSleep;
return wakeLocksHeld > 0;
}

Pinetime::Controllers::NimbleController& nimble() {
Expand Down Expand Up @@ -124,7 +124,7 @@ namespace Pinetime {
bool isBleDiscoveryTimerRunning = false;
uint8_t bleDiscoveryTimer = 0;
TimerHandle_t measureBatteryTimer;
bool doNotGoToSleep = false;
uint8_t wakeLocksHeld = 0;
SystemTaskState state = SystemTaskState::Running;

void HandleButtonAction(Controllers::ButtonActions action);
Expand Down
Loading

0 comments on commit 992e555

Please sign in to comment.