#include "gpio.h"
#include "driver/gpio.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_lcd_st77916.h"
#include "esp_lvgl_port.h"

void test_display(esp_lcd_panel_handle_t panel_handle){
    uint16_t *color_buf = malloc(LCD_WID * 100 * sizeof(uint16_t));  // 10行，每行360像素（RGB565）
    if (color_buf == NULL) {
        ESP_LOGE(LCD_TAG, "红色缓冲区分配失败");
        return;
    }
    for (int i = 0; i < LCD_WID * 100; i++) {
        color_buf[i] = 0xF800;
    }
    esp_err_t err = esp_lcd_panel_draw_bitmap(
        panel_handle,
        0, 0,
        LCD_WID - 1, 100,
        color_buf
    );
    if (err != ESP_OK) {
        ESP_LOGE(LCD_TAG, "绘制红色条失败: %s", esp_err_to_name(err));
    } else {
        ESP_LOGI(LCD_TAG, "红色条绘制命令已发送");
    }
    free(color_buf);
}

void test_gpio(){
    gpio_config_t led_conf = {};
    led_conf.pin_bit_mask = (1ULL << PIN_LCD_CLK);
    led_conf.mode = GPIO_MODE_OUTPUT;
    led_conf.pull_up_en = GPIO_PULLUP_DISABLE;
    led_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
    led_conf.intr_type = GPIO_INTR_DISABLE;
    gpio_config(&led_conf);

    led_conf.pin_bit_mask = (1ULL << PIN_LCD_D0);
    led_conf.mode = GPIO_MODE_OUTPUT;
    led_conf.pull_up_en = GPIO_PULLUP_DISABLE;
    led_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
    led_conf.intr_type = GPIO_INTR_DISABLE;
    gpio_config(&led_conf);

    led_conf.pin_bit_mask = (1ULL << PIN_LCD_D1);
    led_conf.mode = GPIO_MODE_OUTPUT;
    led_conf.pull_up_en = GPIO_PULLUP_DISABLE;
    led_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
    led_conf.intr_type = GPIO_INTR_DISABLE;
    gpio_config(&led_conf);

    led_conf.pin_bit_mask = (1ULL << PIN_LCD_D2);
    led_conf.mode = GPIO_MODE_OUTPUT;
    led_conf.pull_up_en = GPIO_PULLUP_DISABLE;
    led_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
    led_conf.intr_type = GPIO_INTR_DISABLE;
    gpio_config(&led_conf);

    led_conf.pin_bit_mask = (1ULL << PIN_LCD_D3);
    led_conf.mode = GPIO_MODE_OUTPUT;
    led_conf.pull_up_en = GPIO_PULLUP_DISABLE;
    led_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
    led_conf.intr_type = GPIO_INTR_DISABLE;
    gpio_config(&led_conf);

    led_conf.pin_bit_mask = (1ULL << PIN_LCD_RST);
    led_conf.mode = GPIO_MODE_OUTPUT;
    led_conf.pull_up_en = GPIO_PULLUP_DISABLE;
    led_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
    led_conf.intr_type = GPIO_INTR_DISABLE;
    gpio_config(&led_conf);

    led_conf.pin_bit_mask = (1ULL << PIN_LCD_CS);
    led_conf.mode = GPIO_MODE_OUTPUT;
    led_conf.pull_up_en = GPIO_PULLUP_DISABLE;
    led_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
    led_conf.intr_type = GPIO_INTR_DISABLE;
    
    gpio_config(&led_conf);

    led_conf.pin_bit_mask = (1ULL << 36);
    led_conf.mode = GPIO_MODE_OUTPUT;
    led_conf.pull_up_en = GPIO_PULLUP_DISABLE;
    led_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
    led_conf.intr_type = GPIO_INTR_DISABLE;
    
    gpio_config(&led_conf);
    gpio_set_level(36,1);

    // gpio_set_level(PIN_LCD_CS,1);
    // gpio_set_level(PIN_LCD_RST,0);
    // gpio_set_level(PIN_LCD_CLK,1);
    // gpio_set_level(PIN_LCD_D0,0);
    // gpio_set_level(PIN_LCD_D1,1);
    // gpio_set_level(PIN_LCD_D2,0);
    // gpio_set_level(PIN_LCD_D3,1);
}