Arduino入門

2. デバイス&サンプル

5. 拡張モジュール&サンプル

アクセサリー

6. アプリケーション

CoreP4X Touch タッチスクリーン

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

サンプルプログラム

コンパイル要件

  • M5Stack ボードマネージャーバージョン >= 3.3.10-a
  • ボード選択 = M5CoreP4X
  • M5Unified ライブラリバージョン >= 0.3.1
  • M5GFX ライブラリバージョン >= 0.3.1

シングルポイントタッチ検出

このサンプルでは M5.Touch.getDetail() で現在のタッチ状態を読み取り、押下状態、タッチ座標、累計クリック回数を画面に表示します。

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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
#include <M5Unified.h>

M5Canvas canvas(&M5.Display);

static bool lastPressed = false;
static bool lastPointValid = false;
static int lastX = 0;
static int lastY = 0;
static uint32_t clickCount = 0;

static void drawTouchScreen(bool pressed, int x, int y)
{
  char coordText[40];
  char countText[32];

  canvas.fillSprite(TFT_BLACK);
  canvas.setFont(&fonts::FreeSansBold18pt7b);
  canvas.setTextDatum(middle_center);
  canvas.setTextColor(TFT_WHITE, TFT_BLACK);
  canvas.drawString("CoreP4X Touch", 240, 52);

  canvas.setFont(&fonts::FreeSans12pt7b);
  canvas.setTextColor(pressed ? TFT_GREEN : TFT_DARKGREY, TFT_BLACK);
  canvas.drawString(pressed ? "PRESSED" : "RELEASED", 240, 108);

  canvas.drawRect(36, 146, 408, 210, TFT_DARKGREY);
  canvas.drawFastVLine(240, 146, 210, 0x2124);
  canvas.drawFastHLine(36, 251, 408, 0x2124);

  if (pressed || lastPointValid) {
    const int markerX = constrain(x, 42, 438);
    const int markerY = constrain(y, 152, 350);
    canvas.fillCircle(markerX, markerY, 12, pressed ? TFT_SKYBLUE : TFT_DARKGREY);
    canvas.drawLine(markerX - 24, markerY, markerX + 24, markerY, TFT_WHITE);
    canvas.drawLine(markerX, markerY - 24, markerX, markerY + 24, TFT_WHITE);
  }

  if (pressed || lastPointValid) {
    snprintf(coordText, sizeof(coordText), "X: %03d    Y: %03d", x, y);
  } else {
    snprintf(coordText, sizeof(coordText), "X: ---    Y: ---");
  }

  snprintf(countText, sizeof(countText), "Clicks: %lu",
           static_cast<unsigned long>(clickCount));

  canvas.setFont(&fonts::FreeSansBold18pt7b);
  canvas.setTextColor(TFT_WHITE, TFT_BLACK);
  canvas.drawString(coordText, 240, 402);
  canvas.setFont(&fonts::FreeSans12pt7b);
  canvas.drawString(countText, 240, 450);
  canvas.pushSprite(0, 0);
}

void setup()
{

  auto cfg = M5.config();
  cfg.serial_baudrate = 115200;
  M5.begin(cfg);

  M5.Display.setBrightness(128);
  canvas.createSprite(M5.Display.width(), M5.Display.height());
  drawTouchScreen(false, 0, 0);
}

void loop()
{
  M5.update();

  const auto touch = M5.Touch.getDetail();
  const bool pressed = touch.isPressed();
  const bool clicked = touch.wasClicked();

  if (clicked) {
    ++clickCount;
    Serial.printf("Touch clicked at %d, %d\n", touch.x, touch.y);
  }

  if (clicked ||
      pressed != lastPressed ||
      (pressed && (touch.x != lastX || touch.y != lastY))) {
    lastPressed = pressed;
    lastX = touch.x;
    lastY = touch.y;
    lastPointValid = lastPointValid || pressed || clicked;
    drawTouchScreen(pressed, lastX, lastY);
  }

  delay(10);
}

書き込み後、画面にタッチが押されているかどうか、現在または最後のタッチ座標、累計クリック回数が表示され、シリアルモニターにも各クリックの位置が出力されます。

マルチポイントタッチ検出

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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
#include <M5Unified.h>

static constexpr uint_fast8_t MAX_TOUCH_POINTS = 5; // support up to 5 points
static const uint16_t POINT_COLORS[MAX_TOUCH_POINTS] = {
  TFT_CYAN, TFT_GREEN, TFT_YELLOW, TFT_ORANGE, TFT_MAGENTA
};

M5Canvas canvas(&M5.Display);
m5::touch_point_t touchPoints[MAX_TOUCH_POINTS];
m5::touch_point_t lastPoints[MAX_TOUCH_POINTS];
uint_fast8_t lastCount = 0;

