lovyan03 / LovyanGFX

SPI LCD graphics library for ESP32 (ESP-IDF/ArduinoESP32) / ESP8266 (ArduinoESP8266) / SAMD51(Seeed ArduinoSAMD51)
Other
997 stars 184 forks source link

Touch Calibration #367

Closed codegrue closed 1 year ago

codegrue commented 1 year ago

Is there support for touch coordinates that don't match the screen coordinates (e.g. touch area extends beyond the visual screen area)? I don't see a way currently to calibrate the values.

tobozo commented 1 year ago

Yes there is support, see the M5Unified implementation of M5Core2 touch config where the touch panel is physically larger than the display panel.

So it's only a matter of performing a basic calibration and a logic check: if (raw.y > 240) >>> outside display panel area.

See the autodetect sequence

codegrue commented 1 year ago

I looked through that code and I'm not sure if my scenario is the same. I think in the M5 case the values returned by the raw touch don't originally match the screen values. In my case, the touch area is wider than the physical screen, so 0,0 is actually off the visual screen area to the top left, what would effectively be -52, -25.

My screen is 480x320, but when I touch the visible corners I get (52, 25) and (464, 314) respectively.

I use this code currently to recalculate the intended touch point from the returned touch values:

#define SCREEN_WIDTH 480
#define SCREEN_HEIGHT 320

// Calibration extents
#define TOUCH_X_MIN 52
#define TOUCH_X_MAX 464
#define TOUCH_Y_MIN 25
#define TOUCH_Y_MAX 314

    lgfx::touch_point_t tp;
    int touched = lcd.getTouch(&tp);

    int touch_x = tp.x;
    int touch_y = tp.y;

    // calibrate for touch extents
    int calibrated_x = 1.0 * (touch_x - TOUCH_X_MIN) / (TOUCH_X_MAX - TOUCH_X_MIN) * SCREEN_WIDTH;
    calibrated_x = max(0, calibrated_x);
    calibrated_x = min(SCREEN_WIDTH, calibrated_x);
    int calibrated_y = 1.0 * (touch_y - TOUCH_Y_MIN) / (TOUCH_Y_MAX - TOUCH_Y_MIN) * SCREEN_HEIGHT;
    calibrated_y = max(0, calibrated_y);
    calibrated_y = min(SCREEN_HEIGHT, calibrated_y);

In your example, I think you are just setting the coordinate values to return so the values match the screen pixels, but this assumes they physically overlay. In case you are wondering, this is the screen I am using:

https://wiki.makerfabs.com/ESP32_S3_Parallel_3.5_TFT_with_Touch.html

And if you look at the image, the class extends to the left and right over a black margins, but picks up touches in this area.

So my question is if this sort of screen offset recalibration was built in?

tobozo commented 1 year ago

It's hard to say which one of the touch driver or the application should be responsible for the coordinates translation and offset, but both are possible.

Maybe you can get some inspiration from the touch section in this config snippet.

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] commented 1 year ago

This issue has been automatically closed because it has not had recent activity. Thank you for your contributions.