![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
WavSpiller having the songs on an SDCard
Dependencies: WavPlayer mbed C12832 SDFileSystem
Dependents: WavPlayerUSB WavPlayerUSB
main.cpp@5:166fa9e3c8e3, 2019-04-14 (annotated)
- Committer:
- JostBaus
- Date:
- Sun Apr 14 16:27:52 2019 +0000
- Revision:
- 5:166fa9e3c8e3
- Parent:
- 4:efd586e5c9a7
Changed pins to match the circuit board, replaced the wavplayer.h lib with another one
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sarthakjaiswal | 0:4b3056d3c684 | 1 | #include "mbed.h" |
JostBaus | 2:b090afa8d3b9 | 2 | #include "string.h" |
JostBaus | 2:b090afa8d3b9 | 3 | #include "stdio.h" |
JostBaus | 2:b090afa8d3b9 | 4 | #include "math.h" |
JostBaus | 2:b090afa8d3b9 | 5 | #include "C12832.h" |
JostBaus | 4:efd586e5c9a7 | 6 | #include "WavPlayer.h" |
JostBaus | 2:b090afa8d3b9 | 7 | #include "SDFileSystem.h" |
sarthakjaiswal | 0:4b3056d3c684 | 8 | #include <string> |
sarthakjaiswal | 0:4b3056d3c684 | 9 | using namespace std; |
sarthakjaiswal | 0:4b3056d3c684 | 10 | |
JostBaus | 2:b090afa8d3b9 | 11 | //Communication\interfaces |
JostBaus | 2:b090afa8d3b9 | 12 | Serial pc(USBTX,USBRX); //USB serial |
JostBaus | 2:b090afa8d3b9 | 13 | C12832 lcd(p5, p7, p6, p8, p11); //LCD |
JostBaus | 4:efd586e5c9a7 | 14 | SDFileSystem sd(p11, p12, p13, p15, "sd"); //SDCard |
JostBaus | 4:efd586e5c9a7 | 15 | DigitalIn sdDetect(p14); //CardDetect |
sarthakjaiswal | 0:4b3056d3c684 | 16 | |
JostBaus | 2:b090afa8d3b9 | 17 | //in- and outputs |
JostBaus | 2:b090afa8d3b9 | 18 | DigitalIn PauseBtn(p21); |
JostBaus | 2:b090afa8d3b9 | 19 | DigitalIn StopBtn(p22); |
JostBaus | 2:b090afa8d3b9 | 20 | |
JostBaus | 2:b090afa8d3b9 | 21 | //joystick |
JostBaus | 4:efd586e5c9a7 | 22 | BusIn joy(p19,p17,p18,p20); |
JostBaus | 2:b090afa8d3b9 | 23 | DigitalIn joyBtn(p14); |
JostBaus | 2:b090afa8d3b9 | 24 | |
JostBaus | 2:b090afa8d3b9 | 25 | //DAC output |
JostBaus | 4:efd586e5c9a7 | 26 | WavPlayer waver; //set up DAC to use wave player library |
sarthakjaiswal | 0:4b3056d3c684 | 27 | |
sarthakjaiswal | 0:4b3056d3c684 | 28 | |
JostBaus | 2:b090afa8d3b9 | 29 | //timer |
JostBaus | 2:b090afa8d3b9 | 30 | Timer dur; |
JostBaus | 2:b090afa8d3b9 | 31 | |
JostBaus | 2:b090afa8d3b9 | 32 | //func |
JostBaus | 2:b090afa8d3b9 | 33 | void read_file_names(char *dir); //SDCard read file names |
JostBaus | 2:b090afa8d3b9 | 34 | void menu(); |
JostBaus | 2:b090afa8d3b9 | 35 | void PlaySong(int m); |
JostBaus | 2:b090afa8d3b9 | 36 | void PauseSong(); |
JostBaus | 2:b090afa8d3b9 | 37 | void StopSong(); |
JostBaus | 2:b090afa8d3b9 | 38 | |
JostBaus | 2:b090afa8d3b9 | 39 | //variables |
JostBaus | 2:b090afa8d3b9 | 40 | int m = 1; //index of choosen songs |
JostBaus | 2:b090afa8d3b9 | 41 | int i = 0; //index of files from folder |
JostBaus | 2:b090afa8d3b9 | 42 | int Paused = 0; //Is the song paused? |
JostBaus | 2:b090afa8d3b9 | 43 | int Stopped = 0; //Is the song stopped? |
JostBaus | 3:eb77b82ce461 | 44 | char* songs[]; |
JostBaus | 2:b090afa8d3b9 | 45 | |
JostBaus | 2:b090afa8d3b9 | 46 | int main() |
JostBaus | 2:b090afa8d3b9 | 47 | { |
JostBaus | 2:b090afa8d3b9 | 48 | while(sdDetect == 0) { |
JostBaus | 2:b090afa8d3b9 | 49 | lcd.locate(0,0); |
JostBaus | 3:eb77b82ce461 | 50 | lcd.printf("Insert SD Card!"); |
JostBaus | 2:b090afa8d3b9 | 51 | wait(0.5); |
JostBaus | 2:b090afa8d3b9 | 52 | } |
JostBaus | 2:b090afa8d3b9 | 53 | menu(); |
JostBaus | 2:b090afa8d3b9 | 54 | } |
JostBaus | 2:b090afa8d3b9 | 55 | |
JostBaus | 2:b090afa8d3b9 | 56 | void menu() |
JostBaus | 2:b090afa8d3b9 | 57 | { |
JostBaus | 2:b090afa8d3b9 | 58 | lcd.cls(); |
JostBaus | 2:b090afa8d3b9 | 59 | sd.disk_initialize(); |
JostBaus | 2:b090afa8d3b9 | 60 | read_file_names("/sd/Music"); |
JostBaus | 2:b090afa8d3b9 | 61 | while(1) { |
JostBaus | 2:b090afa8d3b9 | 62 | lcd.locate(0,0); |
JostBaus | 2:b090afa8d3b9 | 63 | lcd.printf("Please select a song"); |
JostBaus | 2:b090afa8d3b9 | 64 | if(joy == 1) { |
JostBaus | 2:b090afa8d3b9 | 65 | m++; |
JostBaus | 2:b090afa8d3b9 | 66 | if(m == 5) { |
JostBaus | 2:b090afa8d3b9 | 67 | m = 0; |
JostBaus | 2:b090afa8d3b9 | 68 | } |
JostBaus | 2:b090afa8d3b9 | 69 | } else if(joy == 2) { |
JostBaus | 2:b090afa8d3b9 | 70 | m--; |
JostBaus | 2:b090afa8d3b9 | 71 | if(m == -1) { |
JostBaus | 2:b090afa8d3b9 | 72 | m = 4; |
JostBaus | 2:b090afa8d3b9 | 73 | } |
JostBaus | 2:b090afa8d3b9 | 74 | } |
JostBaus | 2:b090afa8d3b9 | 75 | lcd.locate(0,15); |
JostBaus | 2:b090afa8d3b9 | 76 | lcd.printf("%s", (songs[m])); |
JostBaus | 2:b090afa8d3b9 | 77 | if(joyBtn == 1) { |
JostBaus | 2:b090afa8d3b9 | 78 | PlaySong(m); |
JostBaus | 2:b090afa8d3b9 | 79 | } |
JostBaus | 2:b090afa8d3b9 | 80 | wait(0.1); |
JostBaus | 2:b090afa8d3b9 | 81 | } |
JostBaus | 2:b090afa8d3b9 | 82 | } |
JostBaus | 2:b090afa8d3b9 | 83 | |
sarthakjaiswal | 1:45d8f6557ff8 | 84 | void read_file_names(char *dir) // function that reads in file names from sd cards |
sarthakjaiswal | 0:4b3056d3c684 | 85 | { |
sarthakjaiswal | 0:4b3056d3c684 | 86 | DIR *dp; |
sarthakjaiswal | 0:4b3056d3c684 | 87 | struct dirent *dirp; |
sarthakjaiswal | 0:4b3056d3c684 | 88 | dp = opendir(dir); |
sarthakjaiswal | 0:4b3056d3c684 | 89 | //read all directory and file names in current directory into filename vector |
sarthakjaiswal | 0:4b3056d3c684 | 90 | while((dirp = readdir(dp)) != NULL) { |
JostBaus | 2:b090afa8d3b9 | 91 | songs[i] = dirp->d_name; |
JostBaus | 2:b090afa8d3b9 | 92 | i++; |
sarthakjaiswal | 0:4b3056d3c684 | 93 | } |
sarthakjaiswal | 0:4b3056d3c684 | 94 | } |
JostBaus | 2:b090afa8d3b9 | 95 | |
JostBaus | 2:b090afa8d3b9 | 96 | void PlaySong(int m) |
sarthakjaiswal | 0:4b3056d3c684 | 97 | { |
JostBaus | 2:b090afa8d3b9 | 98 | string songname = songs[m]; |
JostBaus | 2:b090afa8d3b9 | 99 | string a = "/sd/Music/"; |
JostBaus | 2:b090afa8d3b9 | 100 | string fname = a + songname; //retrieves the file name |
JostBaus | 2:b090afa8d3b9 | 101 | FILE *wave_file; |
JostBaus | 2:b090afa8d3b9 | 102 | dur.start(); |
sarthakjaiswal | 0:4b3056d3c684 | 103 | lcd.cls(); |
JostBaus | 2:b090afa8d3b9 | 104 | lcd.locate(0,0); |
JostBaus | 2:b090afa8d3b9 | 105 | lcd.printf("Now playing"); |
JostBaus | 2:b090afa8d3b9 | 106 | wave_file = fopen(fname.c_str(),"r"); //opens the music file |
JostBaus | 4:efd586e5c9a7 | 107 | waver.open(wave_file); |
JostBaus | 4:efd586e5c9a7 | 108 | waver.play(); //plays the music file |
JostBaus | 2:b090afa8d3b9 | 109 | lcd.locate(0,10); |
JostBaus | 2:b090afa8d3b9 | 110 | lcd.printf("%s", (songs[m])); |
JostBaus | 2:b090afa8d3b9 | 111 | while(Stopped == 0) { |
JostBaus | 2:b090afa8d3b9 | 112 | if(StopBtn == 1) { |
JostBaus | 2:b090afa8d3b9 | 113 | StopSong(); |
JostBaus | 2:b090afa8d3b9 | 114 | } |
JostBaus | 2:b090afa8d3b9 | 115 | if(PauseBtn == 1) { |
JostBaus | 2:b090afa8d3b9 | 116 | PauseSong(); |
JostBaus | 2:b090afa8d3b9 | 117 | } |
JostBaus | 2:b090afa8d3b9 | 118 | lcd.locate(0,20); |
JostBaus | 2:b090afa8d3b9 | 119 | lcd.printf("%2.f s", dur.read()); |
JostBaus | 2:b090afa8d3b9 | 120 | } |
JostBaus | 2:b090afa8d3b9 | 121 | fclose(wave_file); |
JostBaus | 2:b090afa8d3b9 | 122 | menu(); |
sarthakjaiswal | 0:4b3056d3c684 | 123 | } |
JostBaus | 2:b090afa8d3b9 | 124 | |
JostBaus | 2:b090afa8d3b9 | 125 | void PauseSong() |
sarthakjaiswal | 0:4b3056d3c684 | 126 | { |
JostBaus | 3:eb77b82ce461 | 127 | while(1) { |
JostBaus | 3:eb77b82ce461 | 128 | if(Paused == 0) { |
JostBaus | 3:eb77b82ce461 | 129 | string songname = songs[m]; |
JostBaus | 3:eb77b82ce461 | 130 | unsigned index = songname.find(".wav"); |
JostBaus | 3:eb77b82ce461 | 131 | songname = songname.substr(0,index); |
JostBaus | 3:eb77b82ce461 | 132 | lcd.printf(songname.c_str()); |
JostBaus | 3:eb77b82ce461 | 133 | dur.stop(); |
JostBaus | 3:eb77b82ce461 | 134 | Paused = 1; |
JostBaus | 3:eb77b82ce461 | 135 | } else if(StopBtn == 1) { |
JostBaus | 3:eb77b82ce461 | 136 | StopSong(); |
JostBaus | 3:eb77b82ce461 | 137 | } else { |
JostBaus | 3:eb77b82ce461 | 138 | Paused = 0; |
JostBaus | 3:eb77b82ce461 | 139 | dur.start(); |
JostBaus | 3:eb77b82ce461 | 140 | } |
sarthakjaiswal | 0:4b3056d3c684 | 141 | } |
sarthakjaiswal | 0:4b3056d3c684 | 142 | } |
sarthakjaiswal | 0:4b3056d3c684 | 143 | |
JostBaus | 2:b090afa8d3b9 | 144 | void StopSong() |
sarthakjaiswal | 0:4b3056d3c684 | 145 | { |
sarthakjaiswal | 0:4b3056d3c684 | 146 | lcd.cls(); |
JostBaus | 2:b090afa8d3b9 | 147 | dur.reset(); |
JostBaus | 2:b090afa8d3b9 | 148 | Stopped = 1; |
sarthakjaiswal | 0:4b3056d3c684 | 149 | } |
JostBaus | 2:b090afa8d3b9 | 150 | |
JostBaus | 2:b090afa8d3b9 | 151 | |
JostBaus | 2:b090afa8d3b9 | 152 | |
JostBaus | 2:b090afa8d3b9 | 153 |