pdf-icon

Arduino入門

2. デバイス&サンプル

Tough Touch タッチスクリーン

Tough のタッチスクリーンに関連するAPIとサンプルプログラム。

サンプルプログラム

コンパイル要件

  • M5Stack ボードマネージャ バージョン >= 2.1.4
  • ボード設定 = M5Tough
  • M5Unified ライブラリ バージョン >= 0.2.5
  • M5GFX ライブラリ バージョン >= 0.2.7
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
#include <M5Unified.h>
#include <M5GFX.h>
m5::touch_detail_t touchDetail;
int width, height;
void setup() {
M5.begin();
M5.Display.setRotation(1);
M5.Display.setFont(&fonts::DejaVu40);
width = M5.Display.width();
height = M5.Display.height();
Serial.begin(115200);
Serial.println("Start touching!");
M5.Display.print("Start touching!");
}
void loop() {
M5.update();
touchDetail = M5.Touch.getDetail();
if (touchDetail.isPressed()) {
Serial.printf("x:%d, y:%d\r\n", touchDetail.x, touchDetail.y);
for (int i = 0; i < width; i += 10) {
for (int j = 0; j < height; j += 10) {
uint8_t cr = map((touchDetail.x - i) % width, 0, width, 0, 256);
uint8_t cg = map((touchDetail.y - j) % height, 0, height, 0, 256);
uint16_t color = M5.Display.color565(cr, cg, 255 - (cr + cg) / 2);
M5.Display.fillRect(i, j, 10, 10, color);
}
}
}
}

このプログラムの主な機能は、指で画面に触れると、タッチ座標をシリアル経由でパソコンに出力し、画面上のカラフルな配列がタッチポイントに追従して移動することです。プログラムでは1点のみのタッチを取得していますが、以下のAPIを使えば Tough の2点タッチ機能も開発できます。

API

Tough のタッチスクリーン機能には、M5Unified ライブラリの Touch_Class が使用されています。関連するAPIの詳細は以下のドキュメントを参照してください:

On This Page