WavSpiller having the songs on an SDCard
Dependencies: WavPlayer mbed C12832 SDFileSystem
Dependents: WavPlayerUSB WavPlayerUSB
main.cpp@3:eb77b82ce461, 2019-04-01 (annotated)
- Committer:
- JostBaus
- Date:
- Mon Apr 01 11:34:23 2019 +0000
- Revision:
- 3:eb77b82ce461
- Parent:
- 2:b090afa8d3b9
- Child:
- 4:efd586e5c9a7
01.04.19; Fixed some things in func PauseSong();
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" |
sarthakjaiswal | 0:4b3056d3c684 | 6 | #include "wave_player.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 | 2:b090afa8d3b9 | 14 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //SDCard |
JostBaus | 2:b090afa8d3b9 | 15 | DigitalIn sdDetect(p17); //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 | 2:b090afa8d3b9 | 22 | BusIn joy(p15,p12,p13,p16); |
JostBaus | 2:b090afa8d3b9 | 23 | DigitalIn joyBtn(p14); |
JostBaus | 2:b090afa8d3b9 | 24 | |
JostBaus | 2:b090afa8d3b9 | 25 | //DAC output |
JostBaus | 2:b090afa8d3b9 | 26 | AnalogOut DACout(p18); |
JostBaus | 2:b090afa8d3b9 | 27 | wave_player waver(&DACout); //set up DAC to use wave player library |
sarthakjaiswal | 0:4b3056d3c684 | 28 | |
sarthakjaiswal | 0:4b3056d3c684 | 29 | |
JostBaus | 2:b090afa8d3b9 | 30 | //timer |
JostBaus | 2:b090afa8d3b9 | 31 | Timer dur; |
JostBaus | 2:b090afa8d3b9 | 32 | |
JostBaus | 2:b090afa8d3b9 | 33 | //func |
JostBaus | 2:b090afa8d3b9 | 34 | void read_file_names(char *dir); //SDCard read file names |
JostBaus | 2:b090afa8d3b9 | 35 | void menu(); |
JostBaus | 2:b090afa8d3b9 | 36 | void PlaySong(int m); |
JostBaus | 2:b090afa8d3b9 | 37 | void PauseSong(); |
JostBaus | 2:b090afa8d3b9 | 38 | void StopSong(); |
JostBaus | 2:b090afa8d3b9 | 39 | |
JostBaus | 2:b090afa8d3b9 | 40 | //variables |
JostBaus | 2:b090afa8d3b9 | 41 | int m = 1; //index of choosen songs |
JostBaus | 2:b090afa8d3b9 | 42 | int i = 0; //index of files from folder |
JostBaus | 2:b090afa8d3b9 | 43 | int Paused = 0; //Is the song paused? |
JostBaus | 2:b090afa8d3b9 | 44 | int Stopped = 0; //Is the song stopped? |
JostBaus | 3:eb77b82ce461 | 45 | char* songs[]; |
JostBaus | 2:b090afa8d3b9 | 46 | |
JostBaus | 2:b090afa8d3b9 | 47 | int main() |
JostBaus | 2:b090afa8d3b9 | 48 | { |
JostBaus | 2:b090afa8d3b9 | 49 | while(sdDetect == 0) { |
JostBaus | 2:b090afa8d3b9 | 50 | lcd.locate(0,0); |
JostBaus | 3:eb77b82ce461 | 51 | lcd.printf("Insert SD Card!"); |
JostBaus | 2:b090afa8d3b9 | 52 | wait(0.5); |
JostBaus | 2:b090afa8d3b9 | 53 | } |
JostBaus | 2:b090afa8d3b9 | 54 | menu(); |
JostBaus | 2:b090afa8d3b9 | 55 | } |
JostBaus | 2:b090afa8d3b9 | 56 | |
JostBaus | 2:b090afa8d3b9 | 57 | void menu() |
JostBaus | 2:b090afa8d3b9 | 58 | { |
JostBaus | 2:b090afa8d3b9 | 59 | lcd.cls(); |
JostBaus | 2:b090afa8d3b9 | 60 | sd.disk_initialize(); |
JostBaus | 2:b090afa8d3b9 | 61 | read_file_names("/sd/Music"); |
JostBaus | 2:b090afa8d3b9 | 62 | while(1) { |
JostBaus | 2:b090afa8d3b9 | 63 | lcd.locate(0,0); |
JostBaus | 2:b090afa8d3b9 | 64 | lcd.printf("Please select a song"); |
JostBaus | 2:b090afa8d3b9 | 65 | if(joy == 1) { |
JostBaus | 2:b090afa8d3b9 | 66 | m++; |
JostBaus | 2:b090afa8d3b9 | 67 | if(m == 5) { |
JostBaus | 2:b090afa8d3b9 | 68 | m = 0; |
JostBaus | 2:b090afa8d3b9 | 69 | } |
JostBaus | 2:b090afa8d3b9 | 70 | } else if(joy == 2) { |
JostBaus | 2:b090afa8d3b9 | 71 | m--; |
JostBaus | 2:b090afa8d3b9 | 72 | if(m == -1) { |
JostBaus | 2:b090afa8d3b9 | 73 | m = 4; |
JostBaus | 2:b090afa8d3b9 | 74 | } |
JostBaus | 2:b090afa8d3b9 | 75 | } |
JostBaus | 2:b090afa8d3b9 | 76 | lcd.locate(0,15); |
JostBaus | 2:b090afa8d3b9 | 77 | lcd.printf("%s", (songs[m])); |
JostBaus | 2:b090afa8d3b9 | 78 | if(joyBtn == 1) { |
JostBaus | 2:b090afa8d3b9 | 79 | PlaySong(m); |
JostBaus | 2:b090afa8d3b9 | 80 | } |
JostBaus | 2:b090afa8d3b9 | 81 | wait(0.1); |
JostBaus | 2:b090afa8d3b9 | 82 | } |
JostBaus | 2:b090afa8d3b9 | 83 | } |
JostBaus | 2:b090afa8d3b9 | 84 | |
sarthakjaiswal | 1:45d8f6557ff8 | 85 | void read_file_names(char *dir) // function that reads in file names from sd cards |
sarthakjaiswal | 0:4b3056d3c684 | 86 | { |
sarthakjaiswal | 0:4b3056d3c684 | 87 | DIR *dp; |
sarthakjaiswal | 0:4b3056d3c684 | 88 | struct dirent *dirp; |
sarthakjaiswal | 0:4b3056d3c684 | 89 | dp = opendir(dir); |
sarthakjaiswal | 0:4b3056d3c684 | 90 | //read all directory and file names in current directory into filename vector |
sarthakjaiswal | 0:4b3056d3c684 | 91 | while((dirp = readdir(dp)) != NULL) { |
JostBaus | 2:b090afa8d3b9 | 92 | songs[i] = dirp->d_name; |
JostBaus | 2:b090afa8d3b9 | 93 | i++; |
sarthakjaiswal | 0:4b3056d3c684 | 94 | } |
sarthakjaiswal | 0:4b3056d3c684 | 95 | } |
JostBaus | 2:b090afa8d3b9 | 96 | |
JostBaus | 2:b090afa8d3b9 | 97 | void PlaySong(int m) |
sarthakjaiswal | 0:4b3056d3c684 | 98 | { |
JostBaus | 2:b090afa8d3b9 | 99 | string songname = songs[m]; |
JostBaus | 2:b090afa8d3b9 | 100 | string a = "/sd/Music/"; |
JostBaus | 2:b090afa8d3b9 | 101 | string fname = a + songname; //retrieves the file name |
JostBaus | 2:b090afa8d3b9 | 102 | FILE *wave_file; |
JostBaus | 2:b090afa8d3b9 | 103 | dur.start(); |
sarthakjaiswal | 0:4b3056d3c684 | 104 | lcd.cls(); |
JostBaus | 2:b090afa8d3b9 | 105 | lcd.locate(0,0); |
JostBaus | 2:b090afa8d3b9 | 106 | lcd.printf("Now playing"); |
JostBaus | 2:b090afa8d3b9 | 107 | wave_file = fopen(fname.c_str(),"r"); //opens the music file |
JostBaus | 2:b090afa8d3b9 | 108 | waver.play(wave_file); //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 |