pdf-icon

Arduino 上手教程

2. 设备开发 & 案例程序

屏幕触摸

触摸信息

getTouch

函数原型:

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

功能说明:

  • 获取触摸面板的触摸信息

传入参数:

  • tp:指向触摸点的指针
  • count:触摸点数量,默认为 1

返回值:

  • uint_fast8_t:触摸点数量

getTouchRaw

函数原型:

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

功能说明:

  • 获取触摸面板的原始触摸信息

传入参数:

  • tp:指向触摸点的指针
  • count:触摸点数量,默认为 1

返回值:

  • uint_fast8_t:触摸点数量
说明:
1.此函数与 getTouch 的区别在于,getTouchRaw 返回的触摸点数据不经过处理,包含原始的触摸坐标和压力值,适用于需要获取更精确的触摸数据的场景。
2.两函数使用方法可见此例程

convertRawXY

函数原型:

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

功能说明:

传入参数:

  • tp:触摸点结构体指针 (关于touch_point_t
  • count:触摸点个数,默认为1个

返回值:

  • null

校准屏幕

calibrateTouch

函数原型:

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

功能说明

  • 校准触摸板,传递一个指针给第一个参数,以获得一个校准值,你可以将这个值记录在 flash 中等等,并在下次启动时使用setTouchCalibrate来省略手工校准。

传入参数:

  • parameters:校准值
  • color_fg:前景色
  • color_bg:背景色
  • size:触摸点大小

返回值:

  • null

setTouchCalibrate

函数原型:

void setTouchCalibrate(uint16_t *parameters)

功能说明:

传入参数:

  • parameters:指向包含校准值的 uint16_t 数组的指针,数组长度为 6。

返回值:

  • null
说明
此程序只有在第一次下载复位后会出现屏幕校准步骤,其他情况下只会显示第一次校准后存储的校准值。

案例程序:

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