Skip to content

Commit 5d4334a

Browse files
committed
Created separate component for backlight control
-> Uses Timer 0, Channel 0 as PWM to control backlight via LED pin Signed-off-by: Rajssss <[email protected]>
1 parent a17a77b commit 5d4334a

File tree

6 files changed

+96
-50
lines changed

6 files changed

+96
-50
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ if(ESP_PLATFORM)
22

33
file(GLOB SOURCES *.c)
44
set(LVGL_INCLUDE_DIRS . lvgl_tft)
5-
list(APPEND SOURCES "lvgl_tft/disp_driver.c")
5+
list(APPEND SOURCES "lvgl_tft/disp_driver.c" "lvgl_tft/esp_lcd_backlight.c")
66

77
#@todo add SimleInclude macro here
88

lvgl_helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ extern "C" {
1616

1717
#include "lvgl_spi_conf.h"
1818
#include "lvgl_tft/disp_driver.h"
19+
#include "lvgl_tft/esp_lcd_backlight.h"
1920
#include "lvgl_touch/touch_driver.h"
2021

2122
/*********************

lvgl_tft/esp_lcd_backlight.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @file esp_lcd_backlight.c
3+
*
4+
*/
5+
6+
/*********************
7+
* INCLUDES
8+
*********************/
9+
#include "esp_lcd_backlight.h"
10+
#include "driver/ledc.h"
11+
#include "esp_log.h"
12+
13+
static const char *TAG = "disp_brightness";
14+
15+
void disp_brightness_control_enable(void)
16+
{
17+
/*
18+
Configure LED (Backlight) pin as PWM for Brightness control.
19+
*/
20+
ledc_channel_config_t LCD_backlight_channel = {
21+
.gpio_num = DISP_PIN_BCKL,
22+
.speed_mode = LEDC_LOW_SPEED_MODE,
23+
.channel = LEDC_CHANNEL_0,
24+
.intr_type = LEDC_INTR_DISABLE,
25+
.timer_sel = LEDC_TIMER_0,
26+
.duty = 0,
27+
.hpoint = 0,
28+
.flags.output_invert = 0
29+
};
30+
ledc_timer_config_t LCD_backlight_timer = {
31+
.speed_mode = LEDC_LOW_SPEED_MODE,
32+
.bit_num = LEDC_TIMER_10_BIT,
33+
.timer_num = LEDC_TIMER_0,
34+
.freq_hz = 5000,
35+
.clk_cfg = LEDC_AUTO_CLK
36+
};
37+
38+
ESP_ERROR_CHECK( ledc_timer_config(&LCD_backlight_timer) );
39+
ESP_ERROR_CHECK( ledc_channel_config(&LCD_backlight_channel) );
40+
41+
}
42+
43+
void disp_set_brightness(uint16_t brightness)
44+
{
45+
/*
46+
Set brightness.
47+
0 -> Display off
48+
100 -> Full brightness
49+
NOTE: brightness value must be between 0 - 100
50+
*/
51+
if(brightness > 100)
52+
{
53+
ESP_LOGE(TAG, "Brightness value must be between 0 - 100");
54+
return;
55+
}
56+
ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, brightness*10) );
57+
ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
58+
}

lvgl_tft/esp_lcd_backlight.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @file esp_lcd_backlight.h
3+
*/
4+
5+
#ifndef ESP_LCD_BACKLIGHT_H
6+
#define ESP_LCD_BACKLIGHT_H
7+
8+
/*********************
9+
* INCLUDES
10+
*********************/
11+
#include <stdbool.h>
12+
#ifdef LV_LVGL_H_INCLUDE_SIMPLE
13+
#include "lvgl.h"
14+
#else
15+
#include "lvgl/lvgl.h"
16+
#endif
17+
18+
19+
/*********************
20+
* DEFINES
21+
*********************/
22+
#define DISP_PIN_BCKL CONFIG_LV_DISP_PIN_BCKL
23+
24+
25+
/**********************
26+
* GLOBAL PROTOTYPES
27+
**********************/
28+
void disp_brightness_control_enable(void);
29+
void disp_set_brightness(uint16_t brightness);
30+
31+
32+
#ifdef __cplusplus
33+
} /* extern "C" */
34+
#endif
35+
36+
#endif /*ESP_LCD_BACKLIGHT_H*/

lvgl_tft/st7735s.c

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "disp_spi.h"
1111
#include "driver/i2c.h"
1212
#include "driver/gpio.h"
13-
#include "driver/ledc.h"
1413
#include "esp_log.h"
1514
#include "freertos/FreeRTOS.h"
1615
#include "freertos/task.h"
@@ -173,51 +172,6 @@ void st7735s_sleep_out()
173172
st7735s_send_cmd(0x11);
174173
}
175174

176-
void st7735s_brightness_control_enable(void)
177-
{
178-
/*
179-
Configure LED (Backlight) pin as PWM for Brightness control.
180-
*/
181-
ledc_channel_config_t LCD_backlight_channel = {
182-
.gpio_num = ST7735S_BCKL,
183-
.speed_mode = LEDC_LOW_SPEED_MODE,
184-
.channel = LEDC_CHANNEL_0,
185-
.intr_type = LEDC_INTR_DISABLE,
186-
.timer_sel = LEDC_TIMER_0,
187-
.duty = 0,
188-
.hpoint = 0,
189-
.flags.output_invert = 0
190-
};
191-
ledc_timer_config_t LCD_backlight_timer = {
192-
.speed_mode = LEDC_LOW_SPEED_MODE,
193-
.bit_num = LEDC_TIMER_10_BIT,
194-
.timer_num = LEDC_TIMER_0,
195-
.freq_hz = 5000,
196-
.clk_cfg = LEDC_AUTO_CLK
197-
};
198-
199-
ESP_ERROR_CHECK( ledc_timer_config(&LCD_backlight_timer) );
200-
ESP_ERROR_CHECK( ledc_channel_config(&LCD_backlight_channel) );
201-
202-
}
203-
204-
void st7735s_set_brightness(uint16_t brightness)
205-
{
206-
/*
207-
Set brightness.
208-
0 -> Display off
209-
100 -> Full brightness
210-
NOTE: brightness value must be between 0 - 100
211-
*/
212-
if(brightness > 100)
213-
{
214-
ESP_LOGE(TAG, "Brightness value must be between 0 - 100");
215-
return;
216-
}
217-
ESP_ERROR_CHECK( ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, brightness*10) );
218-
ESP_ERROR_CHECK( ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0) );
219-
}
220-
221175
/**********************
222176
* STATIC FUNCTIONS
223177
**********************/

lvgl_tft/st7735s.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ extern "C" {
2626
#define DISP_BUF_SIZE (LV_HOR_RES_MAX * 40)
2727

2828
#define ST7735S_DC CONFIG_LV_DISP_PIN_DC
29-
#define ST7735S_BCKL CONFIG_LV_DISP_PIN_BCKL
3029
#define ST7735S_RST CONFIG_LV_DISP_PIN_RST
3130
#define ST7735S_USE_RST CONFIG_LV_DISP_USE_RST
3231

@@ -138,8 +137,6 @@ void st7735s_flush(lv_disp_drv_t * drv, const lv_area_t * area, lv_color_t * col
138137
void st7735s_enable_backlight(bool backlight);
139138
void st7735s_sleep_in(void);
140139
void st7735s_sleep_out(void);
141-
void st7735s_brightness_control_enable(void);
142-
void st7735s_set_brightness(uint16_t brightness);
143140

144141
/**********************
145142
* MACROS

0 commit comments

Comments
 (0)