Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use callback for backlight fade #641

Merged
merged 3 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 40 additions & 14 deletions src/dev/esp32/esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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<Esp32Device*>(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;
Expand All @@ -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"));
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/dev/esp32/esp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "hasp_conf.h"
#include "../device.h"
#include "driver/ledc.h"

#if defined(ESP32)

Expand Down Expand Up @@ -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
Expand Down