Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sleep settings #2230

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ list(APPEND SOURCE_FILES
displayapp/screens/settings/SettingWakeUp.cpp
displayapp/screens/settings/SettingDisplay.cpp
displayapp/screens/settings/SettingSteps.cpp
displayapp/screens/settings/SettingSleep.cpp
displayapp/screens/settings/SettingSetDateTime.cpp
displayapp/screens/settings/SettingSetDate.cpp
displayapp/screens/settings/SettingSetTime.cpp
Expand Down
12 changes: 11 additions & 1 deletion src/components/motion/MotionController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ namespace {
}
}

MotionController::MotionController(Controllers::Settings& settingsController) : settingsController {settingsController} {
}

void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps) {
if (this->nbSteps != nbSteps && service != nullptr) {
service->OnNewStepCountValue(nbSteps);
Expand All @@ -57,9 +60,16 @@ void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps)
stats = GetAccelStats();

int32_t deltaSteps = nbSteps - this->nbSteps;

if (deltaSteps > 0) {
currentTripSteps += deltaSteps;
if (settingsController.isSleepOptionOn(Settings::SleepOption::IgnoreSteps) &&
settingsController.GetNotificationStatus() == Pinetime::Controllers::Settings::Notification::Sleep) {
ignoreSteps += deltaSteps;
} else {
currentTripSteps += deltaSteps;
}
}

this->nbSteps = nbSteps;
}

Expand Down
14 changes: 13 additions & 1 deletion src/components/motion/MotionController.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@

#include "drivers/Bma421.h"
#include "components/ble/MotionService.h"
#include "components/settings/Settings.h"
#include "utility/CircularBuffer.h"

