Arduino入門

2. デバイス&サンプル

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

アクセサリー

6. アプリケーション

CoreP4X Speaker スピーカー

CoreP4X のスピーカーに関する API とサンプルプログラムです。

サンプルプログラム

コンパイル要件

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

CoreP4X は ES8311 オーディオコーデックと AW8737A アンプを使用し、スピーカーアンプの電源は M5IOE1 IO3 で制御されます。M5.Speaker.begin() を呼び出すと、M5Unified が I2S、オーディオ電源、アンプイネーブルを設定します。

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
#include <M5Unified.h>

static const uint16_t NOTES[] = {
  262, 294, 330, 349, 392, 440, 494, 523,
};

static const char* NOTE_NAMES[] = {
  "C4", "D4", "E4", "F4", "G4", "A4", "B4", "C5",
};

static const size_t NOTE_COUNT = sizeof(NOTES) / sizeof(NOTES[0]);
static size_t noteIndex = 0;
static bool lastPlaying = false;

M5Canvas canvas(&M5.Display);

static void drawNote()
{
  const bool playing = M5.Speaker.isPlaying();

  canvas.fillSprite(TFT_BLACK);
  canvas.setTextDatum(middle_center);
  canvas.setTextColor(TFT_WHITE, TFT_BLACK);

  canvas.setFont(&fonts::FreeSansBold18pt7b);
  canvas.drawString("Speaker Test", 240, 52);

  canvas.setFont(&fonts::FreeSansBold24pt7b);
  canvas.setTextSize(1.4f);
  canvas.drawString(NOTE_NAMES[noteIndex], 240, 168);
  canvas.setTextSize(1.0f);

  char freqText[24];
  snprintf(freqText, sizeof(freqText), "%u Hz", NOTES[noteIndex]);
  canvas.setFont(&fonts::FreeSans18pt7b);
  canvas.setTextColor(TFT_CYAN, TFT_BLACK);
  canvas.drawString(freqText, 240, 246);

  canvas.setTextColor(playing ? TFT_GREEN : TFT_DARKGREY, TFT_BLACK);
  canvas.setFont(&fonts::FreeSans12pt7b);
  canvas.drawString(playing ? "Playing" : "Ready", 240, 304);

  const char* labels[3] = {"PREV", playing ? "STOP" : "PLAY", "NEXT"};
  const uint16_t colors[3] = {TFT_BLUE, playing ? TFT_RED : TFT_DARKGREEN, TFT_BLUE};
  const int zoneX[3] = {24, 172, 320};

  canvas.setFont(&fonts::FreeSansBold12pt7b);
  for (int i = 0; i < 3; ++i) {
    canvas.fillRoundRect(zoneX[i], 344, 136, 92, 12, colors[i]);
    canvas.drawRoundRect(zoneX[i], 344, 136, 92, 12, TFT_WHITE);
    canvas.setTextColor(TFT_WHITE, colors[i]);
    canvas.drawString(labels[i], zoneX[i] + 68, 390);
  }

  canvas.setFont(&fonts::FreeSans9pt7b);
  canvas.setTextColor(TFT_DARKGREY, TFT_BLACK);
  canvas.drawString("Tap a control below", 240, 454);
  canvas.pushSprite(0, 0);
}

static void playNote()
{
  M5.Speaker.tone(NOTES[noteIndex], 350);
  Serial.printf("Play %s: %u Hz\n", NOTE_NAMES[noteIndex], NOTES[noteIndex]);
  lastPlaying = true;
  drawNote();
}

void setup()
{

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

  M5.Display.setRotation(0);
  M5.Display.setBrightness(128);
  canvas.createSprite(M5.Display.width(), M5.Display.height());

  M5.Speaker.begin();
  // Raise the master volume from the default value.
  M5.Speaker.setVolume(160);
  drawNote();
}

void loop()
{
  M5.update();

  if (M5.Touch.getDetail().wasClicked()) {
    const int x = M5.Touch.getDetail().x;

    if (x < 160) {
      noteIndex = (noteIndex + NOTE_COUNT - 1) % NOTE_COUNT;
      playNote();
    } else if (x > 320) {
      noteIndex = (noteIndex + 1) % NOTE_COUNT;
      playNote();
    } else if (M5.Speaker.isPlaying()) {
      M5.Speaker.stop();
      Serial.println("Speaker stopped");
    } else {
      playNote();
    }

    lastPlaying = M5.Speaker.isPlaying();
    drawNote();
  }

  const bool playing = M5.Speaker.isPlaying();
  if (playing != lastPlaying) {
    lastPlaying = playing;
    drawNote();
  }

  delay(20);
}

書き込み後、画面に現在の音名と周波数が表示されます。左側の PREV または右側の NEXT をタップして音階を切り替え、中央のボタンをタップすると現在の音を再生または停止します。

API

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

Page Tools
PDF
On This Page