Created separate component for backlight control

-> Uses Timer 0, Channel 0 as PWM to control
   backlight via LED pin

Signed-off-by: Rajssss <sssraj.sssraj@gmail.com>
This commit is contained in:
Rajssss 2021-06-26 12:10:41 +05:30
parent a17a77b923
commit 5d4334a5b9
No known key found for this signature in database
GPG key ID: 5D2F803A3F7DD428
6 changed files with 96 additions and 50 deletions

View file

@ -2,7 +2,7 @@ if(ESP_PLATFORM)
file(GLOB SOURCES *.c)
set(LVGL_INCLUDE_DIRS . lvgl_tft)
list(APPEND SOURCES "lvgl_tft/disp_driver.c")
list(APPEND SOURCES "lvgl_tft/disp_driver.c" "lvgl_tft/esp_lcd_backlight.c")
#@todo add SimleInclude macro here

View file

@ -16,6 +16,7 @@ extern "C" {
#include "lvgl_spi_conf.h"
#include "lvgl_tft/disp_driver.h"
#include "lvgl_tft/esp_lcd_backlight.h"
#include "lvgl_touch/touch_driver.h"
/*********************

View file

@ -0,0 +1,58 @@
/**
* @file esp_lcd_backlight.c
*
*/
/*********************
* INCLUDES
*********************/
#include "esp_lcd_backlight.h"
#include "driver/ledc.h"
#include "esp_log.h"
static const char *TAG = "disp_brightness";
void disp_brightness_control_enable(void)
{
/*
Configure LED (Backlight) pin as PWM for Brightness control.
*/
ledc_channel_config_t LCD_backlight_channel = {
.gpio_num = DISP_PIN_BCKL,
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_0,
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = LEDC_TIMER_0,
.duty = 0,
.hpoint = 0,
.flags.output_invert = 0
};
ledc_timer_config_t LCD_backlight_timer = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.bit_num = LEDC_TIMER_10_BIT,
.timer_num = LEDC_TIMER_0,
.freq_hz = 5000,
.clk_cfg = LEDC_AUTO_CLK
};
ESP_ERROR_CHECK( ledc_timer_config(&LCD_backlight_timer) );
ESP_ERROR_CHECK( ledc_channel_config(&LCD_backlight_channel) );
}
void disp_set_brightness(uint16_t brightness)
{
/*
Set brightness.
0 -> Display off
100 -> Full brightness
NOTE: brightness value must be between 0 - 100
*/
if(brightness > 100)
{
ESP_LOGE(TAG, "Brightness value must be between 0 - 100");
return;
}
ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, brightness*10) );
ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
}

View file

@ -0,0 +1,36 @@
/**
* @file esp_lcd_backlight.h
*/
#ifndef ESP_LCD_BACKLIGHT_H
#define ESP_LCD_BACKLIGHT_H
/*********************
* INCLUDES
*********************/
#include <stdbool.h>
#ifdef LV_LVGL_H_INCLUDE_SIMPLE
#include "lvgl.h"
#else
#include "lvgl/lvgl.h"
#endif
/*********************
* DEFINES
*********************/
#define DISP_PIN_BCKL CONFIG_LV_DISP_PIN_BCKL
/**********************
* GLOBAL PROTOTYPES
**********************/
void disp_brightness_control_enable(void);
void disp_set_brightness(uint16_t brightness);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /*ESP_LCD_BACKLIGHT_H*/

View file

@ -10,7 +10,6 @@
#include "disp_spi.h"
#include "driver/i2c.h"
#include "driver/gpio.h"
#include "driver/ledc.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
@ -173,51 +172,6 @@ void st7735s_sleep_out()
st7735s_send_cmd(0x11);
}
void st7735s_brightness_control_enable(void)
{
/*
Configure LED (Backlight) pin as PWM for Brightness control.
*/
ledc_channel_config_t LCD_backlight_channel = {
.gpio_num = ST7735S_BCKL,
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_0,
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = LEDC_TIMER_0,
.duty = 0,
.hpoint = 0,
.flags.output_invert = 0
};
ledc_timer_config_t LCD_backlight_timer = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.bit_num = LEDC_TIMER_10_BIT,
.timer_num = LEDC_TIMER_0,
.freq_hz = 5000,
.clk_cfg = LEDC_AUTO_CLK
};
ESP_ERROR_CHECK( ledc_timer_config(&LCD_backlight_timer) );
ESP_ERROR_CHECK( ledc_channel_config(&LCD_backlight_channel) );
}
void st7735s_set_brightness(uint16_t brightness)
{
/*
Set brightness.
0 -> Display off
100 -> Full brightness
NOTE: brightness value must be between 0 - 100
*/
if(brightness > 100)
{
ESP_LOGE(TAG, "Brightness value must be between 0 - 100");
return;
}
ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, brightness*10) );
ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
}
/**********************
* STATIC FUNCTIONS
**********************/

View file

@ -26,7 +26,6 @@ extern "C" {
#define DISP_BUF_SIZE (LV_HOR_RES_MAX * 40)
#define ST7735S_DC CONFIG_LV_DISP_PIN_DC
#define ST7735S_BCKL CONFIG_LV_DISP_PIN_BCKL
#define ST7735S_RST CONFIG_LV_DISP_PIN_RST
#define ST7735S_USE_RST CONFIG_LV_DISP_USE_RST
@ -138,8 +137,6 @@ void st7735s_flush(lv_disp_drv_t * drv, const lv_area_t * area, lv_color_t * col
void st7735s_enable_backlight(bool backlight);
void st7735s_sleep_in(void);
void st7735s_sleep_out(void);
void st7735s_brightness_control_enable(void);
void st7735s_set_brightness(uint16_t brightness);
/**********************
* MACROS