esp-who/components/modules/lcd/who_lcd.c

130 lines
4.0 KiB
C
Raw Normal View History

2021-09-08 20:04:59 +08:00
#include "who_lcd.h"
#include "esp_camera.h"
2021-07-31 11:23:18 +08:00
#include <string.h>
2021-11-26 20:03:10 +08:00
#include "logo_en_240x240_lcd.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_vendor.h"
#include "esp_lcd_panel_ops.h"
2021-07-31 11:23:18 +08:00
2021-09-08 20:04:59 +08:00
static const char *TAG = "who_lcd";
2021-07-31 11:23:18 +08:00
static esp_lcd_panel_handle_t panel_handle = NULL;
2021-09-08 20:04:59 +08:00
static QueueHandle_t xQueueFrameI = NULL;
static QueueHandle_t xQueueFrameO = NULL;
static bool gReturnFB = true;
static void task_process_handler(void *arg)
{
camera_fb_t *frame = NULL;
while (true)
{
if (xQueueReceive(xQueueFrameI, &frame, portMAX_DELAY))
{
esp_lcd_panel_draw_bitmap(panel_handle, 0, 0, frame->width, frame->height, (uint16_t *)frame->buf);
2021-09-08 20:04:59 +08:00
if (xQueueFrameO)
{
xQueueSend(xQueueFrameO, &frame, portMAX_DELAY);
}
else if (gReturnFB)
{
esp_camera_fb_return(frame);
}
else
{
free(frame);
}
}
}
}
esp_err_t register_lcd(const QueueHandle_t frame_i, const QueueHandle_t frame_o, const bool return_fb)
2021-07-31 11:23:18 +08:00
{
ESP_LOGI(TAG, "Initialize SPI bus");
spi_bus_config_t bus_conf = {
2021-09-08 20:04:59 +08:00
.sclk_io_num = BOARD_LCD_SCK,
.mosi_io_num = BOARD_LCD_MOSI,
.miso_io_num = BOARD_LCD_MISO,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = BOARD_LCD_H_RES * BOARD_LCD_V_RES * sizeof(uint16_t),
2021-07-31 11:23:18 +08:00
};
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &bus_conf, SPI_DMA_CH_AUTO));
ESP_LOGI(TAG, "Install panel IO");
esp_lcd_panel_io_handle_t io_handle = NULL;
esp_lcd_panel_io_spi_config_t io_config = {
.dc_gpio_num = BOARD_LCD_DC,
.cs_gpio_num = BOARD_LCD_CS,
.pclk_hz = BOARD_LCD_PIXEL_CLOCK_HZ,
.lcd_cmd_bits = BOARD_LCD_CMD_BITS,
.lcd_param_bits = BOARD_LCD_PARAM_BITS,
.spi_mode = 0,
.trans_queue_depth = 10,
2021-07-31 11:23:18 +08:00
};
// Attach the LCD to the SPI bus
ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)SPI2_HOST, &io_config, &io_handle));
// ESP_LOGI(TAG, "Install ST7789 panel driver");
esp_lcd_panel_dev_config_t panel_config = {
.reset_gpio_num = BOARD_LCD_RST,
.rgb_endian = LCD_RGB_ENDIAN_RGB,
.bits_per_pixel = 16,
2021-07-31 11:23:18 +08:00
};
ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle));
ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
esp_lcd_panel_invert_color(panel_handle, true);// Set inversion for esp32s3eye
2021-07-31 11:23:18 +08:00
// turn on display
esp_lcd_panel_disp_on_off(panel_handle, true);
2021-09-08 20:04:59 +08:00
app_lcd_set_color(0x000000);
2021-11-26 20:03:10 +08:00
vTaskDelay(pdMS_TO_TICKS(200));
2021-09-08 20:04:59 +08:00
app_lcd_draw_wallpaper();
2021-11-26 20:03:10 +08:00
vTaskDelay(pdMS_TO_TICKS(200));
2021-09-08 20:04:59 +08:00
xQueueFrameI = frame_i;
xQueueFrameO = frame_o;
gReturnFB = return_fb;
2021-11-26 20:03:10 +08:00
xTaskCreatePinnedToCore(task_process_handler, TAG, 4 * 1024, NULL, 5, NULL, 0);
2021-09-08 20:04:59 +08:00
2021-07-31 11:23:18 +08:00
return ESP_OK;
}
void app_lcd_draw_wallpaper()
{
2021-11-26 20:03:10 +08:00
uint16_t *pixels = (uint16_t *)heap_caps_malloc((logo_en_240x240_lcd_width * logo_en_240x240_lcd_height) * sizeof(uint16_t), MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
2021-07-31 11:23:18 +08:00
if (NULL == pixels)
{
ESP_LOGE(TAG, "Memory for bitmap is not enough");
return;
}
2021-11-26 20:03:10 +08:00
memcpy(pixels, logo_en_240x240_lcd, (logo_en_240x240_lcd_width * logo_en_240x240_lcd_height) * sizeof(uint16_t));
esp_lcd_panel_draw_bitmap(panel_handle, 0, 0, logo_en_240x240_lcd_width, logo_en_240x240_lcd_height, (uint16_t *)pixels);
2021-07-31 11:23:18 +08:00
heap_caps_free(pixels);
}
void app_lcd_set_color(int color)
{
uint16_t *buffer = (uint16_t *)malloc(BOARD_LCD_H_RES * sizeof(uint16_t));
2021-07-31 11:23:18 +08:00
if (NULL == buffer)
{
2021-11-26 20:03:10 +08:00
ESP_LOGE(TAG, "Memory for bitmap is not enough");
2021-07-31 11:23:18 +08:00
}
else
{
for (size_t i = 0; i < BOARD_LCD_H_RES; i++)
2021-07-31 11:23:18 +08:00
{
buffer[i] = color;
}
for (int y = 0; y < BOARD_LCD_V_RES; y++)
2021-07-31 11:23:18 +08:00
{
esp_lcd_panel_draw_bitmap(panel_handle, 0, y, BOARD_LCD_H_RES, y+1, buffer);
2021-07-31 11:23:18 +08:00
}
free(buffer);
}
}