1/*Using LVGL with Arduino requires some extra steps: 2 *Be sure to read the docs here: https://docs.lvgl.io/master/integration/framework/arduino.html */ 3 4#include <lvgl.h> 5 6#if LV_USE_TFT_ESPI 7#include <TFT_eSPI.h> 8#endif 9 10/*To use the built-in examples and demos of LVGL uncomment the includes below respectively. 11 *You also need to copy `lvgl/examples` to `lvgl/src/examples`. Similarly for the demos `lvgl/demos` to `lvgl/src/demos`. 12 *Note that the `lv_examples` library is for LVGL v7 and you shouldn't install it for this version (since LVGL v8) 13 *as the examples and demos are now part of the main LVGL library. */ 14 15//#include <examples/lv_examples.h> 16//#include <demos/lv_demos.h> 17 18/*Set to your screen resolution and rotation*/ 19#define TFT_HOR_RES 320 20#define TFT_VER_RES 240 21#define TFT_ROTATION LV_DISPLAY_ROTATION_0 22 23/*LVGL draw into this buffer, 1/10 screen size usually works well. The size is in bytes*/ 24#define DRAW_BUF_SIZE (TFT_HOR_RES * TFT_VER_RES / 10 * (LV_COLOR_DEPTH / 8)) 25uint32_t draw_buf[DRAW_BUF_SIZE / 4]; 26 27#if LV_USE_LOG != 0 28void my_print( lv_log_level_t level, const char * buf ) 29{ 30 LV_UNUSED(level); 31 Serial.println(buf); 32 Serial.flush(); 33} 34#endif 35 36/* LVGL calls it when a rendered image needs to copied to the display*/ 37void my_disp_flush( lv_display_t *disp, const lv_area_t *area, uint8_t * px_map) 38{ 39 /*Copy `px map` to the `area`*/ 40 41 /*For example ("my_..." functions needs to be implemented by you) 42 uint32_t w = lv_area_get_width(area); 43 uint32_t h = lv_area_get_height(area); 44 45 my_set_window(area->x1, area->y1, w, h); 46 my_draw_bitmaps(px_map, w * h); 47 */ 48 49 /*Call it to tell LVGL you are ready*/ 50 lv_display_flush_ready(disp); 51} 52 53/*Read the touchpad*/ 54void my_touchpad_read( lv_indev_t * indev, lv_indev_data_t * data ) 55{ 56 /*For example ("my_..." functions needs to be implemented by you) 57 int32_t x, y; 58 bool touched = my_get_touch( &x, &y ); 59 60 if(!touched) { 61 data->state = LV_INDEV_STATE_RELEASED; 62 } else { 63 data->state = LV_INDEV_STATE_PRESSED; 64 65 data->point.x = x; 66 data->point.y = y; 67 } 68 */ 69} 70 71/*use Arduinos millis() as tick source*/ 72static uint32_t my_tick(void) 73{ 74 return millis(); 75} 76 77void setup() 78{ 79 String LVGL_Arduino = "Hello Arduino! "; 80 LVGL_Arduino += String('V') + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch(); 81 82 Serial.begin( 115200 ); 83 Serial.println( LVGL_Arduino ); 84 85 lv_init(); 86 87 /*Set a tick source so that LVGL will know how much time elapsed. */ 88 lv_tick_set_cb(my_tick); 89 90 /* register print function for debugging */ 91#if LV_USE_LOG != 0 92 lv_log_register_print_cb( my_print ); 93#endif 94 95 lv_display_t * disp; 96#if LV_USE_TFT_ESPI 97 /*TFT_eSPI can be enabled lv_conf.h to initialize the display in a simple way*/ 98 disp = lv_tft_espi_create(TFT_HOR_RES, TFT_VER_RES, draw_buf, sizeof(draw_buf)); 99 lv_display_set_rotation(disp, TFT_ROTATION); 100 101#else 102 /*Else create a display yourself*/ 103 disp = lv_display_create(TFT_HOR_RES, TFT_VER_RES); 104 lv_display_set_flush_cb(disp, my_disp_flush); 105 lv_display_set_buffers(disp, draw_buf, NULL, sizeof(draw_buf), LV_DISPLAY_RENDER_MODE_PARTIAL); 106#endif 107 108 /*Initialize the (dummy) input device driver*/ 109 lv_indev_t * indev = lv_indev_create(); 110 lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER); /*Touchpad should have POINTER type*/ 111 lv_indev_set_read_cb(indev, my_touchpad_read); 112 113 /* Create a simple label 114 * --------------------- 115 lv_obj_t *label = lv_label_create( lv_screen_active() ); 116 lv_label_set_text( label, "Hello Arduino, I'm LVGL!" ); 117 lv_obj_align( label, LV_ALIGN_CENTER, 0, 0 ); 118 119 * Try an example. See all the examples 120 * - Online: https://docs.lvgl.io/master/examples.html 121 * - Source codes: https://github.com/lvgl/lvgl/tree/master/examples 122 * ---------------------------------------------------------------- 123 124 lv_example_btn_1(); 125 126 * Or try out a demo. Don't forget to enable the demos in lv_conf.h. E.g. LV_USE_DEMO_WIDGETS 127 * ------------------------------------------------------------------------------------------- 128 129 lv_demo_widgets(); 130 */ 131 132 lv_obj_t *label = lv_label_create( lv_screen_active() ); 133 lv_label_set_text( label, "Hello Arduino, I'm LVGL!" ); 134 lv_obj_align( label, LV_ALIGN_CENTER, 0, 0 ); 135 136 Serial.println( "Setup done" ); 137} 138 139void loop() 140{ 141 lv_timer_handler(); /* let the GUI do its work */ 142 delay(5); /* let this time pass */ 143} 144