help :(

Dependencies:   FFT

Committer:
dimitryjl23
Date:
Thu Dec 03 00:33:19 2020 +0000
Revision:
2:a6e81c45ef48
Parent:
1:5dd6801bb0d6
Child:
3:3dbf078989b1
Child:
5:598d94b45bad
Merged SD card with FFT display. Wave player does NOT work simultaneously so it is commented out.

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