1. 前提条件
- 使用 arduino IDE开发
- 使用 ESP32 作为主控
- 屏幕使用 ST7789 驱动,240*240像素TFT屏幕
注意:屏幕驱动部分不再这个赘述,以后开贴另行发布
2. 安装 LVGL 库
打开 arduino 菜单栏中 -> 项目 -> 加载库 -> 管理库
搜索 LVGL 本人使用版本是8.0.2, 8.0一下版本貌似不支持GIF
1. 成功安装后,LVGL库就安装到了 首选项中, 项目文件夹位置 一栏中所设置的项目文件夹位置,我这里设置的是把库文件安装在该项目文件下。
2. 编译, 编译的时候会报各种错误,这里主要是 lv_conf.h 找不到一类的,因为库只给了模板,我们把lvgl文件下的 lv_conf_templet.h 改为 lv_conf.h,然后使能 lv_conf.h 文件
3. 搜索 lv_conf_internal.h 文件,修改一下 lv_conf.h 的路径,如下图所示:
3. 工程测试
- 在库文件lvgl中example文件下有arduino的测试demo,我们拷贝到我们的自己工程目录,以下是拷贝完毕的
- 我的工程没有用到 touchpad 触摸屏幕功能,所以我都去掉了
- 我的屏幕背光由ESP32控制,所以加入了背光控制,如果直接点亮可以去掉这个功能
//#include <lv_demo.h>
#include <stdint.h>
#include <lvgl.h>
#include <TFT_eSPI.h>
/*If you want to use the LVGL examples,
make sure to install the lv_examples Arduino library
and uncomment the following line.
#include <lv_examples.h>
*/
#include "lv_gif.h"
#define LCD_BL_PIN 5
#define LCD_BL_PWM_CHANNEL 0
TFT_eSPI tft = TFT_eSPI(); /* TFT instance */
/*Change to your screen resolution*/
static const uint32_t screenWidth = 240;
static const uint32_t screenHeight = 240;
static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[ screenWidth * 10 ];
#if LV_USE_LOG != 0
/* Serial debugging */
void my_print( lv_log_level_t level, const char * file, uint32_t line, const char * fn_name, const char * dsc )
{
Serial.printf( "%s(%s)@%d->%s\r\n", file, fn_name, line, dsc );
Serial.flush();
}
#endif
/* 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 );
tft.startWrite();
tft.setAddrWindow( area->x1, area->y1, w, h );
tft.pushColors( ( uint16_t * )&color_p->full, w * h, true );
tft.endWrite();
lv_disp_flush_ready( disp );
}
void setBackLight(float duty)
{
duty = constrain(duty, 0, 1);
duty = 1 - duty;
ledcWrite(LCD_BL_PWM_CHANNEL, (int)(duty * 255));
}
/**
* Open a GIF image from a file and a variable
*/
void lv_example_gif_1(void)
{
LV_IMG_DECLARE(img_bulb_gif);
lv_obj_t * img;
img = lv_gif_create(lv_scr_act());
lv_gif_set_src(img, &img_bulb_gif);
lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
}
void setup()
{
Serial.begin( 115200 ); /* prepare for possible serial debug */
Serial.println( "Hello Arduino! (V8.0.X)" );
Serial.println( "I am LVGL_Arduino" );
ledcSetup(LCD_BL_PWM_CHANNEL, 5000, 8);
ledcAttachPin(LCD_BL_PIN, LCD_BL_PWM_CHANNEL);
lv_init();
#if LV_USE_LOG != 0
lv_log_register_print_cb( my_print ); /* register print function for debugging */
#endif
tft.begin(); /* TFT init */
tft.setRotation( 3 ); /* Landscape orientation, flipped */
/*Set the touchscreen calibration data,
the actual data for your display can be aquired using
the Generic -> Touch_calibrate example from the TFT_eSPI library*/
// uint16_t calData[5] = { 275, 3620, 264, 3532, 1 };
// tft.setTouch( calData );
lv_disp_draw_buf_init( &draw_buf, buf, NULL, screenWidth * 10 );
/*Initialize the display*/
static lv_disp_drv_t disp_drv;
lv_disp_drv_init( &disp_drv );
/*Change the following line to your display resolution*/
disp_drv.hor_res = screenWidth;
disp_drv.ver_res = screenHeight;
disp_drv.flush_cb = my_disp_flush;
disp_drv.draw_buf = &draw_buf;
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;
// indev_drv.read_cb = my_touchpad_read;
// lv_indev_drv_register( &indev_drv );
#if 1
/* Create simple label */
lv_obj_t *label = lv_label_create( lv_scr_act() );
lv_label_set_recolor(label, true);
lv_label_set_text( label, "#0000ff Hello Arduino! #(V8.0.X)" );
lv_obj_align( label, LV_ALIGN_CENTER, 0, 0 );
lv_obj_t *btn1 = lv_btn_create( lv_scr_act() );
lv_obj_align( btn1, LV_ALIGN_CENTER, 0, 60 );
lv_obj_t *label2 = lv_label_create(btn1);
lv_label_set_text(label2, "Button 1");
#else
/* Try an example from the lv_examples Arduino library
make sure to include it as written above.
lv_example_btn_1();
*/
//lv_demo_widgets();
//
lv_example_gif_1();
// uncomment one of these demos
//lv_demo_widgets(); // OK
// lv_demo_benchmark(); // OK
// lv_demo_keypad_encoder(); // works, but I haven't an encoder
// lv_demo_music(); // NOK
// lv_demo_printer();
#endif
Serial.println( "Setup done" );
setBackLight(0.9);
}
void loop()
{
lv_timer_handler(); /* let the GUI do its work */
delay( 10 );
}