Touch

TOUGH integrates a 2-inch resistive touchscreen on the front, with the NS2009 as the driver chip. In the M5Tough library, we provide a series of APIs for touch interaction detection, details as follows.

Touch class

An instance touch is already created and initialized within M5.begin() initialization. You can use the APIs in this instance to get touch interaction information.

  • Function: Initialize the touchscreen. This function will be called by default during the touchscreen initialization in M5.begin

  • void begin(bool EEPROMEnabled = true)

  • Function: Perform screen touch calibration (calibration data will be saved to EEPROM). If already calibrated, returns True, otherwise, enters the calibration page where the user needs to touch the specified positions on the screen as prompted

  • bool calibrationTouch(TFT_eSPI *fb)

Usage example:

#include <M5Tough.h>

void setup()
{
    M5.begin(true, true, true, true);
    while (!M5.touch.calibrationTouch(&M5.Lcd));
    M5.Lcd.fillScreen(TFT_BLACK);
    M5.Lcd.print("Calibration Successfully");
}                                  

void loop()
{
   
}
  • Function: Read touch data

  • void read()

  • Function: Read the X coordinate of the touch finger

  • int GetTouchX()

  • Function: Read the Y coordinate of the touch finger

  • int GetTouchY()

  • Function: Detect finger press on the touchscreen

  • bool isPressed()

  • Function: Detect finger release on the touchscreen

  • bool isRelease()

  • Function: Detect finger movement on the touchscreen

  • bool isMoving()

  • Function: Wait for the finger to be released from the touchscreen

  • bool WaitRelease()

  • Function: Wait for the finger to be released from the touchscreen and set a timeout

  • bool WaitRelease(uint64_t AutoReleseTime)

  • Function: Read touch point information, returning a TouchPoint structure containing X, Y coordinate information, and support for direct assignment and comparison of TouchPoint type structures through internal operator overload

  • TouchPoint getPoint()

TouchPoint

By calling the getPoint method, you can obtain a TouchPoint structure type of data. Its internal members and methods are as follows.

class TouchPoint
{
public:
    TouchPoint(int16_t x_ = -1, int16_t y_ = -1);
    bool operator==(const TouchPoint &p);
    bool operator!=(const TouchPoint &p);
    bool operator=(const TouchPoint &p);

    bool Equals(const TouchPoint &p);

    void set(int16_t x_ = -1, int16_t y_ = -1);
    bool valid();

    int16_t x, y;
};

Users can create a TouchZone structure instance to define a screen area. Using the built-in contains method, it can determine whether a TouchPoint is contained within that area.

TouchZone

typedef struct TouchZone
{
    uint16_t x;
    uint16_t y;
    uint16_t w;
    uint16_t h;
    TouchZone() : x(-1), y(-1), w(0), h(0) {}
    TouchZone(uint16_t _x, uint16_t _y, uint16_t _w, uint16_t _h) : x(_x), y(_y), w(_w), h(_h){};
    bool contains(const TouchPoint &p)
    {
        if ((this->x == -1) || (this->y == -1) || (this->w == 0) || (this->h == 0))
            return false;
        if ((p.x >= this->x) && (p.x <= (this->x + this->w)) &&
            (p.y >= this->y) && (p.y <= (this->y + this->h)))
        {
            return true;
        }
        return false;
    }
} TouchZone_t;

To dynamically read touch data, you need to add void update() to the loop for refreshing touch data.

Usage example:

#include <M5Tough.h>

TouchZone clearBtn(0,0,50,50);

void setup()
{
    M5.begin(true, true, true, true);
    while (!M5.touch.calibrationTouch(&M5.Lcd));
    M5.lcd.drawString("CLEAR",5,5,3);
}                                  

void loop()
{
    M5.update();
    if(M5.touch.isPressed()){
        TouchPoint Point = M5.touch.getPoint();
        if(M5.touch.isMoving()){
            char buffer[24];
            sprintf(buffer, "X

: %3d, Y: %3d", Point.x, Point.y);
            M5.Lcd.drawPixel(Point.x, Point.y ,TFT_GREEN);
            Serial.println(buffer);
        }
        if(clearBtn.contains(Point)){
            M5.lcd.fillScreen(TFT_BLACK);
            M5.lcd.drawString("CLEAR",5,5,3);
        }
    }
}


On This Page