
Arduino入門
CoreP4X のスピーカーに関する API とサンプルプログラムです。
3.3.10-aM5CoreP4X0.3.10.3.1CoreP4X は ES8311 オーディオコーデックと AW8737A アンプを使用し、スピーカーアンプの電源は M5IOE1 IO3 で制御されます。M5.Speaker.begin() を呼び出すと、M5Unified が I2S、オーディオ電源、アンプイネーブルを設定します。
#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 をタップして音階を切り替え、中央のボタンをタップすると現在の音を再生または停止します。
CoreP4X のスピーカーは M5Unified ライブラリの Speaker_Class を使用します。その他の API については以下のドキュメントを参照してください: