From c01e4a799ecd3a18fc348cde3b9d85504d55db40 Mon Sep 17 00:00:00 2001 From: Ryan Press Date: Fri, 9 Feb 2024 19:28:57 -0800 Subject: [PATCH 1/2] Use callback for backlight fade --- src/dev/esp32/esp32.cpp | 48 ++++++++++++++++++++++++++++++++--------- src/dev/esp32/esp32.h | 6 ++++++ 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/dev/esp32/esp32.cpp b/src/dev/esp32/esp32.cpp index 049f3c1bc..76eece8a6 100644 --- a/src/dev/esp32/esp32.cpp +++ b/src/dev/esp32/esp32.cpp @@ -110,6 +110,8 @@ Esp32Device::Esp32Device() _backlight_power = 1; _backlight_level = 255; _backlight_pin = 255; // not TFT_BCKL because it is unknown at this stage + _backlight_pending = false; + _backlight_fading = false; /* fill unique identifier with wifi mac */ byte mac[6]; @@ -241,6 +243,22 @@ const char* Esp32Device::get_hardware_id() return _hardware_id.c_str(); } +bool Esp32Device::cb_backlight(const ledc_cb_param_t *param, void *user_arg) { + reinterpret_cast(user_arg)->end_backlight_fade(); + return false; +} + +void Esp32Device::end_backlight_fade() +{ + if (_backlight_pending) { + _backlight_pending = false; + _backlight_fading = false; + update_backlight(_backlight_fade); + } else { + _backlight_fading = false; + } +} + void Esp32Device::set_backlight_pin(uint8_t pin) { _backlight_pin = pin; @@ -255,6 +273,8 @@ void Esp32Device::set_backlight_pin(uint8_t pin) #endif ledcAttachPin(pin, BACKLIGHT_CHANNEL); ledc_fade_func_install(0); + ledc_cbs_t cbs = {cb_backlight}; + ledc_cb_register(LEDC_LOW_SPEED_MODE, (ledc_channel_t) BACKLIGHT_CHANNEL, &cbs, this); update_backlight(false); } else { LOG_VERBOSE(TAG_GUI, F("Backlight : Pin not set")); @@ -296,28 +316,36 @@ bool Esp32Device::get_backlight_power() void Esp32Device::update_backlight(bool fade) { - static uint32_t last_duty = 0; if(_backlight_pin < GPIO_NUM_MAX) { #if !defined(CONFIG_IDF_TARGET_ESP32S2) uint32_t duty = _backlight_power ? map(_backlight_level, 0, 255, 0, 1023) : 0; if(_backlight_invert) duty = 1023 - duty; - if(fade) { - ledcWrite(BACKLIGHT_CHANNEL, last_duty); // this will stop an in-progress fade - ledc_set_fade_time_and_start(LEDC_LOW_SPEED_MODE, (ledc_channel_t) BACKLIGHT_CHANNEL, duty, BACKLIGHT_FADEMS, LEDC_FADE_NO_WAIT); + if(_backlight_fading) { + _backlight_fade = fade; + _backlight_pending = true; } else { - ledcWrite(BACKLIGHT_CHANNEL, duty); // ledChannel and value + if(fade) { + _backlight_fading = true; + ledc_set_fade_time_and_start(LEDC_LOW_SPEED_MODE, (ledc_channel_t) BACKLIGHT_CHANNEL, duty, BACKLIGHT_FADEMS, LEDC_FADE_NO_WAIT); + } else { + ledcWrite(BACKLIGHT_CHANNEL, duty); // ledChannel and value + } } #else uint32_t duty = _backlight_power ? map(_backlight_level, 0, 255, 0, 1023) : 0; if(_backlight_invert) duty = 1023 - duty; - if(fade) { - ledcWrite(BACKLIGHT_CHANNEL, last_duty); // this will stop an in-progress fade - ledc_set_fade_time_and_start(LEDC_LOW_SPEED_MODE, (ledc_channel_t) BACKLIGHT_CHANNEL, duty, BACKLIGHT_FADEMS, LEDC_FADE_NO_WAIT); + if(_backlight_fading) { + _backlight_fade = fade; + _backlight_pending = true; } else { - ledcWrite(BACKLIGHT_CHANNEL, duty); // ledChannel and value + if(fade) { + _backlight_fading = true; + ledc_set_fade_time_and_start(LEDC_LOW_SPEED_MODE, (ledc_channel_t) BACKLIGHT_CHANNEL, duty, BACKLIGHT_FADEMS, LEDC_FADE_NO_WAIT); + } else { + ledcWrite(BACKLIGHT_CHANNEL, duty); // ledChannel and value + } } #endif - last_duty = duty; } // haspTft.tft.writecommand(0x53); // Write CTRL Display diff --git a/src/dev/esp32/esp32.h b/src/dev/esp32/esp32.h index 660d0df81..25dc157bd 100644 --- a/src/dev/esp32/esp32.h +++ b/src/dev/esp32/esp32.h @@ -6,6 +6,7 @@ #include "hasp_conf.h" #include "../device.h" +#include "driver/ledc.h" #if defined(ESP32) @@ -68,8 +69,13 @@ class Esp32Device : public BaseDevice { uint8_t _backlight_level; uint8_t _backlight_power; uint8_t _backlight_invert; + bool _backlight_pending; + bool _backlight_fading; + bool _backlight_fade; void update_backlight(bool fade); + static bool cb_backlight(const ledc_cb_param_t *param, void *user_arg); + void end_backlight_fade(); }; } // namespace dev From baa125d4b6cc6264be155f792a008faa8241be59 Mon Sep 17 00:00:00 2001 From: Ryan Press Date: Mon, 12 Feb 2024 10:25:58 -0800 Subject: [PATCH 2/2] enable backlight fade --- src/dev/esp32/esp32.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/dev/esp32/esp32.cpp b/src/dev/esp32/esp32.cpp index dd7f8530a..76eece8a6 100644 --- a/src/dev/esp32/esp32.cpp +++ b/src/dev/esp32/esp32.cpp @@ -295,8 +295,7 @@ bool Esp32Device::get_backlight_invert() void Esp32Device::set_backlight_level(uint8_t level) { _backlight_level = level; - // update_backlight(true); - update_backlight(false); + update_backlight(true); } uint8_t Esp32Device::get_backlight_level() @@ -307,8 +306,7 @@ uint8_t Esp32Device::get_backlight_level() void Esp32Device::set_backlight_power(bool power) { _backlight_power = power; - // update_backlight(true); - update_backlight(false); + update_backlight(true); } bool Esp32Device::get_backlight_power()