Music Player
Dependencies: C12832 PinDetect USBHost mbed wave_player
Fork of musicplayer by
main.cpp@0:4b3056d3c684, 2014-03-13 (annotated)
- Committer:
- sarthakjaiswal
- Date:
- Thu Mar 13 00:23:46 2014 +0000
- Revision:
- 0:4b3056d3c684
- Child:
- 1:45d8f6557ff8
Debugged version
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:4b3056d3c684 | 9 | |
sarthakjaiswal | 0:4b3056d3c684 | 10 | DigitalOut myled(LED1); |
sarthakjaiswal | 0:4b3056d3c684 | 11 | DigitalOut myled2(LED2); |
sarthakjaiswal | 0:4b3056d3c684 | 12 | DigitalOut myled3(LED3); |
sarthakjaiswal | 0:4b3056d3c684 | 13 | DigitalOut myled4(LED4); |
sarthakjaiswal | 0:4b3056d3c684 | 14 | |
sarthakjaiswal | 0:4b3056d3c684 | 15 | using namespace std; |
sarthakjaiswal | 0:4b3056d3c684 | 16 | |
sarthakjaiswal | 0:4b3056d3c684 | 17 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card |
sarthakjaiswal | 0:4b3056d3c684 | 18 | TextLCD lcd(p9, p10, p11, p12, p13, p14); // rs, e, d4-d7 |
sarthakjaiswal | 0:4b3056d3c684 | 19 | DigitalIn sdDetect(p17); |
sarthakjaiswal | 0:4b3056d3c684 | 20 | |
sarthakjaiswal | 0:4b3056d3c684 | 21 | PinDetect pb1(p28); // up shift |
sarthakjaiswal | 0:4b3056d3c684 | 22 | PinDetect pb2(p29); // down shift |
sarthakjaiswal | 0:4b3056d3c684 | 23 | PinDetect pb3(p30); // pause |
sarthakjaiswal | 0:4b3056d3c684 | 24 | PinDetect pb4(p27); // volume or play |
sarthakjaiswal | 0:4b3056d3c684 | 25 | |
sarthakjaiswal | 0:4b3056d3c684 | 26 | |
sarthakjaiswal | 0:4b3056d3c684 | 27 | AnalogOut DACout(p18); |
sarthakjaiswal | 0:4b3056d3c684 | 28 | wave_player waver(&DACout); |
sarthakjaiswal | 0:4b3056d3c684 | 29 | int pos = 0; |
sarthakjaiswal | 0:4b3056d3c684 | 30 | int xx = -1; |
sarthakjaiswal | 0:4b3056d3c684 | 31 | int vol = 0; |
sarthakjaiswal | 0:4b3056d3c684 | 32 | //extern "C" { |
sarthakjaiswal | 0:4b3056d3c684 | 33 | //int SD_inserted(); |
sarthakjaiswal | 0:4b3056d3c684 | 34 | //} |
sarthakjaiswal | 0:4b3056d3c684 | 35 | //extern "C" virtual disk_initialize(); |
sarthakjaiswal | 0:4b3056d3c684 | 36 | /*int disk_initialize(); |
sarthakjaiswal | 0:4b3056d3c684 | 37 | }*/ |
sarthakjaiswal | 0:4b3056d3c684 | 38 | bool playing = false; |
sarthakjaiswal | 0:4b3056d3c684 | 39 | bool old_playing = false; |
sarthakjaiswal | 0:4b3056d3c684 | 40 | vector<string> filenames; //filenames are stored in a vector string |
sarthakjaiswal | 0:4b3056d3c684 | 41 | void read_file_names(char *dir) |
sarthakjaiswal | 0:4b3056d3c684 | 42 | { |
sarthakjaiswal | 0:4b3056d3c684 | 43 | DIR *dp; |
sarthakjaiswal | 0:4b3056d3c684 | 44 | struct dirent *dirp; |
sarthakjaiswal | 0:4b3056d3c684 | 45 | dp = opendir(dir); |
sarthakjaiswal | 0:4b3056d3c684 | 46 | //read all directory and file names in current directory into filename vector |
sarthakjaiswal | 0:4b3056d3c684 | 47 | while((dirp = readdir(dp)) != NULL) { |
sarthakjaiswal | 0:4b3056d3c684 | 48 | filenames.push_back(string(dirp->d_name)); |
sarthakjaiswal | 0:4b3056d3c684 | 49 | } |
sarthakjaiswal | 0:4b3056d3c684 | 50 | } |
sarthakjaiswal | 0:4b3056d3c684 | 51 | |
sarthakjaiswal | 0:4b3056d3c684 | 52 | void pb1_hit_callback (void) |
sarthakjaiswal | 0:4b3056d3c684 | 53 | { |
sarthakjaiswal | 0:4b3056d3c684 | 54 | int l = filenames.size(); |
sarthakjaiswal | 0:4b3056d3c684 | 55 | if (pos < (l-1)) { |
sarthakjaiswal | 0:4b3056d3c684 | 56 | pos++; |
sarthakjaiswal | 0:4b3056d3c684 | 57 | } else if (pos == (l-1)) { |
sarthakjaiswal | 0:4b3056d3c684 | 58 | pos = 0; |
sarthakjaiswal | 0:4b3056d3c684 | 59 | } |
sarthakjaiswal | 0:4b3056d3c684 | 60 | string songname = filenames[pos]; |
sarthakjaiswal | 0:4b3056d3c684 | 61 | unsigned index = songname.find(".wav"); |
sarthakjaiswal | 0:4b3056d3c684 | 62 | songname = songname.substr(0,index); |
sarthakjaiswal | 0:4b3056d3c684 | 63 | lcd.cls(); |
sarthakjaiswal | 0:4b3056d3c684 | 64 | lcd.printf(songname.c_str()); |
sarthakjaiswal | 0:4b3056d3c684 | 65 | } |
sarthakjaiswal | 0:4b3056d3c684 | 66 | void pb2_hit_callback (void) |
sarthakjaiswal | 0:4b3056d3c684 | 67 | { |
sarthakjaiswal | 0:4b3056d3c684 | 68 | int l = filenames.size(); |
sarthakjaiswal | 0:4b3056d3c684 | 69 | if (pos > 0) { |
sarthakjaiswal | 0:4b3056d3c684 | 70 | pos--; |
sarthakjaiswal | 0:4b3056d3c684 | 71 | } else if (pos == 0 ) { |
sarthakjaiswal | 0:4b3056d3c684 | 72 | pos = l-1; |
sarthakjaiswal | 0:4b3056d3c684 | 73 | } |
sarthakjaiswal | 0:4b3056d3c684 | 74 | string songname = filenames[pos]; |
sarthakjaiswal | 0:4b3056d3c684 | 75 | unsigned index = songname.find(".wav"); |
sarthakjaiswal | 0:4b3056d3c684 | 76 | songname = songname.substr(0,index); |
sarthakjaiswal | 0:4b3056d3c684 | 77 | lcd.cls(); |
sarthakjaiswal | 0:4b3056d3c684 | 78 | lcd.printf(songname.c_str()); |
sarthakjaiswal | 0:4b3056d3c684 | 79 | } |
sarthakjaiswal | 0:4b3056d3c684 | 80 | |
sarthakjaiswal | 0:4b3056d3c684 | 81 | void pb3_hit_callback (void) |
sarthakjaiswal | 0:4b3056d3c684 | 82 | { |
sarthakjaiswal | 0:4b3056d3c684 | 83 | if (playing == false) { |
sarthakjaiswal | 0:4b3056d3c684 | 84 | playing = true; |
sarthakjaiswal | 0:4b3056d3c684 | 85 | // old_playing = false; |
sarthakjaiswal | 0:4b3056d3c684 | 86 | } else if (playing == true) { |
sarthakjaiswal | 0:4b3056d3c684 | 87 | lcd.cls(); |
sarthakjaiswal | 0:4b3056d3c684 | 88 | playing = false; |
sarthakjaiswal | 0:4b3056d3c684 | 89 | // old_playing = true; |
sarthakjaiswal | 0:4b3056d3c684 | 90 | string songname = filenames[pos]; |
sarthakjaiswal | 0:4b3056d3c684 | 91 | unsigned index = songname.find(".wav"); |
sarthakjaiswal | 0:4b3056d3c684 | 92 | songname = songname.substr(0,index); |
sarthakjaiswal | 0:4b3056d3c684 | 93 | lcd.printf(songname.c_str()); |
sarthakjaiswal | 0:4b3056d3c684 | 94 | } |
sarthakjaiswal | 0:4b3056d3c684 | 95 | } |
sarthakjaiswal | 0:4b3056d3c684 | 96 | void pb4_hit_callback (void){ |
sarthakjaiswal | 0:4b3056d3c684 | 97 | vol = (vol+1) % 16; |
sarthakjaiswal | 0:4b3056d3c684 | 98 | } |
sarthakjaiswal | 0:4b3056d3c684 | 99 | |
sarthakjaiswal | 0:4b3056d3c684 | 100 | int main() |
sarthakjaiswal | 0:4b3056d3c684 | 101 | { |
sarthakjaiswal | 0:4b3056d3c684 | 102 | sdDetect.mode(PullUp); |
sarthakjaiswal | 0:4b3056d3c684 | 103 | wait(.1); |
sarthakjaiswal | 0:4b3056d3c684 | 104 | //wait after pulling up the sd card, |
sarthakjaiswal | 0:4b3056d3c684 | 105 | // read file names into vector of strings |
sarthakjaiswal | 0:4b3056d3c684 | 106 | pb1.mode(PullUp); |
sarthakjaiswal | 0:4b3056d3c684 | 107 | pb2.mode(PullUp); |
sarthakjaiswal | 0:4b3056d3c684 | 108 | pb3.mode(PullUp); |
sarthakjaiswal | 0:4b3056d3c684 | 109 | pb4.mode(PullUp); |
sarthakjaiswal | 0:4b3056d3c684 | 110 | // Delay for initial pullup to take effect |
sarthakjaiswal | 0:4b3056d3c684 | 111 | wait(.01); |
sarthakjaiswal | 0:4b3056d3c684 | 112 | // Setup Interrupt callback functions for a pb hit |
sarthakjaiswal | 0:4b3056d3c684 | 113 | pb1.attach_deasserted(&pb1_hit_callback); |
sarthakjaiswal | 0:4b3056d3c684 | 114 | pb2.attach_deasserted(&pb2_hit_callback); |
sarthakjaiswal | 0:4b3056d3c684 | 115 | pb3.attach_deasserted(&pb3_hit_callback); |
sarthakjaiswal | 0:4b3056d3c684 | 116 | pb4.attach_deasserted(&pb4_hit_callback); |
sarthakjaiswal | 0:4b3056d3c684 | 117 | // Start sampling pb inputs using interrupts |
sarthakjaiswal | 0:4b3056d3c684 | 118 | pb1.setSampleFrequency(); |
sarthakjaiswal | 0:4b3056d3c684 | 119 | pb2.setSampleFrequency(); |
sarthakjaiswal | 0:4b3056d3c684 | 120 | pb3.setSampleFrequency(); |
sarthakjaiswal | 0:4b3056d3c684 | 121 | pb4.setSampleFrequency(); |
sarthakjaiswal | 0:4b3056d3c684 | 122 | lcd.cls(); |
sarthakjaiswal | 0:4b3056d3c684 | 123 | while(sdDetect ==0) { |
sarthakjaiswal | 0:4b3056d3c684 | 124 | lcd.locate(0,0); |
sarthakjaiswal | 0:4b3056d3c684 | 125 | lcd.printf("Insert SD Card"); |
sarthakjaiswal | 0:4b3056d3c684 | 126 | wait(.5); |
sarthakjaiswal | 0:4b3056d3c684 | 127 | } |
sarthakjaiswal | 0:4b3056d3c684 | 128 | //sd.disk_initialize(); |
sarthakjaiswal | 0:4b3056d3c684 | 129 | lcd.cls(); |
sarthakjaiswal | 0:4b3056d3c684 | 130 | //SD_inserted(); |
sarthakjaiswal | 0:4b3056d3c684 | 131 | //lcd.locate(0,0); |
sarthakjaiswal | 0:4b3056d3c684 | 132 | //lcd.printf("Insert SD Card"); |
sarthakjaiswal | 0:4b3056d3c684 | 133 | wait(.5); |
sarthakjaiswal | 0:4b3056d3c684 | 134 | sd.disk_initialize(); |
sarthakjaiswal | 0:4b3056d3c684 | 135 | read_file_names("/sd/Music"); |
sarthakjaiswal | 0:4b3056d3c684 | 136 | while(1) { |
sarthakjaiswal | 0:4b3056d3c684 | 137 | //while pb3 is low, then we can start playing the song |
sarthakjaiswal | 0:4b3056d3c684 | 138 | while(playing == true) { |
sarthakjaiswal | 0:4b3056d3c684 | 139 | string songname = filenames[pos]; |
sarthakjaiswal | 0:4b3056d3c684 | 140 | string a = "/sd/Music/"; |
sarthakjaiswal | 0:4b3056d3c684 | 141 | string fname = a + songname; |
sarthakjaiswal | 0:4b3056d3c684 | 142 | FILE *wave_file; |
sarthakjaiswal | 0:4b3056d3c684 | 143 | lcd.locate(0,1); |
sarthakjaiswal | 0:4b3056d3c684 | 144 | lcd.printf("Now Playing"); |
sarthakjaiswal | 0:4b3056d3c684 | 145 | wave_file = fopen(fname.c_str(),"r"); |
sarthakjaiswal | 0:4b3056d3c684 | 146 | waver.play(wave_file); |
sarthakjaiswal | 0:4b3056d3c684 | 147 | fclose(wave_file); |
sarthakjaiswal | 0:4b3056d3c684 | 148 | } |
sarthakjaiswal | 0:4b3056d3c684 | 149 | } |
sarthakjaiswal | 0:4b3056d3c684 | 150 | } |