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: FFT
main.cpp
00001 #include "mbed.h" 00002 #include "SDFileSystem.h" 00003 #include "wave_player.h" 00004 #include "Adafruit_LEDBackpack.h" 00005 #include "Adafruit_GFX.h" 00006 #include "rtos.h" 00007 #include "FFT.h" 00008 #include <string> 00009 00010 SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD card 00011 00012 I2C i2c(p28, p27); // LED display 00013 Adafruit_8x8matrix matrix = Adafruit_8x8matrix(&i2c); 00014 00015 AnalogOut DACout(p18); // speaker 00016 wave_player waver(&DACout); 00017 Mutex speaker_lock; 00018 DigitalOut myled(LED1); // mbed LED 00019 Serial pc (USBTX,USBRX); 00020 string dir; // "/sd/" + song + ".wav" 00021 string song; 00022 bool play; 00023 00024 <<<<<<< working copy 00025 #define BUFFER_SIZE 50 00026 00027 // delete later 00028 /*int sample[12][8] = { 00029 34085,32666,29810,29823,27514,24175,24468,32559, 00030 36401,33075,29037,21933,20189,21751,20975,20087, 00031 17530,27723,30891,21128,19982,15446,12413,13597, 00032 15776,14233,15622,16334,15485,17136,16389,17954, 00033 20349,18486,23377,34447,34400,34681,33107,31171, 00034 30007,33640,37007,38136,39187,38169,51831,52041, 00035 47831,48634,44189,43878,44743,46468,43170,47172, 00036 53518,52294,54825,51992,49619,48147,47901,48413, 00037 46049,49090,47590,46154,45423,41223,41782,54695, 00038 57452,51210,52462,59096,56711,52268,49364,42865, 00039 41483,37964,39215,35908,31570,28332,24492,23121, 00040 22874,18154,15541,15263,12463,12198,13301,9951 00041 };*/ 00042 ======= 00043 #define BUFFER_SIZE 64 00044 >>>>>>> merge rev 00045 00046 // states for display output 00047 const int on[8][8] = { 00048 1, 1, 1, 1, 1, 1, 1, 1, 00049 1, 1, 1, 1, 1, 1, 1, 1, 00050 1, 1, 1, 1, 1, 1, 1, 1, 00051 1, 1, 1, 1, 1, 1, 1, 1, 00052 1, 1, 1, 1, 1, 1, 1, 1, 00053 1, 1, 1, 1, 1, 1, 1, 1, 00054 1, 1, 1, 1, 1, 1, 1, 1, 00055 1, 1, 1, 1, 1, 1, 1, 1 00056 }; 00057 00058 const int high[8][8] = { 00059 0, 0, 0, 1, 1, 0, 0, 0, 00060 0, 0, 0, 1, 1, 0, 0, 0, 00061 0, 0, 1, 1, 1, 1, 0, 0, 00062 0, 1, 1, 1, 1, 1, 1, 0, 00063 0, 1, 1, 1, 1, 1, 1, 0, 00064 1, 1, 1, 1, 1, 1, 1, 1, 00065 1, 1, 1, 1, 1, 1, 1, 1, 00066 1, 1, 1, 1, 1, 1, 1, 1 00067 }; 00068 00069 const int med[8][8] = { 00070 0, 0, 0, 0, 0, 0, 0, 0, 00071 0, 0, 0, 0, 0, 0, 0, 0, 00072 0, 0, 0, 1, 1, 0, 0, 0, 00073 0, 0, 1, 1, 1, 1, 0, 0, 00074 0, 0, 1, 1, 1, 1, 0, 0, 00075 0, 1, 1, 1, 1, 1, 1, 0, 00076 1, 1, 1, 1, 1, 1, 1, 1, 00077 1, 1, 1, 1, 1, 1, 1, 1 00078 }; 00079 00080 const int low[8][8] = { 00081 0, 0, 0, 0, 0, 0, 0, 0, 00082 0, 0, 0, 0, 0, 0, 0, 0, 00083 0, 0, 0, 0, 0, 0, 0, 0, 00084 0, 0, 0, 0, 0, 0, 0, 0, 00085 0, 0, 0, 0, 0, 0, 0, 0, 00086 0, 0, 1, 1, 1, 1, 0, 0, 00087 0, 1, 1, 1, 1, 1, 1, 0, 00088 1, 1, 1, 1, 1, 1, 1, 1 00089 }; 00090 00091 const int off[8][8] = { 00092 0, 0, 0, 0, 0, 0, 0, 0, 00093 0, 0, 0, 0, 0, 0, 0, 0, 00094 0, 0, 0, 0, 0, 0, 0, 0, 00095 0, 0, 0, 0, 0, 0, 0, 0, 00096 0, 0, 0, 0, 0, 0, 0, 0, 00097 0, 0, 0, 0, 0, 0, 0, 0, 00098 0, 0, 0, 0, 0, 0, 0, 0, 00099 0, 0, 0, 0, 0, 0, 0, 0 00100 }; 00101 00102 00103 00104 <<<<<<< working copy 00105 void display_fft(int sample[]) 00106 { 00107 00108 ======= 00109 void display_fft(float sample[]) { 00110 00111 >>>>>>> merge rev 00112 int state = 0; 00113 float freq = 0; 00114 matrix.begin(0x70); 00115 <<<<<<< working copy 00116 00117 for (int a = 0; a < 50; a++) { 00118 00119 freq = sample[a]; 00120 00121 ======= 00122 00123 int s = sizeof(sample); 00124 00125 pc.printf("%d\n", s); 00126 00127 for (int a = 0; a < 64; a++) { 00128 00129 freq = sample[a]; 00130 00131 pc.printf("%f\n",freq); 00132 00133 >>>>>>> merge rev 00134 // determine state based on the FFT output range 00135 if (freq < 10000) { 00136 state = 5; // OFF 00137 } else if (freq >= 10000 && freq < 25000) { 00138 state = 4; // LOW 00139 } else if (freq >= 25000 && freq < 40000) { 00140 state = 3; // MED 00141 } else if (freq >= 40000 && freq < 55000) { 00142 state = 2; // HIGH 00143 } else if (freq >= 55000) { 00144 <<<<<<< working copy 00145 state = 1; // ON 00146 } 00147 00148 ======= 00149 state = 1; // HIGHEST 00150 } 00151 00152 >>>>>>> merge rev 00153 // switch statement based on the state 00154 switch (state) { 00155 case(1): // CASE HIGHEST 00156 matrix.clear(); 00157 for (int i = 0; i < 8; i++) { 00158 for (int j = 0; j < 8; j++) { 00159 if (on[i][j] == 1) { 00160 matrix.drawPixel(i, j, LED_ON); 00161 } 00162 } 00163 } 00164 matrix.writeDisplay(); 00165 wait(.2); 00166 break; 00167 00168 case(2): // CASE HIGH 00169 matrix.clear(); 00170 for (int i = 0; i < 8; i++) { 00171 for (int j = 0; j < 8; j++) { 00172 if (high[i][j] == 1) { 00173 matrix.drawPixel(i, j, LED_ON); 00174 } 00175 } 00176 } 00177 matrix.writeDisplay(); 00178 <<<<<<< working copy 00179 wait(1); 00180 00181 ======= 00182 wait(.2); 00183 00184 >>>>>>> merge rev 00185 case(3): // CASE MEDIUM 00186 matrix.clear(); 00187 for (int i = 0; i < 8; i++) { 00188 for (int j = 0; j < 8; j++) { 00189 if (med[i][j] == 1) { 00190 matrix.drawPixel(i, j, LED_ON); 00191 } 00192 } 00193 } 00194 matrix.writeDisplay(); 00195 wait(.2); 00196 break; 00197 00198 case(4): // CASE LOW 00199 matrix.clear(); 00200 for (int i = 0; i < 8; i++) { 00201 for (int j = 0; j < 8; j++) { 00202 if (low[i][j] == 1) { 00203 matrix.drawPixel(i, j, LED_ON); 00204 } 00205 } 00206 } 00207 matrix.writeDisplay(); 00208 wait(.2); 00209 break; 00210 00211 case(5): // CASE OFF 00212 matrix.clear(); 00213 for (int i = 0; i < 8; i++) { 00214 for (int j = 0; j < 8; j++) { 00215 if (off[i][j] == 1) { 00216 matrix.drawPixel(i, j, LED_ON); 00217 } 00218 } 00219 } 00220 matrix.writeDisplay(); 00221 wait(.2); 00222 break; 00223 00224 default: 00225 break; 00226 } 00227 <<<<<<< working copy 00228 00229 00230 } 00231 00232 ======= 00233 00234 pc.printf("done"); 00235 00236 >>>>>>> merge rev 00237 } 00238 00239 //Reads SD card and passes value to frequency display 00240 void display_thread(void const* args) 00241 { 00242 FILE *fp = fopen("/sd/africa-toto-array.txt","r"); 00243 if(fp == NULL) { 00244 pc.printf("Error opening file\n"); 00245 return; 00246 } 00247 <<<<<<< working copy 00248 00249 int buffer[BUFFER_SIZE]; 00250 ======= 00251 00252 float buffer[BUFFER_SIZE]; 00253 >>>>>>> merge rev 00254 int buffer_index = 0; 00255 int value; 00256 <<<<<<< working copy 00257 pc.printf("Thread start\n"); 00258 while(fscanf(fp,"%d,",&value) == 1) { 00259 buffer[buffer_index] = value; 00260 buffer_index++; 00261 pc.printf("%d\n",value); 00262 buffer_index++; 00263 if(buffer_index == BUFFER_SIZE) { 00264 //fast fourier tranform function here 00265 display_fft(buffer); 00266 buffer_index = 0; 00267 memset(buffer,0,sizeof(buffer)); 00268 00269 } 00270 ======= 00271 00272 while(fscanf(fp,"%d,",&value) == 1){ 00273 buffer[buffer_index] = (float)value; 00274 buffer_index++; 00275 //pc.printf("%d\n",value); 00276 //buffer_index++; 00277 if(buffer_index == BUFFER_SIZE) 00278 { 00279 //fast fourier tranform function here 00280 vRealFFT(buffer, 4); 00281 display_fft(buffer); 00282 buffer_index = 0; 00283 memset(buffer,0,sizeof(buffer)); 00284 00285 } 00286 >>>>>>> merge rev 00287 } 00288 00289 fclose(fp); 00290 } 00291 00292 void speaker_thread(void const* args) 00293 { 00294 while(1) { 00295 // check helper function for new song in GUI 00296 // grab file here and put together string 00297 // string song_title = "/sd/" + "" + ".wav"; 00298 00299 00300 /* 00301 FILE *wave_file; 00302 dir = "/sd/" + song + ".wav"; 00303 wave_file=fopen(dir.c_str(),"r"); 00304 00305 speaker_lock.lock(); 00306 if (play) 00307 { 00308 waver.play(wave_file); 00309 } 00310 speaker_lock.unlock(); 00311 fclose(wave_file); 00312 Thread::wait(100); 00313 */ 00314 } 00315 } 00316 00317 00318 int main() 00319 { 00320 00321 Thread th1(speaker_thread); 00322 Thread th2(display_thread); 00323 char c; 00324 while(1) { 00325 myled = 1; 00326 wait(0.2); 00327 myled = 0; 00328 wait(0.2); 00329 while(!pc.readable()) { 00330 Thread::wait(1); 00331 } 00332 if (pc.getc() == '!') { 00333 c = pc.getc(); 00334 switch(c) { 00335 case '1': 00336 song = "africa-toto"; 00337 break; 00338 case '2': 00339 song = "around_the_world-atc"; 00340 break; 00341 case '3': 00342 song = "beautiful_life-ace_of_base"; 00343 break; 00344 case '4': 00345 song = "dont_speak-no_doubt"; 00346 break; 00347 case '5': 00348 song = "my-love"; 00349 break; 00350 case '6': 00351 song = "Song1_test"; 00352 break; 00353 case 'P': 00354 // wave_player plays 00355 play = true; 00356 break; 00357 case 'S': 00358 // wave_player stops 00359 play = false; 00360 break; 00361 default: 00362 break; 00363 } 00364 } 00365 Thread::wait(100); 00366 } 00367 00368 }
Generated on Tue Jul 12 2022 20:47:44 by
