m5stackで組込み!!

Arduinoによるm5stack開発のいろいろと...

タッチの座標

M5stack Core2の一番の魅力は画面をタッチして操作できること。
そのためにはまずタッチした座標を知る必要があります。
では座標を表示してみましょう。

座標位置を取得する関数

戻り値はTouchPoint_t構造体です。
構造体のメンバにはX座標、Y座標が含まれています。

  M5.Touch.getPressPoint();

サンプルコード

#include <M5Core2.h>

void setup() {
  // 初期化
  M5.begin();
  // 文字列サイズ指定
  M5.Lcd.setTextSize(2);

  // X座標表示
  M5.Lcd.setCursor(100,70);
  M5.Lcd.printf("x:%d \r\n", 0);
  // Y座標表示
  M5.Lcd.setCursor(100,90);
  M5.Lcd.printf("y:%d \r\n", 0);
}

void loop() {
  TouchPoint_t atTouchPoint;

  // タッチ中
  if( M5.Touch.ispressed() )
  {
    // タッチしたX座標、Y座標を取得する
    atTouchPoint = M5.Touch.getPressPoint();
    // X座標表示
    M5.Lcd.setCursor(100,70);
    M5.Lcd.printf("x:%d \r\n", atTouchPoint.x);
    // Y座標表示
    M5.Lcd.setCursor(100,90);
    M5.Lcd.printf("y:%d \r\n", atTouchPoint.y);
  }
  
  return;
}


youtu.be

これでタッチによる操作が出来そうです。
手を離すとマイナスになるみたいです。