Skip to content

Commit 6c3f56b

Browse files
authored
Merge pull request #740 from Riksu9000/flashlight_brightness
Flashlight brightness control
2 parents 062007a + fd4fbfd commit 6c3f56b

4 files changed

Lines changed: 102 additions & 32 deletions

File tree

src/displayapp/DisplayApp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction)
401401
break;
402402
case Apps::FlashLight:
403403
currentScreen = std::make_unique<Screens::FlashLight>(this, *systemTask, brightnessController);
404-
ReturnApp(Apps::Clock, FullRefreshDirections::Down, TouchEvents::None);
404+
ReturnApp(Apps::QuickSettings, FullRefreshDirections::Down, TouchEvents::SwipeDown);
405405
break;
406406
case Apps::StopWatch:
407407
currentScreen = std::make_unique<Screens::StopWatch>(this, *systemTask);

src/displayapp/screens/FlashLight.cpp

Lines changed: 89 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,41 @@
55
using namespace Pinetime::Applications::Screens;
66

77
namespace {
8-
static void event_handler(lv_obj_t* obj, lv_event_t event) {
9-
FlashLight* screen = static_cast<FlashLight*>(obj->user_data);
8+
void event_handler(lv_obj_t* obj, lv_event_t event) {
9+
auto* screen = static_cast<FlashLight*>(obj->user_data);
1010
screen->OnClickEvent(obj, event);
1111
}
1212
}
1313

1414
FlashLight::FlashLight(Pinetime::Applications::DisplayApp* app,
1515
System::SystemTask& systemTask,
16-
Controllers::BrightnessController& brightness)
16+
Controllers::BrightnessController& brightnessController)
1717
: Screen(app),
1818
systemTask {systemTask},
19-
brightness {brightness}
19+
brightnessController {brightnessController}
2020

2121
{
22-
brightness.Backup();
23-
brightness.Set(Controllers::BrightnessController::Levels::High);
24-
// Set the background
25-
lv_obj_set_style_local_bg_color(lv_scr_act(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0xFFFFFF));
22+
brightnessController.Backup();
2623

27-
flashLight = lv_label_create(lv_scr_act(), NULL);
28-
lv_obj_set_style_local_text_color(flashLight, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x000000));
24+
brightnessLevel = brightnessController.Level();
25+
26+
flashLight = lv_label_create(lv_scr_act(), nullptr);
2927
lv_obj_set_style_local_text_font(flashLight, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &lv_font_sys_48);
3028
lv_label_set_text_static(flashLight, Symbols::highlight);
31-
lv_obj_align(flashLight, NULL, LV_ALIGN_CENTER, 0, 0);
29+
lv_obj_align(flashLight, nullptr, LV_ALIGN_CENTER, 0, 0);
30+
31+
for (auto & i : indicators) {
32+
i = lv_obj_create(lv_scr_act(), nullptr);
33+
lv_obj_set_size(i, 15, 10);
34+
lv_obj_set_style_local_border_width(i, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 2);
35+
}
36+
37+
lv_obj_align(indicators[1], flashLight, LV_ALIGN_OUT_BOTTOM_MID, 0, 5);
38+
lv_obj_align(indicators[0], indicators[1], LV_ALIGN_OUT_LEFT_MID, -8, 0);
39+
lv_obj_align(indicators[2], indicators[1], LV_ALIGN_OUT_RIGHT_MID, 8, 0);
40+
41+
SetIndicators();
42+
SetColors();
3243

