LVGL Demos and XPT2046 setup

I am very new to LVGL, so I apologize in advance if I’m missing something obvious. I am trying to get the demos working on an ST7796/XPT2046 combo screen. Getting the ST7796 setup and working was very easy and straightforward, and the demo’s load and display correctly. What I am struggling with is the touch input. I don’t see any place in any of the configs to set the pins, select the driver, etc. Can anyone point me to the right place to get started? I’ve been googling for days and the only thing I can find is youtube video’s showing it working with zero info on how lol. Example LVGL widgets demo on ILI9488/XPT2046 on RBM33G - YouTube

I’m using a Teensy 4.1, so I have plenty of pins/inputs, I just need to know where in the code/config’s to tell it where I have everything plugged in. Do I need a driver that doesn’t come with LVGL? Am I missing an obvious section in the docs somewhere? Can the XPT2046 use the same SPI bus as the ST7796, or does it need its own?

Thanks for any help, I’m truly at a loss.


I was able to figure this out, the key for me at least was to just dig into the lvgl docs on how to setup an input device, and after going through that it made sense what I needed to do to the demo to get everything working. Yes, you can use the same SPI bus for the XPT2046 as the SP7796 and it works great. Here is what I had to do

#include "lv_demo_widgets.h"

#include <XPT2046_Touchscreen.h>

#define CS_PIN 32
#define TIRQ_PIN 31

int x = 0;
int y = 0;

XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);

bool my_input_read(lv_indev_drv_t * drv, lv_indev_data_t*data)
       if (ts.touched()) {
        TS_Point p = ts.getPoint();
        x = map(p.x, 220, 3850, 1, 480); //
        y = map(p.y, 310, 3773, 1, 320); // Feel pretty good about this
        data->point.x = x;
        data->point.y = y;
        data->state = LV_INDEV_STATE_PR; 
      } else {
        data->state = LV_INDEV_STATE_REL; 
    return false; /*No buffering now so no more data read*/

I had to add this import and this little function, and then I had to add one line below in the Dummy input setup section

      /* Initialize the (dummy) input device driver */
      static lv_indev_drv_t indev_drv;
      indev_drv.type = LV_INDEV_TYPE_POINTER;
      indev_drv.read_cb = my_input_read; // ADDED THIS LINE


      Serial.println("Setup done");

After that everything works great! Maybe this empty thread will be helpful to someone else in the future so I didn’t waste my time :slight_smile:

Happy to hear that you’ve found a solution! :slight_smile:

Is there something we can add to the docs to make things simpler to follow and understand?

That’s a great question. I think just a small blurp like my edit to the README for the demo’s as an example of getting touch working would have been super helpful. But I’m not great at documentation, so take that as a vague suggestion :slight_smile:

1 Like