
Arduino Quick Start
APIs and example programs related to the speaker of CoreP4X.
3.3.10-aM5CoreP4X0.3.10.3.1CoreP4X uses the ES8311 audio codec and the AW8737A amplifier; the speaker amplifier power is controlled by M5IOE1 IO3. After M5.Speaker.begin() is called, M5Unified configures I2S, the audio power, and the amplifier enable.
#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);
}After uploading, the screen displays the current note name and frequency. Tap PREV on the left or NEXT on the right to switch notes, and tap the middle button to play or stop the current sound.
The speaker section of CoreP4X uses the Speaker_Class in the M5Unified library. For more related APIs, refer to the documentation below: