TOUCH

This is the M5Stack Core2 touchscreen library. You can use the APIs in this library to obtain touch interaction information and listen for touch and gesture events, specifying corresponding handlers. The actual size of the touchscreen is 320x280, with the remaining 40px height covering the area with red circles on the panel. Users can simulate physical buttons through programming.

TouchButton Class

The TouchButton class inherits from the TouchZone class. Before using it, create instances of the button and call methods contained in the instance for usage.

Constructor:

TouchButton(uint16_t x_, uint16_t y_, uint16_t w_, uint16_t h_, const char* name_ = "")

Functionality:

Creates an instance of a rectangular button area.

Get State:

  • Set button state

  • bool setState(bool)

  • Check if the button is pressed

  • bool isPressed()

  • Check if the button is released

  • bool isReleased()

  • Check if the button was pressed once

  • bool wasPressed()

  • Check if the button was released once

  • bool wasReleased()

  • Check if the button was pressed for a specified duration

  • bool pressedFor(uint32_t ms)

  • Check if the button was pressed for a specified duration and continuous time

  • bool pressedFor(uint32_t ms, uint32_t continuous_time)

  • Check if the button was released for a specified duration

  • bool releasedFor(uint32_t ms)

  • Check if the button was released for a specified duration

  • bool wasReleasefor(uint32_t ms)

Get Properties:

  • int32_t lastChange()
  • uint8_t finger;
  • bool changed;
  • char name[16];

Event Listening:

  • Add touch event handling for the button, specifying the type of event to trigger

  • void addHandler(void (*fn)(TouchEvent&), uint16_t eventMask = TE_ALL)

  • Event Types

#define NUM_EVENTS  8
#define TE_TOUCH    0x0001
#define TE_RELEASE  0x0002
#define TE_MOVE     0x0004
#define TE_GESTURE  0x0008
#define TE_TAP      0x0010
#define TE_DBLTAP   0x0020
#define TE_DRAGGED  0x0040
#define TE_PRESSED  0x0080
#define TE_ALL      0x0FFF
#define TE_BTNONLY  0x1000

Usage Example:

#include <M5Core2.h>

Button lt(0, 0, 160, 120, "left-top");
Button lb(0, 120, 160, 120, "left-bottom");
Button rt(160, 0, 160, 120, "right-top");
Button rb(160, 120, 160, 120, "right-bottom");

void colorButtons(Event& e) {
  Button& b = *e.button;
  M5.Lcd.fillRect(b.x, b.y, b.w, b.h, b.isPressed() ? WHITE : BLACK);
}

void dblTapped(Event& e) {
  Serial.println("--- TOP RIGHT BUTTON WAS DOUBLETAPPED ---");
}

void setup() {
  M5.begin();
  M5.Buttons.addHandler(colorButtons,  E_TOUCH + E_RELEASE);
  rt.addHandler(dblTapped, E_DBLTAP);
}

void loop() {
  M5.update();
}

Gesture Class

The Gesture class supports passing two TouchButton areas to create a touch gesture object with a gesture name. Use addHandler to create a handler function for when the gesture is triggered, which will be triggered when moving from one area to another and meeting the configured gesture conditions.

Constructor:

Gesture(TouchZone fromZone_, TouchZone toZone_, const char* name_ = "", uint16_t maxTime_ = GESTURE_MAXTIME, uint16_t minDistance_ = GESTURE_MINDIST)

Functionality:

Creates an instance of a touch gesture object.

Usage Example:

#include <M5Core2.h>

TouchZone topHalf(0,0,320,120);
TouchZone bottomHalf(0,120,320,160);
Gesture swipeDown(topHalf, bottomHalf, "Swipe Down");

void yayWeSwiped(TouchEvent& e) {
  Serial.println("--- SWIPE DOWN DETECTED ---");
}

void setup() {
  M5.begin();
  swipeDown.addHandler(yayWeSwiped);
}

void loop() {
  M5.update();
}

update

All touch state updates depend on M5.update().

TouchEvent Structure

When a touch event is triggered, the handler function bound using addHandler will be automatically called, and the TouchEvent structure will be passed as a parameter.

struct TouchEvent {
    uint8_t finger;  // Finger number, supports up to two points
    uint16_t type;  // Event type
    TouchPoint from;  // Initial touch point coordinates --> x, y
    TouchPoint to;   // End touch point coordinates --> x, y
    uint16_t duration; // Event duration
    TouchButton* button; // Event trigger object
    Gesture* gesture; // Event trigger gesture
};

Usage Example:

#include <M5Core2.h>

TouchButton lt = TouchButton(0, 0, 160, 120, "left-top");
TouchButton lb = TouchButton(0, 120, 160, 120, "left-bottom");
TouchButton rt = TouchButton(160, 0, 160, 120, "right-top");
TouchButton rb = TouchButton(160, 120, 160, 120, "right-bottom");

void eventDisplay(TouchEvent& e) {
  Serial.printf("%-12s finger%d  %-18s (%3d, %3d)", M5.Touch.eventTypeName(e), e.finger, M5.Touch.eventObjName(e),  e.from.x, e.from.y);
  if (e.type != TE_TOUCH && e.type != TE_TAP && e.type != TE_DBLTAP) {
    Serial.printf("--> (%3d, %3d)  %5d ms", e.to.x, e.to.y, e.duration);
  }
  Serial.println();
}

void setup() {
  M5.begin();
  M5.Touch.addHandler(eventDisplay);
}

void loop() {
  M5.update();
}

Touch Object

With M5.begin() initialization, a Touch instance will be generated, which can obtain some touch operation information of the current screen, such as coordinates, status, etc.

getPressPoint()

Function:

Get touch coordinates.

Function Prototype:

TouchPoint_t getPressPoint()

#include <M5Core2.h>

void setep() {
    M5.begin();
}

void loop() {
    TouchPoint_t coordinate;
    coordinate = M5.Touch.getPressPoint();
    Serial.printf("x:%d, y:%d \r\n", coordinate.x, coordinate

.y);
}

ispressed()

Function:

Check if the screen is pressed.

Function Prototype:

bool ispressed()

Example:

#include <M5Core2.h>

void setup() {
    M5.begin();
}

void loop() {
    if(M5.Touch.ispressed()) {
        Serial.println("Pressed");
    }
}

HotZone_t* createHotZone()

Function:

Create a touch hot zone.

Function Prototype:

HotZone_t* creatHotZone(uint16_t x0, uint16_t y0, uint16_t w, uint16_t h, void (*fun)() = nullptr )

Example:

#include <M5Core2.h>

void doFunc(void){
  Serial.println("executed doFunc()");
}

HotZone Btn(0, 0, 320, 240, &doFunc));

void setup(){
    M5.begin();
}
void loop() {

}

inHotZone()

Function:

Check if the point is inside the hot zone.

Function Prototype:

bool inHotZone(TouchPoint_t point)

Example:

#include<M5Core2.h>

HotZone Btn(140, 100, 200, 160);

void setup() {
    M5.begin();
}

void loop() {
    TouchPoint_t pos = M5.Touch.getPressPoint();
    if(Btn.inHotZone(pos)) {
        Serial.printf("%d, %d\r\n", pos.x, pos.y);
    }

}
On This Page