/*
 * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
 *
 * SPDX-License-Identifier: Apache-2.0
 */
#pragma once

#include <stddef.h>
#include <stdint.h>

#include "esp_err.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_ops.h"
#include "esp_lcd_panel_vendor.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief ST77916 初始化命令
 */
typedef struct {
    int cmd;                /*!< LCD 命令 */
    const void *data;       /*!< 命令参数 */
    size_t data_bytes;      /*!< 参数字节数 */
    unsigned int delay_ms;  /*!< 命令后延时，单位 ms */
} st77916_lcd_init_cmd_t;

/**
 * @brief ST77916 QSPI 屏厂商初始化配置
 */
typedef struct {
    const st77916_lcd_init_cmd_t *init_cmds; /*!< 自定义初始化命令，为 NULL 时使用驱动默认表 */
    uint16_t init_cmds_size;                 /*!< 初始化命令数量 */
} st77916_vendor_config_t;

/**
 * @brief 创建 ST77916 QSPI LCD 面板
 *
 * @param[in]  io LCD panel IO 句柄
 * @param[in]  panel_dev_config LCD panel 设备配置
 * @param[out] ret_panel 返回 LCD panel 句柄
 * @return ESP_OK 成功，其它值失败
 */
esp_err_t esp_lcd_new_panel_st77916(const esp_lcd_panel_io_handle_t io,
                                    const esp_lcd_panel_dev_config_t *panel_dev_config,
                                    esp_lcd_panel_handle_t *ret_panel);

/**
 * @brief ST77916 QSPI 总线配置
 */
#define ST77916_PANEL_BUS_QSPI_CONFIG(sclk, d0, d1, d2, d3, max_trans_sz) \
    {                                                                     \
        .sclk_io_num = sclk,                                              \
        .data0_io_num = d0,                                               \
        .data1_io_num = d1,                                               \
        .data2_io_num = d2,                                               \
        .data3_io_num = d3,                                               \
        .max_transfer_sz = max_trans_sz,                                  \
    }

/**
 * @brief ST77916 QSPI IO 配置
 */
#define ST77916_PANEL_IO_QSPI_CONFIG(cs, cb, cb_ctx) \
    {                                                \
        .cs_gpio_num = cs,                           \
        .dc_gpio_num = -1,                           \
        .spi_mode = 0,                               \
        .pclk_hz = 40 * 1000 * 1000,                 \
        .trans_queue_depth = 10,                     \
        .on_color_trans_done = cb,                   \
        .user_ctx = cb_ctx,                          \
        .lcd_cmd_bits = 32,                          \
        .lcd_param_bits = 8,                         \
        .flags = {                                   \
            .quad_mode = true,                       \
        },                                           \
    }

#ifdef __cplusplus
}
#endif
