Enrico Casti
Created December 19, 2021

Christmas Jukebox

High quality JukeBox with M5Stack node (WM8978)

BeginnerFull instructions provided1
Christmas Jukebox

Things used in this project

Story

Read more

Code

Christmas jukebox

Arduino
#include <WM8978.h> /* https://github.com/CelliesProjects/wm8978-esp32 */
#include <Audio.h>  /* https://github.com/schreibfaul1/ESP32-audioI2S */
#include <M5Stack.h>
#include <SD.h>
/* M5Stack Node WM8978 I2C pins */
#define I2C_SDA     21
#define I2C_SCL     22

/* M5Stack Node I2S pins */
#define I2S_BCK      5
#define I2S_WS      13
#define I2S_DOUT     2
#define I2S_DIN     34

/* M5Stack WM8978 MCLK gpio number */
#define I2S_MCLKPIN  0

int play = 0;
/* list of mp3 files */
String*  filedir[] = {};
int filenum = 0;
struct files {
  String name;
  int size;
};
struct files myfiles[100];

Audio audio;
WM8978 dac;

void setup() {
  M5.begin();
  M5.Power.begin();
  listDir(SD, "/", 0);
  printsongs();
  pinMode(36, INPUT); // Pir unit
  /* Setup wm8978 I2C interface */
  if (!dac.begin(I2C_SDA, I2C_SCL)) {
    ESP_LOGE(TAG, "Error setting up dac. System halted");
    while (1) delay(100);
  }

  /* set the i2s pins */
  audio.setPinout(I2S_BCK, I2S_WS, I2S_DOUT);

  /* Select I2S MCLK pin */
  audio.i2s_mclk_pin_select(I2S_MCLKPIN);

  dac.setSPKvol(40); /* max 63 */
  dac.setHPvol(32, 32);

}

void startsong() {
  int song = random(filenum);

  audio.connecttoFS(SD, myfiles[song].name.c_str());

  play = 1;
  M5.Lcd.fillRect(50, 100, 220, 100, RED);
  M5.Lcd.setCursor(60, 110);
  M5.Lcd.setTextColor(BLACK);
  M5.Lcd.print("Playing :");
  M5.Lcd.setCursor(60, 160);
  M5.Lcd.setTextColor(WHITE);
  M5.Lcd.print(myfiles[song].name);
}

void audio_eof_mp3(const char *info) { //end of file
  play = 0;
  printsongs();
}

void listDir(fs::FS &fs, const char * dirname, uint8_t levels) {
  String filename;
  Serial.printf("Listing directory: %s\n", dirname);

  File root = fs.open(dirname);
  if (!root) {
    Serial.println("Failed to open directory");
    return;
  }
  if (!root.isDirectory()) {
    Serial.println("Not a directory");
    return;
  }

  File file = root.openNextFile();
  while (file) {
    if (file.isDirectory()) {

    } else {
      filename = file.name();
      Serial.print("  FILE: ");
      Serial.print(file.name());
      Serial.print("  SIZE: ");
      Serial.println(file.size());

      if ((filename.indexOf(".mp3") > 0) && (filename.indexOf(".") > 0) ) {
        Serial.println("Found MP3");
        myfiles[filenum].name = filename;
        filenum++;
      }
    }
    if (filenum > 100) {
      break ;
    };
    file = root.openNextFile();
  }
}

void printsongs () {
  M5.Lcd.fillRect(0, 0, 320, 240, WHITE);
  M5.Lcd.fillRect(0, 0, 320, 35, RED);
  M5.Lcd.setCursor(10, 0);
  M5.Lcd.setTextColor(BLACK);
  M5.Lcd.setTextFont(2);
  M5.Lcd.setTextSize(2);

  M5.Lcd.print("Christmas JukeBox");
  M5.Lcd.setCursor(0, 40);

  M5.Lcd.setTextSize(1);
  for (int i = 0; i < filenum ; i++) {
    M5.Lcd.print(" > ");
    M5.Lcd.println(myfiles[i].name);
  }
}

void loop() {
  M5.update(); //Read the press state of the key
  if (M5.BtnA.wasReleased() || M5.BtnA.pressedFor(1000, 200)) {
    play = 0;
    printsongs();
  }
  if ( play == 1) {
    audio.loop();
  } else {
    if (digitalRead(36) == 1) {
      startsong();
    }
  }
}

Credits

Enrico Casti

Enrico Casti

3 projects • 3 followers
My name is Enrico, I work in the IT sector but I have a great passion for electronics and microcontrollers

Comments