pdf-icon

Arduino入門

2. デバイス&サンプル

Paper Touch タッチスクリーン

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

サンプルプログラム

コンパイル要件

  • M5Stack ボードマネージャのバージョン >= 2.1.4
  • 使用ボード = M5Paper
  • 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
#include <M5Unified.h>
#include <M5GFX.h>

m5::touch_detail_t touchDetail;
uint16_t color;

void setup() {
  M5.begin();
  M5.Display.setRotation(0);
  M5.Display.setFont(&fonts::DejaVu40);
  M5.Display.setEpdMode(epd_fastest);  // epd_quality, epd_text, epd_fast, epd_fastest

  color = random(65535);

  Serial.begin(115200);
  Serial.println("Start drawing!");
  M5.Display.print(" Start drawing! ");
}

void loop() {
  M5.update();
  touchDetail = M5.Touch.getDetail();

  if (touchDetail.isPressed()) {
    Serial.printf("x:%d, y:%d\r\n", touchDetail.x, touchDetail.y);
    color = (color + 5) % 65536;
    M5.Display.fillCircle(touchDetail.x, touchDetail.y, 15, color);
  }
}

このプログラムの主な機能は、指で画面に触れると、タッチポイントの座標をシリアル経由でパソコンに出力し、その位置に異なるグレースケールの円を描画することです。プログラムではタッチポイントを1つだけ読み取りますが、以下の API を使用して Paper の2点マルチタッチ機能を開発することもできます。

API

Paper のタッチスクリーンは M5Unified ライブラリの Touch_Class を使用しています。さらに詳しい API 情報は、以下のドキュメントをご参照ください:

On This Page