diff --git a/src/drivers/display.cpp b/src/drivers/display.cpp index f986f35d..257327d7 100644 --- a/src/drivers/display.cpp +++ b/src/drivers/display.cpp @@ -6,6 +6,7 @@ #include "display.hpp" +#include #include #include #include @@ -100,7 +101,7 @@ auto Display::Create(IGpios* expander, ledc_timer_config_t led_config{ .speed_mode = LEDC_LOW_SPEED_MODE, - .duty_resolution = LEDC_TIMER_8_BIT, + .duty_resolution = LEDC_TIMER_10_BIT, .timer_num = LEDC_TIMER_0, .freq_hz = 50000, .clk_cfg = LEDC_AUTO_CLK, @@ -186,7 +187,9 @@ Display::Display(IGpios* gpio, spi_device_handle_t handle) handle_(handle), worker_task_(tasks::Worker::Start()), display_on_(false), - brightness_(4096) {} + brightness_(0) { + SetBrightness(50); +} Display::~Display() { ledc_fade_func_uninstall(); @@ -194,8 +197,19 @@ Display::~Display() { auto Display::SetDisplayOn(bool enabled) -> void { display_on_ = enabled; - int new_duty = display_on_ ? brightness_ : 0; + SetDutyCycle(new_duty); +} + +auto Display::SetBrightness(uint_fast8_t percent) -> void { + brightness_ = + std::pow(static_cast(percent) / 100.0, 2.8) * 1024.0 + 0.5; + if (display_on_) { + SetDutyCycle(brightness_); + } +} + +auto Display::SetDutyCycle(uint_fast8_t new_duty) -> void { ledc_set_fade_with_time(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, new_duty, 100); ledc_fade_start(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, LEDC_FADE_NO_WAIT); } diff --git a/src/drivers/include/display.hpp b/src/drivers/include/display.hpp index 23bbbab9..3e667be7 100644 --- a/src/drivers/include/display.hpp +++ b/src/drivers/include/display.hpp @@ -37,6 +37,7 @@ class Display { ~Display(); auto SetDisplayOn(bool) -> void; + auto SetBrightness(uint_fast8_t) -> void; /* Driver callback invoked by LVGL when there is new data to display. */ void OnLvglFlush(lv_disp_drv_t* disp_drv, @@ -54,7 +55,7 @@ class Display { std::unique_ptr worker_task_; bool display_on_; - uint32_t brightness_; + uint_fast8_t brightness_; lv_disp_draw_buf_t buffers_; lv_disp_drv_t driver_; @@ -74,6 +75,8 @@ class Display { void SendTransaction(TransactionType type, const uint8_t* data, size_t length); + + auto SetDutyCycle(uint_fast8_t) -> void; }; } // namespace drivers