pdf-icon

Touch

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

#include <M5CoreS3.h>

void setup(void) {
    auto cfg = M5.config();
    CoreS3.begin(cfg);

    CoreS3.Display.setTextColor(GREEN);
    CoreS3.Display.setTextDatum(middle_center);
    CoreS3.Display.setFont(&fonts::Orbitron_Light_24);
    CoreS3.Display.setTextSize(1);

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

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

static m5::touch_state_t prev_state;

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

    auto t = CoreS3.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]);
        CoreS3.Display.fillRect(0, 0, CoreS3.Display.width(), 140, BLACK);

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

API

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

On This Page