Final code
Dependencies: C12832 PinDetect USBHost mbed wave_player
Fork of MusicPlayer_ThaoLeMinh by
main.cpp@3:82b68ab94e3e, 2016-12-13 (annotated)
- Committer:
- LyCosine
- Date:
- Tue Dec 13 10:00:51 2016 +0000
- Revision:
- 3:82b68ab94e3e
- Parent:
- 2:087f60c3c445
- Child:
- 4:314af6e95daf
Yan revised
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 "wave_player.h" |
sarthakjaiswal | 0:4b3056d3c684 | 3 | #include "PinDetect.h" |
thaolmk54 | 2:087f60c3c445 | 4 | #include "USBHostMSD.h" |
thaolmk54 | 2:087f60c3c445 | 5 | #include "C12832.h" |
LyCosine | 3:82b68ab94e3e | 6 | #include "ReadTime.h" |
sarthakjaiswal | 0:4b3056d3c684 | 7 | #include <vector> |
sarthakjaiswal | 0:4b3056d3c684 | 8 | #include <string> |
sarthakjaiswal | 1:45d8f6557ff8 | 9 | |
sarthakjaiswal | 1:45d8f6557ff8 | 10 | //Set up LEDs |
thaolmk54 | 2:087f60c3c445 | 11 | DigitalOut led1( LED1 ); |
thaolmk54 | 2:087f60c3c445 | 12 | DigitalOut led2( LED2 ); |
thaolmk54 | 2:087f60c3c445 | 13 | DigitalOut led3( LED3 ); |
thaolmk54 | 2:087f60c3c445 | 14 | DigitalOut led4( LED4 ); |
thaolmk54 | 2:087f60c3c445 | 15 | |
thaolmk54 | 2:087f60c3c445 | 16 | //Setup RGB led |
thaolmk54 | 2:087f60c3c445 | 17 | PwmOut r (p23); //RGB LED pins |
thaolmk54 | 2:087f60c3c445 | 18 | PwmOut g (p24); |
thaolmk54 | 2:087f60c3c445 | 19 | PwmOut b (p25); |
sarthakjaiswal | 0:4b3056d3c684 | 20 | |
sarthakjaiswal | 0:4b3056d3c684 | 21 | using namespace std; |
sarthakjaiswal | 0:4b3056d3c684 | 22 | |
thaolmk54 | 2:087f60c3c445 | 23 | C12832 lcd(p5, p7, p6, p8, p11); |
sarthakjaiswal | 0:4b3056d3c684 | 24 | |
thaolmk54 | 2:087f60c3c445 | 25 | //Joystick controller |
thaolmk54 | 2:087f60c3c445 | 26 | PinDetect pb1(p13);//joyleft |
thaolmk54 | 2:087f60c3c445 | 27 | PinDetect pb2(p16);//joyright |
thaolmk54 | 2:087f60c3c445 | 28 | PinDetect pb3(p12); //joyup |
thaolmk54 | 2:087f60c3c445 | 29 | PinDetect pb4(p15); //joydown |
thaolmk54 | 2:087f60c3c445 | 30 | PinDetect pb5(p14);//center |
sarthakjaiswal | 0:4b3056d3c684 | 31 | |
LyCosine | 3:82b68ab94e3e | 32 | Timer timer; //timer |
LyCosine | 3:82b68ab94e3e | 33 | |
sarthakjaiswal | 1:45d8f6557ff8 | 34 | AnalogOut DACout(p18); //set up speaker |
sarthakjaiswal | 1:45d8f6557ff8 | 35 | wave_player waver(&DACout); //set up wave player library |
sarthakjaiswal | 1:45d8f6557ff8 | 36 | int pos = 0; // index of the song |
sarthakjaiswal | 1:45d8f6557ff8 | 37 | int vol = 0; // volume controller |
sarthakjaiswal | 1:45d8f6557ff8 | 38 | |
thaolmk54 | 2:087f60c3c445 | 39 | bool playing = false; //variable for pause/play |
thaolmk54 | 2:087f60c3c445 | 40 | bool firstplay = false; //variable for first play |
LyCosine | 3:82b68ab94e3e | 41 | int total_time = 0; |
sarthakjaiswal | 0:4b3056d3c684 | 42 | vector<string> filenames; //filenames are stored in a vector string |
thaolmk54 | 2:087f60c3c445 | 43 | |
sarthakjaiswal | 1:45d8f6557ff8 | 44 | void read_file_names(char *dir) // function that reads in file names from sd cards |
sarthakjaiswal | 0:4b3056d3c684 | 45 | { |
sarthakjaiswal | 0:4b3056d3c684 | 46 | DIR *dp; |
sarthakjaiswal | 0:4b3056d3c684 | 47 | struct dirent *dirp; |
sarthakjaiswal | 0:4b3056d3c684 | 48 | dp = opendir(dir); |
sarthakjaiswal | 0:4b3056d3c684 | 49 | //read all directory and file names in current directory into filename vector |
sarthakjaiswal | 0:4b3056d3c684 | 50 | while((dirp = readdir(dp)) != NULL) { |
sarthakjaiswal | 0:4b3056d3c684 | 51 | filenames.push_back(string(dirp->d_name)); |
sarthakjaiswal | 0:4b3056d3c684 | 52 | } |
sarthakjaiswal | 0:4b3056d3c684 | 53 | } |
thaolmk54 | 2:087f60c3c445 | 54 | |
sarthakjaiswal | 1:45d8f6557ff8 | 55 | //interrupt handler for pb1 |
sarthakjaiswal | 0:4b3056d3c684 | 56 | void pb1_hit_callback (void) |
thaolmk54 | 2:087f60c3c445 | 57 | { |
sarthakjaiswal | 0:4b3056d3c684 | 58 | int l = filenames.size(); |
sarthakjaiswal | 0:4b3056d3c684 | 59 | if (pos < (l-1)) { |
sarthakjaiswal | 0:4b3056d3c684 | 60 | pos++; |
sarthakjaiswal | 0:4b3056d3c684 | 61 | } else if (pos == (l-1)) { |
sarthakjaiswal | 0:4b3056d3c684 | 62 | pos = 0; |
sarthakjaiswal | 0:4b3056d3c684 | 63 | } |
thaolmk54 | 2:087f60c3c445 | 64 | led1 = 1; |
thaolmk54 | 2:087f60c3c445 | 65 | led2 = 0; |
thaolmk54 | 2:087f60c3c445 | 66 | led3 = 0; |
thaolmk54 | 2:087f60c3c445 | 67 | led4 = 0; |
sarthakjaiswal | 0:4b3056d3c684 | 68 | } |
sarthakjaiswal | 1:45d8f6557ff8 | 69 | //interrupt handler for pb2 |
sarthakjaiswal | 0:4b3056d3c684 | 70 | void pb2_hit_callback (void) |
sarthakjaiswal | 0:4b3056d3c684 | 71 | { |
sarthakjaiswal | 0:4b3056d3c684 | 72 | int l = filenames.size(); |
sarthakjaiswal | 0:4b3056d3c684 | 73 | if (pos > 0) { |
sarthakjaiswal | 0:4b3056d3c684 | 74 | pos--; |
sarthakjaiswal | 0:4b3056d3c684 | 75 | } else if (pos == 0 ) { |
sarthakjaiswal | 0:4b3056d3c684 | 76 | pos = l-1; |
sarthakjaiswal | 0:4b3056d3c684 | 77 | } |
thaolmk54 | 2:087f60c3c445 | 78 | led1 = 0; |
thaolmk54 | 2:087f60c3c445 | 79 | led2 = 1; |
thaolmk54 | 2:087f60c3c445 | 80 | led3 = 0; |
thaolmk54 | 2:087f60c3c445 | 81 | led4 = 0; |
thaolmk54 | 2:087f60c3c445 | 82 | } |
thaolmk54 | 2:087f60c3c445 | 83 | |
thaolmk54 | 2:087f60c3c445 | 84 | //interrupt handler for pb3 |
thaolmk54 | 2:087f60c3c445 | 85 | void pb3_hit_callback (void){ |
thaolmk54 | 2:087f60c3c445 | 86 | vol = (vol+1) % 16; |
thaolmk54 | 2:087f60c3c445 | 87 | led1 = 0; |
thaolmk54 | 2:087f60c3c445 | 88 | led2 = 0; |
thaolmk54 | 2:087f60c3c445 | 89 | led3 = 1; |
thaolmk54 | 2:087f60c3c445 | 90 | led4 = 0; |
sarthakjaiswal | 0:4b3056d3c684 | 91 | } |
thaolmk54 | 2:087f60c3c445 | 92 | |
thaolmk54 | 2:087f60c3c445 | 93 | //interrupt handler for pb4 |
thaolmk54 | 2:087f60c3c445 | 94 | void pb4_hit_callback (void){ |
thaolmk54 | 2:087f60c3c445 | 95 | if (vol > 1) { |
thaolmk54 | 2:087f60c3c445 | 96 | vol = (vol-1) % 16; |
thaolmk54 | 2:087f60c3c445 | 97 | } |
thaolmk54 | 2:087f60c3c445 | 98 | led1 = 0; |
thaolmk54 | 2:087f60c3c445 | 99 | led2 = 0; |
thaolmk54 | 2:087f60c3c445 | 100 | led3 = 0; |
thaolmk54 | 2:087f60c3c445 | 101 | led4 = 1; |
thaolmk54 | 2:087f60c3c445 | 102 | } |
thaolmk54 | 2:087f60c3c445 | 103 | |
sarthakjaiswal | 1:45d8f6557ff8 | 104 | //interrupt handler for 3rd pushbutton |
thaolmk54 | 2:087f60c3c445 | 105 | void pb5_hit_callback (void) |
sarthakjaiswal | 0:4b3056d3c684 | 106 | { |
sarthakjaiswal | 1:45d8f6557ff8 | 107 | //this interrupt handler changes the play to pause mode or vice versa |
thaolmk54 | 2:087f60c3c445 | 108 | if (playing == false && firstplay == false) { |
sarthakjaiswal | 1:45d8f6557ff8 | 109 | playing = true; |
thaolmk54 | 2:087f60c3c445 | 110 | firstplay = true; |
thaolmk54 | 2:087f60c3c445 | 111 | r = 1; |
sarthakjaiswal | 0:4b3056d3c684 | 112 | } else if (playing == true) { |
sarthakjaiswal | 0:4b3056d3c684 | 113 | string songname = filenames[pos]; |
thaolmk54 | 2:087f60c3c445 | 114 | playing = false; |
thaolmk54 | 2:087f60c3c445 | 115 | firstplay = false; |
thaolmk54 | 2:087f60c3c445 | 116 | g = 1; |
sarthakjaiswal | 0:4b3056d3c684 | 117 | } |
sarthakjaiswal | 0:4b3056d3c684 | 118 | } |
thaolmk54 | 2:087f60c3c445 | 119 | |
LyCosine | 3:82b68ab94e3e | 120 | void show_information(const void *argv) { |
LyCosine | 3:82b68ab94e3e | 121 | while (1) { |
LyCosine | 3:82b68ab94e3e | 122 | int t = timer.read();//playing time |
LyCosine | 3:82b68ab94e3e | 123 | int left_time = total_time - t;//left time |
LyCosine | 3:82b68ab94e3e | 124 | int bar_len = 21 * t / total_time;//length of bar |
LyCosine | 3:82b68ab94e3e | 125 | lcd.cls(); |
LyCosine | 3:82b68ab94e3e | 126 | lcd.locate(0,0); |
LyCosine | 3:82b68ab94e3e | 127 | |
LyCosine | 3:82b68ab94e3e | 128 | lcd.locate(100,0); |
LyCosine | 3:82b68ab94e3e | 129 | lcd.printf("vol: %d", 15 - vol); |
LyCosine | 3:82b68ab94e3e | 130 | lcd.locate(0,20); |
LyCosine | 3:82b68ab94e3e | 131 | for (int i = 0; i < bar_len; i++) { |
LyCosine | 3:82b68ab94e3e | 132 | lcd.printf("_"); |
LyCosine | 3:82b68ab94e3e | 133 | } |
LyCosine | 3:82b68ab94e3e | 134 | int min1 = t / 60; |
LyCosine | 3:82b68ab94e3e | 135 | int sec1 = t % 60; |
LyCosine | 3:82b68ab94e3e | 136 | int min2 = left_time / 60; |
LyCosine | 3:82b68ab94e3e | 137 | int sec2 = left_time % 60; |
LyCosine | 3:82b68ab94e3e | 138 | lcd.locate(0,18); |
LyCosine | 3:82b68ab94e3e | 139 | lcd.printf("%02d:%02d", min1, sec1); |
LyCosine | 3:82b68ab94e3e | 140 | lcd.locate(105,18); |
LyCosine | 3:82b68ab94e3e | 141 | lcd.printf("%02d:%02d", min2, sec2); |
LyCosine | 3:82b68ab94e3e | 142 | Thread::wait(500); |
LyCosine | 3:82b68ab94e3e | 143 | } |
LyCosine | 3:82b68ab94e3e | 144 | } |
LyCosine | 3:82b68ab94e3e | 145 | |
sarthakjaiswal | 0:4b3056d3c684 | 146 | |
sarthakjaiswal | 0:4b3056d3c684 | 147 | int main() |
sarthakjaiswal | 0:4b3056d3c684 | 148 | { |
thaolmk54 | 2:087f60c3c445 | 149 | //test LCD display |
thaolmk54 | 2:087f60c3c445 | 150 | lcd.cls(); |
thaolmk54 | 2:087f60c3c445 | 151 | lcd.locate(0,3); |
thaolmk54 | 2:087f60c3c445 | 152 | lcd.printf("MBED Music Player"); |
thaolmk54 | 2:087f60c3c445 | 153 | |
thaolmk54 | 2:087f60c3c445 | 154 | pb1.mode(PullDown); |
thaolmk54 | 2:087f60c3c445 | 155 | pb2.mode(PullDown); |
thaolmk54 | 2:087f60c3c445 | 156 | pb3.mode(PullDown); |
thaolmk54 | 2:087f60c3c445 | 157 | pb4.mode(PullDown); |
thaolmk54 | 2:087f60c3c445 | 158 | pb5.mode(PullDown); |
thaolmk54 | 2:087f60c3c445 | 159 | |
sarthakjaiswal | 0:4b3056d3c684 | 160 | wait(.01); |
sarthakjaiswal | 0:4b3056d3c684 | 161 | // Setup Interrupt callback functions for a pb hit |
sarthakjaiswal | 0:4b3056d3c684 | 162 | pb1.attach_deasserted(&pb1_hit_callback); |
sarthakjaiswal | 0:4b3056d3c684 | 163 | pb2.attach_deasserted(&pb2_hit_callback); |
sarthakjaiswal | 0:4b3056d3c684 | 164 | pb3.attach_deasserted(&pb3_hit_callback); |
sarthakjaiswal | 0:4b3056d3c684 | 165 | pb4.attach_deasserted(&pb4_hit_callback); |
thaolmk54 | 2:087f60c3c445 | 166 | pb5.attach_deasserted(&pb5_hit_callback); |
sarthakjaiswal | 0:4b3056d3c684 | 167 | // Start sampling pb inputs using interrupts |
sarthakjaiswal | 0:4b3056d3c684 | 168 | pb1.setSampleFrequency(); |
sarthakjaiswal | 0:4b3056d3c684 | 169 | pb2.setSampleFrequency(); |
sarthakjaiswal | 0:4b3056d3c684 | 170 | pb3.setSampleFrequency(); |
sarthakjaiswal | 0:4b3056d3c684 | 171 | pb4.setSampleFrequency(); |
thaolmk54 | 2:087f60c3c445 | 172 | pb5.setSampleFrequency(); |
thaolmk54 | 2:087f60c3c445 | 173 | |
sarthakjaiswal | 0:4b3056d3c684 | 174 | lcd.cls(); |
thaolmk54 | 2:087f60c3c445 | 175 | USBHostMSD msc("msc"); |
thaolmk54 | 2:087f60c3c445 | 176 | // Check if a USB is connected |
thaolmk54 | 2:087f60c3c445 | 177 | while(!msc.connect()) { |
sarthakjaiswal | 0:4b3056d3c684 | 178 | lcd.locate(0,0); |
thaolmk54 | 2:087f60c3c445 | 179 | lcd.printf("Insert USB"); |
sarthakjaiswal | 1:45d8f6557ff8 | 180 | } |
thaolmk54 | 2:087f60c3c445 | 181 | // Read the songs array, please note that your wav files have to be stored in the same directory |
thaolmk54 | 2:087f60c3c445 | 182 | read_file_names("/msc/music_wav"); |
thaolmk54 | 2:087f60c3c445 | 183 | |
thaolmk54 | 2:087f60c3c445 | 184 | while(1) { |
thaolmk54 | 2:087f60c3c445 | 185 | lcd.cls(); |
thaolmk54 | 2:087f60c3c445 | 186 | lcd.locate(0,2); |
thaolmk54 | 2:087f60c3c445 | 187 | lcd.printf("Press joystick to play"); |
thaolmk54 | 2:087f60c3c445 | 188 | //while pb3 is low, press fire button to start playing a song |
thaolmk54 | 2:087f60c3c445 | 189 | while(playing == true && firstplay == false) { |
sarthakjaiswal | 0:4b3056d3c684 | 190 | string songname = filenames[pos]; |
thaolmk54 | 2:087f60c3c445 | 191 | string a = "/msc/music_wav/"; |
sarthakjaiswal | 1:45d8f6557ff8 | 192 | string fname = a + songname; //retrieves the file name |
sarthakjaiswal | 1:45d8f6557ff8 | 193 | FILE *wave_file; |
thaolmk54 | 2:087f60c3c445 | 194 | lcd.cls(); |
LyCosine | 3:82b68ab94e3e | 195 | total_time = (int)ReadTime(fname.c_str()); |
sarthakjaiswal | 1:45d8f6557ff8 | 196 | wave_file = fopen(fname.c_str(),"r"); //opens the music file |
LyCosine | 3:82b68ab94e3e | 197 | Thread thread(show_information); |
LyCosine | 3:82b68ab94e3e | 198 | timer.start(); |
thaolmk54 | 2:087f60c3c445 | 199 | waver.play(wave_file); |
LyCosine | 3:82b68ab94e3e | 200 | timer.stop(); |
LyCosine | 3:82b68ab94e3e | 201 | timer.reset(); |
sarthakjaiswal | 0:4b3056d3c684 | 202 | fclose(wave_file); |
sarthakjaiswal | 0:4b3056d3c684 | 203 | } |
thaolmk54 | 2:087f60c3c445 | 204 | firstplay = false; |
thaolmk54 | 2:087f60c3c445 | 205 | // if device disconnected, try to connect again |
thaolmk54 | 2:087f60c3c445 | 206 | if (!msc.connected()) |
thaolmk54 | 2:087f60c3c445 | 207 | break; |
sarthakjaiswal | 0:4b3056d3c684 | 208 | } |
sarthakjaiswal | 0:4b3056d3c684 | 209 | } |
thaolmk54 | 2:087f60c3c445 | 210 |