/***************************************************
* This is an example for the Adafruit VS1053 Codec Breakout
* Designed specifically to work with the Adafruit VS1053 Codec Breakout
* ----> https://www.adafruit.com/products/1381
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Limor Fried/Ladyada for Adafruit Industries.
* BSD license, all text above must be included in any redistribution
****************************************************/
// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
#include "Button.h"
#include "ZPlayer.h"
//#include "MomentaryButton.h"
// These are the pins used for the breakout example
#define BREAKOUT_RESET 9 // VS1053 reset pin (output)
#define BREAKOUT_CS 10 // VS1053 chip select pin (output)
#define BREAKOUT_DCS 8 // VS1053 Data/command select pin (output)
// These are the pins used for the music maker shield
#define SHIELD_RESET -1 // VS1053 reset pin (unused!)
#define SHIELD_CS 7 // VS1053 chip select pin (output)
#define SHIELD_DCS 6 // VS1053 Data/command select pin (output)
// These are common pins between breakout and shield
#define CARDCS 4 // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3 // VS1053 Data request, ideally an Interrupt pin
ZPlayer musicPlayer =
// create breakout-example object!
//Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
// create shield-example object!
ZPlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
int lVolume=20;
int rVolume=20;
int Volume=20;
int inPin[4] = {
A1, 5, A2, A5};
int counterA = 0; // how many times we have seen new value
int counterB = 0;
int counterC = 0;
int counterD = 0;
int reading[4];
int lastStateD = 0;
int MAXVOLUME = 100;
int MaxTrack = 2;
int state[4] = {
HIGH, HIGH, HIGH, HIGH};
long timeA = 0;
long timeB = 0;
long timeC = 0;
long timeD = 0;
int debounce_count =2;
//int i=0;
////
//char tracks[2][13] = { //The first number is the number of tracks. 12 Character names + null terminator.
// "track001.mp3",
// "track002.mp3",
//}
Button buttonA(A1);
Button buttonB(5);
Button buttonC(A2);
Button buttonD(A5);
uint16_t aBuffer[16];
uint8_t dBuffer[32];
void setup() {
//pinMode(inPin[0], INPUT_PULLUP);
//pinMode(inPin[1], INPUT_PULLUP);
//pinMode(inPin[2], INPUT_PULLUP);
//pinMode(inPin[3], INPUT_PULLUP);
Serial.begin(9600);
Serial.println("Adafruit VS1053 Library Test");
// initialise the music player
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"));
//musicPlayer.sineTest(0x44, 500); // Make a tone to indicate VS1053 is working
if (!SD.begin(CARDCS)) {
Serial.println(F("SD failed, or not present"));
while (1); // don't do anything more
}
Serial.println("SD OK!");
// list files
printDirectory(SD.open("/"), 0);
// Set volume for left, right channels. lower numbers == louder volume!
musicPlayer.setVolume(lVolume,rVolume);
/***** Two interrupt options! *******/
// This option uses timer0, this means timer1 & t2 are not required
// (so you can use 'em for Servos, etc) BUT millis() can lose time
// since we're hitchhiking on top of the millis() tracker
//musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT);
// This option uses a pin interrupt. No timers required! But DREQ
// must be on an interrupt pin. For Uno/Duemilanove/Diecimilla
// that's Digital #2 or #3
// See http://arduino.cc/en/Reference/attachInterrupt for other pins
// *** This method is preferred
if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT))
Serial.println(F("DREQ pin is not an interrupt pin"));
}
void loop() {
Serial.println("Started Loop");
/*
uint8_t header[44] = {
'R' , 'I' , 'F' , 'F' ,
0x20, 0x20, 0x20, 0x20,
'W' , 'A' , 'V' , 'E' ,
'f' , 'm' , 't' , 0x20,
0x0F, 0x00, 0x00, 0x00,
0x01, 0x00,
0x01, 0x00,
0x44, 0xAC,
0x00, 0x00,
0x44, 0xAC,
0x00, 0x00,
0x01, 0x00,
0x08, 0x00,
'd' , 'a' , 't' , 'a' ,
0x20, 0x20, 0x20, 0x20,
};
*/
musicPlayer.playData(header, 44);
while(true) {
if(!musicPlayer.started) {
if(!musicPlayer.startFile("track001.wav")) {
break;
}
musicPlayer.play();
musicPlayer.play();
}
// VolumeControl();
// if(queryButton(3)) {
// handleInput();
// if(musicPlayer.playingMusic) {
// musicPlayer.play();
// }
while(!musicPlayer.readyForData());
for(uint16_t i = 0; i < 16; i++) {
aBuffer[i] = analogRead(0);
//aBuffer[i] = 0xFFFF;
//Serial.println(aBuffer[i]);
}
Serial.print("A:{ ");
for(int i = 0; i < 16; i++) {
Serial.print(aBuffer[i], HEX);
Serial.print(" ");
}
Serial.print("}");
Serial.print("\n");
for (uint16_t i = 0; i < 16; i++) {
uint16_t t = aBuffer[i];
dBuffer[i * 2] = t & 0xFF;
dBuffer[(i * 2) + 1] = (t >> 8) & 0xFF;
}
Serial.print("D:{ ");
for(int i = 0; i < 32; i += 2) {
Serial.print(dBuffer[i], HEX);
Serial.print(" ");
Serial.print(dBuffer[i+1], HEX);
Serial.print(" ");
}
Serial.print("}");
Serial.print("\n");
musicPlayer.playData(dBuffer, 32);
}
}
void handleInput() {
//Play / Pause
if(buttonD.pressed()) {
if(!musicPlayer.playingMusic) {
musicPlayer.playingMusic = true; //Unpause
}
else {
musicPlayer.playingMusic = false; //Pause
Serial.println("Paused.");
}
}
//Volume
if (buttonA.pressed()) { //A
VolumeDown();
Serial.println("Changing Volume");
Serial.print("Volume is now set to: ");
Serial.println(lVolume);
musicPlayer.setVolume(lVolume,rVolume);
}
timeB = millis();
if (buttonB.pressed()) { //B
VolumeUp();
Serial.println("Changing Volume");
Serial.print("Volume is now set to: ");
Serial.println(lVolume);
musicPlayer.setVolume(lVolume,rVolume);
}
//Skip
}
boolean queryButton(int n) { //Checks the state of a given button
int read = digitalRead(inPin[n]); //Read the physical pin
if(read != state[n]) { //If the state has changed
state[n] = read; //Store the new state
if (state[n] == LOW) { //If the state is high
return true; //Yes, the button is being pressed
}
return false; //Nope
}
return false; //Nope
}
void VolumeDown(){
int MAX = MAXVOLUME;
int MIN = 1;
if( Volume < MAX){ //Lower Volume
//Volume++;
Volume = Volume +5; //use this to increase by a number other than 1
lVolume = Volume;
rVolume = Volume;
}
}
void VolumeUp(){
int MAX = MAXVOLUME;
int MIN = 1;
if(Volume > MIN) {//&& Volume <= MAX){ //Raise health
//Volume--;
Volume = Volume -5; //use this to increase by a number other than 1
lVolume = Volume;
rVolume = Volume;
}
}
//void NextTrack(){
// timeB = millis();
// readingB = digitalRead(inPinB);
//if(readingB != current_stateB)
//{
//current_stateB = readingB;
//if (current_stateB == HIGH)
//{
//VolumeUp();
//Serial.println("Changing Volume");
//Serial.print("Volume is now set to: ");
//Serial.println(lVolume);
//musicPlayer.stopPlaying() ;
//}
//}
//timeB = millis()
//}
/// File listing helper
void printDirectory(File dir, int numTabs) {
while(true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
//Serial.println("**nomorefiles**");
break;
}
for (uint8_t i=0; i<numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs+1);
}
else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}