No event on touchscreen pressed on ESP32-S2

Description

Hi !

I don’t manage to get my touchscreen working. After several hours, I think i’m not so far.

lv_conf is configurated with LV_TICK_CUSTOM 1
lv simple button example is ok but don’t react.
with some Serial.print, i know that i go inside callback function with LV_EVENT_ALL but I think that the code LV_EVENT_CLICKED never come .
I have x, y coordinates injected inside lv_indev_data_t data

logs don’t show anything at WARN level and INFO is not acurate.
level TRACE has huge amont of

What MCU/Processor/Board and compiler are you using?

Makerfabs TFT3.5 with ESP32-s2 parallel wiring

Platformio.ini :

[env:esp32Makerfabs]
platform = espressif32
board = esp32dev
framework = arduino
board_build.mcu = esp32s2
upload_protocol = esptool
platform_packages = 
	framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32#2.0.0-alpha1
	platformio/tool-esptoolpy @ ~1.30100
src_filter = +<*> -<.git/> -<.svn/> -<example/> -<examples/> -<test/> -<tests/> -<main_sensor.cpp*> -<main_ihm.cpp*> -<main_ihm_ili9486.cpp*>
build_flags = -DLV_CONF_INCLUDE_SIMPLE -DLV_DEMO_CONF_INCLUDE_SIMPLE -Iinclude
lib_deps = 
	lovyan03/LovyanGFX@^0.4.11
	lvgl/lvgl@^8.1.0

What do you want to achieve?

touchscreen working with this config

What have you tried so far?

this demo works well (display and touchscreen):

Code to reproduce

my main.cpp, just a simple example adapted with the previous one :

#include <lvgl.h>
#include <lv_port_indev.h> /* input Driver for lvgl */
//测试通过
#define LGFX_USE_V1

#include <LovyanGFX.hpp> /* graphic drivers */


#define I2C_SCL 39
#define I2C_SDA 38

#define LCD_CS 37
#define LCD_BLK 45


class LGFX : public lgfx::LGFX_Device
{
    //lgfx::Panel_ILI9341 _panel_instance;
    lgfx::Panel_ILI9488 _panel_instance;
    lgfx::Bus_Parallel16 _bus_instance; // 8ビットパラレルバスのインスタンス (ESP32のみ)

public:
    // コンストラクタを作成し、ここで各種設定を行います。
    // クラス名を変更した場合はコンストラクタも同じ名前を指定してください。
    LGFX(void)
    {
        {                                      // バス制御の設定を行います。
            auto cfg = _bus_instance.config(); // バス設定用の構造体を取得します。

            // 16位设置
            cfg.i2s_port = I2S_NUM_0;  // 使用するI2Sポートを選択 (0 or 1) (ESP32のI2S LCDモードを使用します)
            cfg.freq_write = 16000000; // 送信クロック (最大20MHz, 80MHzを整数で割った値に丸められます)
            cfg.pin_wr = 35;           // WR を接続しているピン番号
            cfg.pin_rd = 34;           // RD を接続しているピン番号
            cfg.pin_rs = 36;           // RS(D/C)を接続しているピン番号

            cfg.pin_d0 = 33;
            cfg.pin_d1 = 21;
            cfg.pin_d2 = 14;
            cfg.pin_d3 = 13;
            cfg.pin_d4 = 12;
            cfg.pin_d5 = 11;
            cfg.pin_d6 = 10;
            cfg.pin_d7 = 9;
            cfg.pin_d8 = 3;
            cfg.pin_d9 = 8;
            cfg.pin_d10 = 16;
            cfg.pin_d11 = 15;
            cfg.pin_d12 = 7;
            cfg.pin_d13 = 6;
            cfg.pin_d14 = 5;
            cfg.pin_d15 = 4;

            _bus_instance.config(cfg);              // 設定値をバスに反映します。
            _panel_instance.setBus(&_bus_instance); // バスをパネルにセットします。
        }

        {                                        // 表示パネル制御の設定を行います。
            auto cfg = _panel_instance.config(); // 表示パネル設定用の構造体を取得します。

            cfg.pin_cs = -1;   // CS要拉低
            cfg.pin_rst = -1;  // RST和开发板RST相连
            cfg.pin_busy = -1; // BUSYが接続されているピン番号 (-1 = disable)

            // ※ 以下の設定値はパネル毎に一般的な初期値が設定されていますので、不明な項目はコメントアウトして試してみてください。

            cfg.memory_width = 320;   // ドライバICがサポートしている最大の幅
            cfg.memory_height = 480;  // ドライバICがサポートしている最大の高さ
            cfg.panel_width = 320;    // 実際に表示可能な幅
            cfg.panel_height = 480;   // 実際に表示可能な高さ
            cfg.offset_x = 0;         // パネルのX方向オフセット量
            cfg.offset_y = 0;         // パネルのY方向オフセット量
            cfg.offset_rotation = 0;  // 回転方向の値のオフセット 0~7 (4~7は上下反転)
            cfg.dummy_read_pixel = 8; // ピクセル読出し前のダミーリードのビット数
            cfg.dummy_read_bits = 1;  // ピクセル以外のデータ読出し前のダミーリードのビット数
            cfg.readable = true;      // データ読出しが可能な場合 trueに設定
            cfg.invert = false;       // パネルの明暗が反転してしまう場合 trueに設定
            cfg.rgb_order = false;    // パネルの赤と青が入れ替わってしまう場合 trueに設定
            cfg.dlen_16bit = true;    // データ長を16bit単位で送信するパネルの場合 trueに設定
            cfg.bus_shared = true;    // SDカードとバスを共有している場合 trueに設定(drawJpgFile等でバス制御を行います)

            _panel_instance.config(cfg);
        }

        setPanel(&_panel_instance); // 使用するパネルをセットします。
    }
};


