Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed C12832 DogM163 FatFileSystem
main.cpp
00001 #include "mbed.h" 00002 #include "string.h" 00003 #include "stdio.h" 00004 #include "math.h" 00005 #include "C12832.h" 00006 #include "SDHCFileSystem.h" 00007 #include "_bitio.h" 00008 #include <string> 00009 using namespace std; 00010 00011 //Communication\interfaces 00012 Serial pc(USBTX,USBRX); //USB serial 00013 C12832 lcd(p5, p7, p6, p8, p9); //LCD 00014 DigitalIn RW(p10); 00015 SDFileSystem sd(p11, p12, p13, p15, "sd"); //SDCard 00016 DigitalIn sdDetect(p14); //CardDetect 00017 00018 //in- and outputs 00019 DigitalIn PauseBtn(p19); 00020 DigitalIn StopBtn(p20); 00021 DigitalIn UpBtn(p18); 00022 DigitalIn DownBtn(p17); 00023 00024 DigitalOut led1(LED1); 00025 DigitalOut led2(LED2); 00026 00027 00028 //DAC output 00029 // BU9480F define 00030 //SPI bu9480f(p23,p24,p21); 00031 //DigitalOut LRCLK(p22); 00032 // BU9480F define.end 00033 00034 //ticker- sampling 00035 Ticker tick; 00036 00037 //timer 00038 Timer dur; 00039 00040 //func 00041 void read_file_names(char *dir); //SDCard read file names 00042 void menu(); 00043 void PlaySong(int m); 00044 void StopSong(); 00045 void dac_out(); 00046 void encode(long int n); 00047 void decode(void); 00048 00049 //variables 00050 int m = 1; //index of choosen songs 00051 int i = 0; //index of files from folder 00052 int Paused = 0;//Is the song paused? 00053 int Stopped = 0; //Is the song stopped? 00054 char* songs[5]; 00055 00056 // a FIFO for the DAC 00057 #define TXFIFO_FULL 1 00058 #define RAM_LENGTH 8192//8192 00059 #define RAM_LIMIT (RAM_LENGTH - 1) 00060 volatile short DAC_fifo[RAM_LENGTH]; 00061 volatile short DAC_wptr=0; 00062 volatile short DAC_rptr=0; 00063 volatile short DAC_on; 00064 volatile short DAC_diff=0; 00065 00066 int main() 00067 { 00068 while(sdDetect == 0) { 00069 lcd.printf("Insert SD Card!"); 00070 wait(0.5); 00071 } 00072 pc.printf("going out of main"); 00073 menu(); 00074 } 00075 00076 void menu() 00077 { 00078 lcd.cls(); 00079 sd.disk_initialize(); 00080 read_file_names("/sd/Music"); 00081 while(1) { 00082 lcd.cls(); 00083 lcd.printf("Please select a song"); 00084 if(UpBtn == 1) { 00085 m++; 00086 if(m == 5) { 00087 m = 0; 00088 } 00089 } else if(DownBtn == 1) { 00090 m--; 00091 if(m == -1) { 00092 m = 4; 00093 } 00094 } 00095 lcd.locate(0,15); 00096 lcd.printf("%s", (songs[m])); 00097 if(PauseBtn == 1) { 00098 pc.printf("going to play song"); 00099 PlaySong(m); 00100 } 00101 wait(0.1); 00102 } 00103 } 00104 00105 void read_file_names(char *dir) // function that reads in file names from sd cards 00106 { 00107 pc.printf("found files"); 00108 DIR *dp; 00109 struct dirent *dirp; 00110 dp = opendir(dir); 00111 //read all directory and file names in current directory into filename vector 00112 while((dirp = readdir(dp)) != NULL) { 00113 songs[i] = dirp->d_name; 00114 i++; 00115 } 00116 } 00117 00118 void PlaySong(int m) 00119 { 00120 string songname = songs[m]; 00121 string a = "/sd/Music/"; 00122 string fname = a + songname; //retrieves the file name 00123 FILE *infp; 00124 dur.start(); 00125 lcd.cls(); 00126 lcd.locate(0,0); 00127 lcd.printf("Now playing"); 00128 infp = fopen(fname.c_str(),"rb"); //opens the music file 00129 // bu9480f init 00130 //bu9480f.format(16,0); 00131 //bu9480f.frequency(16000000); //16MHz 00132 // bu9480f init.end 00133 tick.attach_us(&dac_out, 41); //set 24.4kHz sampling data 00134 decode(); 00135 //plays the music file 00136 00137 tick.detach(); 00138 fclose( infp ); 00139 menu(); 00140 } 00141 00142 void StopSong() 00143 { 00144 pc.printf("stopped"); 00145 lcd.cls(); 00146 dur.reset(); 00147 tick.detach(); 00148 menu(); 00149 } 00150 00151 void dac_out() 00152 { 00153 pc.printf("playing"); 00154 if(StopBtn == 1) { 00155 StopSong(); 00156 } 00157 if(PauseBtn == 1) { 00158 dur.stop(); 00159 if(Paused == 1) { 00160 Paused = 0; 00161 dur.start(); 00162 } else { 00163 Paused = 1; 00164 } 00165 } 00166 if(Paused == 0) { 00167 lcd.locate(0,10); 00168 lcd.printf("%s", (songs[m])); 00169 // printf("\t%d\r\n",DAC_diff); 00170 if (DAC_diff > 1) { 00171 led2 = 0; 00172 00173 //LRCLK = 0; 00174 //bu9480f.write(DAC_fifo[DAC_rptr++]); 00175 DAC_rptr &= RAM_LIMIT; 00176 00177 //LRCLK = 1; 00178 //bu9480f.write(DAC_fifo[DAC_rptr++]); 00179 DAC_rptr &= RAM_LIMIT; 00180 00181 DAC_diff-=2; 00182 00183 } else led2 = 1; 00184 lcd.locate(0,20); 00185 lcd.printf("%2.f s", dur.read()); 00186 } else { 00187 lcd.printf("PAUSED"); 00188 } 00189 } 00190 00191 void encode(long int n) 00192 { 00193 pc.printf("encoding"); 00194 int zero_shift = 0; 00195 00196 if(n < 0) { 00197 putbit(0); // sign (put 0:if n as negative) 00198 n = -n; // n = abs(n) 00199 //printf("\t 0"); 00200 } else { 00201 putbit(1); // sign (put 1:if n as positive) 00202 //printf("\t 1"); 00203 } 00204 zero_shift = (n >> (k)); 00205 //printf("\t shift= %d",zero_shift); 00206 while(zero_shift > 0) { 00207 00208 zero_shift--; 00209 putbit(0); 00210 } // put n/(2^k) 0's 00211 00212 putbit(1); // terminating "1" 00213 putbits(k,rightbits(k,n)); 00214 //printf("\t finish= %d \r\n",(n & ((1U<<k)-1))); 00215 } 00216 00217 00218 00219 void decode(void) 00220 { 00221 pc.printf("decoding"); 00222 //short dac_data; 00223 long int decode_buff; 00224 short diff,diff2; 00225 unsigned int buff_sign,zero_shift; 00226 //char flag; 00227 00228 diff = 0; 00229 diff2 = 0; 00230 // get sign(1 for positive, 0 for negative) 00231 while(1) { 00232 00233 if((buff_sign = getbit())==OVERRUN)break; 00234 zero_shift = 0; 00235 while(getbit()==0)zero_shift++; 00236 00237 decode_buff = (signed int)((1U << k)*zero_shift); 00238 decode_buff += (getbits(k)); 00239 00240 if(!buff_sign)decode_buff =- decode_buff; 00241 /* return decode_buff; */ 00242 diff =(diff + decode_buff); 00243 DAC_fifo[DAC_wptr++]=(short)diff; 00244 DAC_wptr &= RAM_LIMIT; 00245 //DAC_diff++; 00246 while (DAC_diff > RAM_LIMIT) { 00247 led1 = 1; 00248 } //wait 00249 led1=0; 00250 00251 if((buff_sign = getbit())==OVERRUN)break; 00252 zero_shift = 0; 00253 while(getbit()==0)zero_shift++; 00254 00255 decode_buff = (signed int)((1U << k)*zero_shift); 00256 decode_buff += (getbits(k)); 00257 00258 if(!buff_sign)decode_buff =- decode_buff; 00259 /* return decode_buff; */ 00260 diff2 =(diff2 + decode_buff); 00261 00262 DAC_fifo[DAC_wptr++]=(short)diff2; 00263 DAC_wptr &= RAM_LIMIT; 00264 DAC_diff+=2; 00265 00266 while (DAC_diff > RAM_LIMIT) { 00267 led1 = 1; 00268 } //wait 00269 led1=0; 00270 00271 00272 00273 } 00274 }
Generated on Wed Jul 20 2022 22:55:22 by
