LVGL Minimal Examples
This example shows a basic usage with LVGL.
In this example, we make a lv_helper.cpp
and lv_helper.h
makes it easy to implement LVGL in Arduino.
And it is a minimal example for you to start with LVGL
Checkout the LVGL documentation for more information.
Note
If you haven’t download the code:
Download examples from github termod-s3
Unzip the downloaded termod-s3-main.zip
Or just clone the repository
git clone https://github.com/TAMCTec/termod-s3.git
Note
If you don’t have lvgl installed, check this out: Install LVGL Library (Optional).
Open termod-s3/examples/lv_example/lv_example.ino
with Arduino IDE.
Remember to select ESP32S3 Dev Module
and port, then click upload.
#include "lv_helper.h"
void setup() {
Serial.begin(115200);
lh_init(DISPLAY_LANDSCAPE);
Serial.println("LVGL Example: Ready");
lv_obj_t* slider = lv_slider_create(lv_scr_act());
lv_obj_align(slider, LV_ALIGN_CENTER, 0, 0);
}
void loop() {
lv_timer_handler();
}
#include "lv_helper.h"
TFT_eSPI lh_tft = TFT_eSPI();
TAMC_FT62X6 lh_tp = TAMC_FT62X6();
static lv_disp_draw_buf_t lh_draw_buf;
static lv_color_t lh_buf[ DISPLAY_WIDTH * 10 ];
static lv_disp_drv_t lh_disp_drv;
static lv_indev_drv_t lh_indev_drv;
uint16_t width, height;
void lh_init(int rotation){
Wire.begin();
lh_tp.begin();
lv_init();
lh_tft.begin();
if (rotation == 1 || rotation == 3){
width = DISPLAY_HEIGHT;
height = DISPLAY_WIDTH;
} else {
width = DISPLAY_WIDTH;
height = DISPLAY_HEIGHT;
}
lh_tft.setRotation(rotation);
lh_tp.setRotation(rotation);
lv_disp_draw_buf_init( &lh_draw_buf, lh_buf, NULL, DISPLAY_WIDTH * 10 );
/*Initialize the display*/
lv_disp_drv_init( &lh_disp_drv );
/*Change the following line to your display resolution*/
lh_disp_drv.hor_res = width;
lh_disp_drv.ver_res = height;
lh_disp_drv.flush_cb = lh_disp_flush;
lh_disp_drv.draw_buf = &lh_draw_buf;
lv_disp_drv_register( &lh_disp_drv );
/*Initialize the (dummy) input device driver*/
lv_indev_drv_init( &lh_indev_drv );
lh_indev_drv.type = LV_INDEV_TYPE_POINTER;
lh_indev_drv.read_cb = lh_touchpad_read;
lv_indev_drv_register( &lh_indev_drv );
}
/* Display flushing */
void lh_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);
lh_tft.startWrite();
lh_tft.setAddrWindow( area->x1, area->y1, w, h );
lh_tft.pushColors( ( uint16_t * )&color_p->full, w * h, true );
lh_tft.endWrite();
lv_disp_flush_ready(disp);
}
/*Read the touchpad*/
void lh_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data) {
lh_tp.read();
if (!lh_tp.isTouched) {
data->state = LV_INDEV_STATE_RELEASED;
}
else{
data->state = LV_INDEV_STATE_PRESSED;
/*Set the coordinates*/
data->point.x = lh_tp.points[0].x;
data->point.y = lh_tp.points[0].y;
}
}
#ifndef LV_HELPER_H
#define LV_HELPER_H
#include <lvgl.h>
#include "TAMC_FT62X6.h"
#include "Wire.h"
#include <TFT_eSPI.h>
#define DISPLAY_PORTRAIT 2
#define DISPLAY_LANDSCAPE 3
#define DISPLAY_PORTRAIT_FLIP 0
#define DISPLAY_LANDSCAPE_FLIP 1
#define DISPLAY_WIDTH 240
#define DISPLAY_HEIGHT 320
/* Display flushing */
void lh_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p);
/*Read the touchpad*/
void lh_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data);
void lh_init(int rotation);
#endif // LV_HELPER_H