LGFX lcd;




//Change to your screen resolution
static const uint32_t screenWidth  = 480;
static const uint32_t screenHeight = 320;
static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[ screenWidth * 10 ];

// 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 );

   lcd.startWrite();
   lcd.setAddrWindow( area->x1, area->y1, w, h );
   //lcd.pushColors( ( uint16_t * )&color_p->full, w * h, true );
   lcd.writePixels((lgfx::rgb565_t *)&color_p->full, w * h);
   lcd.endWrite();

   lv_disp_flush_ready( disp );
}

 void btn_event_cb(lv_event_t * e)
{
    //Serial.println("button event");
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * btn = lv_event_get_target(e);

    if(code == LV_EVENT_CLICKED) {
        static uint8_t cnt = 0;
        cnt++;

        /*Get the first child of the button which is the label and change its text*/
        lv_obj_t * label = lv_obj_get_child(btn, 0);
        lv_label_set_text_fmt(label, "Button: %d", cnt);
    }

}

/**
 * Create a button with a label and react on click event.
 */
void lv_example_get_started_1(void)
{
    lv_obj_t * btn = lv_btn_create(lv_scr_act());     /*Add a button the current screen*/
    lv_obj_set_size(btn, 120, 50);                          /*Set its size*/
    lv_obj_align(btn, LV_ALIGN_CENTER, 0,0);
    lv_obj_add_event_cb(btn, btn_event_cb, LV_EVENT_ALL, NULL);           /*Assign a callback to the button*/

    lv_obj_t * label = lv_label_create(btn);          /*Add a label to the button*/
    lv_label_set_text(label, "Button");                     /*Set the labels text*/
    lv_obj_center(label);
}

void setup(void)
{
    pinMode(LCD_CS, OUTPUT);
    pinMode(LCD_BLK, OUTPUT);

    digitalWrite(LCD_CS, LOW);
    digitalWrite(LCD_BLK, HIGH);

    Serial.begin(9600);

    /* LGVL init - graphic library */
    lv_init();
    lv_disp_draw_buf_init( &draw_buf, buf, NULL, screenWidth * 10 );

    /* LCD screen begin */
    lcd.init();
    lcd.setRotation(1);
    lcd.setBrightness(255);


    //Initialize the display inside LGVL
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);

    //Initialize the (dummy) input device driver in LGVL (lib/lv_port_indev_init)
    lv_port_indev_init();


    //Change the following line to your display resolution in LGVL
    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);


    lv_example_get_started_1();

}



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

the lv_port_indev.h :

/**
 * @file lv_port_indev_templ.c
 *
 */

 /*Copy this file as "lv_port_indev.c" and set this value to "1" to enable content*/

#if 1

#include "lv_port_indev.h"

#define get_pos ft6236_pos

/* Driver Hardware FT6236 */
const int i2c_touch_addr = TOUCH_I2C_ADD;


lv_indev_t * indev_touchpad;
static lv_indev_data_t data;

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

void lv_port_indev_init(void)
{
    /**
     * Here you will find implementation of input device Touchpad supported by LittelvGL:
     *   
     *
     *  The `..._read()` is shaped according to hardware
     */

    static lv_indev_drv_t indev_drv;



    /*Register a touchpad input device*/
    lv_indev_drv_init(&indev_drv);
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = touchpad_read;
    indev_touchpad = lv_indev_drv_register(&indev_drv);
    
    /*Initialize  touchpad */
    touchpad_init();

}

