this hurts

Dependencies:   FFT

Committer:
shyamgatech
Date:
Thu Dec 03 01:34:44 2020 +0000
Revision:
5:598d94b45bad
Parent:
2:a6e81c45ef48
Child:
6:d311a95f1e9b
added code to read inputs from GUI ;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
annieluo2 0:d6c9b09b4042 1 #include "mbed.h"
annieluo2 0:d6c9b09b4042 2 #include "SDFileSystem.h"
annieluo2 0:d6c9b09b4042 3 #include "wave_player.h"
annieluo2 0:d6c9b09b4042 4 #include "Adafruit_LEDBackpack.h"
annieluo2 0:d6c9b09b4042 5 #include "Adafruit_GFX.h"
annieluo2 0:d6c9b09b4042 6 #include "rtos.h"
annieluo2 0:d6c9b09b4042 7 #include "FFT.h"
shyamgatech 5:598d94b45bad 8 #include <string>
annieluo2 0:d6c9b09b4042 9
annieluo2 0:d6c9b09b4042 10 SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD card
annieluo2 0:d6c9b09b4042 11
shyamgatech 5:598d94b45bad 12 I2C i2c(p28, p27); // LED display
annieluo2 0:d6c9b09b4042 13 Adafruit_8x8matrix matrix = Adafruit_8x8matrix(&i2c);
annieluo2 0:d6c9b09b4042 14
annieluo2 0:d6c9b09b4042 15 AnalogOut DACout(p18); // speaker
annieluo2 0:d6c9b09b4042 16 wave_player waver(&DACout);
annieluo2 0:d6c9b09b4042 17 Mutex speaker_lock;
dimitryjl23 2:a6e81c45ef48 18 DigitalOut myled(LED1); // mbed LED
dimitryjl23 2:a6e81c45ef48 19 Serial pc (USBTX,USBRX);
shyamgatech 5:598d94b45bad 20 string dir; // "/sd/" + song + ".wav"
shyamgatech 5:598d94b45bad 21 string song;
shyamgatech 5:598d94b45bad 22 bool play;
annieluo2 0:d6c9b09b4042 23
dimitryjl23 2:a6e81c45ef48 24 #define BUFFER_SIZE 50
annieluo2 0:d6c9b09b4042 25
annieluo2 1:5dd6801bb0d6 26 // delete later
dimitryjl23 2:a6e81c45ef48 27 /*int sample[12][8] = {
shyamgatech 5:598d94b45bad 28 34085,32666,29810,29823,27514,24175,24468,32559,
shyamgatech 5:598d94b45bad 29 36401,33075,29037,21933,20189,21751,20975,20087,
shyamgatech 5:598d94b45bad 30 17530,27723,30891,21128,19982,15446,12413,13597,
shyamgatech 5:598d94b45bad 31 15776,14233,15622,16334,15485,17136,16389,17954,
shyamgatech 5:598d94b45bad 32 20349,18486,23377,34447,34400,34681,33107,31171,
shyamgatech 5:598d94b45bad 33 30007,33640,37007,38136,39187,38169,51831,52041,
shyamgatech 5:598d94b45bad 34 47831,48634,44189,43878,44743,46468,43170,47172,
shyamgatech 5:598d94b45bad 35 53518,52294,54825,51992,49619,48147,47901,48413,
shyamgatech 5:598d94b45bad 36 46049,49090,47590,46154,45423,41223,41782,54695,
shyamgatech 5:598d94b45bad 37 57452,51210,52462,59096,56711,52268,49364,42865,
shyamgatech 5:598d94b45bad 38 41483,37964,39215,35908,31570,28332,24492,23121,
annieluo2 1:5dd6801bb0d6 39 22874,18154,15541,15263,12463,12198,13301,9951
dimitryjl23 2:a6e81c45ef48 40 };*/
annieluo2 1:5dd6801bb0d6 41
annieluo2 1:5dd6801bb0d6 42 // states for display output
shyamgatech 5:598d94b45bad 43 const int on[8][8] = {
shyamgatech 5:598d94b45bad 44 1, 1, 1, 1, 1, 1, 1, 1,
shyamgatech 5:598d94b45bad 45 1, 1, 1, 1, 1, 1, 1, 1,
shyamgatech 5:598d94b45bad 46 1, 1, 1, 1, 1, 1, 1, 1,
shyamgatech 5:598d94b45bad 47 1, 1, 1, 1, 1, 1, 1, 1,
shyamgatech 5:598d94b45bad 48 1, 1, 1, 1, 1, 1, 1, 1,
shyamgatech 5:598d94b45bad 49 1, 1, 1, 1, 1, 1, 1, 1,
shyamgatech 5:598d94b45bad 50 1, 1, 1, 1, 1, 1, 1, 1,
shyamgatech 5:598d94b45bad 51 1, 1, 1, 1, 1, 1, 1, 1
shyamgatech 5:598d94b45bad 52 };
annieluo2 1:5dd6801bb0d6 53
shyamgatech 5:598d94b45bad 54 const int high[8][8] = {
shyamgatech 5:598d94b45bad 55 0, 0, 0, 1, 1, 0, 0, 0,
shyamgatech 5:598d94b45bad 56 0, 0, 0, 1, 1, 0, 0, 0,
shyamgatech 5:598d94b45bad 57 0, 0, 1, 1, 1, 1, 0, 0,
shyamgatech 5:598d94b45bad 58 0, 1, 1, 1, 1, 1, 1, 0,
shyamgatech 5:598d94b45bad 59 0, 1, 1, 1, 1, 1, 1, 0,
shyamgatech 5:598d94b45bad 60 1, 1, 1, 1, 1, 1, 1, 1,
shyamgatech 5:598d94b45bad 61 1, 1, 1, 1, 1, 1, 1, 1,
shyamgatech 5:598d94b45bad 62 1, 1, 1, 1, 1, 1, 1, 1
shyamgatech 5:598d94b45bad 63 };
annieluo2 1:5dd6801bb0d6 64
shyamgatech 5:598d94b45bad 65 const int med[8][8] = {
shyamgatech 5:598d94b45bad 66 0, 0, 0, 0, 0, 0, 0, 0,
shyamgatech 5:598d94b45bad 67 0, 0, 0, 0, 0, 0, 0, 0,
shyamgatech 5:598d94b45bad 68 0, 0, 0, 1, 1, 0, 0, 0,
shyamgatech 5:598d94b45bad 69 0, 0, 1, 1, 1, 1, 0, 0,
shyamgatech 5:598d94b45bad 70 0, 0, 1, 1, 1, 1, 0, 0,
shyamgatech 5:598d94b45bad 71 0, 1, 1, 1, 1, 1, 1, 0,
shyamgatech 5:598d94b45bad 72 1, 1, 1, 1, 1, 1, 1, 1,
shyamgatech 5:598d94b45bad 73 1, 1, 1, 1, 1, 1, 1, 1
shyamgatech 5:598d94b45bad 74 };
annieluo2 1:5dd6801bb0d6 75
shyamgatech 5:598d94b45bad 76 const int low[8][8] = {
shyamgatech 5:598d94b45bad 77 0, 0, 0, 0, 0, 0, 0, 0,
shyamgatech 5:598d94b45bad 78 0, 0, 0, 0, 0, 0, 0, 0,
shyamgatech 5:598d94b45bad 79 0, 0, 0, 0, 0, 0, 0, 0,
shyamgatech 5:598d94b45bad 80 0, 0, 0, 0, 0, 0, 0, 0,
shyamgatech 5:598d94b45bad 81 0, 0, 0, 0, 0, 0, 0, 0,
shyamgatech 5:598d94b45bad 82 0, 0, 1, 1, 1, 1, 0, 0,
shyamgatech 5:598d94b45bad 83 0, 1, 1, 1, 1, 1, 1, 0,
shyamgatech 5:598d94b45bad 84 1, 1, 1, 1, 1, 1, 1, 1
shyamgatech 5:598d94b45bad 85 };
annieluo2 1:5dd6801bb0d6 86
shyamgatech 5:598d94b45bad 87 const int off[8][8] = {
shyamgatech 5:598d94b45bad 88 0, 0, 0, 0, 0, 0, 0, 0,
shyamgatech 5:598d94b45bad 89 0, 0, 0, 0, 0, 0, 0, 0,
shyamgatech 5:598d94b45bad 90 0, 0, 0, 0, 0, 0, 0, 0,
shyamgatech 5:598d94b45bad 91 0, 0, 0, 0, 0, 0, 0, 0,
shyamgatech 5:598d94b45bad 92 0, 0, 0, 0, 0, 0, 0, 0,
shyamgatech 5:598d94b45bad 93 0, 0, 0, 0, 0, 0, 0, 0,
shyamgatech 5:598d94b45bad 94 0, 0, 0, 0, 0, 0, 0, 0,
shyamgatech 5:598d94b45bad 95 0, 0, 0, 0, 0, 0, 0, 0
shyamgatech 5:598d94b45bad 96 };
annieluo2 1:5dd6801bb0d6 97
dimitryjl23 2:a6e81c45ef48 98
dimitryjl23 2:a6e81c45ef48 99
shyamgatech 5:598d94b45bad 100 void display_fft(int sample[])
shyamgatech 5:598d94b45bad 101 {
shyamgatech 5:598d94b45bad 102
annieluo2 1:5dd6801bb0d6 103 int state = 0;
dimitryjl23 2:a6e81c45ef48 104 int freq = 0;
annieluo2 1:5dd6801bb0d6 105 matrix.begin(0x70);
shyamgatech 5:598d94b45bad 106
shyamgatech 5:598d94b45bad 107 for (int a = 0; a < 50; a++) {
shyamgatech 5:598d94b45bad 108
shyamgatech 5:598d94b45bad 109 freq = sample[a];
shyamgatech 5:598d94b45bad 110
annieluo2 1:5dd6801bb0d6 111 // determine state based on the FFT output range
annieluo2 1:5dd6801bb0d6 112 if (freq >= 0 && freq < 10000) {
annieluo2 1:5dd6801bb0d6 113 state = 5; // OFF
annieluo2 1:5dd6801bb0d6 114 } else if (freq >= 10000 && freq < 25000) {
annieluo2 1:5dd6801bb0d6 115 state = 4; // LOW
annieluo2 1:5dd6801bb0d6 116 } else if (freq >= 25000 && freq < 40000) {
annieluo2 1:5dd6801bb0d6 117 state = 3; // MED
annieluo2 1:5dd6801bb0d6 118 } else if (freq >= 40000 && freq < 55000) {
annieluo2 1:5dd6801bb0d6 119 state = 2; // HIGH
annieluo2 1:5dd6801bb0d6 120 } else if (freq >= 55000) {
annieluo2 1:5dd6801bb0d6 121 state = 1; // ON
shyamgatech 5:598d94b45bad 122 }
shyamgatech 5:598d94b45bad 123
annieluo2 1:5dd6801bb0d6 124 // switch statement based on the state
annieluo2 1:5dd6801bb0d6 125 switch (state) {
annieluo2 1:5dd6801bb0d6 126 case(1): // CASE HIGHEST
annieluo2 1:5dd6801bb0d6 127 matrix.clear();
annieluo2 1:5dd6801bb0d6 128 for (int i = 0; i < 8; i++) {
annieluo2 1:5dd6801bb0d6 129 for (int j = 0; j < 8; j++) {
annieluo2 1:5dd6801bb0d6 130 if (on[i][j] == 1) {
annieluo2 1:5dd6801bb0d6 131 matrix.drawPixel(i, j, LED_ON);
annieluo2 1:5dd6801bb0d6 132 }
annieluo2 1:5dd6801bb0d6 133 }
annieluo2 1:5dd6801bb0d6 134 }
annieluo2 1:5dd6801bb0d6 135 matrix.writeDisplay();
annieluo2 1:5dd6801bb0d6 136 wait(1);
annieluo2 1:5dd6801bb0d6 137 break;
shyamgatech 5:598d94b45bad 138
annieluo2 1:5dd6801bb0d6 139 case(2): // CASE HIGH
annieluo2 1:5dd6801bb0d6 140 matrix.clear();
annieluo2 1:5dd6801bb0d6 141 for (int i = 0; i < 8; i++) {
annieluo2 1:5dd6801bb0d6 142 for (int j = 0; j < 8; j++) {
annieluo2 1:5dd6801bb0d6 143 if (high[i][j] == 1) {
annieluo2 1:5dd6801bb0d6 144 matrix.drawPixel(i, j, LED_ON);
annieluo2 1:5dd6801bb0d6 145 }
annieluo2 1:5dd6801bb0d6 146 }
annieluo2 1:5dd6801bb0d6 147 }
annieluo2 1:5dd6801bb0d6 148 matrix.writeDisplay();
annieluo2 1:5dd6801bb0d6 149 wait(1);
shyamgatech 5:598d94b45bad 150
annieluo2 1:5dd6801bb0d6 151 case(3): // CASE MEDIUM
annieluo2 1:5dd6801bb0d6 152 matrix.clear();
annieluo2 1:5dd6801bb0d6 153 for (int i = 0; i < 8; i++) {
annieluo2 1:5dd6801bb0d6 154 for (int j = 0; j < 8; j++) {
annieluo2 1:5dd6801bb0d6 155 if (med[i][j] == 1) {
shyamgatech 5:598d94b45bad 156 matrix.drawPixel(i, j, LED_ON);
annieluo2 1:5dd6801bb0d6 157 }
annieluo2 1:5dd6801bb0d6 158 }
annieluo2 1:5dd6801bb0d6 159 }
annieluo2 1:5dd6801bb0d6 160 matrix.writeDisplay();
annieluo2 1:5dd6801bb0d6 161 wait(1);
annieluo2 1:5dd6801bb0d6 162 break;
shyamgatech 5:598d94b45bad 163
annieluo2 1:5dd6801bb0d6 164 case(4): // CASE LOW
annieluo2 1:5dd6801bb0d6 165 matrix.clear();
annieluo2 1:5dd6801bb0d6 166 for (int i = 0; i < 8; i++) {
annieluo2 1:5dd6801bb0d6 167 for (int j = 0; j < 8; j++) {
annieluo2 1:5dd6801bb0d6 168 if (low[i][j] == 1) {
annieluo2 1:5dd6801bb0d6 169 matrix.drawPixel(i, j, LED_ON);
annieluo2 1:5dd6801bb0d6 170 }
annieluo2 1:5dd6801bb0d6 171 }
annieluo2 1:5dd6801bb0d6 172 }
annieluo2 1:5dd6801bb0d6 173 matrix.writeDisplay();
annieluo2 1:5dd6801bb0d6 174 wait(1);
annieluo2 1:5dd6801bb0d6 175 break;
shyamgatech 5:598d94b45bad 176
annieluo2 1:5dd6801bb0d6 177 case(5): // CASE OFF
annieluo2 1:5dd6801bb0d6 178 matrix.clear();
annieluo2 1:5dd6801bb0d6 179 for (int i = 0; i < 8; i++) {
annieluo2 1:5dd6801bb0d6 180 for (int j = 0; j < 8; j++) {
annieluo2 1:5dd6801bb0d6 181 if (off[i][j] == 1) {
annieluo2 1:5dd6801bb0d6 182 matrix.drawPixel(i, j, LED_ON);
annieluo2 1:5dd6801bb0d6 183 }
annieluo2 1:5dd6801bb0d6 184 }
annieluo2 1:5dd6801bb0d6 185 }
annieluo2 1:5dd6801bb0d6 186 matrix.writeDisplay();
annieluo2 1:5dd6801bb0d6 187 wait(1);
annieluo2 1:5dd6801bb0d6 188 break;
shyamgatech 5:598d94b45bad 189
annieluo2 1:5dd6801bb0d6 190 default:
annieluo2 1:5dd6801bb0d6 191 break;
annieluo2 1:5dd6801bb0d6 192 }
shyamgatech 5:598d94b45bad 193
shyamgatech 5:598d94b45bad 194
shyamgatech 5:598d94b45bad 195 }
shyamgatech 5:598d94b45bad 196
dimitryjl23 2:a6e81c45ef48 197 }
dimitryjl23 2:a6e81c45ef48 198
dimitryjl23 2:a6e81c45ef48 199 //Reads SD card and passes value to frequency display
dimitryjl23 2:a6e81c45ef48 200 void display_thread(void const* args)
dimitryjl23 2:a6e81c45ef48 201 {
dimitryjl23 2:a6e81c45ef48 202 FILE *fp = fopen("/sd/africa-toto-array.txt","r");
dimitryjl23 2:a6e81c45ef48 203 if(fp == NULL) {
dimitryjl23 2:a6e81c45ef48 204 pc.printf("Error opening file\n");
dimitryjl23 2:a6e81c45ef48 205 return;
annieluo2 0:d6c9b09b4042 206 }
shyamgatech 5:598d94b45bad 207
dimitryjl23 2:a6e81c45ef48 208 int buffer[BUFFER_SIZE];
dimitryjl23 2:a6e81c45ef48 209 int buffer_index = 0;
dimitryjl23 2:a6e81c45ef48 210 int value;
dimitryjl23 2:a6e81c45ef48 211 pc.printf("Thread start\n");
shyamgatech 5:598d94b45bad 212 while(fscanf(fp,"%d,",&value) == 1) {
shyamgatech 5:598d94b45bad 213 buffer[buffer_index] = value;
shyamgatech 5:598d94b45bad 214 buffer_index++;
shyamgatech 5:598d94b45bad 215 pc.printf("%d\n",value);
shyamgatech 5:598d94b45bad 216 buffer_index++;
shyamgatech 5:598d94b45bad 217 if(buffer_index == BUFFER_SIZE) {
shyamgatech 5:598d94b45bad 218 //fast fourier tranform function here
shyamgatech 5:598d94b45bad 219 display_fft(buffer);
shyamgatech 5:598d94b45bad 220 buffer_index = 0;
shyamgatech 5:598d94b45bad 221 memset(buffer,0,sizeof(buffer));
shyamgatech 5:598d94b45bad 222
shyamgatech 5:598d94b45bad 223 }
dimitryjl23 2:a6e81c45ef48 224 }
shyamgatech 5:598d94b45bad 225
dimitryjl23 2:a6e81c45ef48 226 fclose(fp);
annieluo2 0:d6c9b09b4042 227 }
annieluo2 0:d6c9b09b4042 228
annieluo2 0:d6c9b09b4042 229 void speaker_thread(void const* args)
annieluo2 0:d6c9b09b4042 230 {
shyamgatech 5:598d94b45bad 231 while(1) {
annieluo2 1:5dd6801bb0d6 232 // check helper function for new song in GUI
annieluo2 0:d6c9b09b4042 233 // grab file here and put together string
annieluo2 0:d6c9b09b4042 234 // string song_title = "/sd/" + "" + ".wav";
shyamgatech 5:598d94b45bad 235
shyamgatech 5:598d94b45bad 236
dimitryjl23 2:a6e81c45ef48 237 /*
annieluo2 0:d6c9b09b4042 238 FILE *wave_file;
shyamgatech 5:598d94b45bad 239 dir = "/sd/" + song + ".wav";
shyamgatech 5:598d94b45bad 240 wave_file=fopen(dir.c_str(),"r");
shyamgatech 5:598d94b45bad 241
annieluo2 0:d6c9b09b4042 242 speaker_lock.lock();
shyamgatech 5:598d94b45bad 243 if (play)
shyamgatech 5:598d94b45bad 244 {
shyamgatech 5:598d94b45bad 245 waver.play(wave_file);
shyamgatech 5:598d94b45bad 246 }
annieluo2 0:d6c9b09b4042 247 speaker_lock.unlock();
annieluo2 0:d6c9b09b4042 248 fclose(wave_file);
annieluo2 0:d6c9b09b4042 249 Thread::wait(100);
dimitryjl23 2:a6e81c45ef48 250 */
annieluo2 0:d6c9b09b4042 251 }
annieluo2 0:d6c9b09b4042 252 }
shyamgatech 5:598d94b45bad 253
annieluo2 0:d6c9b09b4042 254
annieluo2 0:d6c9b09b4042 255 int main()
annieluo2 0:d6c9b09b4042 256 {
shyamgatech 5:598d94b45bad 257
annieluo2 0:d6c9b09b4042 258 Thread th1(speaker_thread);
annieluo2 1:5dd6801bb0d6 259 Thread th2(display_thread);
shyamgatech 5:598d94b45bad 260 char c;
annieluo2 0:d6c9b09b4042 261 while(1) {
annieluo2 1:5dd6801bb0d6 262 myled = 1;
annieluo2 1:5dd6801bb0d6 263 wait(0.2);
annieluo2 1:5dd6801bb0d6 264 myled = 0;
annieluo2 1:5dd6801bb0d6 265 wait(0.2);
shyamgatech 5:598d94b45bad 266 while(!pc.readable()) {
shyamgatech 5:598d94b45bad 267 Thread::wait(1);
shyamgatech 5:598d94b45bad 268 }
shyamgatech 5:598d94b45bad 269 if (pc.getc() == '!') {
shyamgatech 5:598d94b45bad 270 c = pc.getc();
shyamgatech 5:598d94b45bad 271 switch(c) {
shyamgatech 5:598d94b45bad 272 case '1':
shyamgatech 5:598d94b45bad 273 song = "africa-toto";
shyamgatech 5:598d94b45bad 274 break;
shyamgatech 5:598d94b45bad 275 case '2':
shyamgatech 5:598d94b45bad 276 song = "around_the_world-atc";
shyamgatech 5:598d94b45bad 277 break;
shyamgatech 5:598d94b45bad 278 case '3':
shyamgatech 5:598d94b45bad 279 song = "beautiful_life-ace_of_base";
shyamgatech 5:598d94b45bad 280 break;
shyamgatech 5:598d94b45bad 281 case '4':
shyamgatech 5:598d94b45bad 282 song = "dont_speak-no_doubt";
shyamgatech 5:598d94b45bad 283 break;
shyamgatech 5:598d94b45bad 284 case '5':
shyamgatech 5:598d94b45bad 285 song = "my-love";
shyamgatech 5:598d94b45bad 286 break;
shyamgatech 5:598d94b45bad 287 case '6':
shyamgatech 5:598d94b45bad 288 song = "Song1_test";
shyamgatech 5:598d94b45bad 289 break;
shyamgatech 5:598d94b45bad 290 case 'P':
shyamgatech 5:598d94b45bad 291 // wave_player plays
shyamgatech 5:598d94b45bad 292 play = true;
shyamgatech 5:598d94b45bad 293 break;
shyamgatech 5:598d94b45bad 294 case 'S':
shyamgatech 5:598d94b45bad 295 // wave_player stops
shyamgatech 5:598d94b45bad 296 play = false;
shyamgatech 5:598d94b45bad 297 break;
shyamgatech 5:598d94b45bad 298 default:
shyamgatech 5:598d94b45bad 299 break;
shyamgatech 5:598d94b45bad 300 }
shyamgatech 5:598d94b45bad 301 }
annieluo2 0:d6c9b09b4042 302 Thread::wait(100);
annieluo2 0:d6c9b09b4042 303 }
shyamgatech 5:598d94b45bad 304
annieluo2 0:d6c9b09b4042 305 }