Skip to content

Instantly share code, notes, and snippets.

@ypelletier
Last active March 29, 2021 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ypelletier/5928498 to your computer and use it in GitHub Desktop.
Save ypelletier/5928498 to your computer and use it in GitHub Desktop.
Lecteur mp3 constitué d'un module VS1053, d'un module carte SD et d'un Arduino Uno.
/***************************************************
Lecture de fichiers mp3 sur une carte SD.
Plus d'infos:
http://electroniqueamateur.blogspot.com/2013/06/lecture-de-fichiers-mp3-avec-arduino-et.html
****************************************************/
#include <SPI.h>
#include <Adafruit_VS1053.h> // https://github.com/adafruit/Adafruit_VS1053_Library
#include <SD.h>
// Définition des broches pour Arduino Uno
#define VS1053_XRST 8
#define VS1053_XCS 6
#define VS1053_XDCS 7
#define SDCARD_CS 9
#define VS1053_DREQ 2
Adafruit_VS1053_FilePlayer musicPlayer =
Adafruit_VS1053_FilePlayer(VS1053_XRST, VS1053_XCS, VS1053_XDCS, VS1053_DREQ, SDCARD_CS);
#define NOMBRE_DE_FICHIERS 10
char listeFichiers [NOMBRE_DE_FICHIERS] [13];
int numeroCourant = 0;
int nombreTotal = 0;
bool enPause = 0; // 1 si l'utilisateur a arrêté la lecture
int volumeSonore = 30;
void setup() {
Serial.begin(115500);
delay(10);
if (! musicPlayer.begin()) {
Serial.println(F("L'initialisation du VS1053 n'a peut-etre pas fonctionne."));
//while (1); // avec mon module VS1053, ça bloque ici!
}
if (!SD.begin(SDCARD_CS)) {
Serial.println(F("Carte SD absente"));
while (1);
}
printDirectory(SD.open("/"));
musicPlayer.setVolume(volumeSonore, volumeSonore);
musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT); // DREQ int
}
void loop() {
if (!musicPlayer.playingMusic && !enPause) { // la pièce est terminée: on joue la prochaine
musicPlayer.startPlayingFile(listeFichiers[numeroCourant]);
if (numeroCourant < nombreTotal) {
numeroCourant++;
}
else {
numeroCourant = 0;
}
}
if (Serial.available()) {
char c = Serial.read();
if (c == 'n') {
Serial.println("Commande recue: n");
musicPlayer.startPlayingFile(listeFichiers[numeroCourant]);
if (numeroCourant < nombreTotal) {
numeroCourant++;
}
else {
numeroCourant = 0;
}
}
// s = stop
if (c == 's') {
musicPlayer.stopPlaying();
enPause = 1;
}
// p = pause / unpause
if (c == 'p') {
if (! musicPlayer.paused()) {
enPause = 1;
Serial.println("Lecture mise en pause.");
musicPlayer.pausePlaying(true);
} else {
enPause = 0;
Serial.println("On continue la lecture.");
musicPlayer.pausePlaying(false);
}
}
// +: augmenter le volume
if (c == '+') {
if (volumeSonore > 0){
Serial.println("Plus fort");
volumeSonore--;
musicPlayer.setVolume(volumeSonore, volumeSonore);
}
}
// -: diminuer le volume
if (c == '-') {
if (volumeSonore < 255){
Serial.println("Moins fort");
volumeSonore++;
musicPlayer.setVolume(volumeSonore, volumeSonore);
}
}
}
delay(100);
}
void printDirectory(File dir) {
int numero = 0;
Serial.println("Liste des fichiers sur la carte SD:");
while (true) {
File entry = dir.openNextFile();
if (! entry) { // il n'y a pas d'autres fichiers
break;
}
if (!entry.isDirectory()) {
Serial.println(entry.name());
if (numero < NOMBRE_DE_FICHIERS) {
strcpy(listeFichiers[numero], entry.name());
numero++;
}
}
nombreTotal = numero - 1;
entry.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment