软件基本环境:

  • win10
  • vscode
  • platformIO
  • arduino框架

软件框架:

  • arduino框架
  • platformIO:LVGL(可以选最新版当前是8.2)
  • platformIO:TFT_eSPI(有触摸,与LCD公用SPI引脚,不同CS)
  • platformIO:XPT2046_Touchscreen(如果触摸和LCD不是同一个SPI,则加这个库,否则TFT_eSPI自带触摸)

硬件:

  • stm32或者esp32开发板
  • LCD:ili9486(SPI引脚)
  • 触摸:xpt2046(SPI引脚)

步骤

1,使用vscode的platformio新建工程

2,添加库LVGL,TFT_eSPI,XPT2046_Touchscreen(可选)

3,把LVGL的lv_conf_template.h文件,复制到LVGL目录旁边,改名为lv_conf.h,跟LVGL目录同级

修改如下

#define LV_COLOR_16_SWAP 1//0
#define LV_MEM_CUSTOM 0
#define LV_MEM_SIZE (32U * 1024U)          /*[bytes]*///默认是48k,我stm32f103rc不够,所以改成32了
#define LV_TICK_CUSTOM 1  //开这个是用arduino框架

4,修改tft库的User_Setup.h文件

stm32的配置

#define STM32
#define ILI9486_DRIVER

// The TFT can be connected to SPI port 1 or 2
#define TFT_SPI_PORT 1 // SPI port 1 maximum clock rate is 55MHz
#define TFT_MOSI PA7
#define TFT_MISO PA6
#define TFT_SCLK PA5

#define TFT_CS   PA2 // Nucleo-F767ZI equivalent of D5
#define TFT_DC   PA3  // Nucleo-F767ZI equivalent of D6
#define TFT_RST  PA4 // Nucleo-F767ZI equivalent of D7

#define SPI_FREQUENCY  55000000 // STM32 SPI1 only (SPI2 maximum is 27MHz)

5,如果使用额外的spi做xpt2046

那么XPT2046_Touchscreen.cpp

//压力大于400有效
#define Z_THRESHOLD     400

SPIClass mySPI2(PB15,PB14,PB13);

把下面  SPI    全部替换成mySPI2

则main.c内如下调用示例

#include <XPT2046_Touchscreen.h>
#include <SPI.h>
HardwareSerial mySerial(PA10, PA9);//uart1-rx,tx
#define CS_PIN  PB11 
// The TIRQ interrupt signal must be used for this example.
#define TIRQ_PIN  PB12
XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);  // Param 2 - Touch IRQ Pin - interrupt enabled polling

void setup() {
  mySerial.begin(9600);
  ts.begin();
  mySerial.print("Pressure = ");
  ts.setRotation(1);
  while (!mySerial && (millis() <= 1000));
}

void loop() {
  // tirqTouched() is much faster than touched().  For projects where other SPI chips
  // or other time sensitive tasks are added to loop(), using tirqTouched() can greatly
  // reduce the delay added to loop() when the screen has not been touched.
  if (ts.tirqTouched()) {
    if (ts.touched()) {
      TS_Point p = ts.getPoint();
      mySerial.print("Pressure = ");
      mySerial.print(p.z);
      mySerial.print(", x = ");
      mySerial.print(p.x);
      mySerial.print(", y = ");
      mySerial.print(p.y);
      delay(30);
      mySerial.println();
    }
  }
}

6,全部main.c(可以先把lvgl下的demo复制到lib下,注意include)

#include <Arduino.h>

#include <lvgl.h>
#include <TFT_eSPI.h>
/*******************************************lb */
#include "lv_demo_widgets.h"
#include <XPT2046_Touchscreen.h>
#define CS_PIN  PB11 
#define TIRQ_PIN  PB12
XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);
HardwareSerial mySerial(PD9, PD8);//uart1-rx,tx   767是u3(pd9,pd8)(rx,tx)
/******************************************* */
/*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(screenWidth, screenHeight); /* TFT instance */

#if LV_USE_LOG != 0
/* Serial debugging */
void my_print(const char * buf)
{
    mySerial.printf(buf);
    mySerial.flush();
}
#endif

/*******************************************lb */
void touch_xpt2046_init(void)
{
    ts.begin();
    ts.setRotation(1);
}
/******************************************* */

/* 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 );
    /****************************************************lb */
    if (ts.tirqTouched()) 
    {
      if (ts.touched()) 
      {
        data->state = LV_INDEV_STATE_PR;
        TS_Point p = ts.getPoint();
      /*Set the coordinates*/
        // data->point.x = p.x;//480*(p.x-300)/3700
        // data->point.y = p.y;
        data->point.x = 480*(p.x-300)/3700;
        data->point.y = 320*(p.y-180)/3520;

        mySerial.print( "touch x " );
        mySerial.println( p.x );

        mySerial.print( "touch y " );
        mySerial.println( p.y );
      }else {data->state = LV_INDEV_STATE_REL;}
    }
    /**************************************************** */
    // 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;

    //     mySerial.print( "Data x " );
    //     mySerial.println( touchX );

    //     mySerial.print( "Data y " );
    //     mySerial.println( touchY );
    // }
}

void setup()
{
    mySerial.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();

    mySerial.println( LVGL_Arduino );
    mySerial.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( 3 ); /* Landscape orientation, flipped */
    tft.invertDisplay(0);//lb
    /*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] = { 275, 3620, 264, 3532, 1 };
    //  tft.setTouch( calData );
    touch_xpt2046_init();//lb

    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 0
    /* Create simple label */
    lv_obj_t *label = lv_label_create( lv_scr_act() );
    lv_label_set_text( label, LVGL_Arduino.c_str() );
    lv_obj_align( label, LV_ALIGN_CENTER, 0, 0 );
#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();            // OK
    //lv_demo_benchmark();          // OK
    // lv_demo_keypad_encoder();     // works, but I haven't an encoder
    // lv_demo_music();              // NOK
    // lv_demo_printer();
    // lv_demo_stress();             // seems to be OK
#endif
    mySerial.println( "Setup done" );
}

void loop()
{
    lv_timer_handler(); /* let the GUI do its work */
    delay( 5 );
}