Files
iroBotAMOLED_Test/main/drivers/display_driver.c
Neil Arora 22bd5a6f70 update: fixed shake animation
The shake animation figures have been fixed. However, the petting emotion working with touch remains an issue. The Lottie JSON for the emotion is unable not parsable for some reason and my next step is to fix that for tomorrow's meeting.
2026-01-27 00:30:12 +05:30

423 lines
13 KiB
C

/**
* @file display_driver.c
* @brief Display driver implementation for SH8601 LCD with LVGL
*
* This module encapsulates all display initialization including:
* - I2C bus setup for IO expander
* - SPI bus configuration for LCD
* - SH8601 panel driver initialization
* - LVGL display driver registration
* - Thread-safe LVGL task management
*/
#include "display_driver.h"
#include "config/display_config.h"
#include "config/hardware_config.h"
#include "driver/gpio.h"
#include "driver/i2c.h"
#include "driver/spi_master.h"
#include "esp_err.h"
#include "esp_io_expander_tca9554.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_ops.h"
#include "esp_lcd_panel_vendor.h"
#include "esp_lcd_sh8601.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/task.h"
#include "lvgl.h"
static const char *TAG = "display_driver";
// ----------------------------------------------------------------------------
// Private State
// ----------------------------------------------------------------------------
static SemaphoreHandle_t lvglMutex = NULL;
static lv_disp_drv_t dispDriver;
static lv_disp_draw_buf_t dispBuffer;
// ----------------------------------------------------------------------------
// LCD Initialization Commands
// The SH8601 requires specific initialization sequences.
// ----------------------------------------------------------------------------
static const sh8601_lcd_init_cmd_t lcdInitCmds[] = {
{0x11, (uint8_t[]){0x00}, 0, 120},
{0x44, (uint8_t[]){0x01, 0xD1}, 2, 0},
{0x35, (uint8_t[]){0x00}, 1, 0},
{0x53, (uint8_t[]){0x20}, 1, 10},
{0x2A, (uint8_t[]){0x00, 0x00, 0x01, 0x6F}, 4, 0},
{0x2B, (uint8_t[]){0x00, 0x00, 0x01, 0xBF}, 4, 0},
{0x51, (uint8_t[]){0x00}, 1, 10},
{0x29, (uint8_t[]){0x00}, 0, 10},
{0x51, (uint8_t[]){0xFF}, 1, 0},
};
// ----------------------------------------------------------------------------
// Private Function Prototypes
// ----------------------------------------------------------------------------
static bool initI2cBus(void);
static bool initIoExpander(void);
static bool initSpi(void);
static bool initLcdPanel(esp_lcd_panel_io_handle_t *ioHandle,
esp_lcd_panel_handle_t *panelHandle);
static bool initLvgl(esp_lcd_panel_handle_t panelHandle);
static void lvglPortTask(void *arg);
static void lvglTickCallback(void *arg);
static bool lvglFlushReady(esp_lcd_panel_io_handle_t panelIo,
esp_lcd_panel_io_event_data_t *eventData,
void *userCtx);
static void lvglFlushCallback(lv_disp_drv_t *drv, const lv_area_t *area,
lv_color_t *colorMap);
static void lvglUpdateCallback(lv_disp_drv_t *drv);
static void lvglRounderCallback(lv_disp_drv_t *drv, lv_area_t *area);
// ----------------------------------------------------------------------------
// Public API Implementation
// ----------------------------------------------------------------------------
bool displayDriverInit(void) {
ESP_LOGI(TAG, "Initializing display subsystem");
// Suppress noisy log messages from sub-components
esp_log_level_set("lcd_panel.io.i2c", ESP_LOG_NONE);
esp_log_level_set("FT5x06", ESP_LOG_NONE);
// Initialize I2C bus (shared with touch and sensors)
if (!initI2cBus()) {
ESP_LOGE(TAG, "Failed to initialize I2C bus");
return false;
}
// Initialize IO expander for power control
if (!initIoExpander()) {
ESP_LOGE(TAG, "Failed to initialize IO expander");
return false;
}
// Initialize SPI bus for LCD
if (!initSpi()) {
ESP_LOGE(TAG, "Failed to initialize SPI bus");
return false;
}
// Initialize LCD panel driver
esp_lcd_panel_io_handle_t ioHandle = NULL;
esp_lcd_panel_handle_t panelHandle = NULL;
if (!initLcdPanel(&ioHandle, &panelHandle)) {
ESP_LOGE(TAG, "Failed to initialize LCD panel");
return false;
}
// Initialize LVGL graphics library
if (!initLvgl(panelHandle)) {
ESP_LOGE(TAG, "Failed to initialize LVGL");
return false;
}
ESP_LOGI(TAG, "Display subsystem initialized successfully");
return true;
}
bool displayDriverLock(int timeoutMs) {
if (lvglMutex == NULL) {
ESP_LOGE(TAG, "Display not initialized");
return false;
}
TickType_t ticks =
(timeoutMs == -1) ? portMAX_DELAY : pdMS_TO_TICKS(timeoutMs);
return xSemaphoreTake(lvglMutex, ticks) == pdTRUE;
}
void displayDriverUnlock(void) {
if (lvglMutex == NULL) {
ESP_LOGE(TAG, "Display not initialized");
return;
}
xSemaphoreGive(lvglMutex);
}
lv_obj_t *displayDriverGetActiveScreen(void) { return lv_scr_act(); }
// ----------------------------------------------------------------------------
// Private Implementation
// ----------------------------------------------------------------------------
static bool initI2cBus(void) {
ESP_LOGI(TAG, "Initializing I2C bus");
const i2c_config_t i2cConfig = {
.mode = I2C_MODE_MASTER,
.sda_io_num = I2C_MASTER_SDA,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_io_num = I2C_MASTER_SCL,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = I2C_MASTER_FREQ_HZ,
};
esp_err_t err = i2c_param_config(I2C_MASTER_PORT, &i2cConfig);
if (err != ESP_OK) {
ESP_LOGE(TAG, "I2C param config failed: %s", esp_err_to_name(err));
return false;
}
err = i2c_driver_install(I2C_MASTER_PORT, i2cConfig.mode, 0, 0, 0);
if (err != ESP_OK) {
ESP_LOGE(TAG, "I2C driver install failed: %s", esp_err_to_name(err));
return false;
}
ESP_LOGI(TAG, "I2C bus initialized successfully");
return true;
}
static bool initIoExpander(void) {
ESP_LOGI(TAG, "Initializing IO expander");
esp_io_expander_handle_t ioExpander = NULL;
esp_err_t err = esp_io_expander_new_i2c_tca9554(
I2C_MASTER_PORT, ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000, &ioExpander);
if (err != ESP_OK) {
ESP_LOGE(TAG, "IO expander init failed: %s", esp_err_to_name(err));
return false;
}
// Configure pins 0-2 as outputs for power control
esp_io_expander_set_dir(ioExpander,
IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1 |
IO_EXPANDER_PIN_NUM_2,
IO_EXPANDER_OUTPUT);
// Power cycle sequence
esp_io_expander_set_level(ioExpander, IO_EXPANDER_PIN_NUM_0, 0);
esp_io_expander_set_level(ioExpander, IO_EXPANDER_PIN_NUM_1, 0);
esp_io_expander_set_level(ioExpander, IO_EXPANDER_PIN_NUM_2, 0);
vTaskDelay(pdMS_TO_TICKS(200));
esp_io_expander_set_level(ioExpander, IO_EXPANDER_PIN_NUM_0, 1);
esp_io_expander_set_level(ioExpander, IO_EXPANDER_PIN_NUM_1, 1);
esp_io_expander_set_level(ioExpander, IO_EXPANDER_PIN_NUM_2, 1);
return true;
}
static bool initSpi(void) {
ESP_LOGI(TAG, "Initializing SPI bus");
const spi_bus_config_t spiConfig = SH8601_PANEL_BUS_QSPI_CONFIG(
LCD_PIN_PCLK, LCD_PIN_DATA0, LCD_PIN_DATA1, LCD_PIN_DATA2, LCD_PIN_DATA3,
DISPLAY_WIDTH * DISPLAY_HEIGHT * LCD_BIT_PER_PIXEL / 8);
esp_err_t err = spi_bus_initialize(LCD_SPI_HOST, &spiConfig, SPI_DMA_CH_AUTO);
if (err != ESP_OK) {
ESP_LOGE(TAG, "SPI bus init failed: %s", esp_err_to_name(err));
return false;
}
return true;
}
static bool initLcdPanel(esp_lcd_panel_io_handle_t *ioHandle,
esp_lcd_panel_handle_t *panelHandle) {
ESP_LOGI(TAG, "Initializing LCD panel");
// Configure panel IO
const esp_lcd_panel_io_spi_config_t ioConfig =
SH8601_PANEL_IO_QSPI_CONFIG(LCD_PIN_CS, lvglFlushReady, &dispDriver);
sh8601_vendor_config_t vendorConfig = {
.init_cmds = lcdInitCmds,
.init_cmds_size = sizeof(lcdInitCmds) / sizeof(lcdInitCmds[0]),
.flags =
{
.use_qspi_interface = 1,
},
};
esp_err_t err = esp_lcd_new_panel_io_spi(
(esp_lcd_spi_bus_handle_t)LCD_SPI_HOST, &ioConfig, ioHandle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Panel IO init failed: %s", esp_err_to_name(err));
return false;
}
// Configure panel device
const esp_lcd_panel_dev_config_t panelConfig = {
.reset_gpio_num = LCD_PIN_RST,
.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB,
.bits_per_pixel = LCD_BIT_PER_PIXEL,
.vendor_config = &vendorConfig,
};
ESP_LOGI(TAG, "Installing SH8601 panel driver");
err = esp_lcd_new_panel_sh8601(*ioHandle, &panelConfig, panelHandle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Panel driver init failed: %s", esp_err_to_name(err));
return false;
}
// Initialize and enable panel
ESP_ERROR_CHECK(esp_lcd_panel_reset(*panelHandle));
ESP_ERROR_CHECK(esp_lcd_panel_init(*panelHandle));
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(*panelHandle, true));
return true;
}
static bool initLvgl(esp_lcd_panel_handle_t panelHandle) {
ESP_LOGI(TAG, "Initializing LVGL");
lv_init();
// Allocate draw buffers with DMA capability
lv_color_t *buf1 = heap_caps_malloc(
DISPLAY_WIDTH * LVGL_BUFFER_HEIGHT * sizeof(lv_color_t), MALLOC_CAP_DMA);
lv_color_t *buf2 = heap_caps_malloc(
DISPLAY_WIDTH * LVGL_BUFFER_HEIGHT * sizeof(lv_color_t), MALLOC_CAP_DMA);
if (buf1 == NULL || buf2 == NULL) {
ESP_LOGE(TAG, "Failed to allocate LVGL draw buffers");
return false;
}
lv_disp_draw_buf_init(&dispBuffer, buf1, buf2,
DISPLAY_WIDTH * LVGL_BUFFER_HEIGHT);
// Register display driver
lv_disp_drv_init(&dispDriver);
dispDriver.hor_res = DISPLAY_WIDTH;
dispDriver.ver_res = DISPLAY_HEIGHT;
dispDriver.flush_cb = lvglFlushCallback;
dispDriver.rounder_cb = lvglRounderCallback;
dispDriver.drv_update_cb = lvglUpdateCallback;
dispDriver.draw_buf = &dispBuffer;
dispDriver.user_data = panelHandle;
lv_disp_drv_register(&dispDriver);
// Create LVGL tick timer
const esp_timer_create_args_t tickTimerArgs = {.callback = lvglTickCallback,
.name = "lvgl_tick"};
esp_timer_handle_t tickTimer = NULL;
ESP_ERROR_CHECK(esp_timer_create(&tickTimerArgs, &tickTimer));
ESP_ERROR_CHECK(
esp_timer_start_periodic(tickTimer, LVGL_TICK_PERIOD_MS * 1000));
// Create mutex and port task
lvglMutex = xSemaphoreCreateMutex();
if (lvglMutex == NULL) {
ESP_LOGE(TAG, "Failed to create LVGL mutex");
return false;
}
BaseType_t ret = xTaskCreate(lvglPortTask, "lvgl_task", LVGL_TASK_STACK_SIZE,
NULL, LVGL_TASK_PRIORITY, NULL);
if (ret != pdPASS) {
ESP_LOGE(TAG, "Failed to create LVGL task");
return false;
}
return true;
}
// ----------------------------------------------------------------------------
// LVGL Callbacks
// ----------------------------------------------------------------------------
static void lvglPortTask(void *arg) {
ESP_LOGI(TAG, "LVGL task started");
uint32_t delayMs = LVGL_TASK_MAX_DELAY_MS;
while (1) {
if (displayDriverLock(-1)) {
delayMs = lv_timer_handler();
displayDriverUnlock();
}
// Clamp delay to valid range
if (delayMs > LVGL_TASK_MAX_DELAY_MS) {
delayMs = LVGL_TASK_MAX_DELAY_MS;
} else if (delayMs < LVGL_TASK_MIN_DELAY_MS) {
delayMs = LVGL_TASK_MIN_DELAY_MS;
}
vTaskDelay(pdMS_TO_TICKS(delayMs));
}
}
static void lvglTickCallback(void *arg) { lv_tick_inc(LVGL_TICK_PERIOD_MS); }
static bool lvglFlushReady(esp_lcd_panel_io_handle_t panelIo,
esp_lcd_panel_io_event_data_t *eventData,
void *userCtx) {
lv_disp_drv_t *driver = (lv_disp_drv_t *)userCtx;
lv_disp_flush_ready(driver);
return false;
}
static void lvglFlushCallback(lv_disp_drv_t *drv, const lv_area_t *area,
lv_color_t *colorMap) {
esp_lcd_panel_handle_t panelHandle = (esp_lcd_panel_handle_t)drv->user_data;
const int offsetX1 = area->x1;
const int offsetX2 = area->x2;
const int offsetY1 = area->y1;
const int offsetY2 = area->y2;
#if LCD_BIT_PER_PIXEL == 24
// Convert from LVGL color format to RGB888
uint8_t *dest = (uint8_t *)colorMap;
uint16_t pixelCount = (offsetX2 - offsetX1 + 1) * (offsetY2 - offsetY1 + 1);
// Handle first pixel
uint8_t temp = colorMap[0].ch.blue;
*dest++ = colorMap[0].ch.red;
*dest++ = colorMap[0].ch.green;
*dest++ = temp;
// Handle remaining pixels
for (int i = 1; i < pixelCount; i++) {
*dest++ = colorMap[i].ch.red;
*dest++ = colorMap[i].ch.green;
*dest++ = colorMap[i].ch.blue;
}
#endif
esp_lcd_panel_draw_bitmap(panelHandle, offsetX1, offsetY1, offsetX2 + 1,
offsetY2 + 1, colorMap);
}
static void lvglUpdateCallback(lv_disp_drv_t *drv) {
esp_lcd_panel_handle_t panelHandle = (esp_lcd_panel_handle_t)drv->user_data;
switch (drv->rotated) {
case LV_DISP_ROT_NONE:
esp_lcd_panel_swap_xy(panelHandle, false);
esp_lcd_panel_mirror(panelHandle, true, false);
break;
case LV_DISP_ROT_90:
esp_lcd_panel_swap_xy(panelHandle, true);
esp_lcd_panel_mirror(panelHandle, true, true);
break;
case LV_DISP_ROT_180:
esp_lcd_panel_swap_xy(panelHandle, false);
esp_lcd_panel_mirror(panelHandle, false, true);
break;
case LV_DISP_ROT_270:
esp_lcd_panel_swap_xy(panelHandle, true);
esp_lcd_panel_mirror(panelHandle, false, false);
break;
}
}
static void lvglRounderCallback(lv_disp_drv_t *drv, lv_area_t *area) {
// Round coordinates to even boundaries for SH8601 requirements
area->x1 = (area->x1 >> 1) << 1;
area->y1 = (area->y1 >> 1) << 1;
area->x2 = ((area->x2 >> 1) << 1) + 1;
area->y2 = ((area->y2 >> 1) << 1) + 1;
}