FT6336 + ESP32 - Touch misalignment

Description

Square Line Studio project has TOUCH MISALIGMENT.
Running the LVGL WIDGETS example for the same board and display (ESP32 + MSP3526) works fine. So I guess the issue is with the the exported files/Square Line config.

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

Arduino IDE 2.3.2
ESP32 (epressif 3.0.1 core)
MSP3526 module (ST7796 tft isplay + FT6336 touch)

What LVGL version are you using?

8.3.11

What do you want to achieve?

I want touch to correspond to where widgets are placed. Currently they are misaligned.

What have you tried so far?

Both display and touch are set to the same W/H ratio.
Also, running the LVGL example presents no issue, so I guess the problem is with the UI exported files from SQLN Studio

Code to reproduce

Add a code snippet which can run in the simulator. It should contain only the relevant code that compiles without errors when separated from your main code base.

The code block(s) should be formatted like:

FROM .INO FILE:

/* Reading input device (simulated encoder here) */
void my_touchpad_read(lv_indev_drv_t *indev_driver, lv_indev_data_t *data)
{
  if (touch_has_signal())
  {
    if (touch_touched())
    {
      data->state = LV_INDEV_STATE_PR;

      /*Set the coordinates*/
      data->point.x = touch_last_x;
      data->point.y = touch_last_y;
    }
    else if (touch_released())
    {
      data->state = LV_INDEV_STATE_REL;
    }
  }
  else
  {
    data->state = LV_INDEV_STATE_REL;
  }
}

FROM TOUCH.H

#include <FT6336.h>
//#include <FT6X36.h>

//#define TOUCH_SWAP_XY
#define TOUCH_MAP_X1 0
#define TOUCH_MAP_X2 320
#define TOUCH_MAP_Y1 0
#define TOUCH_MAP_Y2 480

int touch_last_x = 0, touch_last_y = 0;
unsigned short int width=0, height=0, rotation,min_x=0,max_x=0,min_y=0,max_y=0;

FT6336 ts = FT6336(TOUCH_FT6336_SDA, TOUCH_FT6336_SCL, TOUCH_FT6336_INT, TOUCH_FT6336_RST, max(TOUCH_MAP_X1, TOUCH_MAP_X2), max(TOUCH_MAP_Y1, TOUCH_MAP_Y2));

void touch_init(unsigned short int w, unsigned short int h,unsigned char r)
{
  width = w; 
  height = h;

  switch (r)
  {
    case ROTATION_NORMAL:
      case ROTATION_INVERTED:
        min_x = TOUCH_MAP_X1;
        max_x = TOUCH_MAP_X2;
        min_y = TOUCH_MAP_Y1;
        max_y = TOUCH_MAP_Y2;
      break;

    case ROTATION_LEFT:
      case ROTATION_RIGHT:
        min_x = TOUCH_MAP_Y1;
        max_x = TOUCH_MAP_Y2;
        min_y = TOUCH_MAP_X1;
        max_y = TOUCH_MAP_X2;
      break;

    default:
    break;
  }

  ts.begin();
  ts.setRotation(r);
}

bool touch_touched(void)
{
   ts.read();

  if (ts.isTouched)
  {
    #if defined(TOUCH_SWAP_XY)
        touch_last_x = map(ts.points[0].y, TOUCH_MAP_X1, TOUCH_MAP_X2, 0, width - 1);
        touch_last_y = map(ts.points[0].x, TOUCH_MAP_Y1, TOUCH_MAP_Y2, 0, height - 1);
    #else
        touch_last_x = map(ts.points[0].x, min_x, max_x, 0, width - 1);
        touch_last_y = map(ts.points[0].y, min_y, max_y, 0, height - 1);
    #endif

//    Serial.print("x = ");
//    Serial.print(touch_last_x);
//    Serial.print(", y = ");
//    Serial.print(touch_last_y);
//    Serial.print("\r\n");

    return true;
  }
  else
  {
    return false;
  }
}

bool touch_has_signal(void)
{
  return true;
}

bool touch_released(void)
{
  return true;
}

FT6336.h


