使用LVGL库和TFT_eSPI库来在一个TFT屏幕上显示一些文本
#include 指令:这些指令包括了你项目所需的各种库。其中包括LVGL库和FreeRTOS库,以及一些其他的自定义和第三方库。
task1 和 task2 函数定义:这些是FreeRTOS中的任务函数,它们将在一个RTOS环境中并行运行。你创建了一个互斥锁 xMutex,这可以用来同步这些任务,防止它们同时访问某些资源,从而避免数据冲突。
温度显示部分:这部分代码可能是用来从某个传感器读取温度,但是你没有提供这部分的具体实现。
lv_event_cb 函数:这是一个LVGL的事件处理函数,但它目前没有做任何事情。根据你的需求,你可能需要在这个函数中处理各种事件,比如触摸事件、按钮事件等。
lable_display 函数:这个函数创建一个标签对象,设置其文本,并允许重新着色。
my_disp_flush 和 my_touchpad_read 函数:这两个函数分别用于在TFT屏幕上绘制一个区域,以及读取触摸屏的数据。
#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 <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <lv_demo.h>
//创建互斥锁
void task1(void* param);
void task2(void* param);
xSemaphoreHandle xMutex;
//温度显示
/*Change to your screen resolution*/
static const uint16_t screenWidth = 480;
static const uint16_t screenHeight = 320;
static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[ screenWidth * 10 ];
TFT_eSPI tft = TFT_eSPI(screenHeight,screenWidth); /* TFT instance */
#if LV_USE_LOG != 0
/* Serial debugging */
void my_print(const char * buf)
{
Serial.printf(buf);
Serial.flush();
}
#endif
//////////////////////////////////////////////////////////
/////////////////* lvgl 事件处理 *////////////////////////
//////////////////////////////////////////////////////////
static void lv_event_cb(lv_event_t *e){
}
//////////////////////////////////////////////////////////
/////////////////*创建屏幕代码*/////////////////////////////
//////////////////////////////////////////////////////////
void lable_display (void){
//部分字体重新换色
lv_obj_t *lable = lv_label_create(lv_scr_act());
lv_label_set_recolor(lable,true);
lv_label_set_text(lable,"LV#ff0000 G#L LOGIINLOGINL\nOGIONLOGIN\n");
//lv_label_set_text(lable,"LVGL LOGIN 登录\n");
}
/* 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 );
}
}
void setup()
{
Serial.begin( 9600 ); /* prepare for possible serial debug */
String LVGL_Arduino = "Hello Arduino! ";
LVGL_Arduino += String('V') + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch();
Serial.println( LVGL_Arduino );
Serial.println( "I am LVGL_Arduino" );
lv_init();
tft.begin(); /* TFT init */
tft.setRotation( 1 ); /* Landscape orientation, flipped */
//////////////////////////////////////////////////////////////////
/*Set the touchscreen calibration data,
the actual data for your display can be acquired using
the Generic -> Touch_calibrate example from the TFT_eSPI library*/
//////////////////////////////////////////////////////////////////
uint16_t calData[5] = { 322, 3520,352, 3164, 5};
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 );
//disp_drv.sw_rotate = 1;
//disp_drv.rotated = LV_DISP_ROT_180;
Serial.print("tft init done");
//////////////////////////////////////////////////////////
/////////////////*启动用户屏幕内容*/////////////////////////
//////////////////////////////////////////////////////////
lable_display();
//////////////////////////////////////////////////////////
/////////////////*多任务处理*/////////////////////////////
//////////////////////////////////////////////////////////
TaskHandle_t task1_handler;
int param = 30;
xTaskCreatePinnedToCore(task1,"task1",2048,(void*)¶m,15,&task1_handler,0);
xTaskCreatePinnedToCore(task2,"task2",2048,NULL,17,NULL,1);
xMutex = xSemaphoreCreateMutex();
}
void loop()
{
lv_timer_handler(); /* let the GUI do its work */
delay(5);
}
void task2 (void* param){
while (1)
{
/* code */
delay(3000);
if (xSemaphoreTake(xMutex,portMAX_DELAY))//获取锁
{
/* code */
//Serial.print(temp1);
xSemaphoreGive(xMutex);释放锁
}
}
}
void task1(void* param){
while (1)
{
delay(1000);
if (xSemaphoreTake(xMutex,portMAX_DELAY))
{
Serial.print("task1");
xSemaphoreGive(xMutex);
}
}
}