TOUCH

これはM5Stack Core2のタッチスクリーンライブラリです。本ライブラリのAPIを利用して、タッチ入力情報を取得できます。また、タッチやジェスチャーのイベントを監視し、指定した処理を呼び出すこともできます。タッチスクリーンの実際のサイズは320x280で、スクリーンを覆い隠す部分を除いて、残りの40pxの高さは面板上の赤い円が印字されている部分を覆い隠し、ユーザはプログラムを通じて、物理的なボタンを模擬制御できます。

TouchButton クラス

TouchButtonはTouchZoneクラスを継承し、使用前にインスタンスを作成し、その中のメソッドを呼び出して使用します

コンストラクタ:

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

機能です:矩形のボタン領域のインスタンスを作成

状態の取得:

  • ボタン状態の設定

  • bool setState(bool)

  • ボタンが押下中か

  • bool isPressed()

  • ボタンが離脱したか

  • bool isReleased()

  • ボタンが押下した瞬間を取得

  • bool wasPressed()

  • ボタンが離脱した瞬間を取得

  • bool wasReleased()

  • ボタンが指定時間押下中

  • bool pressedFor(uint32_t ms)

  • ボタンが指定時間連続押下中

  • bool pressedFor(uint32_t ms, uint32_t continuous_time)

  • ボタンが指定時間離脱中

  • bool releasedFor(uint32_t ms)

  • ボタンが指定時間前離脱したか

  • bool wasReleasefor(uint32_t ms)

获取属性:

  • int32_t lastChange()

  • uint8_t finger;

  • bool changed;

  • char name[16];

事件监听:

  • ボタンのタッチ処理イベントを追加し、イベントの觸発タイプを設定

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

  • イベントのタイプ

#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

使用例です:

  #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 クラス

Gestureクラスは2つのTouchButton領域を渡し、タッチジェスチャーのオブジェクトと名前を作成できます。addHandlerを通じて、ジェスチャー觸発後の処理関数を作成し、領域1から領域2へ移動し、ジェスチャーの設定に合致した時、処理関数を觸発します。

构造函数:

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

機能です:创建矩形按键区域实例

使用例です:

#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

タッチ状態の更新は全てM5.update()に依存します。

TouchEvent结构体

タッチイベントが觸発した時、自動的に該当の領域でaddHandlerでバインドした処理関数を呼び出し、同時にTouchEventの構造体を引数として渡します。

struct TouchEvent {
    uint8_t finger;  //触摸点序号,最大支持两点
    uint16_t type;  //事件类型
    TouchPoint from;  //事件初始触摸点坐标-->x,y
    TouchPoint to;   //事件结束触摸点坐标-->x,y
    uint16_t duration; //事件持续时间
    TouchButton* button; //事件触发对象
    Gesture* gesture; //事件触发手势
};

使用例です:

#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对象

M5.begin()の初期化時、Touchのインスタンスが生成され、該当のインスタンスから、現在のスクリーンのタッチ操作情報、例えば座標や状態等を取得できます。

getPressPoint()

機能です

タッチ座標の取得

原型関数です:

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()

機能です

画面が押下中かの検査

原型関数です:

bool ispressed()

Example

#include <M5Core2.h>

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

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

HotZone_t* createHotZone()

機能です

タッチヒットエリアの作成

原型関数です:

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()

機能です

ヒットエリア内にいるかの判定

原型関数です:

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