3344
backgroundAction = lv_label_create(lv_scr_act(), nullptr);
3445
lv_label_set_long_mode(backgroundAction, LV_LABEL_LONG_CROP);
@@ -44,27 +55,80 @@ FlashLight::FlashLight(Pinetime::Applications::DisplayApp* app,
4455

4556
FlashLight::~FlashLight() {
4657
lv_obj_clean(lv_scr_act());
47-
lv_obj_set_style_local_bg_color(lv_scr_act(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x000000));
48-
brightness.Restore();
58+
lv_obj_set_style_local_bg_color(lv_scr_act(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
59+
brightnessController.Restore();
4960
systemTask.PushMessage(Pinetime::System::Messages::EnableSleeping);
5061
}
5162

52-
void FlashLight::OnClickEvent(lv_obj_t* obj, lv_event_t event) {
53-
if (obj == backgroundAction) {
54-
if (event == LV_EVENT_CLICKED) {
55-
isOn = !isOn;
56-
57-
if (isOn) {
58-
lv_obj_set_style_local_bg_color(lv_scr_act(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0xFFFFFF));
59-
lv_obj_set_style_local_text_color(flashLight, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x000000));
60-
} else {
61-
lv_obj_set_style_local_bg_color(lv_scr_act(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x000000));
62-
lv_obj_set_style_local_text_color(flashLight, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0xFFFFFF));
63-
}
63+
void FlashLight::SetColors() {
64+
if (isOn) {
65+
lv_obj_set_style_local_bg_color(lv_scr_act(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
66+
lv_obj_set_style_local_text_color(flashLight, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
67+
for (auto & i : indicators) {
68+
lv_obj_set_style_local_bg_color(i, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
69+
lv_obj_set_style_local_bg_color(i, LV_OBJ_PART_MAIN, LV_STATE_DISABLED, LV_COLOR_WHITE);
70+
lv_obj_set_style_local_border_color(i, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
6471
}
72+
} else {
73+
lv_obj_set_style_local_bg_color(lv_scr_act(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
74+
lv_obj_set_style_local_text_color(flashLight, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
75+
for (auto & i : indicators) {
76+
lv_obj_set_style_local_bg_color(i, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
77+
lv_obj_set_style_local_bg_color(i, LV_OBJ_PART_MAIN, LV_STATE_DISABLED, LV_COLOR_BLACK);
78+
lv_obj_set_style_local_border_color(i, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
79+
}
80+
}
81+
}
82+
83+
void FlashLight::SetIndicators() {
84+
using namespace Pinetime::Controllers;
85+
86+
if (brightnessLevel == BrightnessController::Levels::High) {
87+
lv_obj_set_state(indicators[1], LV_STATE_DEFAULT);
88+
lv_obj_set_state(indicators[2], LV_STATE_DEFAULT);
89+
} else if (brightnessLevel == BrightnessController::Levels::Medium) {
90+
lv_obj_set_state(indicators[1], LV_STATE_DEFAULT);
91+
lv_obj_set_state(indicators[2], LV_STATE_DISABLED);
92+
} else {
93+
lv_obj_set_state(indicators[1], LV_STATE_DISABLED);
94+
lv_obj_set_state(indicators[2], LV_STATE_DISABLED);
95+
}
96+
}
97+
98+
void FlashLight::OnClickEvent(lv_obj_t* obj, lv_event_t event) {
99+
if (obj == backgroundAction && event == LV_EVENT_CLICKED) {
100+
isOn = !isOn;
101+
SetColors();
65102
}
66103
}
67104

68105
bool FlashLight::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
106+
using namespace Pinetime::Controllers;
107+
108+
if (event == TouchEvents::SwipeLeft) {
109+
if (brightnessLevel == BrightnessController::Levels::High) {
110+
brightnessLevel = BrightnessController::Levels::Medium;
111+
brightnessController.Set(brightnessLevel);
112+
SetIndicators();
113+
} else if (brightnessLevel == BrightnessController::Levels::Medium) {
114+
brightnessLevel = BrightnessController::Levels::Low;
115+
brightnessController.Set(brightnessLevel);
116+
SetIndicators();
117+
}
118+
return true;
119+
}
120+
if (event == TouchEvents::SwipeRight) {
121+
if (brightnessLevel == BrightnessController::Levels::Low) {
122+
brightnessLevel = BrightnessController::Levels::Medium;
123+
brightnessController.Set(brightnessLevel);
124+
SetIndicators();
125+
} else if (brightnessLevel == BrightnessController::Levels::Medium) {
126+
brightnessLevel = BrightnessController::Levels::High;
127+
brightnessController.Set(brightnessLevel);
128+
SetIndicators();
129+
}
130+
return true;
131+
}
132+
69133
return false;
70134
}

src/displayapp/screens/FlashLight.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#pragma once
22

3-
#include <cstdint>
43
#include "Screen.h"
5-
#include <lvgl/lvgl.h>
6-
#include "systemtask/SystemTask.h"
74
#include "components/brightness/BrightnessController.h"
5+
#include "systemtask/SystemTask.h"
6+
#include <cstdint>
7+
#include <lvgl/lvgl.h>
88

99
namespace Pinetime {
1010

@@ -20,12 +20,18 @@ namespace Pinetime {
2020
void OnClickEvent(lv_obj_t* obj, lv_event_t event);
2121

2222
private:
23+
void SetIndicators();
24+
void SetColors();
25+
2326
Pinetime::System::SystemTask& systemTask;
24-
Controllers::BrightnessController& brightness;
27+
Controllers::BrightnessController& brightnessController;
28+
29+
Controllers::BrightnessController::Levels brightnessLevel;
2530

2631
lv_obj_t* flashLight;
2732
lv_obj_t* backgroundAction;
28-
bool isOn = true;
33+
lv_obj_t* indicators[3];
34+
bool isOn = false;
2935
};
3036
}
3137
}

src/displayapp/screens/settings/QuickSettings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ void QuickSettings::OnButtonEvent(lv_obj_t* object, lv_event_t event) {
131131
if (object == btn2 && event == LV_EVENT_CLICKED) {
132132

133133
running = false;
134-
app->StartApp(Apps::FlashLight, DisplayApp::FullRefreshDirections::None);
134+
app->StartApp(Apps::FlashLight, DisplayApp::FullRefreshDirections::Up);
135135

136136
} else if (object == btn1 && event == LV_EVENT_CLICKED) {
137137

0 commit comments

Comments
 (0)