Final code

Dependencies:   C12832 PinDetect USBHost mbed wave_player

Fork of MusicPlayer_ThaoLeMinh by Yan Long

Revision:
0:4b3056d3c684
Child:
1:45d8f6557ff8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Mar 13 00:23:46 2014 +0000
@@ -0,0 +1,150 @@
+#include "mbed.h"
+#include "SDFileSystem.h"
+#include "wave_player.h"
+#include "TextLCD.h"
+#include "PinDetect.h"
+#include "Speaker.h"
+#include <vector>
+#include <string>
+ 
+DigitalOut myled(LED1);
+DigitalOut myled2(LED2);
+DigitalOut myled3(LED3);
+DigitalOut myled4(LED4);
+
+using namespace std;
+
+SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
+TextLCD lcd(p9, p10, p11, p12, p13, p14); // rs, e, d4-d7
+DigitalIn sdDetect(p17);
+
+PinDetect pb1(p28); // up shift
+PinDetect pb2(p29); // down shift
+PinDetect pb3(p30); // pause
+PinDetect pb4(p27); // volume or play
+
+
+AnalogOut DACout(p18);
+wave_player waver(&DACout);
+int pos = 0;
+int xx = -1;
+int vol = 0;
+//extern "C" {
+//int SD_inserted();
+//}
+//extern "C" virtual disk_initialize();
+/*int disk_initialize();
+}*/
+bool playing = false;
+bool old_playing = false;
+vector<string> filenames; //filenames are stored in a vector string
+void read_file_names(char *dir)
+{
+    DIR *dp;
+    struct dirent *dirp;
+    dp = opendir(dir);
+    //read all directory and file names in current directory into filename vector
+    while((dirp = readdir(dp)) != NULL) {
+        filenames.push_back(string(dirp->d_name));
+    }
+}
+
+void pb1_hit_callback (void)
+{
+    int l = filenames.size();
+    if (pos < (l-1)) {
+        pos++;
+    } else if (pos == (l-1)) {
+        pos = 0;
+    }
+    string songname = filenames[pos];
+    unsigned index = songname.find(".wav");
+    songname = songname.substr(0,index);
+    lcd.cls();
+    lcd.printf(songname.c_str());
+}
+void pb2_hit_callback (void)
+{
+    int l = filenames.size();
+    if (pos > 0) {
+        pos--;
+    } else if (pos == 0 ) {
+        pos = l-1;
+    }
+    string songname = filenames[pos];
+    unsigned index = songname.find(".wav");
+    songname = songname.substr(0,index);
+    lcd.cls();
+    lcd.printf(songname.c_str());
+}
+
+void pb3_hit_callback (void)
+{
+    if (playing == false) {
+        playing = true;
+     //   old_playing = false;
+    } else if (playing == true) {
+        lcd.cls();
+        playing = false;
+       // old_playing = true;
+        string songname = filenames[pos];
+        unsigned index = songname.find(".wav");
+        songname = songname.substr(0,index);
+        lcd.printf(songname.c_str());
+    }
+}
+void pb4_hit_callback (void){
+    vol = (vol+1) % 16;
+}
+
+int main()
+{
+    sdDetect.mode(PullUp);
+     wait(.1);
+     //wait after pulling up the sd card, 
+    // read file names into vector of strings
+    pb1.mode(PullUp);
+    pb2.mode(PullUp);
+    pb3.mode(PullUp);
+    pb4.mode(PullUp);
+    // Delay for initial pullup to take effect
+    wait(.01);
+    // Setup Interrupt callback functions for a pb hit
+    pb1.attach_deasserted(&pb1_hit_callback);
+    pb2.attach_deasserted(&pb2_hit_callback);
+    pb3.attach_deasserted(&pb3_hit_callback);
+    pb4.attach_deasserted(&pb4_hit_callback);
+    // Start sampling pb inputs using interrupts
+    pb1.setSampleFrequency();
+    pb2.setSampleFrequency();
+    pb3.setSampleFrequency();
+    pb4.setSampleFrequency();
+    lcd.cls();
+    while(sdDetect ==0) {
+        lcd.locate(0,0);
+        lcd.printf("Insert SD Card");
+        wait(.5);
+    }   
+    //sd.disk_initialize();
+    lcd.cls();
+    //SD_inserted();
+    //lcd.locate(0,0);
+    //lcd.printf("Insert SD Card");
+    wait(.5);
+    sd.disk_initialize();
+    read_file_names("/sd/Music");
+    while(1) {
+        //while pb3 is low, then we can start playing the song
+        while(playing == true) {
+            string songname = filenames[pos];
+            string a = "/sd/Music/";
+            string fname = a + songname;
+            FILE *wave_file;
+            lcd.locate(0,1);
+            lcd.printf("Now Playing");
+            wave_file = fopen(fname.c_str(),"r");
+            waver.play(wave_file);
+            fclose(wave_file);
+        }
+    }
+}