diff --git a/src/dev/esp32/esp32.cpp b/src/dev/esp32/esp32.cpp index 7e20d8ba0..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")); @@ -275,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() @@ -287,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() @@ -298,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 e3a8bd17a..401a44355 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