ESP32 DEVKI TV1连GC9A01(1)能run就行

329 阅读3分钟

“我正在参加「掘金·启航计划」”

ESP32 DEVKI TV1和GC9A01

准备硬件

  1. 杜邦线连接ESP32 DEVKI TV1及GC9A01

1.1. ESP32 DEVKI TV1引脚图

引脚图.png

根据表格连接(引脚与软件相配

esp32
GNDGND
VCC3V3
SCLGPIO22
SDAGPIO21
RESGPIO2
DCGPIO27
CSGPIO5
BLKGPIO16

1.2. 代码修改引脚 以下为主要代码

Arduino_DataBus *bus = new Arduino_ESP32SPI(27 /* DC */,5 /* CS */, 22 /* SCK */, 21 /* MOSI */, -1 /* MISO */, HSPI /* spi_num */);

Arduino_GFX *gfx = new Arduino_GC9A01(bus, 2 /* RST */, 0 /* rotation */, true /* IPS */);

准备软件

  1. 准备代码
#include <lvgl.h>

/*******************************************************************************
 * LVGL Hello World
 * This is a simple examplle for LVGL - Light and Versatile Graphics Library
 *
 * Dependent libraries:
 * LVGL: https://github.com/lvgl/lvgl.git
 ******************************************************************************/


/*******************************************************************************
 * Start of Arduino_GFX setting
 * 
 * Arduino_GFX try to find the settings depends on selected board in Arduino IDE
 * Or you can define the display dev kit not in the board list
 * Defalult pin list for non display dev kit:
 * Arduino Nano, Micro and more: CS:  9, DC:  8, RST:  7, BL:  6
 * ESP32 various dev board     : CS:  5, DC: 27, RST: 33, BL: 22
 * ESP32-C3 various dev board  : CS:  7, DC:  2, RST:  1, BL:  3
 * ESP32-S2 various dev board  : CS: 34, DC: 26, RST: 33, BL: 21
 * ESP8266 various dev board   : CS: 15, DC:  4, RST:  2, BL:  5
 * Raspberry Pi Pico dev board : CS: 17, DC: 27, RST: 26, BL: 28
 * RTL8720 BW16 old patch core : CS: 18, DC: 17, RST:  2, BL: 23
 * RTL8720_BW16 Official core  : CS:  9, DC:  8, RST:  6, BL:  3
 * RTL8722 dev board           : CS: 18, DC: 17, RST: 22, BL: 23
 * RTL8722_mini dev board      : CS: 12, DC: 14, RST: 15, BL: 13
 * Seeeduino XIAO dev board    : CS:  3, DC:  2, RST:  1, BL:  0
 * Teensy 4.1 dev board        : CS: 39, DC: 41, RST: 40, BL: 22
 ******************************************************************************/
#include <Arduino_GFX_Library.h>

/* More dev device declaration: https://github.com/moononournation/Arduino_GFX/wiki/Dev-Device-Declaration */
#if defined(DISPLAY_DEV_KIT)
Arduino_GFX *gfx = create_default_Arduino_GFX();
#else /* !defined(DISPLAY_DEV_KIT) */

/* More data bus class: https://github.com/moononournation/Arduino_GFX/wiki/Data-Bus-Class */
//Arduino_DataBus *bus = create_default_Arduino_DataBus();
//Arduino_DataBus *bus = new Arduino_ESP32SPI(4 /* DC */, -1 /* CS */, 22 /* SCK */, 21 /* MOSI */, -1 /* MISO */, HSPI /* spi_num */);
Arduino_DataBus *bus = new Arduino_ESP32SPI(27 /* DC */,5 /* CS */, 22 /* SCK */, 21 /* MOSI */, -1 /* MISO */, HSPI /* spi_num */);

/* More display class: https://github.com/moononournation/Arduino_GFX/wiki/Display-Class */
//Arduino_GFX *gfx = new Arduino_ILI9341(bus, DF_GFX_RST, 0 /* rotation */, false /* IPS */);
Arduino_GFX *gfx = new Arduino_GC9A01(bus, 2 /* RST */, 0 /* rotation */, true /* IPS */);

#endif /* !defined(DISPLAY_DEV_KIT) */
/*******************************************************************************
 * End of Arduino_GFX setting
 ******************************************************************************/


#define DF_GFX_BL  22

/* Change to your screen resolution */
static uint32_t screenWidth;
static uint32_t screenHeight;
static lv_disp_draw_buf_t draw_buf;
static lv_color_t *disp_draw_buf;
static lv_disp_drv_t disp_drv;
static lv_obj_t* meter;

/* Display flushing  实现并注册一个函数,该函数可以将渲染图像复制到显示区域:*/
void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
{
   uint32_t w = (area->x2 - area->x1 + 1);
   uint32_t h = (area->y2 - area->y1 + 1);

   gfx->draw16bitBeRGBBitmap(area->x1, area->y1, (uint16_t *)&color_p->full, w, h);

   lv_disp_flush_ready(disp);
}

void setup()
{
   Serial.begin(115200);
   // while (!Serial);
   Serial.println("LVGL Hello World");

   // Init Display
   gfx->begin();
   gfx->fillScreen(BLACK);

#ifdef DF_GFX_BL
   pinMode(DF_GFX_BL, OUTPUT);
   digitalWrite(DF_GFX_BL, HIGH);

   delay(100);
#endif
   //初始化lvgl库
   lv_init();

   screenWidth = gfx->width();
   screenHeight = gfx->height();
   disp_draw_buf = (lv_color_t *)malloc(sizeof(lv_color_t) * screenWidth * 10);
   if (!disp_draw_buf)
   {
      Serial.println("LVGL disp_draw_buf allocate failed!");
   }
   else
   {
     // 创建一个绘制缓冲区
     /**
     *LVGL将首先在此渲染图形,并将渲染的图像发送到显示器。
     *缓冲区大小可以自由设置,但1/10屏幕大小是一个很好的起点
     */
      lv_disp_draw_buf_init(&draw_buf, disp_draw_buf, NULL, screenWidth * 10);

      /* Initialize the display */
      lv_disp_drv_init(&disp_drv);
      /* Change the following line to your display resolution */
      disp_drv.hor_res = screenWidth;
      disp_drv.ver_res = screenHeight;
      //set your driver function
      disp_drv.flush_cb = my_disp_flush;
      // assign the buffer to the display
      disp_drv.draw_buf = &draw_buf;
      // register the driver
      lv_disp_drv_register(&disp_drv);

      /* Initialize the (dummy) input device driver */
      static lv_indev_drv_t indev_drv;
      lv_indev_drv_init(&indev_drv);
      indev_drv.type = LV_INDEV_TYPE_POINTER;
      lv_indev_drv_register(&indev_drv);

      ///* Create simple label */
      lv_obj_t *label = lv_label_create(lv_scr_act());
      lv_label_set_text(label, "Hello World");
      lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);


   }
}



void loop()
{
   lv_timer_handler(); /* let the GUI do its work */
   delay(5);
}

欣赏

lADPJxRxTTw6mLfNBQDNA8A_960_1280.jpg_720x720q90g.jpg

解决问题:

  1. 前一天mac插上还好好的但是win 10识别不到串口,因为缺USB TO UART驱动,安装即可解决
安装USB TO UART驱动
https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers?tab=downloads
  1. 注意下载lvgl后会报lvgl.conf找不到,这个时候需要将template文件重命名放到指定目录下

下载后,找到lv_conf_template.h文件 image.png

将其命名为lv_conf.h并移动到Libraries下

image.png

参考

百问网