TOUGHの正面に2インチのレジスタンス式タッチスクリーンが統合され、駆動チップはNS2009です。M5Toughのライブラリ中、タッチ交互検出用のAPIシリーズが提供されています。詳細は以下の通りです。
m5.begin()初期化では、インスタンス
touch`を作成して初期化しました。このインスタンスではAPIを使ってタッチインタラクション情報を取得できます。
機能: タッチスクリーンを初期化。M5.begin中で、デフォルトでこの関数を呼び出ししてタッチスクリーンを初期化します。
void begin(bool EEPROMEnabled = true)
機能: 画面タッチを校正(校正データはEEPROMに保存されます)。既に校正が完了した場合はTrueを返します、未完了の場合は校正画面に進み、ユーザーは画面の指示に従い指定の位置をタッチする必要があります。
bool calibrationTouch(TFT_eSPI *fb)
使用示例:
#include <M5Tough.h> void setup(){ M5.begin(true, true, true, true); while (!M5.touch.calibrationTouch(&M5.Lcd)); M5.Lcd.fillScreen(TFT_BLACK); M5.Lcd.print("Calibration Successfully");} void loop(){ }
機能: タッチデータを読み取る
void read()
機能: タッチ指のX座標を読み取る
int GetTouchX()
機能: タッチ指のY座標を読み取る
int GetTouchY()
機能: タッチ指の押下を検出
bool isPressed()
機能: タッチ指の離脱を検出
bool isRelease()
機能: タッチ指の移動を検出
bool isMoving()
機能: タッチ指の離脱を待つ
bool WaitRelease()
機能: タッチ指の離脱を待ち、タイムアウト時間を設定
bool WaitRelease(uint64_t AutoReleseTime)
機能: タッチ点情報を読み取る、TouchPoint構造体を返します。この中にX、Y座標情報が含まれ、内部で演算子のオーバーロードが実装され、TouchPoint型の構造体を直接割り当て比較できます。
TouchPoint getPoint()
getPointメソッドを呼び出すと、TouchPoint構造体のデータを取得できます。内部に含まれるメンバーとメソッドは以下の通りです。
class TouchPoint{public: TouchPoint(int16_t x_ = -1, int16_t y_ = -1); bool operator==(const TouchPoint &p); bool operator!=(const TouchPoint &p); bool operator=(const TouchPoint &p); bool Equals(const TouchPoint &p); void set(int16_t x_ = -1, int16_t y_ = -1); bool vaild(); int16_t x, y;};
ユーザーはTouchZoon構造体のインスタンスを作成することで、画面領域を作成し、内蔵のメソッドを利用して、TouchPointがその領域内に含まれるかを判断できます。
typedef struct TouchZone{ uint16_t x; uint16_t y; uint16_t w; uint16_t h; TouchZone() : x(-1), y(-1), w(0), h(0) {} TouchZone(uint16_t _x, uint16_t _y, uint16_t _w, uint16_t _h) : x(_x), y(_y), w(_w), h(_h){}; bool contains(const TouchPoint &p) { if ((this->x == -1) || (this->y == -1) || (this->w == 0) || (this->h == 0)) return false; if ((p.x >= this->x) && (p.x <= (this->x + this->w)) && (p.y >= this->y) && (p.y <= (this->y + this->h))) { return true; } return false; }} TouchZone_t;
タッチデータを働的に読み出す場合は、void update()
をループに追加してタッチデータをリフレッシュする必要があります。
使用示例:
#include <M5Tough.h> TouchZone clearBtn(0,0,50,50); void setup(){ M5.begin(true, true, true, true); while (!M5.touch.calibrationTouch(&M5.Lcd)); M5.lcd.drawString("CLEAR",5,5,3);} void loop(){ M5.update(); if(M5.touch.isPressed()){ TouchPoint Point = M5.touch.getPoint(); if(M5.touch.isMoving()){ char buffer[24]; sprintf(buffer, "X: %3d, Y: %3d", Point.x, Point.y); M5.Lcd.drawPixel(Point.x, Point.y ,TFT_GREEN); Serial.println(buffer); } if(clearBtn.contains(Point)){ M5.lcd.fillScreen(TFT_BLACK); M5.lcd.drawString("CLEAR",5,5,3); } }}