Yan commited

Dependencies:   C12832 PinDetect USBHost mbed wave_player

Fork of MusicPlayer_ThaoLeMinh by Thao Le Minh

Committer:
sarthakjaiswal
Date:
Mon Mar 17 18:48:13 2014 +0000
Revision:
1:45d8f6557ff8
Parent:
0:4b3056d3c684
Child:
2:087f60c3c445
added comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sarthakjaiswal 0:4b3056d3c684 1 #include "mbed.h"
sarthakjaiswal 0:4b3056d3c684 2 #include "SDFileSystem.h"
sarthakjaiswal 0:4b3056d3c684 3 #include "wave_player.h"
sarthakjaiswal 0:4b3056d3c684 4 #include "TextLCD.h"
sarthakjaiswal 0:4b3056d3c684 5 #include "PinDetect.h"
sarthakjaiswal 0:4b3056d3c684 6 #include "Speaker.h"
sarthakjaiswal 0:4b3056d3c684 7 #include <vector>
sarthakjaiswal 0:4b3056d3c684 8 #include <string>
sarthakjaiswal 1:45d8f6557ff8 9
sarthakjaiswal 1:45d8f6557ff8 10 //Set up LEDs
sarthakjaiswal 0:4b3056d3c684 11 DigitalOut myled(LED1);
sarthakjaiswal 0:4b3056d3c684 12 DigitalOut myled2(LED2);
sarthakjaiswal 0:4b3056d3c684 13 DigitalOut myled3(LED3);
sarthakjaiswal 0:4b3056d3c684 14 DigitalOut myled4(LED4);
sarthakjaiswal 0:4b3056d3c684 15
sarthakjaiswal 0:4b3056d3c684 16 using namespace std;
sarthakjaiswal 0:4b3056d3c684 17
sarthakjaiswal 0:4b3056d3c684 18 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
sarthakjaiswal 0:4b3056d3c684 19 TextLCD lcd(p9, p10, p11, p12, p13, p14); // rs, e, d4-d7
sarthakjaiswal 1:45d8f6557ff8 20 DigitalIn sdDetect(p17); // Set up a pin for SD Card Detect
sarthakjaiswal 0:4b3056d3c684 21
sarthakjaiswal 1:45d8f6557ff8 22 PinDetect pb1(p28); // pb forup shift
sarthakjaiswal 1:45d8f6557ff8 23 PinDetect pb2(p29); // pb fordown shift
sarthakjaiswal 1:45d8f6557ff8 24 PinDetect pb3(p30); // pb for pause
sarthakjaiswal 1:45d8f6557ff8 25 PinDetect pb4(p27); // pb for volume or play
sarthakjaiswal 0:4b3056d3c684 26
sarthakjaiswal 0:4b3056d3c684 27
sarthakjaiswal 1:45d8f6557ff8 28 AnalogOut DACout(p18); //set up speaker
sarthakjaiswal 1:45d8f6557ff8 29 wave_player waver(&DACout); //set up wave player library
sarthakjaiswal 1:45d8f6557ff8 30 int pos = 0; // index of the song
sarthakjaiswal 1:45d8f6557ff8 31 int vol = 0; // volume controller
sarthakjaiswal 1:45d8f6557ff8 32
sarthakjaiswal 1:45d8f6557ff8 33 bool playing = false; //variable for pause/play since we only have 1 pb for that
sarthakjaiswal 0:4b3056d3c684 34 vector<string> filenames; //filenames are stored in a vector string
sarthakjaiswal 1:45d8f6557ff8 35 void read_file_names(char *dir) // function that reads in file names from sd cards
sarthakjaiswal 0:4b3056d3c684 36 {
sarthakjaiswal 0:4b3056d3c684 37 DIR *dp;
sarthakjaiswal 0:4b3056d3c684 38 struct dirent *dirp;
sarthakjaiswal 0:4b3056d3c684 39 dp = opendir(dir);
sarthakjaiswal 0:4b3056d3c684 40 //read all directory and file names in current directory into filename vector
sarthakjaiswal 0:4b3056d3c684 41 while((dirp = readdir(dp)) != NULL) {
sarthakjaiswal 0:4b3056d3c684 42 filenames.push_back(string(dirp->d_name));
sarthakjaiswal 0:4b3056d3c684 43 }
sarthakjaiswal 0:4b3056d3c684 44 }
sarthakjaiswal 1:45d8f6557ff8 45 //interrupt handler for pb1
sarthakjaiswal 0:4b3056d3c684 46 void pb1_hit_callback (void)
sarthakjaiswal 0:4b3056d3c684 47 {
sarthakjaiswal 1:45d8f6557ff8 48 // it checks for the total number of songs in the sd card..then increments the index until it reaches the last one, then resets to 0
sarthakjaiswal 0:4b3056d3c684 49 int l = filenames.size();
sarthakjaiswal 0:4b3056d3c684 50 if (pos < (l-1)) {
sarthakjaiswal 0:4b3056d3c684 51 pos++;
sarthakjaiswal 0:4b3056d3c684 52 } else if (pos == (l-1)) {
sarthakjaiswal 0:4b3056d3c684 53 pos = 0;
sarthakjaiswal 0:4b3056d3c684 54 }
sarthakjaiswal 0:4b3056d3c684 55 string songname = filenames[pos];
sarthakjaiswal 0:4b3056d3c684 56 unsigned index = songname.find(".wav");
sarthakjaiswal 0:4b3056d3c684 57 songname = songname.substr(0,index);
sarthakjaiswal 0:4b3056d3c684 58 lcd.cls();
sarthakjaiswal 1:45d8f6557ff8 59 lcd.printf(songname.c_str()); //it clears screen and then sets the new index song in the lcd display
sarthakjaiswal 0:4b3056d3c684 60 }
sarthakjaiswal 1:45d8f6557ff8 61 //interrupt handler for pb2
sarthakjaiswal 0:4b3056d3c684 62 void pb2_hit_callback (void)
sarthakjaiswal 0:4b3056d3c684 63 {
sarthakjaiswal 1:45d8f6557ff8 64 //does opposite of pb1..moves the index down..from 2nd song to 1st song via changing the index number and then calling in the vector
sarthakjaiswal 0:4b3056d3c684 65 int l = filenames.size();
sarthakjaiswal 0:4b3056d3c684 66 if (pos > 0) {
sarthakjaiswal 0:4b3056d3c684 67 pos--;
sarthakjaiswal 0:4b3056d3c684 68 } else if (pos == 0 ) {
sarthakjaiswal 0:4b3056d3c684 69 pos = l-1;
sarthakjaiswal 0:4b3056d3c684 70 }
sarthakjaiswal 0:4b3056d3c684 71 string songname = filenames[pos];
sarthakjaiswal 0:4b3056d3c684 72 unsigned index = songname.find(".wav");
sarthakjaiswal 0:4b3056d3c684 73 songname = songname.substr(0,index);
sarthakjaiswal 0:4b3056d3c684 74 lcd.cls();
sarthakjaiswal 0:4b3056d3c684 75 lcd.printf(songname.c_str());
sarthakjaiswal 0:4b3056d3c684 76 }
sarthakjaiswal 1:45d8f6557ff8 77 //interrupt handler for 3rd pushbutton
sarthakjaiswal 0:4b3056d3c684 78 void pb3_hit_callback (void)
sarthakjaiswal 0:4b3056d3c684 79 {
sarthakjaiswal 1:45d8f6557ff8 80 //this interrupt handler changes the play to pause mode or vice versa
sarthakjaiswal 1:45d8f6557ff8 81 //this is done using the boolean playing
sarthakjaiswal 0:4b3056d3c684 82 if (playing == false) {
sarthakjaiswal 1:45d8f6557ff8 83 playing = true;
sarthakjaiswal 0:4b3056d3c684 84 } else if (playing == true) {
sarthakjaiswal 0:4b3056d3c684 85 lcd.cls();
sarthakjaiswal 1:45d8f6557ff8 86 playing = false;
sarthakjaiswal 0:4b3056d3c684 87 string songname = filenames[pos];
sarthakjaiswal 0:4b3056d3c684 88 unsigned index = songname.find(".wav");
sarthakjaiswal 0:4b3056d3c684 89 songname = songname.substr(0,index);
sarthakjaiswal 0:4b3056d3c684 90 lcd.printf(songname.c_str());
sarthakjaiswal 0:4b3056d3c684 91 }
sarthakjaiswal 0:4b3056d3c684 92 }
sarthakjaiswal 1:45d8f6557ff8 93 //interrupt handler for pb4
sarthakjaiswal 0:4b3056d3c684 94 void pb4_hit_callback (void){
sarthakjaiswal 1:45d8f6557ff8 95 // this pb changes the volume by lowering the volume until it reaches 0. then it resets to the max volume
sarthakjaiswal 1:45d8f6557ff8 96 // the volume range has been divided into 16 possible ranges. and hence, it toggles through those 16 values
sarthakjaiswal 1:45d8f6557ff8 97 // this only changes the variable vol, which is then used in the wave player file to actually adjust the volume
sarthakjaiswal 0:4b3056d3c684 98 vol = (vol+1) % 16;
sarthakjaiswal 0:4b3056d3c684 99 }
sarthakjaiswal 0:4b3056d3c684 100
sarthakjaiswal 0:4b3056d3c684 101 int main()
sarthakjaiswal 0:4b3056d3c684 102 {
sarthakjaiswal 0:4b3056d3c684 103 sdDetect.mode(PullUp);
sarthakjaiswal 0:4b3056d3c684 104 wait(.1);
sarthakjaiswal 0:4b3056d3c684 105 //wait after pulling up the sd card,
sarthakjaiswal 0:4b3056d3c684 106 // read file names into vector of strings
sarthakjaiswal 0:4b3056d3c684 107 pb1.mode(PullUp);
sarthakjaiswal 0:4b3056d3c684 108 pb2.mode(PullUp);
sarthakjaiswal 0:4b3056d3c684 109 pb3.mode(PullUp);
sarthakjaiswal 0:4b3056d3c684 110 pb4.mode(PullUp);
sarthakjaiswal 0:4b3056d3c684 111 // Delay for initial pullup to take effect
sarthakjaiswal 0:4b3056d3c684 112 wait(.01);
sarthakjaiswal 0:4b3056d3c684 113 // Setup Interrupt callback functions for a pb hit
sarthakjaiswal 0:4b3056d3c684 114 pb1.attach_deasserted(&pb1_hit_callback);
sarthakjaiswal 0:4b3056d3c684 115 pb2.attach_deasserted(&pb2_hit_callback);
sarthakjaiswal 0:4b3056d3c684 116 pb3.attach_deasserted(&pb3_hit_callback);
sarthakjaiswal 0:4b3056d3c684 117 pb4.attach_deasserted(&pb4_hit_callback);
sarthakjaiswal 0:4b3056d3c684 118 // Start sampling pb inputs using interrupts
sarthakjaiswal 0:4b3056d3c684 119 pb1.setSampleFrequency();
sarthakjaiswal 0:4b3056d3c684 120 pb2.setSampleFrequency();
sarthakjaiswal 0:4b3056d3c684 121 pb3.setSampleFrequency();
sarthakjaiswal 0:4b3056d3c684 122 pb4.setSampleFrequency();
sarthakjaiswal 0:4b3056d3c684 123 lcd.cls();
sarthakjaiswal 1:45d8f6557ff8 124 //detects whethere there is a SD card or not.. if not then it prints and informs the user
sarthakjaiswal 0:4b3056d3c684 125 while(sdDetect ==0) {
sarthakjaiswal 0:4b3056d3c684 126 lcd.locate(0,0);
sarthakjaiswal 0:4b3056d3c684 127 lcd.printf("Insert SD Card");
sarthakjaiswal 0:4b3056d3c684 128 wait(.5);
sarthakjaiswal 1:45d8f6557ff8 129 }
sarthakjaiswal 1:45d8f6557ff8 130 lcd.cls();
sarthakjaiswal 0:4b3056d3c684 131 wait(.5);
sarthakjaiswal 0:4b3056d3c684 132 sd.disk_initialize();
sarthakjaiswal 0:4b3056d3c684 133 read_file_names("/sd/Music");
sarthakjaiswal 0:4b3056d3c684 134 while(1) {
sarthakjaiswal 0:4b3056d3c684 135 //while pb3 is low, then we can start playing the song
sarthakjaiswal 1:45d8f6557ff8 136 while(playing == true) { //we have 2 while loops..one while loop makes sure the music player is always on, the other one is for the song
sarthakjaiswal 0:4b3056d3c684 137 string songname = filenames[pos];
sarthakjaiswal 0:4b3056d3c684 138 string a = "/sd/Music/";
sarthakjaiswal 1:45d8f6557ff8 139 string fname = a + songname; //retrieves the file name
sarthakjaiswal 1:45d8f6557ff8 140 FILE *wave_file;
sarthakjaiswal 0:4b3056d3c684 141 lcd.locate(0,1);
sarthakjaiswal 0:4b3056d3c684 142 lcd.printf("Now Playing");
sarthakjaiswal 1:45d8f6557ff8 143 wave_file = fopen(fname.c_str(),"r"); //opens the music file
sarthakjaiswal 1:45d8f6557ff8 144 waver.play(wave_file); //plays the music file
sarthakjaiswal 0:4b3056d3c684 145 fclose(wave_file);
sarthakjaiswal 0:4b3056d3c684 146 }
sarthakjaiswal 0:4b3056d3c684 147 }
sarthakjaiswal 0:4b3056d3c684 148 }