1
0
Fork 0

Flashlight brightness control

This commit is contained in:
Riku Isokoski 2021-10-13 13:04:40 +03:00
parent 92aeae73de
commit fd4fbfddb9
4 changed files with 102 additions and 32 deletions

View file

@ -390,7 +390,7 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction)
break; break;
case Apps::FlashLight: case Apps::FlashLight:
currentScreen = std::make_unique<Screens::FlashLight>(this, *systemTask, brightnessController); currentScreen = std::make_unique<Screens::FlashLight>(this, *systemTask, brightnessController);
ReturnApp(Apps::Clock, FullRefreshDirections::Down, TouchEvents::None); ReturnApp(Apps::QuickSettings, FullRefreshDirections::Down, TouchEvents::SwipeDown);
break; break;
case Apps::StopWatch: case Apps::StopWatch:
currentScreen = std::make_unique<Screens::StopWatch>(this, *systemTask); currentScreen = std::make_unique<Screens::StopWatch>(this, *systemTask);

View file

@ -5,30 +5,41 @@
using namespace Pinetime::Applications::Screens; using namespace Pinetime::Applications::Screens;
namespace { namespace {
static void event_handler(lv_obj_t* obj, lv_event_t event) { void event_handler(lv_obj_t* obj, lv_event_t event) {
FlashLight* screen = static_cast<FlashLight*>(obj->user_data); auto* screen = static_cast<FlashLight*>(obj->user_data);
screen->OnClickEvent(obj, event); screen->OnClickEvent(obj, event);
} }
} }
FlashLight::FlashLight(Pinetime::Applications::DisplayApp* app, FlashLight::FlashLight(Pinetime::Applications::DisplayApp* app,
System::SystemTask& systemTask, System::SystemTask& systemTask,
Controllers::BrightnessController& brightness) Controllers::BrightnessController& brightnessController)
: Screen(app), : Screen(app),
systemTask {systemTask}, systemTask {systemTask},
brightness {brightness} brightnessController {brightnessController}
{ {
brightness.Backup(); brightnessController.Backup();
brightness.Set(Controllers::BrightnessController::Levels::High);
// Set the background
lv_obj_set_style_local_bg_color(lv_scr_act(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0xFFFFFF));
flashLight = lv_label_create(lv_scr_act(), NULL); brightnessLevel = brightnessController.Level();
lv_obj_set_style_local_text_color(flashLight, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x000000));
flashLight = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_font(flashLight, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &lv_font_sys_48); lv_obj_set_style_local_text_font(flashLight, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &lv_font_sys_48);
lv_label_set_text_static(flashLight, Symbols::highlight); lv_label_set_text_static(flashLight, Symbols::highlight);
lv_obj_align(flashLight, NULL, LV_ALIGN_CENTER, 0, 0); lv_obj_align(flashLight, nullptr, LV_ALIGN_CENTER, 0, 0);
for (auto & i : indicators) {
i = lv_obj_create(lv_scr_act(), nullptr);
lv_obj_set_size(i, 15, 10);
lv_obj_set_style_local_border_width(i, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 2);
}
lv_obj_align(indicators[1], flashLight, LV_ALIGN_OUT_BOTTOM_MID, 0, 5);
lv_obj_align(indicators[0], indicators[1], LV_ALIGN_OUT_LEFT_MID, -8, 0);
lv_obj_align(indicators[2], indicators[1], LV_ALIGN_OUT_RIGHT_MID, 8, 0);
SetIndicators();
SetColors();
backgroundAction = lv_label_create(lv_scr_act(), nullptr); backgroundAction = lv_label_create(lv_scr_act(), nullptr);
lv_label_set_long_mode(backgroundAction, LV_LABEL_LONG_CROP); lv_label_set_long_mode(backgroundAction, LV_LABEL_LONG_CROP);
@ -44,27 +55,80 @@ FlashLight::FlashLight(Pinetime::Applications::DisplayApp* app,
FlashLight::~FlashLight() { FlashLight::~FlashLight() {
lv_obj_clean(lv_scr_act()); 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_hex(0x000000)); lv_obj_set_style_local_bg_color(lv_scr_act(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
brightness.Restore(); brightnessController.Restore();
systemTask.PushMessage(Pinetime::System::Messages::EnableSleeping); systemTask.PushMessage(Pinetime::System::Messages::EnableSleeping);
} }
void FlashLight::OnClickEvent(lv_obj_t* obj, lv_event_t event) { void FlashLight::SetColors() {
if (obj == backgroundAction) {
if (event == LV_EVENT_CLICKED) {
isOn = !isOn;
if (isOn) { if (isOn) {
lv_obj_set_style_local_bg_color(lv_scr_act(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0xFFFFFF)); lv_obj_set_style_local_bg_color(lv_scr_act(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
lv_obj_set_style_local_text_color(flashLight, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x000000)); lv_obj_set_style_local_text_color(flashLight, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
for (auto & i : indicators) {
lv_obj_set_style_local_bg_color(i, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
lv_obj_set_style_local_bg_color(i, LV_OBJ_PART_MAIN, LV_STATE_DISABLED, LV_COLOR_WHITE);
lv_obj_set_style_local_border_color(i, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
}
} else { } else {
lv_obj_set_style_local_bg_color(lv_scr_act(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x000000)); lv_obj_set_style_local_bg_color(lv_scr_act(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
lv_obj_set_style_local_text_color(flashLight, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0xFFFFFF)); lv_obj_set_style_local_text_color(flashLight, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
for (auto & i : indicators) {
lv_obj_set_style_local_bg_color(i, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
lv_obj_set_style_local_bg_color(i, LV_OBJ_PART_MAIN, LV_STATE_DISABLED, LV_COLOR_BLACK);
lv_obj_set_style_local_border_color(i, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
} }
} }
} }
void FlashLight::SetIndicators() {
using namespace Pinetime::Controllers;
if (brightnessLevel == BrightnessController::Levels::High) {
lv_obj_set_state(indicators[1], LV_STATE_DEFAULT);
lv_obj_set_state(indicators[2], LV_STATE_DEFAULT);
} else if (brightnessLevel == BrightnessController::Levels::Medium) {
lv_obj_set_state(indicators[1], LV_STATE_DEFAULT);
lv_obj_set_state(indicators[2], LV_STATE_DISABLED);
} else {
lv_obj_set_state(indicators[1], LV_STATE_DISABLED);
lv_obj_set_state(indicators[2], LV_STATE_DISABLED);
}
}
void FlashLight::OnClickEvent(lv_obj_t* obj, lv_event_t event) {
if (obj == backgroundAction && event == LV_EVENT_CLICKED) {
isOn = !isOn;
SetColors();
}
} }
bool FlashLight::OnTouchEvent(Pinetime::Applications::TouchEvents event) { bool FlashLight::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
using namespace Pinetime::Controllers;
if (event == TouchEvents::SwipeLeft) {
if (brightnessLevel == BrightnessController::Levels::High) {
brightnessLevel = BrightnessController::Levels::Medium;
brightnessController.Set(brightnessLevel);
SetIndicators();
} else if (brightnessLevel == BrightnessController::Levels::Medium) {
brightnessLevel = BrightnessController::Levels::Low;
brightnessController.Set(brightnessLevel);
SetIndicators();
}
return true;
}
if (event == TouchEvents::SwipeRight) {
if (brightnessLevel == BrightnessController::Levels::Low) {
brightnessLevel = BrightnessController::Levels::Medium;
brightnessController.Set(brightnessLevel);
SetIndicators();
} else if (brightnessLevel == BrightnessController::Levels::Medium) {
brightnessLevel = BrightnessController::Levels::High;
brightnessController.Set(brightnessLevel);
SetIndicators();
}
return true;
}
return false; return false;
} }

View file

@ -1,10 +1,10 @@
#pragma once #pragma once
#include <cstdint>
#include "Screen.h" #include "Screen.h"
#include <lvgl/lvgl.h>
#include "systemtask/SystemTask.h"
#include "components/brightness/BrightnessController.h" #include "components/brightness/BrightnessController.h"
#include "systemtask/SystemTask.h"
#include <cstdint>
#include <lvgl/lvgl.h>
namespace Pinetime { namespace Pinetime {
@ -20,12 +20,18 @@ namespace Pinetime {
void OnClickEvent(lv_obj_t* obj, lv_event_t event); void OnClickEvent(lv_obj_t* obj, lv_event_t event);
private: private:
void SetIndicators();
void SetColors();
Pinetime::System::SystemTask& systemTask; Pinetime::System::SystemTask& systemTask;
Controllers::BrightnessController& brightness; Controllers::BrightnessController& brightnessController;
Controllers::BrightnessController::Levels brightnessLevel;
lv_obj_t* flashLight; lv_obj_t* flashLight;
lv_obj_t* backgroundAction; lv_obj_t* backgroundAction;
bool isOn = true; lv_obj_t* indicators[3];
bool isOn = false;
}; };
} }
} }

View file

@ -131,7 +131,7 @@ void QuickSettings::OnButtonEvent(lv_obj_t* object, lv_event_t event) {
if (object == btn2 && event == LV_EVENT_CLICKED) { if (object == btn2 && event == LV_EVENT_CLICKED) {
running = false; running = false;
app->StartApp(Apps::FlashLight, DisplayApp::FullRefreshDirections::None); app->StartApp(Apps::FlashLight, DisplayApp::FullRefreshDirections::Up);
} else if (object == btn1 && event == LV_EVENT_CLICKED) { } else if (object == btn1 && event == LV_EVENT_CLICKED) {