bool touchChanged(const m5::touch_point_t* points, uint_fast8_t count)
{
  if (count != lastCount) return true;

  for (uint_fast8_t i = 0; i < count; ++i) {
    if (points[i].x != lastPoints[i].x ||
        points[i].y != lastPoints[i].y ||
        points[i].id != lastPoints[i].id) {
      return true;
    }
  }
  return false;
}

void saveTouchPoints(const m5::touch_point_t* points, uint_fast8_t count)
{
  lastCount = count;
  for (uint_fast8_t i = 0; i < count; ++i) {
    lastPoints[i] = points[i];
  }
}

void drawTouchScreen(const m5::touch_point_t* points, uint_fast8_t count)
{
  const int32_t w = canvas.width();

  canvas.fillSprite(TFT_WHITE);
  canvas.setTextDatum(top_center);
  canvas.setFont(&fonts::FreeSansBold18pt7b);
  canvas.setTextColor(TFT_BLACK, TFT_WHITE);
  canvas.drawString("CoreP4X Multi Touch", w / 2, 24);

  char countText[32];
  snprintf(countText, sizeof(countText), "Points: %u / %u",
           static_cast<unsigned>(count),
           static_cast<unsigned>(MAX_TOUCH_POINTS));
  canvas.setFont(&fonts::FreeSans12pt7b);
  canvas.setTextColor(count ? TFT_BLUE : TFT_DARKGREY, TFT_WHITE);
  canvas.drawString(countText, w / 2, 82);
  canvas.drawFastHLine(20, 116, w - 40, TFT_LIGHTGREY);

  if (count == 0) {
    canvas.setTextDatum(middle_center);
    canvas.setTextColor(TFT_DARKGREY, TFT_WHITE);
    canvas.drawString("Touch with up to 5 fingers", w / 2, 220);
  }

  canvas.fillRoundRect(16, 326, w - 32, 138, 12, 0xE71C);
  canvas.drawRoundRect(16, 326, w - 32, 138, 12, TFT_LIGHTGREY);

  for (uint_fast8_t i = 0; i < MAX_TOUCH_POINTS; ++i) {
    char pointText[32];
    const bool active = i < count;
    if (active) {
      snprintf(pointText, sizeof(pointText), "P%u  X:%03d  Y:%03d",
               static_cast<unsigned>(i + 1), points[i].x, points[i].y);
    } else {
      snprintf(pointText, sizeof(pointText), "P%u  X:---  Y:---",
               static_cast<unsigned>(i + 1));
    }

    const int32_t x = (i < 3) ? 32 : (w / 2 + 12);
    const int32_t y = (i < 3) ? (344 + i * 40) : (344 + (i - 3) * 40);
    canvas.fillCircle(x + 6, y + 9, 5, active ? POINT_COLORS[i] : TFT_DARKGREY);
    canvas.setTextDatum(top_left);
    canvas.setTextColor(active ? TFT_BLACK : TFT_DARKGREY, 0xE71C);
    canvas.drawString(pointText, x + 16, y);
  }

  for (uint_fast8_t i = 0; i < count; ++i) {
    char label[4];
    snprintf(label, sizeof(label), "P%u", static_cast<unsigned>(i + 1));
    canvas.fillCircle(points[i].x, points[i].y, 22, POINT_COLORS[i]);
    canvas.drawCircle(points[i].x, points[i].y, 24, TFT_BLACK);
    canvas.setTextDatum(middle_center);
    canvas.setTextColor(TFT_BLACK, POINT_COLORS[i]);
    canvas.drawString(label, points[i].x, points[i].y);
  }

  canvas.pushSprite(0, 0);
}

void setup()
{

  auto cfg = M5.config();
  cfg.serial_baudrate = 115200;
  M5.begin(cfg);

  M5.Display.setBrightness(128);
  canvas.createSprite(M5.Display.width(), M5.Display.height());
  drawTouchScreen(touchPoints, 0);
}

void loop()
{
  M5.update();

  const uint_fast8_t count =
      M5.Display.getTouch(touchPoints, MAX_TOUCH_POINTS);

  if (touchChanged(touchPoints, count)) {
    drawTouchScreen(touchPoints, count);
    saveTouchPoints(touchPoints, count);

    Serial.printf("Touch points: %u\r\n", static_cast<unsigned>(count));
    for (uint_fast8_t i = 0; i < count; ++i) {
      Serial.printf("Point %u: X:%d Y:%d\r\n",
                    static_cast<unsigned>(i + 1),
                    touchPoints[i].x, touchPoints[i].y);
    }
  }

  delay(20);
}

書き込み後、複数の指で画面をタッチすると、現在のタッチポイント数と各ポイントの座標が表示され、シリアルモニターにも同じ情報が出力されます。最大 5 点までのマルチタッチに対応します。

API

CoreP4X のタッチは M5Unified ライブラリの Touch_Class を使用します。その他のタッチ API については以下のドキュメントを参照してください:

Page Tools
PDF
On This Page