pdf-icon

Touch

M5Dial屏幕触摸相关API与案例程序。

案例程序

#include <M5Dial.h>

void setup(void) {
    auto cfg = M5.config();
    M5Dial.begin(cfg, false, false);

    M5Dial.Display.setTextColor(GREEN);
    M5Dial.Display.setTextDatum(middle_center);
    M5Dial.Display.setTextFont(&fonts::Orbitron_Light_24);
    M5Dial.Display.setTextSize(1);

    M5Dial.Display.drawString("Touch Test", M5Dial.Display.width() / 2,
                              M5Dial.Display.height() / 2);
}

int prev_x = -1;
int prev_y = -1;

static m5::touch_state_t prev_state;

void loop(void) {
    M5Dial.update();

    auto t = M5Dial.Touch.getDetail();
    if (prev_state != t.state) {
        prev_state                                  = t.state;
        static constexpr const char* state_name[16] = {
            "none", "touch", "touch_end", "touch_begin",
            "___",  "hold",  "hold_end",  "hold_begin",
            "___",  "flick", "flick_end", "flick_begin",
            "___",  "drag",  "drag_end",  "drag_begin"};
        M5_LOGI("%s", state_name[t.state]);
        M5Dial.Display.fillRect(0, 0, M5Dial.Display.width(),
                                M5Dial.Display.height() / 2, BLACK);

        M5Dial.Display.drawString(state_name[t.state],
                                  M5Dial.Display.width() / 2,
                                  M5Dial.Display.height() / 2 - 30);
    }
    if (prev_x != t.x || prev_y != t.y) {
        M5Dial.Display.fillRect(0, M5Dial.Display.height() / 2,
                                M5Dial.Display.width(),
                                M5Dial.Display.height() / 2, BLACK);
        M5Dial.Display.drawString(
            "X:" + String(t.x) + " / " + "Y:" + String(t.y),
            M5Dial.Display.width() / 2, M5Dial.Display.height() / 2 + 30);
        prev_x = t.x;
        prev_y = t.y;
        M5Dial.Display.drawPixel(prev_x, prev_y);
    }
}

API

M5Dial库基于M5Unified库实现, 屏幕触摸部分使用了M5Unified库中的Touch_Class, 更多相关的API可以参考下方文档:

On This Page