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