Skip to content

Commit

Permalink
Updated example and BSP for same changes like in PR #136.
Browse files Browse the repository at this point in the history
  • Loading branch information
espzav committed Mar 13, 2023
1 parent 7498f56 commit df7a3fc
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 75 deletions.
156 changes: 88 additions & 68 deletions esp-box-lite/esp-box-lite.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
* SPDX-License-Identifier: CC0-1.0
*/

#include "esp_timer.h"
#include "driver/gpio.h"
#include "driver/ledc.h"
#include "driver/spi_master.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_check.h"
#include "esp_spiffs.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_vendor.h"
#include "esp_lcd_panel_ops.h"

#include "bsp/esp-box-lite.h"
#include "bsp/display.h"
#include "esp_lvgl_port.h"
#include "bsp_err_check.h"
#include "esp_codec_dev_defaults.h"
Expand Down Expand Up @@ -243,8 +244,65 @@ esp_codec_dev_handle_t bsp_audio_codec_microphone_init(void)
#define LCD_PARAM_BITS 8
#define LCD_LEDC_CH CONFIG_BSP_DISPLAY_BRIGHTNESS_LEDC_CH

static lv_disp_t *bsp_display_lcd_init(void)
static esp_err_t bsp_display_brightness_init(void)
{
// Setup LEDC peripheral for PWM backlight control
const ledc_channel_config_t LCD_backlight_channel = {
.gpio_num = BSP_LCD_BACKLIGHT,
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LCD_LEDC_CH,
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = 1,
.duty = 0,
.hpoint = 0
};
const ledc_timer_config_t LCD_backlight_timer = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.duty_resolution = LEDC_TIMER_10_BIT,
.timer_num = 1,
.freq_hz = 5000,
.clk_cfg = LEDC_AUTO_CLK
};

BSP_ERROR_CHECK_RETURN_ERR(ledc_timer_config(&LCD_backlight_timer));
BSP_ERROR_CHECK_RETURN_ERR(ledc_channel_config(&LCD_backlight_channel));

return ESP_OK;
}

esp_err_t bsp_display_brightness_set(int brightness_percent)
{
if (brightness_percent > 100) {
brightness_percent = 100;
}
if (brightness_percent < 0) {
brightness_percent = 0;
}

ESP_LOGI(TAG, "Setting LCD backlight: %d%%", brightness_percent);
uint32_t duty_cycle = (1023 * (100 - brightness_percent)) / 100; // LEDC resolution set to 10bits, thus: 100% = 1023
BSP_ERROR_CHECK_RETURN_ERR(ledc_set_duty(LEDC_LOW_SPEED_MODE, LCD_LEDC_CH, duty_cycle));
BSP_ERROR_CHECK_RETURN_ERR(ledc_update_duty(LEDC_LOW_SPEED_MODE, LCD_LEDC_CH));

return ESP_OK;
}

esp_err_t bsp_display_backlight_off(void)
{
return bsp_display_brightness_set(0);
}

esp_err_t bsp_display_backlight_on(void)
{
return bsp_display_brightness_set(100);
}

esp_err_t bsp_display_new(esp_lcd_panel_handle_t *ret_panel, esp_lcd_panel_io_handle_t *ret_io)
{
esp_err_t ret = ESP_OK;

ESP_RETURN_ON_ERROR(bsp_display_brightness_init(), TAG, "Brightness init failed");

ESP_LOGD(TAG, "Initialize SPI bus");
const spi_bus_config_t buscfg = {
.sclk_io_num = BSP_LCD_PCLK,
Expand All @@ -254,10 +312,9 @@ static lv_disp_t *bsp_display_lcd_init(void)
.quadhd_io_num = GPIO_NUM_NC,
.max_transfer_sz = BSP_LCD_H_RES * 80 * sizeof(uint16_t),
};
BSP_ERROR_CHECK_RETURN_NULL(spi_bus_initialize(BSP_LCD_SPI_NUM, &buscfg, SPI_DMA_CH_AUTO));
ESP_RETURN_ON_ERROR(spi_bus_initialize(BSP_LCD_SPI_NUM, &buscfg, SPI_DMA_CH_AUTO), TAG, "SPI init failed");

ESP_LOGD(TAG, "Install panel IO");
esp_lcd_panel_io_handle_t io_handle = NULL;
const esp_lcd_panel_io_spi_config_t io_config = {
.dc_gpio_num = BSP_LCD_DC,
.cs_gpio_num = BSP_LCD_CS,
Expand All @@ -267,24 +324,40 @@ static lv_disp_t *bsp_display_lcd_init(void)
.spi_mode = 0,
.trans_queue_depth = 10,
};
ESP_GOTO_ON_ERROR(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)BSP_LCD_SPI_NUM, &io_config, ret_io), err, TAG, "New panel IO failed");

