代码
#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_demo.h>
#define LV_USE_LOG 1
TFT_eSPI tft = TFT_eSPI(); /* TFT instance */
/*Change to your screen resolution*/
static const uint32_t screenWidth = 320;
static const uint32_t screenHeight = 240;
static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[ screenWidth * 10 ];
static lv_style_t style_btn;
static lv_style_t style_slider;
#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 );
}
/*Read the touchpad*/
void my_touchpad_read( lv_indev_drv_t * indev_driver, lv_indev_data_t * data )
{
uint16_t touchX, touchY;
bool touched = tft.getTouch( &touchX, &touchY, 600 );
if( !touched )
{
data->state = LV_INDEV_STATE_REL;
}
else
{
data->state = LV_INDEV_STATE_PR;
/*Set the coordinates*/
data->point.x = touchX;
data->point.y = touchY;
Serial.print( "Data x " );
Serial.println( touchX );
Serial.print( "Data y " );
Serial.println( touchY );
}
}
/*
static void btn_event_callback(lv_event_t* event)
{
static uint32_t counter = 1;
lv_obj_t* btn = lv_event_get_target(event); //获取事件对象
if (btn != NULL)
{
lv_obj_t* label = lv_obj_get_child(btn, 0); // 获取第一个子对象
if (label != NULL)
{
lv_label_set_text_fmt(label, "%d", counter); //设置显示内容
counter++;
}
}
}
void lv_button_click_event_test()
{
lv_obj_t* btn = lv_btn_create(lv_scr_act()); // 创建Button对象
if (btn != NULL)
{
lv_obj_add_style(btn, &style_btn, LV_STATE_DEFAULT); // 给按钮对象添加默认样式
lv_style_set_bg_color(&style_btn, lv_palette_main(LV_PALETTE_YELLOW)); // 样式背景色为黄色
lv_obj_set_style_radius(btn, LV_RADIUS_CIRCLE, 0); // 添加按钮对象的圆角弧度样
lv_obj_set_size(btn, 100, 50); // 设置对象大小,宽度和高度
lv_obj_set_pos(btn, 110, 100);; // 设置按钮位置,即X和Y坐标
lv_obj_add_event_cb(btn, btn_event_callback, LV_EVENT_CLICKED, NULL); // 给对象添加CLICK事件和事件处理回调函数
lv_obj_t* label = lv_label_create(btn); // 基于Button对象创建Label对象
if (label != NULL)
{
lv_label_set_text(label, "Click me!"); // 设置显示内容
lv_obj_center(label); // 对象居中显示
}
}
}*/
/*
static void slider_event_callback(lv_event_t* event)
{
lv_obj_t * slider = lv_event_get_target(event);
if (slider != NULL)
{
lv_obj_add_style(slider, &style_slider, LV_STATE_DEFAULT); // 给按钮对象添加默认样式
lv_style_set_bg_color(&style_slider, lv_palette_main(LV_PALETTE_YELLOW)); // 样式背景色为黄色
lv_obj_t* label = (lv_obj_t* )lv_event_get_user_data(event);
if (label != NULL)
{
// 刷新label对象的文本
lv_label_set_text_fmt(label, "%d", lv_slider_get_value(slider));
lv_obj_align_to(label, slider, LV_ALIGN_OUT_TOP_MID, 0, -15); // 设置对齐,label对象在slider对象上方中间位置
}
}
}
void lv_slider_test()
{
lv_obj_t * slider = lv_slider_create(lv_scr_act()); // 创建滑块对象
if (slider != NULL)
{
lv_obj_set_width(slider, 200); // 设置slider的宽度
lv_obj_center(slider); // 对样显示在屏幕中央
lv_slider_set_range(slider, 0, 1024); // 设置滑块值的变化范围0-1024
lv_obj_t* label = lv_label_create(lv_scr_act()); // 创建一个标签,用于显示滑块的滑动值
if (label != NULL)
{
lv_label_set_text_fmt(label, "%d", lv_slider_get_min_value(slider)); // 标签默认显示滑块的最小值
lv_obj_align_to(label, slider, LV_ALIGN_OUT_TOP_MID, 0, -15); // 标签对象放在slider对象的上方中间位置
// 添加滑块值变化事件和事件回调函数,并将label对象最为事件的user_data
lv_obj_add_event_cb(slider, slider_event_callback, LV_EVENT_VALUE_CHANGED, (void *)label);
}
}
}
*/
/*
void lv_size_style_test()
{
static lv_style_t style; // 注意:这个变量不能为局部变量,必须是static/全局/动态申请的变量
lv_style_init(&style);
lv_style_reset(&style); // 重置样式
lv_style_set_radius(&style, 5); // 设置样式圆角弧度
lv_style_set_width(&style, 150); // 设置样式宽度
lv_style_set_height(&style, LV_SIZE_CONTENT); // 设置样式高度
lv_style_set_pad_ver(&style, 20); // 设置垂直方向的填充,即padding top和padding bottom分别填充20
lv_style_set_pad_left(&style, 5); // 设置左边填充
lv_style_set_x(&style, lv_pct(30)); // 设置水平坐标,lv_pct转换百分比到坐标值,即X坐标为屏幕宽度的20%
lv_style_set_y(&style, 100); // 设置垂直坐标
// 基于屏幕创建一个对象并添加样式
lv_obj_t * obj = lv_obj_create(lv_scr_act());
if (obj != NULL)
{
lv_obj_add_style(obj, &style, 0);
lv_obj_t * label = lv_label_create(obj); // 基于obj创建一个label对象
if (label != NULL)
{
lv_label_set_text(label, "Hello World!");
}
}
}*/
/*
void lv_example_bar_1(void)
{
lv_obj_t * bar1 = lv_bar_create(lv_scr_act());//构建进度条对象
lv_obj_set_size(bar1, 200, 20);//设置宽度和高度
lv_obj_center(bar1);//居中
lv_bar_set_value(bar1, 20, LV_ANIM_OFF);//设置加载进度
}
*/
/*
static void event_handler(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * obj = lv_event_get_target(e);
if(code == LV_EVENT_VALUE_CHANGED) {//如果开关的状态变化了
LV_LOG_USER("State: %s\n", lv_obj_has_state(obj, LV_STATE_CHECKED) ? "On" : "Off");
Serial.println(lv_obj_has_state(obj, LV_STATE_CHECKED));//打印出开关的值
if(lv_obj_has_state(obj, LV_STATE_CHECKED)== 1)
{
Serial.println("on");
}
else
{
Serial.println("off");
}
}
}
void lv_example_switch_1(void)
{
lv_obj_set_flex_flow(lv_scr_act(), LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(lv_scr_act(), LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
lv_obj_t * sw;
sw = lv_switch_create(lv_scr_act());
lv_obj_add_event_cb(sw, event_handler, LV_EVENT_ALL, NULL);
sw = lv_switch_create(lv_scr_act());
lv_obj_add_state(sw, LV_STATE_CHECKED);
lv_obj_add_event_cb(sw, event_handler, LV_EVENT_ALL, NULL);
}*/
/*
void lv_example_arc_1(void)
{
lv_obj_t * arc = lv_arc_create(lv_scr_act());//创建一个对象
lv_obj_set_size(arc, 150, 150);//设置大小
lv_arc_set_rotation(arc, 135);//设置值的范围
lv_arc_set_bg_angles(arc, 0, 270);//设置弧的大小,360为一个圆
lv_arc_set_value(arc, 40);//设置现在的值
lv_obj_center(arc);
}*/
/*
static void event_handler(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * obj = lv_event_get_target(e);
if(code == LV_EVENT_VALUE_CHANGED) {
uint32_t id = lv_btnmatrix_get_selected_btn(obj);
const char * txt = lv_btnmatrix_get_btn_text(obj, id);
LV_LOG_USER("%s was pressed\n", txt);
Serial.println(txt);
}
}
static const char * btnm_map[] = {"1", "2", "3", "4", "5", "\n",
"6", "7", "8", "9", "0", "\n",
"Action1", "Action2", ""};
void lv_example_btnmatrix_1(void)
{
lv_obj_t * btnm1 = lv_btnmatrix_create(lv_scr_act());
lv_btnmatrix_set_map(btnm1, btnm_map);
lv_btnmatrix_set_btn_width(btnm1, 10, 2); //Make "Action1" twice as wide as "Action2"
lv_btnmatrix_set_btn_ctrl(btnm1, 10, LV_BTNMATRIX_CTRL_CHECKABLE);
lv_btnmatrix_set_btn_ctrl(btnm1, 11, LV_BTNMATRIX_CTRL_CHECKED);
lv_obj_align(btnm1, LV_ALIGN_CENTER, 0, 0);
lv_obj_add_event_cb(btnm1, event_handler, LV_EVENT_ALL, NULL);
}*/
/*
static void event_handler(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * obj = lv_event_get_target(e);
if(code == LV_EVENT_VALUE_CHANGED) {
const char * txt = lv_checkbox_get_text(obj);
const char * state = lv_obj_get_state(obj) & LV_STATE_CHECKED ? "Checked" : "Unchecked";
LV_LOG_USER("%s: %s", txt, state);
}
}
void lv_example_checkbox_1(void)
{
lv_obj_set_flex_flow(lv_scr_act(), LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(lv_scr_act(), LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER);
lv_obj_t * cb;
cb = lv_checkbox_create(lv_scr_act());
lv_checkbox_set_text(cb, "Apple");
lv_obj_add_event_cb(cb, event_handler, LV_EVENT_ALL, NULL);
cb = lv_checkbox_create(lv_scr_act());
lv_checkbox_set_text(cb, "Banana");
lv_obj_add_state(cb, LV_STATE_CHECKED);
lv_obj_add_event_cb(cb, event_handler, LV_EVENT_ALL, NULL);
cb = lv_checkbox_create(lv_scr_act());
lv_checkbox_set_text(cb, "Lemon");
lv_obj_add_state(cb, LV_STATE_DISABLED);
lv_obj_add_event_cb(cb, event_handler, LV_EVENT_ALL, NULL);
cb = lv_checkbox_create(lv_scr_act());
lv_obj_add_state(cb, LV_STATE_CHECKED | LV_STATE_DISABLED);
lv_checkbox_set_text(cb, "Melon\nand a new line");
lv_obj_add_event_cb(cb, event_handler, LV_EVENT_ALL, NULL);
lv_obj_update_layout(cb);
}
*/
/*
static void event_handler(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * obj = lv_event_get_target(e);
if(code == LV_EVENT_VALUE_CHANGED) {
char buf[32];
lv_dropdown_get_selected_str(obj, buf, sizeof(buf));
LV_LOG_USER("Option: %s", buf);
Serial.println(buf);
}
}
void lv_example_dropdown_1(void)
{
//Create a normal drop down list
lv_obj_t * dd = lv_dropdown_create(lv_scr_act());
lv_dropdown_set_options(dd, "Apple\n"
"Banana\n"
"Orange\n"
"Cherry\n"
"Grape\n"
"Raspberry\n"
"Melon\n"
"Orange\n"
"Lemon\n"
"Nuts");
lv_obj_align(dd, LV_ALIGN_TOP_MID, 0, 20);
lv_obj_add_event_cb(dd, event_handler, LV_EVENT_ALL, NULL);
}
*/
void lv_example_label_1(void)
{
lv_obj_t * label1 = lv_label_create(lv_scr_act());
lv_label_set_long_mode(label1, LV_LABEL_LONG_WRAP); /*Break the long lines*/
lv_label_set_recolor(label1, true); /*Enable re-coloring by commands in the text*/
lv_label_set_text(label1, "#0000ff Re-color# #ff00ff words# #ff0000 of a# label, align the lines to the center"
"and wrap long text automatically.");
lv_obj_set_width(label1, 150); /*Set smaller width to make the lines wrap*/
lv_obj_set_style_text_align(label1, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_align(label1, LV_ALIGN_CENTER, 0, -40);
lv_obj_t * label2 = lv_label_create(lv_scr_act());
lv_label_set_long_mode(label2, LV_LABEL_LONG_SCROLL_CIRCULAR); /*Circular scroll*/
lv_obj_set_width(label2, 150);
lv_label_set_text(label2, "It is a circularly scrolling text. ");
lv_obj_align(label2, LV_ALIGN_CENTER, 0, 40);
}
void setup()
{
Serial.begin( 115200 ); /* prepare for possible serial debug */
Serial.println( "Hello Arduino! (V8.0.X)" );
Serial.println( "I am LVGL_Arduino" );
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( 1 ); /* 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] = { 442, 3414, 336, 3425, 3 };
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 );
// lv_button_click_event_test();
// lv_slider_test();
// lv_size_style_test();
// lv_example_bar_1();
// lv_example_switch_1();
// lv_example_arc_1();
// lv_example_btnmatrix_1();
// lv_example_checkbox_1();
// lv_example_dropdown_1();
lv_example_label_1();
Serial.println( "Setup done" );
}
void loop()
{
lv_timer_handler(); /* let the GUI do its work */
lv_tick_inc(5);
delay(5);
}
效果