/**********************
 *   STATIC FUNCTIONS
 **********************/


/*Initialize touchpad*/
static void touchpad_init(void)
{
    /*Your code comes here*/
    //I2C init
    Wire.begin(I2C_TOUCH_SDA, I2C_TOUCH_SCL);
    byte error, address;

    Wire.beginTransmission(i2c_touch_addr);
    error = Wire.endTransmission();

    if (error == 0)
    {
        Serial.print("I2C device found at address 0x");
        Serial.print(i2c_touch_addr, HEX);
        Serial.println("  !");
    }
    else if (error == 4)
    {
        Serial.print("Unknown error at address 0x");
        Serial.println(i2c_touch_addr, HEX);
    }

}

/*Will be called by the library to read the touchpad*/
static void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
    Serial.println("touchpad_read() called");

    static lv_coord_t last_x = 0;
    static lv_coord_t last_y = 0;

    /*Save the pressed coordinates and the state*/
    if (get_pos)
    {
        last_x = getTouchPointX();
        last_y = getTouchPointY();
        //touchpad_get_xy(&last_x, &last_y);
        data->state = LV_INDEV_STATE_PR;

        Serial.print("Touch coord. | x:");
        Serial.print(last_x);
        Serial.print(", y : ");
        Serial.print(last_y);
        Serial.println();
    } else {
        data->state = LV_INDEV_STATE_REL;
    }

    /*Set the last pressed coordinates*/
    data->point.x = last_x;
    data->point.y = last_y;
}


/* Get the x and y coordinates if the touchpad is pressed */


#else /*Enable this file at the top*/

/*This dummy typedef exists purely to silence -Wpedantic.*/
typedef int keep_pedantic_happy;
#endif

And finaly, i took FT6236 driver from the first example (witch works)

the log give me that :
the warn message doesn’t appear on all surface of touchscreen.

[Info]  (241.628, +109)  indev_proc_press: pressed at x:134 y:348       (in lv_indev.c line #782)
[Warn]  (241.733, +105)  indev_pointer_proc: Y is 348 which is greater than hor. res    (in lv_indev.c line #353)
[Info]  (241.841, +108)  indev_proc_press: pressed at x:134 y:348       (in lv_indev.c line #782)
[Warn]  (241.946, +105)  indev_pointer_proc: Y is 348 which is greater than hor. res    (in lv_indev.c line #353)
[Info]  (242.055, +109)  indev_proc_press: pressed at x:134 y:348       (in lv_indev.c line #782)
[Warn]  (242.160, +105)  indev_pointer_proc: Y is 348 which is greater than hor. res    (in lv_indev.c line #353)
[Info]  (242.268, +108)  indev_proc_press: pressed at x:134 y:348       (in lv_indev.c line #782)
[Warn]  (242.373, +105)  indev_pointer_proc: Y is 348 which is greater than hor. res    (in lv_indev.c line #353)
[Info]  (242.482, +109)  indev_proc_press: pressed at x:134 y:348       (in lv_indev.c line #782)
[Warn]  (242.587, +105)  indev_pointer_proc: Y is 348 which is greater than hor. res    (in lv_indev.c line #353)
[Info]  (242.695, +108)  indev_proc_press: pressed at x:134 y:348       (in lv_indev.c line #782)
[Warn]  (242.800, +105)  indev_pointer_proc: Y is 348 which is greater than hor. res    (in lv_indev.c line #353)
[Info]  (242.909, +109)  indev_proc_press: pressed at x:134 y:348       (in lv_indev.c line #782)
[Warn]  (243.018, +109)  indev_pointer_proc: Y is 348 which is greater than hor. res    (in lv_indev.c line #353)

Thanks for help !

ok, FT6236 drivers from makerfabs sucks.

I had to write a touched() function because their get_pos() wasn’t trigged by touching screen.

it works !

Could you share the touched() function code? I’m having a similar problem.

@Juanito could you please please please supply us with the updated code. I have been at is as a beginner and it is breaking my head

Hi Juanito,
have the same problem, how did you solve it?
On my side, the touch pad (I2C) works at the beginning ( touch calibration )
if I add the following into loop(), I can see correct values for X and Y.
But the lvgl environment gets no events and / or wrong values for X and Y.

void loop()
{
lv_timer_handler(); /* let the GUI do its work */
if (tft.touch())
{
uint16_t touchX, touchY;
if( tft.getTouch( &touchX, &touchY ) )
{
Serial.printf( “X = %d”, touchX );
Serial.printf( " Y = %d\n", touchY);
}
}
delay( 5);
}
If you have any hint, where to search, it will help me to get sleep at night :slight_smile:
Thanks