Skip to content

Commit d6647eb

Browse files
committed
Add auto sleep InfiniTimeOrg#1461
commit d2fc306 Author: Boteium <boteium@users.noreply.github.com> Date: Fri Dec 2 14:58:31 2022 +0800 update option name commit efbe748 Author: SuIông N <Boteium@users.noreply.github.com> Date: Fri Dec 2 14:55:49 2022 +0800 Update title Co-authored-by: Itai Nelken <70802936+Itai-Nelken@users.noreply.github.com> commit 0f1cc69 Author: Boteium <boteium@users.noreply.github.com> Date: Sat Nov 26 18:18:43 2022 +0800 add auto sleep
1 parent 5c3f255 commit d6647eb

8 files changed

Lines changed: 214 additions & 9 deletions

File tree

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ list(APPEND SOURCE_FILES
423423
displayapp/screens/settings/SettingSetDate.cpp
424424
displayapp/screens/settings/SettingSetTime.cpp
425425
displayapp/screens/settings/SettingChimes.cpp
426+
displayapp/screens/settings/SettingAutoSleep.cpp
426427
displayapp/screens/settings/SettingShakeThreshold.cpp
427428
displayapp/screens/settings/SettingBluetooth.cpp
428429

src/components/datetime/DateTimeController.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ void DateTime::UpdateTime(uint32_t systickCounter) {
8989
if (systemTask != nullptr) {
9090
systemTask->PushMessage(System::Messages::OnNewHalfHour);
9191
}
92+
Controllers::Settings::AutoSleepOption* autosleep = settingsController.GetAutoSleep();
93+
if (autosleep[0].is_enabled && hour == autosleep[0].hour && minute == autosleep[0].minute) {
94+
settingsController.SetNotificationStatus(Controllers::Settings::Notification::Sleep);
95+
}
96+
if (autosleep[1].is_enabled && hour == autosleep[1].hour && minute == autosleep[1].minute) {
97+
settingsController.SetNotificationStatus(Controllers::Settings::Notification::On);
98+
}
9299
} else if (minute != 0 && minute != 30) {
93100
isHalfHourAlreadyNotified = false;
94101
}

src/components/settings/Settings.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ namespace Pinetime {
3535
};
3636
enum class PTSGaugeStyle : uint8_t { Full, Half, Numeric };
3737

38+
struct AutoSleepOption {
39+
bool is_enabled = false;
40+
uint8_t hour = 0;
41+
uint8_t minute = 0;
42+
};
43+
3844
struct PineTimeStyle {
3945
Colors ColorTime = Colors::Teal;
4046
Colors ColorBar = Colors::Teal;
@@ -57,6 +63,20 @@ namespace Pinetime {
5763
void Init();
5864
void SaveSettings();
5965

66+
void SetAutoSleep(AutoSleepOption autoSleepOptions[2]) {
67+
for (uint8_t i = 0; i < 2; i++) {
68+
if (autoSleepOptions[i].is_enabled != settings.autoSleepOptions[i].is_enabled ||
69+
autoSleepOptions[i].hour != settings.autoSleepOptions[i].hour ||
70+
autoSleepOptions[i].minute != settings.autoSleepOptions[i].minute) {
71+
settingsChanged = true;
72+
}
73+
settings.autoSleepOptions[i] = autoSleepOptions[i];
74+
}
75+
};
76+
AutoSleepOption* GetAutoSleep() {
77+
return settings.autoSleepOptions;
78+
};
79+
6080
void SetWatchFace(Pinetime::Applications::WatchFace face) {
6181
if (face != settings.watchFace) {
6282
settingsChanged = true;
@@ -262,8 +282,7 @@ namespace Pinetime {
262282
private:
263283
Pinetime::Controllers::FS& fs;
264284

265-
static constexpr uint32_t settingsVersion = 0x0005;
266-
285+
static constexpr uint32_t settingsVersion = 0x0006;
267286
struct SettingsData {
268287
uint32_t version = settingsVersion;
269288
uint32_t stepsGoal = 10000;
@@ -276,6 +295,7 @@ namespace Pinetime {
276295
ChimesOption chimesOption = ChimesOption::None;
277296

278297
PineTimeStyle PTS;
298+
AutoSleepOption autoSleepOptions[2];
279299

280300
WatchFaceInfineat watchFaceInfineat;
281301

src/displayapp/Apps.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ namespace Pinetime {
3636
SettingSteps,
3737
SettingSetDateTime,
3838
SettingChimes,
39+
SettingAutoSleep,
3940
SettingShakeThreshold,
4041
SettingBluetooth,
4142
Error

src/displayapp/DisplayApp.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "displayapp/screens/settings/SettingSteps.h"
4747
#include "displayapp/screens/settings/SettingSetDateTime.h"
4848
#include "displayapp/screens/settings/SettingChimes.h"
49+
#include "displayapp/screens/settings/SettingAutoSleep.h"
4950
#include "displayapp/screens/settings/SettingShakeThreshold.h"
5051
#include "displayapp/screens/settings/SettingBluetooth.h"
5152

@@ -493,6 +494,9 @@ void DisplayApp::LoadScreen(Apps app, DisplayApp::FullRefreshDirections directio
493494
case Apps::SettingChimes:
494495
currentScreen = std::make_unique<Screens::SettingChimes>(settingsController);
495496
break;
497+
case Apps::SettingAutoSleep:
498+
currentScreen = std::make_unique<Screens::SettingAutoSleep>(settingsController);
499+
break;
496500
case Apps::SettingShakeThreshold:
497501
currentScreen = std::make_unique<Screens::SettingShakeThreshold>(settingsController, motionController, *systemTask);
498502
break;
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#include "displayapp/screens/settings/SettingAutoSleep.h"
2+
#include <lvgl/lvgl.h>
3+
#include "displayapp/DisplayApp.h"
4+
#include "displayapp/screens/Screen.h"
5+
#include "displayapp/screens/Symbols.h"
6+
#include "components/settings/Settings.h"
7+
8+
using namespace Pinetime::Applications::Screens;
9+
using AutoSleepOption = Pinetime::Controllers::Settings::AutoSleepOption;
10+
11+
namespace {
12+
void event_handler(lv_obj_t* obj, lv_event_t event) {
13+
auto* screen = static_cast<SettingAutoSleep*>(obj->user_data);
14+
screen->UpdateSelected(obj, event);
15+
}
16+
}
17+
18+
SettingAutoSleep::SettingAutoSleep(Pinetime::Controllers::Settings& settingsController)
19+
: settingsController {settingsController} {
20+
21+
lv_obj_t* title = lv_label_create(lv_scr_act(), nullptr);
22+
lv_label_set_text_static(title, "Auto Sleep");
23+
lv_label_set_align(title, LV_LABEL_ALIGN_CENTER);
24+
lv_obj_align(title, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 15, 15);
25+
26+
lv_obj_t* icon = lv_label_create(lv_scr_act(), nullptr);
27+
lv_obj_set_style_local_text_color(icon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_ORANGE);
28+
lv_label_set_text_static(icon, Symbols::clock);
29+
lv_label_set_align(icon, LV_LABEL_ALIGN_CENTER);
30+
lv_obj_align(icon, title, LV_ALIGN_OUT_LEFT_MID, -10, 0);
31+
32+
lv_obj_t* container1 = lv_cont_create(lv_scr_act(), nullptr);
33+
lv_obj_set_style_local_bg_opa(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP);
34+
lv_obj_set_style_local_pad_all(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 10);
35+
lv_obj_set_style_local_pad_inner(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 5);
36+
lv_obj_set_style_local_border_width(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
37+
38+
lv_obj_set_pos(container1, 10, 40); // orig: 10, 60
39+
lv_obj_set_width(container1, LV_HOR_RES - 20);
40+
lv_obj_set_height(container1, LV_VER_RES - 30); // res - 50
41+
lv_cont_set_layout(container1, LV_LAYOUT_COLUMN_LEFT);
42+
43+
char optionName[2][12] = {
44+
"Start Sleep",
45+
"Stop Sleep",
46+
};
47+
48+
AutoSleepOption* saved_options = settingsController.GetAutoSleep();
49+
for (uint8_t i = 0; i < 2; i++) {
50+
// check box
51+
cbOption[i] = lv_checkbox_create(container1, nullptr);
52+
lv_checkbox_set_text(cbOption[i], optionName[i]);
53+
if (saved_options[i].is_enabled) {
54+
lv_checkbox_set_checked(cbOption[i], true);
55+
}
56+
cbOption[i]->user_data = this;
57+
lv_obj_set_event_cb(cbOption[i], event_handler);
58+
59+
time_container[i] = lv_cont_create(container1, nullptr);
60+
lv_obj_set_style_local_bg_opa(time_container[i], LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP);
61+
lv_obj_set_size(time_container[i], LV_HOR_RES - 60, 50);
62+
63+
// hour button
64+
btnHour[i] = lv_btn_create(time_container[i], nullptr);
65+
btnHour[i]->user_data = this;
66+
lv_obj_set_event_cb(btnHour[i], event_handler);
67+
lv_obj_set_size(btnHour[i], LV_HOR_RES / 3, 50);
68+
lv_obj_align(btnHour[i], NULL, LV_ALIGN_IN_LEFT_MID, 0, 0);
69+
70+
txtHour[i] = lv_label_create(btnHour[i], nullptr);
71+
lv_label_set_align(txtHour[i], LV_LABEL_ALIGN_CENTER);
72+
73+
// minute button
74+
btnMinute[i] = lv_btn_create(time_container[i], nullptr);
75+
btnMinute[i]->user_data = this;
76+
lv_obj_set_event_cb(btnMinute[i], event_handler);
77+
lv_obj_set_size(btnMinute[i], LV_HOR_RES / 3, 50);
78+
lv_obj_align(btnMinute[i], NULL, LV_ALIGN_IN_RIGHT_MID, 0, 0);
79+
80+
txtMinute[i] = lv_label_create(btnMinute[i], nullptr);
81+
lv_label_set_align(txtMinute[i], LV_LABEL_ALIGN_CENTER);
82+
}
83+
UpdateButtonText();
84+
}
85+
86+
SettingAutoSleep::~SettingAutoSleep() {
87+
lv_obj_clean(lv_scr_act());
88+
settingsController.SaveSettings();
89+
}
90+
91+
void SettingAutoSleep::UpdateButtonText() {
92+
AutoSleepOption* saved_options = settingsController.GetAutoSleep();
93+
for (uint8_t i = 0; i < 2; i++) {
94+
if (saved_options[i].is_enabled == true) {
95+
lv_btn_set_state(btnHour[i], LV_BTN_STATE_DISABLED);
96+
lv_btn_set_state(btnMinute[i], LV_BTN_STATE_DISABLED);
97+
} else {
98+
lv_btn_set_state(btnHour[i], LV_BTN_STATE_RELEASED);
99+
lv_btn_set_state(btnMinute[i], LV_BTN_STATE_RELEASED);
100+
}
101+
lv_label_set_text_fmt(txtHour[i], "%02d h", saved_options[i].hour);
102+
lv_label_set_text_fmt(txtMinute[i], "%02d m", saved_options[i].minute);
103+
}
104+
}
105+
106+
void SettingAutoSleep::UpdateSelected(lv_obj_t* obj, lv_event_t event) {
107+
if (event == LV_EVENT_VALUE_CHANGED) {
108+
for (uint8_t i = 0; i < 2; i++) {
109+
if (cbOption[i] == obj) {
110+
if (lv_checkbox_is_inactive(obj)) {
111+
return;
112+
}
113+
AutoSleepOption* saved_options = settingsController.GetAutoSleep();
114+
// toggle setting
115+
if (lv_checkbox_is_checked(obj) != saved_options[i].is_enabled) {
116+
saved_options[i].is_enabled = lv_checkbox_is_checked(obj);
117+
// if start time == stop time, uncheck the other option
118+
if (saved_options[0].is_enabled == true && saved_options[1].is_enabled == true &&
119+
saved_options[0].hour == saved_options[1].hour && saved_options[0].minute == saved_options[1].minute) {
120+
lv_checkbox_set_checked(cbOption[!i], false);
121+
saved_options[!i].is_enabled = false;
122+
}
123+
settingsController.SetAutoSleep(saved_options);
124+
}
125+
UpdateButtonText();
126+
break;
127+
}
128+
}
129+
} else if (event == LV_EVENT_CLICKED) {
130+
for (uint8_t i = 0; i < 2; i++) {
131+
if (btnHour[i] == obj) {
132+
AutoSleepOption* saved_options = settingsController.GetAutoSleep();
133+
saved_options[i].hour = (saved_options[i].hour + 1) % 24;
134+
settingsController.SetAutoSleep(saved_options);
135+
UpdateButtonText();
136+
break;
137+
} else if (btnMinute[i] == obj) {
138+
AutoSleepOption* saved_options = settingsController.GetAutoSleep();
139+
saved_options[i].minute = (saved_options[i].minute + 30) % 60;
140+
settingsController.SetAutoSleep(saved_options);
141+
UpdateButtonText();
142+
}
143+
}
144+
}
145+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
#include <lvgl/lvgl.h>
5+
#include "components/settings/Settings.h"
6+
#include "displayapp/screens/Screen.h"
7+
8+
namespace Pinetime {
9+
10+
namespace Applications {
11+
namespace Screens {
12+
13+
class SettingAutoSleep : public Screen {
14+
public:
15+
SettingAutoSleep(Pinetime::Controllers::Settings& settingsController);
16+
~SettingAutoSleep() override;
17+
18+
void UpdateSelected(lv_obj_t* object, lv_event_t event);
19+
void UpdateButtonText();
20+
21+
private:
22+
lv_obj_t *cbOption[2], *btnHour[2], *txtHour[2], *btnMinute[2], *txtMinute[2], *time_container[2];
23+
24+
Controllers::Settings& settingsController;
25+
};
26+
}
27+
}
28+
}

src/displayapp/screens/settings/Settings.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace Pinetime {
2929
static constexpr int entriesPerScreen = 4;
3030

3131
// Increment this when more space is needed
32-
static constexpr int nScreens = 3;
32+
static constexpr int nScreens = 4;
3333

3434
static constexpr std::array<List::Applications, entriesPerScreen * nScreens> entries {{
3535
{Symbols::sun, "Display", Apps::SettingDisplay},
@@ -45,13 +45,12 @@ namespace Pinetime {
4545
{Symbols::tachometer, "Shake Calib.", Apps::SettingShakeThreshold},
4646
{Symbols::check, "Firmware", Apps::FirmwareValidation},
4747
{Symbols::bluetooth, "Bluetooth", Apps::SettingBluetooth},
48-
{Symbols::list, "About", Apps::SysInfo},
49-
50-
// {Symbols::none, "None", Apps::None},
51-
// {Symbols::none, "None", Apps::None},
52-
// {Symbols::none, "None", Apps::None},
53-
// {Symbols::none, "None", Apps::None},
48+
{Symbols::clock, "Auto Sleep", Apps::SettingAutoSleep},
5449

50+
{Symbols::list, "About", Apps::SysInfo},
51+
{Symbols::none, "None", Apps::None},
52+
{Symbols::none, "None", Apps::None},
53+
{Symbols::none, "None", Apps::None},
5554
}};
5655
ScreenList<nScreens> screens;
5756
};

0 commit comments

Comments
 (0)