pdf-icon

Arduino Quick Start

2. Devices & Examples

Screen Touch

Touch Information

getTouch

Function Prototype:

uint_fast8_t getTouch(touch_point_t *tp, uint_fast8_t count = 1)

Description:

  • Get the touch information of the touch panel

Parameters:

  • tp: pointer to the touch point
  • count: number of touch points, default is 1

Return Value:

  • uint_fast8_t: number of touch points

getTouchRaw

Function Prototype:

uint_fast8_t getTouchRaw(touch_point_t *tp, uint_fast8_t count = 1)

Description:

  • Get the raw touch information of the touch panel

Parameters:

  • tp: pointer to the touch point
  • count: number of touch points, default is 1

Return Value:

  • uint_fast8_t: number of touch points
Note:
1. The difference between this function and getTouch is that getTouchRaw returns unprocessed touch point data, including raw touch coordinates and pressure values, suitable for scenarios where more precise touch data is required.
2. For usage of both functions, see this example

convertRawXY

Function Prototype:

void convertRawXY(touch_point_t *tp, uint_fast8_t count = 1)

Description:

Parameters:

  • tp: pointer to touch point struct (about touch_point_t)
  • count: number of touch points, default is 1

Return Value:

  • null

Calibrate Screen

calibrateTouch

Function Prototype:

void calibrateTouch(uint16_t* parameters, const T &color_fg, const T &color_bg, uint8_t size = 10)

Description

  • Calibrate the touch panel. Pass a pointer as the first parameter to obtain a calibration value, which you can record in flash, etc., and use setTouchCalibrate on the next startup to skip manual calibration.

Parameters:

  • parameters: calibration value
  • color_fg: foreground color
  • color_bg: background color
  • size: touch point size

Return Value:

  • null

setTouchCalibrate

Function Prototype:

void setTouchCalibrate(uint16_t *parameters)

Description:

Parameters:

  • parameters: pointer to a uint16_t array containing calibration values, array length is 6.

Return Value:

  • null
Note
The screen calibration step will only appear after the first download and reset. In other cases, only the calibration value stored after the first calibration will be displayed.

Example Program:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#include <Arduino.h>
#include <Preferences.h>
#include <M5GFX.h>

M5GFX display;
uint16_t w;
uint16_t h;
const char* NAMESPACE = "m5_data";
const char* DATA_KEY = "user_data";
struct Calibration {
  bool calibration_flag = false;
  uint16_t touch_point[8];//4 point, each has (X,Y)
};


Calibration Cal;

void setup() {
    display.begin();

    display.setRotation(3);
    if(display.isEPD())
    {
        display.setColorDepth(8);//The ink screen product supports a maximum bit depth of 8 bits.
        display.setEpdMode(epd_fastest);
    }
    else
    {
        display.setColorDepth(16);
    }
    
    display.clear(TFT_WHITE);
    display.setFont(&fonts::FreeMonoBoldOblique12pt7b);
    display.setTextColor(TFT_BLACK);
    display.setTextSize(1);
    display.setCursor(0, 0);
    
    // Initiate preferences
    Preferences preferences;
    
    // Open the namespace (create it if it does not exist)
    preferences.begin(NAMESPACE, false); // false -- read-write mode
    size_t readBytes = preferences.getBytes(DATA_KEY, &Cal, sizeof(Calibration));
    // Check whether the data has been successfully read
    if (readBytes == sizeof(Calibration)) {
        if(Cal.calibration_flag){
            display.setTouchCalibrate(Cal.touch_point);
            for (int i=0; i<8; i++) {
                display.printf("%d  ", Cal.touch_point[i]);
            }
            // display.setTextDatum(top_center);
            display.println("Read calibration setting data successfully!");
        }    
    } 
    else {
        // Initiate default
        display.calibrateTouch(Cal.touch_point, BLACK, YELLOW);
        display.clear();
        display.println("Set calibration data successfully, show as belows:\n");

        for (int i=0; i<8; i++) {
            display.printf("%d  ", Cal.touch_point[i]);
        }
        display.println();
        Cal.calibration_flag = true;

        // Save calibration data
        size_t writtenBytes = preferences.putBytes(DATA_KEY, &Cal, sizeof(Calibration));
        
        if (writtenBytes == sizeof(Calibration)) {
            display.println("\nCalibration Data Save Successfully");
        } else {
            display.println("\nCalibration Data Save Failed");
        }
        
        // Close namespace
        preferences.end();
    }
}

void loop() {
}
On This Page