// Attach the LCD to the SPI bus
BSP_ERROR_CHECK_RETURN_NULL(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)BSP_LCD_SPI_NUM, &io_config, &io_handle));

ESP_LOGD(TAG, "Install LCD driver of st7789");
esp_lcd_panel_handle_t panel_handle = NULL;
ESP_LOGD(TAG, "Install LCD driver");
const esp_lcd_panel_dev_config_t panel_config = {
.reset_gpio_num = BSP_LCD_RST,
.color_space = ESP_LCD_COLOR_SPACE_RGB,
.bits_per_pixel = 16,
};
BSP_ERROR_CHECK_RETURN_NULL(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle));
ESP_GOTO_ON_ERROR(esp_lcd_new_panel_st7789(*ret_io, &panel_config, ret_panel), err, TAG, "New panel failed");

esp_lcd_panel_reset(*ret_panel);
esp_lcd_panel_init(*ret_panel);
esp_lcd_panel_mirror(*ret_panel, false, true);
esp_lcd_panel_swap_xy(*ret_panel, true);
esp_lcd_panel_invert_color(*ret_panel, true);
return ret;

err:
if (*ret_panel) {
esp_lcd_panel_del(*ret_panel);
}
if (*ret_io) {
esp_lcd_panel_io_del(*ret_io);
}
spi_bus_free(BSP_LCD_SPI_NUM);
return ret;
}

static lv_disp_t *bsp_display_lcd_init(void)
{
esp_lcd_panel_io_handle_t io_handle = NULL;
esp_lcd_panel_handle_t panel_handle = NULL;
BSP_ERROR_CHECK_RETURN_NULL(bsp_display_new(&panel_handle, &io_handle));

esp_lcd_panel_reset(panel_handle);
esp_lcd_panel_init(panel_handle);
esp_lcd_panel_mirror(panel_handle, false, true);
esp_lcd_panel_swap_xy(panel_handle, true);
esp_lcd_panel_invert_color(panel_handle, true);
esp_lcd_panel_disp_on_off(panel_handle, true);

/* Add LCD screen */
Expand Down Expand Up @@ -323,59 +396,6 @@ static lv_indev_t *bsp_display_indev_init(lv_disp_t *disp)
return lvgl_port_add_navigation_buttons(&btns);
}

static esp_err_t bsp_display_brightness_init(void)
{
// Setup LEDC peripheral for PWM backlight control
const ledc_channel_config_t LCD_backlight_channel = {
.gpio_num = BSP_LCD_BACKLIGHT,
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LCD_LEDC_CH,
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = 1,
.duty = 0,
.hpoint = 0
};
const ledc_timer_config_t LCD_backlight_timer = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.duty_resolution = LEDC_TIMER_10_BIT,
.timer_num = 1,
.freq_hz = 5000,
.clk_cfg = LEDC_AUTO_CLK
};

BSP_ERROR_CHECK_RETURN_ERR(ledc_timer_config(&LCD_backlight_timer));
BSP_ERROR_CHECK_RETURN_ERR(ledc_channel_config(&LCD_backlight_channel));

return ESP_OK;
}

