Skip to content

Commit

Permalink
Merge pull request #626 from presslab-us/backlight_fade
Browse files Browse the repository at this point in the history
On ESP32 fade the backlight
  • Loading branch information
fvanroie authored Feb 2, 2024
2 parents 54ca244 + 91faf61 commit 71b1e5d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
28 changes: 21 additions & 7 deletions src/dev/esp32/esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "esp_system.h"
#include <rom/rtc.h> // needed to get the ResetInfo
#include "driver/adc.h"
#include "driver/ledc.h"
#include "esp_adc_cal.h"
#include "esp_efuse.h"

Expand Down Expand Up @@ -253,7 +254,8 @@ void Esp32Device::set_backlight_pin(uint8_t pin)
ledcSetup(BACKLIGHT_CHANNEL, BACKLIGHT_FREQUENCY, 10);
#endif
ledcAttachPin(pin, BACKLIGHT_CHANNEL);
update_backlight();
ledc_fade_func_install(0);
update_backlight(false);
} else {
LOG_VERBOSE(TAG_GUI, F("Backlight : Pin not set"));
}
Expand All @@ -262,7 +264,7 @@ void Esp32Device::set_backlight_pin(uint8_t pin)
void Esp32Device::set_backlight_invert(bool invert)
{
_backlight_invert = invert;
update_backlight();
update_backlight(false);
}

bool Esp32Device::get_backlight_invert()
Expand All @@ -273,7 +275,7 @@ bool Esp32Device::get_backlight_invert()
void Esp32Device::set_backlight_level(uint8_t level)
{
_backlight_level = level;
update_backlight();
update_backlight(true);
}

uint8_t Esp32Device::get_backlight_level()
Expand All @@ -284,26 +286,38 @@ uint8_t Esp32Device::get_backlight_level()
void Esp32Device::set_backlight_power(bool power)
{
_backlight_power = power;
update_backlight();
update_backlight(true);
}

bool Esp32Device::get_backlight_power()
{
return _backlight_power != 0;
}

void Esp32Device::update_backlight()
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;
ledcWrite(BACKLIGHT_CHANNEL, duty); // ledChannel and value
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);
} 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;
ledcWrite(BACKLIGHT_CHANNEL, duty); // ledChannel and value
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);
} else {
ledcWrite(BACKLIGHT_CHANNEL, duty); // ledChannel and value
}
#endif
last_duty = duty;
}

// haspTft.tft.writecommand(0x53); // Write CTRL Display
Expand Down
4 changes: 3 additions & 1 deletion src/dev/esp32/esp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#define BACKLIGHT_FREQUENCY 20000
#endif

#define BACKLIGHT_FADEMS 200

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -67,7 +69,7 @@ class Esp32Device : public BaseDevice {
uint8_t _backlight_power;
uint8_t _backlight_invert;

void update_backlight();
void update_backlight(bool fade);
};

} // namespace dev
Expand Down

0 comments on commit 71b1e5d

Please sign in to comment.