2021-10-13 16:08:35 -04:00
|
|
|
#include "displayapp/screens/Brightness.h"
|
2020-11-15 10:49:36 -05:00
|
|
|
#include <lvgl/lvgl.h>
|
2020-03-15 13:03:11 -04:00
|
|
|
|
|
|
|
using namespace Pinetime::Applications::Screens;
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
void slider_event_cb(lv_obj_t* slider, lv_event_t event) {
|
|
|
|
if (event == LV_EVENT_VALUE_CHANGED) {
|
2020-03-15 13:03:11 -04:00
|
|
|
auto* brightnessSlider = static_cast<Brightness*>(slider->user_data);
|
|
|
|
brightnessSlider->OnValueChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
Brightness::Brightness(Pinetime::Applications::DisplayApp* app, Controllers::BrightnessController& brightness)
|
|
|
|
: Screen(app), brightness {brightness} {
|
2020-10-04 07:53:11 -04:00
|
|
|
slider = lv_slider_create(lv_scr_act(), nullptr);
|
2020-03-15 13:03:11 -04:00
|
|
|
lv_obj_set_user_data(slider, this);
|
|
|
|
lv_obj_set_width(slider, LV_DPI * 2);
|
2020-10-04 07:53:11 -04:00
|
|
|
lv_obj_align(slider, nullptr, LV_ALIGN_CENTER, 0, 0);
|
2020-03-15 13:03:11 -04:00
|
|
|
lv_obj_set_event_cb(slider, slider_event_cb);
|
|
|
|
lv_slider_set_range(slider, 0, 2);
|
|
|
|
lv_slider_set_value(slider, LevelToInt(brightness.Level()), LV_ANIM_OFF);
|
|
|
|
|
2020-10-04 07:53:11 -04:00
|
|
|
slider_label = lv_label_create(lv_scr_act(), nullptr);
|
2020-03-15 13:03:11 -04:00
|
|
|
lv_label_set_text(slider_label, LevelToString(brightness.Level()));
|
|
|
|
lv_obj_set_auto_realign(slider_label, true);
|
2021-01-28 12:13:28 -05:00
|
|
|
lv_obj_align(slider_label, slider, LV_ALIGN_OUT_BOTTOM_MID, 0, 30);
|
2020-03-15 13:03:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Brightness::~Brightness() {
|
|
|
|
lv_obj_clean(lv_scr_act());
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
const char* Brightness::LevelToString(Pinetime::Controllers::BrightnessController::Levels level) {
|
|
|
|
switch (level) {
|
|
|
|
case Pinetime::Controllers::BrightnessController::Levels::Off:
|
|
|
|
return "Off";
|
|
|
|
case Pinetime::Controllers::BrightnessController::Levels::Low:
|
|
|
|
return "Low";
|
|
|
|
case Pinetime::Controllers::BrightnessController::Levels::Medium:
|
|
|
|
return "Medium";
|
|
|
|
case Pinetime::Controllers::BrightnessController::Levels::High:
|
|
|
|
return "High";
|
|
|
|
default:
|
|
|
|
return "???";
|
2020-03-15 13:03:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Brightness::OnValueChanged() {
|
|
|
|
SetValue(lv_slider_get_value(slider));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Brightness::SetValue(uint8_t value) {
|
2021-04-18 13:28:14 -04:00
|
|
|
switch (value) {
|
|
|
|
case 0:
|
|
|
|
brightness.Set(Controllers::BrightnessController::Levels::Low);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
brightness.Set(Controllers::BrightnessController::Levels::Medium);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
brightness.Set(Controllers::BrightnessController::Levels::High);
|
|
|
|
break;
|
2020-03-15 13:03:11 -04:00
|
|
|
}
|
|
|
|
lv_label_set_text(slider_label, LevelToString(brightness.Level()));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t Brightness::LevelToInt(Pinetime::Controllers::BrightnessController::Levels level) {
|
2021-04-18 13:28:14 -04:00
|
|
|
switch (level) {
|
|
|
|
case Pinetime::Controllers::BrightnessController::Levels::Off:
|
|
|
|
return 0;
|
|
|
|
case Pinetime::Controllers::BrightnessController::Levels::Low:
|
|
|
|
return 0;
|
|
|
|
case Pinetime::Controllers::BrightnessController::Levels::Medium:
|
|
|
|
return 1;
|
|
|
|
case Pinetime::Controllers::BrightnessController::Levels::High:
|
|
|
|
return 2;
|
|
|
|
default:
|
|
|
|
return 0;
|
2020-03-15 13:03:11 -04:00
|
|
|
}
|
|
|
|
}
|
2020-03-15 16:01:24 -04:00
|
|
|
|
|
|
|
bool Brightness::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
|
2021-04-18 13:28:14 -04:00
|
|
|
switch (event) {
|
2020-03-15 16:01:24 -04:00
|
|
|
case TouchEvents::SwipeLeft:
|
|
|
|
brightness.Lower();
|
2021-04-18 13:28:14 -04:00
|
|
|
if (brightness.Level() == Pinetime::Controllers::BrightnessController::Levels::Off) {
|
2021-01-28 12:13:28 -05:00
|
|
|
brightness.Set(Controllers::BrightnessController::Levels::Low);
|
|
|
|
}
|
2020-03-15 16:01:24 -04:00
|
|
|
SetValue();
|
|
|
|
return true;
|
|
|
|
case TouchEvents::SwipeRight:
|
|
|
|
brightness.Higher();
|
|
|
|
SetValue();
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Brightness::SetValue() {
|
|
|
|
lv_slider_set_value(slider, LevelToInt(brightness.Level()), LV_ANIM_OFF);
|
|
|
|
lv_label_set_text(slider_label, LevelToString(brightness.Level()));
|
|
|
|
}
|