Skip to content

Instantly share code, notes, and snippets.

@Sigafoos Sigafoos/mwr.ino
Created Sep 1, 2017

Embed
What would you like to do?
// Specifically for use with the Adafruit Feather, the pins are pre-set here!
// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <SD.h>
#include <Adafruit_VS1053.h>
#include "config.h" // not included here, but the standard config.h file used with io.adafruit.com
AdafruitIO_Feed *knock = io.feed("knock");
AdafruitIO_Feed *hello = io.feed("hello");
// These are the pins used
#define VS1053_RESET -1 // VS1053 reset pin (not used!)
// Feather M0 or 32u4
#if defined(__AVR__) || defined(ARDUINO_SAMD_FEATHER_M0)
#define VS1053_CS 6 // VS1053 chip select pin (output)
#define VS1053_DCS 10 // VS1053 Data/command select pin (output)
#define CARDCS 5 // Card chip select pin
// DREQ should be an Int pin *if possible* (not possible on 32u4)
#define VS1053_DREQ 9 // VS1053 Data request, ideally an Interrupt pin
// Feather ESP8266
#elif defined(ESP8266)
#define VS1053_CS 16 // VS1053 chip select pin (output)
#define VS1053_DCS 15 // VS1053 Data/command select pin (output)
#define CARDCS 2 // Card chip select pin
#define VS1053_DREQ 0 // VS1053 Data request, ideally an Interrupt pin
// Feather ESP32
#elif defined(ESP32)
#define VS1053_CS 32 // VS1053 chip select pin (output)
#define VS1053_DCS 33 // VS1053 Data/command select pin (output)
#define CARDCS 14 // Card chip select pin
#define VS1053_DREQ 15 // VS1053 Data request, ideally an Interrupt pin
// Feather Teensy3
#elif defined(TEENSYDUINO)
#define VS1053_CS 3 // VS1053 chip select pin (output)
#define VS1053_DCS 10 // VS1053 Data/command select pin (output)
#define CARDCS 8 // Card chip select pin
#define VS1053_DREQ 4 // VS1053 Data request, ideally an Interrupt pin
// WICED feather
#elif defined(ARDUINO_STM32_FEATHER)
#define VS1053_CS PC7 // VS1053 chip select pin (output)
#define VS1053_DCS PB4 // VS1053 Data/command select pin (output)
#define CARDCS PC5 // Card chip select pin
#define VS1053_DREQ PA15 // VS1053 Data request, ideally an Interrupt pin
#elif defined(ARDUINO_FEATHER52)
#define VS1053_CS 30 // VS1053 chip select pin (output)
#define VS1053_DCS 11 // VS1053 Data/command select pin (output)
#define CARDCS 27 // Card chip select pin
#define VS1053_DREQ 31 // VS1053 Data request, ideally an Interrupt pin
#endif
Adafruit_VS1053_FilePlayer musicPlayer =
Adafruit_VS1053_FilePlayer(VS1053_RESET, VS1053_CS, VS1053_DCS, VS1053_DREQ, CARDCS);
String file;
char cfile[50];
int knockIndex;
int helloIndex;
int knockCount;
int helloCount;
void setup() {
Serial.begin(115200);
// Wait for serial port to be opened, remove this line for 'standalone' operation
while (!Serial) {
delay(1);
}
if (! musicPlayer.begin()) { // initialise the music player
Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
while (1);
}
Serial.println(F("VS1053 found"));
if (!SD.begin(CARDCS)) {
Serial.println(F("SD failed, or not present"));
while (1); // don't do anything more
}
Serial.println("SD OK!");
// Set volume for left, right channels. lower numbers == louder volume!
int vol = 1;
musicPlayer.setVolume(vol, vol);
#if defined(__AVR_ATmega32U4__)
// Timer interrupts are not suggested, better to use DREQ interrupt!
// but we don't have them on the 32u4 feather...
musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT); // timer int
#elif defined(ESP32)
// no IRQ! doesn't work yet :/
#else
// If DREQ is on an interrupt pin we can do background
// audio playing
musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT); // DREQ int
#endif
// connect to io.adafruit.com
Serial.print("Connecting to Adafruit IO");
io.connect();
// set message handlers
knock->onMessage(handleKnock);
hello->onMessage(handleHello);
// wait for a connection
while (io.status() < AIO_CONNECTED) {
Serial.print(".");
delay(500);
}
// we are connected
Serial.println();
Serial.println(io.statusText());
musicPlayer.sineTest(0x44, 500); // Make a tone to indicate VS1053 is working
knockIndex = 1;
helloIndex = 1;
knockCount = countFiles("knock");
helloCount = countFiles("hello");
Serial.print("knocks: "); Serial.println(knockCount);
Serial.print("hellos: "); Serial.println(helloCount);
}
void loop() {
io.run();
}
void handleKnock(AdafruitIO_Data *data) {
int vol = 1;
musicPlayer.setVolume(vol, vol);
file = "knock/";
file.concat(knockIndex);
file.concat(".mp3");
Serial.print("playing file "); Serial.println(file);
file.toCharArray(cfile, 50);
musicPlayer.playFullFile(cfile);
knockIndex++;
if (knockIndex > knockCount) {
knockIndex = 1;
}
}
void handleHello(AdafruitIO_Data *data) {
int vol = 1;
musicPlayer.setVolume(vol, vol);
file = "hello/";
file.concat(helloIndex);
file.concat(".mp3");
Serial.print("playing file "); Serial.println(file);
file.toCharArray(cfile, 50);
musicPlayer.playFullFile(cfile);
helloIndex++;
if (helloIndex > helloCount) {
helloIndex = 1;
}
}
int countFiles(String directory) {
File dir = SD.open(directory + "/");
int i = 0;
while (true) {
File entry = dir.openNextFile();
if (!entry) {
break;
}
if (!entry.isDirectory()) {
i++;
}
entry.close();
}
return i;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.