namespace Pinetime {
namespace Controllers {
class MotionController {
public:
MotionController(Controllers::Settings& settingsController);

enum class DeviceTypes {
Unknown,
BMA421,
Expand All @@ -33,13 +36,20 @@ namespace Pinetime {
}

uint32_t NbSteps() const {
return nbSteps;
if (nbSteps > ignoreSteps) {
return nbSteps - ignoreSteps;
}
return 0;
}

void ResetTrip() {
currentTripSteps = 0;
}

void ResetIgnoreSteps() {
ignoreSteps = 0;
}

uint32_t GetTripSteps() const {
return currentTripSteps;
}
Expand Down Expand Up @@ -69,6 +79,7 @@ namespace Pinetime {
private:
uint32_t nbSteps = 0;
uint32_t currentTripSteps = 0;
uint32_t ignoreSteps = 0;

TickType_t lastTime = 0;
TickType_t time = 0;
Expand Down Expand Up @@ -100,6 +111,7 @@ namespace Pinetime {

DeviceTypes deviceType = DeviceTypes::Unknown;
Pinetime::Controllers::MotionService* service = nullptr;
Controllers::Settings& settingsController;
};
}
}
36 changes: 35 additions & 1 deletion src/components/settings/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Pinetime {
enum class Notification : uint8_t { On, Off, Sleep };
enum class ChimesOption : uint8_t { None, Hours, HalfHours };
enum class WakeUpMode : uint8_t { SingleTap = 0, DoubleTap = 1, RaiseWrist = 2, Shake = 3, LowerWrist = 4 };
enum class SleepOption : uint8_t { AllowChimes = 0, AllowNotify = 1, EnableAOD = 2, DisableBle = 3, IgnoreSteps = 4 };
enum class Colors : uint8_t {
White,
Silver,
Expand Down Expand Up @@ -268,6 +269,37 @@ namespace Pinetime {
return getWakeUpModes()[static_cast<size_t>(mode)];
}

bool sleepDisabledBle = false;

void setSleepOption(SleepOption option, bool enabled) {
if (enabled != isSleepOptionOn(option)) {
settingsChanged = true;
}
settings.sleepOption.set(static_cast<size_t>(option), enabled);

// Handle special behavior
if (enabled) {
switch (option) {
case SleepOption::AllowNotify:
settings.sleepOption.set(static_cast<size_t>(SleepOption::DisableBle), false);
break;
case SleepOption::DisableBle:
settings.sleepOption.set(static_cast<size_t>(SleepOption::AllowNotify), false);
break;
default:
break;
}
}
};

std::bitset<5> getSleepOptions() const {
return settings.sleepOption;
}

bool isSleepOptionOn(const SleepOption option) const {
return getSleepOptions()[static_cast<size_t>(option)];
}

void SetBrightness(Controllers::BrightnessController::Levels level) {
if (level != settings.brightLevel) {
settingsChanged = true;
Expand Down Expand Up @@ -301,7 +333,7 @@ namespace Pinetime {
private:
Pinetime::Controllers::FS& fs;

static constexpr uint32_t settingsVersion = 0x0008;
static constexpr uint32_t settingsVersion = 0x0009;

struct SettingsData {
uint32_t version = settingsVersion;
Expand All @@ -324,6 +356,8 @@ namespace Pinetime {
std::bitset<5> wakeUpMode {0};
uint16_t shakeWakeThreshold = 150;

std::bitset<5> sleepOption {0};

Controllers::BrightnessController::Levels brightLevel = Controllers::BrightnessController::Levels::Medium;
};

Expand Down
6 changes: 6 additions & 0 deletions src/displayapp/DisplayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "displayapp/screens/settings/SettingWakeUp.h"
#include "displayapp/screens/settings/SettingDisplay.h"
#include "displayapp/screens/settings/SettingSteps.h"
#include "displayapp/screens/settings/SettingSleep.h"
#include "displayapp/screens/settings/SettingSetDateTime.h"
#include "displayapp/screens/settings/SettingChimes.h"
#include "displayapp/screens/settings/SettingShakeThreshold.h"
Expand Down Expand Up @@ -301,6 +302,7 @@ void DisplayApp::Refresh() {
if (state != States::Running || !systemTask->IsSleeping()) {
break;
}

while (brightnessController.Level() != Controllers::BrightnessController::Levels::Low) {
brightnessController.Lower();
vTaskDelay(100);
Expand All @@ -311,6 +313,7 @@ void DisplayApp::Refresh() {
} else {
brightnessController.Set(Controllers::BrightnessController::Levels::Off);
}

// Since the active screen is not really an app, go back to Clock.
if (currentApp == Apps::Launcher || currentApp == Apps::Notifications || currentApp == Apps::QuickSettings ||
currentApp == Apps::Settings) {
Expand Down Expand Up @@ -611,6 +614,9 @@ void DisplayApp::LoadScreen(Apps app, DisplayApp::FullRefreshDirections directio
case Apps::SettingSteps:
currentScreen = std::make_unique<Screens::SettingSteps>(settingsController);
break;
case Apps::SettingSleep:
currentScreen = std::make_unique<Screens::SettingSleep>(settingsController);
break;
case Apps::SettingSetDateTime:
currentScreen = std::make_unique<Screens::SettingSetDateTime>(this, dateTimeController, settingsController);
break;
Expand Down
1 change: 1 addition & 0 deletions src/displayapp/apps/Apps.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace Pinetime {
SettingDisplay,
SettingWakeUp,
SettingSteps,
SettingSleep,
SettingSetDateTime,
SettingChimes,
SettingShakeThreshold,
Expand Down
2 changes: 1 addition & 1 deletion src/displayapp/fonts/fonts.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
{
"file": "FontAwesome5-Solid+Brands+Regular.woff",
"range": "0xf294, 0xf242, 0xf54b, 0xf21e, 0xf1e6, 0xf017, 0xf129, 0xf03a, 0xf185, 0xf560, 0xf001, 0xf3fd, 0xf1fc, 0xf45d, 0xf59f, 0xf5a0, 0xf027, 0xf028, 0xf6a9, 0xf04b, 0xf04c, 0xf048, 0xf051, 0xf095, 0xf3dd, 0xf04d, 0xf2f2, 0xf024, 0xf252, 0xf569, 0xf06e, 0xf015, 0xf00c, 0xf0f3, 0xf522, 0xf743"
"range": "0xf294, 0xf242, 0xf54b, 0xf21e, 0xf1e6, 0xf017, 0xf129, 0xf03a, 0xf185, 0xf560, 0xf001, 0xf3fd, 0xf1fc, 0xf45d, 0xf59f, 0xf5a0, 0xf027, 0xf028, 0xf6a9, 0xf04b, 0xf04c, 0xf048, 0xf051, 0xf095, 0xf3dd, 0xf04d, 0xf2f2, 0xf024, 0xf252, 0xf569, 0xf06e, 0xf015, 0xf00c, 0xf0f3, 0xf522, 0xf743, 0xf186"
}
],
"bpp": 1,
Expand Down
3 changes: 2 additions & 1 deletion src/displayapp/screens/Symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace Pinetime {
static constexpr const char* dice = "\xEF\x94\xA2";
static constexpr const char* eye = "\xEF\x81\xAE";
static constexpr const char* home = "\xEF\x80\x95";
static constexpr const char* sleep = "\xEE\xBD\x84";
static constexpr const char* crescentMoon = "\xEF\x86\x86";

// fontawesome_weathericons.c
// static constexpr const char* sun = "\xEF\x86\x85";
Expand All @@ -61,6 +61,7 @@ namespace Pinetime {

static constexpr const char* notificationsOff = "\xEE\x9F\xB6";
static constexpr const char* notificationsOn = "\xEE\x9F\xB7";
static constexpr const char* sleep = "\xEE\xBD\x84";

static constexpr const char* flashlight = "\xEF\x80\x8B";
static constexpr const char* paintbrushLg = "\xEE\x90\x8A";
Expand Down
27 changes: 25 additions & 2 deletions src/displayapp/screens/settings/QuickSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,22 +146,45 @@ void QuickSettings::OnButtonEvent(lv_obj_t* object) {
settingsController.SetBrightness(brightness.Level());

} else if (object == btn3) {

// Turn notifications off
if (settingsController.GetNotificationStatus() == Controllers::Settings::Notification::On) {
settingsController.SetNotificationStatus(Controllers::Settings::Notification::Off);
lv_label_set_text_static(btn3_lvl, Symbols::notificationsOff);
lv_obj_set_state(btn3, static_cast<lv_state_t>(ButtonState::NotificationsOff));
// Turn sleep on
} else if (settingsController.GetNotificationStatus() == Controllers::Settings::Notification::Off) {
settingsController.SetNotificationStatus(Controllers::Settings::Notification::Sleep);
lv_label_set_text_static(btn3_lvl, Symbols::sleep);
lv_obj_set_state(btn3, static_cast<lv_state_t>(ButtonState::Sleep));
} else {

if (settingsController.isSleepOptionOn(Controllers::Settings::SleepOption::DisableBle)) {
if (settingsController.GetBleRadioEnabled()) {
settingsController.SetBleRadioEnabled(false);
app->PushMessage(Pinetime::Applications::Display::Messages::BleRadioEnableToggle);
settingsController.sleepDisabledBle = true;
}
}
// Turn notifications on
} else if (settingsController.GetNotificationStatus() == Controllers::Settings::Notification::Sleep) {
settingsController.SetNotificationStatus(Controllers::Settings::Notification::On);
lv_label_set_text_static(btn3_lvl, Symbols::notificationsOn);
lv_obj_set_state(btn3, static_cast<lv_state_t>(ButtonState::NotificationsOn));
motorController.RunForDuration(35);
}

// Re-enable Bluetooth settings for all notification statuses except Sleep
// (Prevents the need to modify logic if additional statuses are added)
if (settingsController.GetNotificationStatus() != Controllers::Settings::Notification::Sleep) {
// Enable Ble if previously disabled
if (settingsController.sleepDisabledBle == true) {
if (!settingsController.GetBleRadioEnabled()) {
settingsController.SetBleRadioEnabled(true);
this->app->PushMessage(Pinetime::Applications::Display::Messages::BleRadioEnableToggle);
}
settingsController.sleepDisabledBle = false;
}
}

} else if (object == btn4) {
settingsController.SetSettingsMenu(0);
app->StartApp(Apps::Settings, DisplayApp::FullRefreshDirections::Up);
Expand Down
79 changes: 79 additions & 0 deletions src/displayapp/screens/settings/SettingSleep.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "displayapp/screens/settings/SettingSleep.h"
#include <lvgl/lvgl.h>
#include "displayapp/DisplayApp.h"
#include "displayapp/screens/Screen.h"
#include "displayapp/screens/Symbols.h"
#include "components/settings/Settings.h"
#include "displayapp/screens/Styles.h"

using namespace Pinetime::Applications::Screens;

constexpr std::array<SettingSleep::Option, 5> SettingSleep::options;

namespace {
void event_handler(lv_obj_t* obj, lv_event_t event) {
auto* screen = static_cast<SettingSleep*>(obj->user_data);
if (event == LV_EVENT_VALUE_CHANGED) {
screen->UpdateSelected(obj);
}
}
}

SettingSleep::SettingSleep(Pinetime::Controllers::Settings& settingsController) : settingsController {settingsController} {
lv_obj_t* container1 = lv_cont_create(lv_scr_act(), nullptr);

lv_obj_set_style_local_bg_opa(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP);
lv_obj_set_style_local_pad_all(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 10);
lv_obj_set_style_local_pad_inner(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 5);
lv_obj_set_style_local_border_width(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);

lv_obj_set_pos(container1, 10, 35);
lv_obj_set_width(container1, LV_HOR_RES - 20);
lv_obj_set_height(container1, LV_VER_RES - 20);
lv_cont_set_layout(container1, LV_LAYOUT_COLUMN_LEFT);

lv_obj_t* title = lv_label_create(lv_scr_act(), nullptr);
lv_label_set_text_static(title, "Sleep Actions");
lv_label_set_align(title, LV_LABEL_ALIGN_CENTER);
lv_obj_align(title, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 15, 15);

lv_obj_t* icon = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_color(icon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_ORANGE);
lv_label_set_text_static(icon, Symbols::crescentMoon);
lv_label_set_align(icon, LV_LABEL_ALIGN_CENTER);
lv_obj_align(icon, title, LV_ALIGN_OUT_LEFT_MID, -10, 0);

for (unsigned int i = 0; i < options.size(); i++) {
cbOption[i] = lv_checkbox_create(container1, nullptr);
lv_checkbox_set_text(cbOption[i], options[i].name);
if (settingsController.isSleepOptionOn(static_cast<Controllers::Settings::SleepOption>(i))) {
lv_checkbox_set_checked(cbOption[i], true);
}
cbOption[i]->user_data = this;
lv_obj_set_event_cb(cbOption[i], event_handler);
}
}

SettingSleep::~SettingSleep() {
lv_obj_clean(lv_scr_act());
settingsController.SaveSettings();
}

void SettingSleep::UpdateSelected(lv_obj_t* object) {
// Find the index of the checkbox that triggered the event
for (size_t i = 0; i < options.size(); i++) {
if (cbOption[i] == object) {
bool currentState = settingsController.isSleepOptionOn(options[i].sleepOption);
settingsController.setSleepOption(options[i].sleepOption, !currentState);
break;
}
}

// Update checkbox according to current sleep options.
// This is needed because we can have extra logic when setting or unsetting sleep options,
// for example, when setting AllowNotify, DisableBle is unset and vice versa.
auto sleepOptions = settingsController.getSleepOptions();
for (size_t i = 0; i < options.size(); ++i) {
lv_checkbox_set_checked(cbOption[i], sleepOptions[i]);
}
}
40 changes: 40 additions & 0 deletions src/displayapp/screens/settings/SettingSleep.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include <array>
#include <cstdint>
#include <lvgl/lvgl.h>
#include "components/settings/Settings.h"
#include "displayapp/screens/Screen.h"

namespace Pinetime {

namespace Applications {
namespace Screens {

class SettingSleep : public Screen {
public:
SettingSleep(Pinetime::Controllers::Settings& settingsController);
~SettingSleep() override;

void UpdateSelected(lv_obj_t* object);

private:
struct Option {
Controllers::Settings::SleepOption sleepOption;
const char* name;
};

Controllers::Settings& settingsController;
static constexpr std::array<Option, 5> options = {{
{Controllers::Settings::SleepOption::AllowChimes, "Allow Chimes"},
{Controllers::Settings::SleepOption::AllowNotify, "Allow Notify"},
{Controllers::Settings::SleepOption::EnableAOD, "Enable AOD"},
{Controllers::Settings::SleepOption::DisableBle, "Disable BLE"},
{Controllers::Settings::SleepOption::IgnoreSteps, "Ignore Steps"},
}};

lv_obj_t* cbOption[options.size()];
};
}
}
}
Loading
Loading