pdf-icon

Arduino入門

2. デバイス&サンプル

Core2 Touch

Core2 触摸屏に関連するAPIとサンプルプログラムです。

サンプルプログラム

コンパイル要件

  • M5Stack ボードマネージャ バージョン >= 2.1.4
  • 開発ボードの選択 = M5Fire
  • M5Unified ライブラリ バージョン >= 0.2.5
  • M5GFX ライブラリ バージョン >= 0.2.7

1.タッチスクリーンボタン検出

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;
static int32_t w;
static int32_t h;

LGFX_Button button;

void setup() {
  M5.begin();

  w = M5.Lcd.width();
  h = M5.Lcd.height();
  
  M5.Lcd.fillScreen(WHITE);
  M5.Display.setRotation(0);
  M5.Display.setTextDatum(top_center);
  M5.Display.drawString("Button Released", w / 2, 0, &fonts::FreeMonoBold24pt7b);

  button.initButton(&M5.Lcd, w / 2, h / 2, 200, 200, TFT_BLUE, TFT_YELLOW, TFT_BLACK, "BTN", 4, 4);
  button.drawButton();
}

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

  if (touchDetail.isPressed()) {
    if(button.contains(touchDetail.x, touchDetail.y)){
        M5.Display.drawString("Button  Pressed", w / 2, 0, &fonts::FreeMonoBold24pt7b);
    }
  }
  else {
    M5.Display.drawString("Button Released", w / 2, 0, &fonts::FreeMonoBold24pt7b);
  }
}

该プログラムの機能は、指がタッチスクリーンに触れたとき、タッチポイントがボタン領域内にある場合は「Button Pressed」と表示し、そうでない場合は「Button Released」と表示します。

2.多点タッチ検出

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 38 39 40 41 42
#include <M5Unified.h>
#include <M5GFX.h>

m5::touch_point_t touchPoint[2];//Core2 supports up to 2-point touch
static bool drawed = false;
static int32_t w;
static int32_t h;

void setup() {
  M5.begin();

  w = M5.Lcd.width();
  h = M5.Lcd.height();
  
  M5.Lcd.fillScreen(WHITE);
  M5.Display.setRotation(1);
  M5.Display.setTextDatum(top_center);
  M5.Display.drawString("Touch not found", w / 2, 0, &fonts::FreeMonoBold12pt7b);
  M5.Display.setFont(&fonts::FreeMonoBold12pt7b);
}

void loop() {
  M5.update();
  int nums = M5.Lcd.getTouchRaw(touchPoint, 2);  

  if (nums)
  {
    M5.Display.drawString(" Touch detail: ", w / 2, 0, &fonts::FreeMonoBold12pt7b);
    for (int i = 0; i < nums; i++)
    {
      M5.Display.setCursor(0, 40 + i * 25);
      M5.Display.printf("Point %d X:%04d  Y:%04d", i+1, touchPoint[i].x, touchPoint[i].y);
    }
    drawed = true;
  }
  else if (drawed){
    drawed = false;
    M5.Display.clear(WHITE);
    M5.Display.drawString("Touch not found", w / 2, 0, &fonts::FreeMonoBold12pt7b);
  }
  vTaskDelay(1);
}  

该プログラムの機能は、指がタッチスクリーンに触れたとき、タッチポイントの座標情報を表示します。Core2は最大2点のタッチをサポートしています。

API

Core2 の Touch 部分は、M5UnifiedライブラリのTouch_Classを使用しています。詳細なAPIについては、以下のドキュメントを参照してください。

On This Page