esp_err_t bsp_display_brightness_set(int brightness_percent)
{
if (brightness_percent > 100) {
brightness_percent = 100;
}
if (brightness_percent < 0) {
brightness_percent = 0;
}

ESP_LOGI(TAG, "Setting LCD backlight: %d%%", brightness_percent);
uint32_t duty_cycle = (1023 * (100 - brightness_percent)) / 100; // LEDC resolution set to 10bits, thus: 100% = 1023
BSP_ERROR_CHECK_RETURN_ERR(ledc_set_duty(LEDC_LOW_SPEED_MODE, LCD_LEDC_CH, duty_cycle));
BSP_ERROR_CHECK_RETURN_ERR(ledc_update_duty(LEDC_LOW_SPEED_MODE, LCD_LEDC_CH));

return ESP_OK;
}

esp_err_t bsp_display_backlight_off(void)
{
return bsp_display_brightness_set(0);
}

esp_err_t bsp_display_backlight_on(void)
{
return bsp_display_brightness_set(100);
}

lv_disp_t *bsp_display_start(void)
{
lv_disp_t *disp;
Expand Down
19 changes: 12 additions & 7 deletions examples/display_audio_photo/main/app_disp_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static SemaphoreHandle_t audio_mux;
static bool play_file_repeat = false;
static bool play_file_stop = false;
static char usb_drive_play_file[250];
static lv_obj_t *play_btn = NULL, *play1_btn = NULL, *rec_btn = NULL;
static lv_obj_t *play_btn = NULL, *play1_btn = NULL, *rec_btn = NULL, *rec_stop_btn = NULL;

/*******************************************************************************
* Public API functions
Expand Down Expand Up @@ -824,9 +824,11 @@ static void rec_file(void *arg)
free(recording_buffer);
}

if (rec_btn) {
if (rec_btn && play1_btn && rec_stop_btn) {
bsp_display_lock(0);
lv_obj_clear_state(rec_btn, LV_STATE_DISABLED);
lv_obj_clear_state(play1_btn, LV_STATE_DISABLED);
lv_obj_clear_state(rec_stop_btn, LV_STATE_DISABLED);
bsp_display_unlock();
}

Expand All @@ -841,14 +843,17 @@ static void rec_event_cb(lv_event_t *e)

if (code == LV_EVENT_CLICKED) {
lv_obj_add_state(obj, LV_STATE_DISABLED);
if (rec_stop_btn && play1_btn) {
lv_obj_add_state(play1_btn, LV_STATE_DISABLED);
lv_obj_add_state(rec_stop_btn, LV_STATE_DISABLED);
}
xTaskCreate(rec_file, "rec_file", 4096, e->user_data, 6, NULL);
}
}

static void app_disp_lvgl_show_record(lv_obj_t *screen, lv_group_t *group)
{
lv_obj_t *label;
lv_obj_t *stop_btn;

/* Disable scrolling in this TAB */
lv_obj_clear_flag(screen, LV_OBJ_FLAG_SCROLLABLE);
Expand Down Expand Up @@ -885,15 +890,15 @@ static void app_disp_lvgl_show_record(lv_obj_t *screen, lv_group_t *group)
lv_obj_add_event_cb(play1_btn, rec_play_event_cb, LV_EVENT_CLICKED, (char *)REC_FILENAME);

/* Stop button */
stop_btn = lv_btn_create(cont_row);
label = lv_label_create(stop_btn);
rec_stop_btn = lv_btn_create(cont_row);
label = lv_label_create(rec_stop_btn);
lv_label_set_text_static(label, LV_SYMBOL_STOP);
lv_obj_add_event_cb(stop_btn, rec_stop_event_cb, LV_EVENT_CLICKED, NULL);
lv_obj_add_event_cb(rec_stop_btn, rec_stop_event_cb, LV_EVENT_CLICKED, NULL);

if (group) {
lv_group_add_obj(group, rec_btn);
lv_group_add_obj(group, play1_btn);
lv_group_add_obj(group, stop_btn);
lv_group_add_obj(group, rec_stop_btn);
}
}

Expand Down

0 comments on commit df7a3fc

Please sign in to comment.