/
exp5_music.ino
156 lines (119 loc) · 4.01 KB
/
exp5_music.ino
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// 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>
// 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
#endif
int buttonPin = 13;
int buttonPrev = 1;
int buttonVal;
Adafruit_VS1053_FilePlayer musicPlayer =
Adafruit_VS1053_FilePlayer(VS1053_RESET, VS1053_CS, VS1053_DCS, VS1053_DREQ, CARDCS);
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT);
Serial.println(buttonPin);
// if you're using Bluefruit or LoRa/RFM Feather, disable the BLE interface
//pinMode(8, INPUT_PULLUP);
// 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"));
//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(10,10);
#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
}
void loop() {
buttonVal = digitalRead(buttonPin);
// Play a file in the background, REQUIRES interrupts!
if((buttonVal==1)&&(buttonPrev==0)) {
Serial.println(F("Playing full track 001"));
musicPlayer.playFullFile("track001.mp3");
}
Serial.println(buttonVal);
// Serial.println(F("Playing track 002"));
// musicPlayer.startPlayingFile("track002.mp3");
// Serial.print(".");
// // File is playing in the background
// if (musicPlayer.stopped()) {
// Serial.println("Done playing music");
// while (1) {
// delay(10); // we're done! do nothing...
// }
// }
// if (Serial.available()) {
// char c = Serial.read();
//
// // if we get an 's' on the serial console, stop!
// if (c == 's') {
// musicPlayer.stopPlaying();
// }
//
// // if we get an 'p' on the serial console, pause/unpause!
// if (c == 'p') {
// if (! musicPlayer.paused()) {
// Serial.println("Paused");
// musicPlayer.pausePlaying(true);
// } else {
// Serial.println("Resumed");
// musicPlayer.pausePlaying(false);
// }
// }
// }
delay(100);
buttonPrev = buttonVal; //store the value of this cycle to compare next loop
}
/// 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();
}
}