English
English
简体中文
日本語

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

CoreP4X Speaker

APIs and example programs related to the speaker of CoreP4X.

Example Program

Compilation Requirements

  • M5Stack Board Manager version >= 3.3.10-a
  • Board selection = M5CoreP4X
  • M5Unified library version >= 0.3.1
  • M5GFX library version >= 0.3.1

CoreP4X 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.

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);
}

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.

API

The speaker section of CoreP4X uses the Speaker_Class in the M5Unified library. For more related APIs, refer to the documentation below:

Page Tools
PDF
On This Page