#ifndef FT6336_H

  #define FT6336_H

  #include "Arduino.h"
  #include <Wire.h>

  #define FT6336_ADDR  (uint8_t)0x38

  #define ROTATION_LEFT       (uint8_t)3
  #define ROTATION_INVERTED   (uint8_t)2
  #define ROTATION_RIGHT      (uint8_t)1
  #define ROTATION_NORMAL     (uint8_t)0

  #define FT6336_DEVIDE_MODE  (uint8_t)0x00
  #define FT6336_TD_STATUS    (uint8_t)0x02

  #define FT6336_TOUCH_1      (uint8_t)0X03
  #define FT6336_TOUCH_2      (uint8_t)0X09

  #define FT6336_TOUCH_REG        {FT6336_TOUCH_1, FT6336_TOUCH_2}

  #define FT6336_ID_G_CIPHER_MID    (uint8_t)0x9F    //default: 0x26
  #define FT6336_ID_G_CIPHER_LOW    (uint8_t)0XA0    //0x01:FT6336G, 0x02:FT6336U
  #define FT6336_ID_G_LIB_VERSION   (uint8_t)0xA1
  #define FT6336_ID_G_CIPHER_HIGH     (uint8_t)0XA3  //default: 0x64
  #define FT6336_ID_G_MODE            (uint8_t)0XA4
  #define FT6336_ID_G_FOCALTECH_ID    (uint8_t)0XA8  //default: 0x11
  #define FT6336_ID_G_THGROUP         (uint8_t)0X80
  #define FT6336_ID_G_PERIODACTIVE    (uint8_t)0X88

  class TP_Point
  {
    public:
      TP_Point(void);
      TP_Point(uint8_t id, uint16_t x, uint16_t y, uint16_t size);

      bool operator==(TP_Point);
      bool operator!=(TP_Point);

      uint8_t id;
      uint16_t x;
      uint16_t y;
      uint8_t size;
  };

  class FT6336
  {
    public:
      FT6336(uint8_t _sda, uint8_t _scl, uint8_t _int, uint8_t _rst, uint16_t _width, uint16_t _height);
      void begin(uint8_t _addr=FT6336_ADDR);
      uint8_t reset();
      void setRotation(uint8_t rot);
      void read(void);
      uint8_t touches = 0;
      bool isTouched = false;
      TP_Point points[2];

    private:
      // static void ARDUINO_ISR_ATTR onInterrupt();
      TP_Point readPoint(uint8_t *data);
      void writeByteData(uint16_t reg, uint8_t val);
      uint8_t readByteData(uint16_t reg);
      void writeBlockData(uint16_t reg, uint8_t *val, uint8_t size);
      void readBlockData(uint8_t *buf, uint16_t reg, uint8_t size);
      uint8_t rotation = ROTATION_NORMAL;
      uint8_t addr;
      uint8_t pinSda;
      uint8_t pinScl;
      uint8_t pinInt;
      uint8_t pinRst;
      uint16_t width;
      uint16_t height;
  };

#endif

FT6336.cpp

#include "Arduino.h"
#include <FT6336.h>
#include <Wire.h>

FT6336::FT6336(uint8_t _sda, uint8_t _scl, uint8_t _int, uint8_t _rst, uint16_t _width, uint16_t _height) :
              pinSda(_sda), pinScl(_scl), pinInt(_int), pinRst(_rst), width(_width), height(_height)
              {
              }

void FT6336::begin(uint8_t _addr)
{
  addr = _addr;
  Wire.begin(pinSda, pinScl);
  
  if(reset())
  {
    Serial.print("touch ic error!\r\n");
  }
}

uint8_t FT6336::reset()
{
  uint8_t tmp[2];
  pinMode(pinInt, INPUT);
  pinMode(pinRst, OUTPUT);
  digitalWrite(pinInt, 1);
  digitalWrite(pinRst, 1);
  delay(20);
  digitalWrite(pinRst, 0);
  delay(20);
  digitalWrite(pinRst, 1);
  delay(500);
  readBlockData(&tmp[0], FT6336_ID_G_FOCALTECH_ID, 1);

  if(tmp[0] != 0x11)
  {
    return 1;
  }

  readBlockData(tmp, FT6336_ID_G_CIPHER_MID, 2);

  if(tmp[0] != 0x26)
  {
    return 1;
  }

  if((tmp[1]!=0x00)&&(tmp[1]!=0x01)&&(tmp[1]!=0x02))
  {
    return 1;
  }

  readBlockData(&tmp[0], FT6336_ID_G_CIPHER_HIGH, 1);

  if(tmp[0] != 0x64)
  {
    return 1;
  }

  return 0;
}

void FT6336::setRotation(uint8_t rot)
{
  rotation = rot;
}

void FT6336::read(void)
{
  uint8_t data[4];
  uint8_t pointInfo = readByteData(FT6336_TD_STATUS);
  touches = pointInfo;
  isTouched = (touches > 0 && touches < 3);

  if (isTouched)
  {
    for (uint8_t i=0; i<touches; i++)
    {
      readBlockData(data, FT6336_TOUCH_1 + i * 6, 4);
      points[i] = readPoint(data);
    }
  }
}

TP_Point FT6336::readPoint(uint8_t *data)
{
  uint16_t temp;
  uint8_t id = data[2] >> 4;
  uint16_t x = (uint16_t)((data[0]&0x0F)<<8) + data[1];
  uint16_t y = (uint16_t)((data[2]&0x0F)<<8) + data[3];

  switch (rotation)
  {
    case ROTATION_NORMAL:
      x = x;
      y = y;
    break;

    case ROTATION_LEFT:
      temp = x;
      x = height - y;
      y = temp;
    break;

    case ROTATION_INVERTED:
      x = width - x;
      y = height - y;
    break;

    case ROTATION_RIGHT:
      temp = x;
      x = y;
      y = width - temp;
    break;

    default:
    break;
  }
  return TP_Point(id, x, y, 0);
}

void FT6336::writeByteData(uint16_t reg, uint8_t val)
{
  Wire.beginTransmission(addr);
 // Wire.write(highByte(reg));
  Wire.write(lowByte(reg));
  Wire.write(val);
  Wire.endTransmission();
}

uint8_t FT6336::readByteData(uint16_t reg)
{
  uint8_t x;
  Wire.beginTransmission(addr);
 // Wire.write(highByte(reg));
  Wire.write(lowByte(reg));
  Wire.endTransmission();
  Wire.requestFrom(addr, (uint8_t)1);
  x = Wire.read();
  return x;
}

void FT6336::writeBlockData(uint16_t reg, uint8_t *val, uint8_t size)
{
  Wire.beginTransmission(addr);
//  Wire.write(highByte(reg));
  Wire.write(lowByte(reg));
  // Wire.write(val, size);
  for (uint8_t i=0; i<size; i++) {
    Wire.write(val[i]);
  }
  Wire.endTransmission();
}

void FT6336::readBlockData(uint8_t *buf, uint16_t reg, uint8_t size)
{
  Wire.beginTransmission(addr);
//  Wire.write(highByte(reg));
  Wire.write(lowByte(reg));
  Wire.endTransmission();
  Wire.requestFrom(addr, size);
  for (uint8_t i=0; i<size; i++) {
    buf[i] = Wire.read();
  }
}

TP_Point::TP_Point(void)
{
  id = x = y = size = 0;
}

TP_Point::TP_Point(uint8_t _id, uint16_t _x, uint16_t _y, uint16_t _size)
{
  id = _id;
  x = _x;
  y = _y;
  size = _size;
}

bool TP_Point::operator==(TP_Point point)
{
  return ((point.x == x) && (point.y == y) && (point.size == size));
}

bool TP_Point::operator!=(TP_Point point)
{
  return ((point.x != x) || (point.y != y) || (point.size != size));
}
/*You code here*/

Screenshot and/or video

If possible, add screenshots and/or videos about the current state.
20240607_150200_1_1_1.mp4 [resize output image]

hi, it’s all about the FT6336, contact the manufacturer to calibrate individually for each. it’s a big surprise…