Skip to content

Commit

Permalink
Add display backlight brightness control
Browse files Browse the repository at this point in the history
  • Loading branch information
bkerler committed Sep 1, 2024
1 parent cefc467 commit d1f8e7a
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 6 deletions.
19 changes: 19 additions & 0 deletions src/gui/MItem_menus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <option/has_leds.h>
#if HAS_LEDS()
#include "led_animations/animator.hpp"
#include "gui_leds.hpp"
#endif
#include "img_resources.hpp"
#include "power_panic.hpp"
Expand Down Expand Up @@ -535,6 +536,24 @@ void MI_LEDS_ENABLE::OnChange(size_t old_index) {
}
}

#if HAS_LEDS()
/**********************************************************************************************/
// MI_DISPLAY_BACKLIGHT_BRIGHTNESS
static constexpr NumericInputConfig led_display_backlight_brightness_spin_config = {
// Keep a min value of 20 percent to make sure the user can still see the lcd content if changed by mistake
.min_value = 20,
.max_value = 100,
};

MI_DISPLAY_BACKLIGHT_BRIGHTNESS::MI_DISPLAY_BACKLIGHT_BRIGHTNESS()
: WiSpin(config_store().leds_display_backlight_brightness.get(), led_display_backlight_brightness_spin_config, _(label), nullptr, is_enabled_t::yes, is_hidden_t::no) {
}
void MI_DISPLAY_BACKLIGHT_BRIGHTNESS::OnClick() {
leds::display_backlight_brightness(GetVal());
leds::store_display_backlight_brightness(GetVal());
}
#endif

// MI_SIDE_LEDS_DIMMING_DURATION
static constexpr NumericInputConfig side_led_dimming_spin_config = {
.max_value = 60 * 60, // 60 Minutes
Expand Down
10 changes: 10 additions & 0 deletions src/gui/MItem_menus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,16 @@ class MI_LEDS_ENABLE : public WI_ICON_SWITCH_OFF_ON_t {
};
#endif

#if HAS_LEDS()
class MI_DISPLAY_BACKLIGHT_BRIGHTNESS : public WiSpin {
static constexpr const char *const label = N_("Display Backlight Brightness");

public:
MI_DISPLAY_BACKLIGHT_BRIGHTNESS();
virtual void OnClick() override;
};
#endif

#if HAS_SIDE_LEDS()
class MI_SIDE_LEDS_ENABLE : public WI_ICON_SWITCH_OFF_ON_t {
static constexpr const char *const label = N_("RGB Side Strip");
Expand Down
3 changes: 2 additions & 1 deletion src/gui/screen_menu_user_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ using ScreenMenuUserInterface__ = ScreenMenu<GuiDefaults::MenuFooter, MI_RETURN,
#endif
#if HAS_LEDS()
,
MI_LEDS_ENABLE
MI_LEDS_ENABLE,
MI_DISPLAY_BACKLIGHT_BRIGHTNESS
#endif
#if HAS_SIDE_LEDS()
,
Expand Down
18 changes: 16 additions & 2 deletions src/guiapi/include/gui_leds.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,25 @@ void TickLoop();
void SetNth(Color clr, index n);

/**
* @brief Set the Brightness of display
* @brief Set the Brightness of display backlight
*
* @param percent Brightness in range 0 - 100
*/
void SetBrightness(unsigned percent);
void display_backlight_brightness(unsigned percent);

/**
* @brief Read the Brightness of the display backlight from ConfigStore
*
* @return percent Brightness in range 20 - 100
*/
uint8_t get_display_backlight_brightness();

/**
* @brief Store the Brightness of the display backlight to ConfigStore
*
* @param percent Brightness in range 20 - 100
*/
void store_display_backlight_brightness(uint8_t percent);

/**
* @brief Forces write on next call of tick loop
Expand Down
15 changes: 12 additions & 3 deletions src/guiapi/src/gui_leds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ Neopixels &getNeopixels() {

void leds::Init() {
// Turn on LCD backlight
// TODO move SetBrightness to display
leds::SetBrightness(100);
// TODO move display_backlight_brightness to display
leds::display_backlight_brightness(leds::get_display_backlight_brightness());
leds::TickLoop();

#if HAS_SIDE_LEDS()
Expand All @@ -42,6 +42,15 @@ void leds::ForceRefresh(size_t cnt) {
getNeopixels().ForceRefresh(cnt);
}

uint8_t leds::get_display_backlight_brightness() {
return config_store().leds_display_backlight_brightness.get();
}

void leds::store_display_backlight_brightness(uint8_t percent) {
leds::display_backlight_brightness(percent);
return config_store().leds_display_backlight_brightness.set(percent);
}

void leds::TickLoop() {
getNeopixels().Tick();

Expand All @@ -63,7 +72,7 @@ void leds::SetNth(Color clr, leds::index n) {
// backlight is on WS2811 (RGB)
// but color structure is meant for WS2812 (GRB)
// so to set RED channel on WS2811 have to set GREEN
void leds::SetBrightness(unsigned percent) {
void leds::display_backlight_brightness(unsigned percent) {
percent = std::min(100u, percent);
percent *= 255;
percent /= 100;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ struct CurrentStore
StoreItem<bool, true, journal::hash("Enable Side LEDs dimming")> side_leds_dimming_enabled;
StoreItem<int, 120, journal::hash("Side LEDs dimming duration")> side_leds_dimming_duration;

StoreItem<uint8_t, 100, journal::hash("Display backlight brightness")> leds_display_backlight_brightness;

StoreItem<bool, true, journal::hash("Enable Tool LEDs")> tool_leds_enabled;
StoreItem<uint8_t, 100, journal::hash("Tool LED brightness")> tool_leds_brightness;

Expand Down

0 comments on commit d1f8e7a

Please sign in to comment.