Angus MacLean / Mbed 2 deprecated Project_MP

Dependencies:   4DGL-uLCD-SE PinDetect SDFileSystem Speaker mbed wave_player

Fork of musicplayer by Sarthak Jaiswal

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SDFileSystem.h"
00003 #include "wave_player.h"
00004 #include "uLCD_4DGL.h"
00005 #include "PinDetect.h"
00006 #include "Speaker.h"
00007 #include <vector>
00008 #include <string>
00009 
00010 //Set up LEDs
00011 DigitalOut myled(LED1);
00012 DigitalOut myled2(LED2);
00013 DigitalOut myled3(LED3);
00014 DigitalOut myled4(LED4);
00015 
00016 using namespace std;
00017 
00018 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
00019 uLCD_4DGL uLCD(p9,p10,p11);
00020 DigitalIn sdDetect(p17); // Set up a pin for SD Card Detect
00021 
00022 PinDetect pb1(p21); // pb forup shift
00023 PinDetect pb2(p22); // pb fordown shift
00024 PinDetect pb3(p23); // pb for pause
00025 
00026 
00027 
00028 
00029 AnalogOut DACout(p18); //set up speaker
00030 wave_player waver(&DACout); //set up wave player library
00031 int pos = 0; // index of the song
00032 int vol = 0;
00033 
00034  
00035 bool playing = false; //variable for pause/play since we only have 1 pb for that
00036 vector<string> filenames; //filenames are stored in a vector string
00037 void read_file_names(char *dir) // function that reads in file names from sd cards
00038 {
00039     DIR *dp;
00040     struct dirent *dirp;
00041     dp = opendir(dir);
00042     //read all directory and file names in current directory into filename vector
00043     while((dirp = readdir(dp)) != NULL) {
00044         filenames.push_back(string(dirp->d_name));
00045     }
00046 }
00047 //interrupt handler for pb1 
00048 void pb1_hit_callback (void)
00049 {
00050     // 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
00051     int l = filenames.size();
00052     if (pos < (l-1)) {
00053         pos++;
00054     } else if (pos == (l-1)) {
00055         pos = 0;
00056     }
00057     string songname = filenames[pos];
00058     unsigned index = songname.find(".wav");
00059     songname = songname.substr(0,index);
00060     uLCD.cls();
00061     uLCD.printf(songname.c_str()); //it clears screen and then sets the new index song in the uLCD display
00062 }
00063 //interrupt handler for pb2
00064 void pb2_hit_callback (void)
00065 {
00066     //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
00067     int l = filenames.size();
00068     if (pos > 0) {
00069         pos--;
00070     } else if (pos == 0 ) {
00071         pos = l-1;
00072     }
00073     string songname = filenames[pos];
00074     unsigned index = songname.find(".wav");
00075     songname = songname.substr(0,index);
00076     uLCD.cls();
00077     uLCD.printf(songname.c_str());
00078 }
00079 //interrupt handler for 3rd pushbutton
00080 void pb3_hit_callback (void)
00081 {
00082     //this interrupt handler changes the play to pause mode or vice versa
00083     //this is done using the boolean playing
00084     if (playing == false) {
00085         playing = true; 
00086     } else if (playing == true) {
00087         uLCD.cls();
00088         playing = false; 
00089         string songname = filenames[pos];
00090         unsigned index = songname.find(".wav");
00091         songname = songname.substr(0,index);
00092         uLCD.printf(songname.c_str());
00093     }
00094 }
00095 
00096 
00097 int main()
00098 {
00099     sdDetect.mode(PullUp);
00100      wait(.1);
00101      //wait after pulling up the sd card, 
00102     // read file names into vector of strings
00103     pb1.mode(PullUp);
00104     pb2.mode(PullUp);
00105     pb3.mode(PullUp);
00106     //pb4.mode(PullUp);
00107     // Delay for initial pullup to take effect
00108     wait(.01);
00109     // Setup Interrupt callback functions for a pb hit
00110     pb1.attach_deasserted(&pb1_hit_callback);
00111     pb2.attach_deasserted(&pb2_hit_callback);
00112     pb3.attach_deasserted(&pb3_hit_callback);
00113     
00114     // Start sampling pb inputs using interrupts
00115     pb1.setSampleFrequency();
00116     pb2.setSampleFrequency();
00117     pb3.setSampleFrequency();
00118     
00119     uLCD.cls();
00120     //detects whethere there is a SD card or not.. if not then it prints and informs the user
00121     while(sdDetect ==0) {
00122         uLCD.locate(0,0);
00123         uLCD.printf("Insert SD Card");
00124         wait(.5);
00125     }    
00126     uLCD.cls(); 
00127     wait(.5);
00128     sd.disk_initialize();
00129     read_file_names("/sd/");
00130     while(1) {
00131         //while pb3 is low, then we can start playing the song
00132         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
00133             string songname = filenames[pos];
00134             string a = "/sd/";
00135             string fname = a + songname; //retrieves the file name
00136             FILE *wave_file; 
00137             uLCD.locate(0,4);
00138             uLCD.printf("Now Playing");
00139             wave_file = fopen(fname.c_str(),"r"); //opens the music file
00140             waver.play(wave_file); //plays the music file
00141             fclose(wave_file);
00142         }
00143     }
00144 }