help :(
Dependencies: FFT
main.cpp@3:3dbf078989b1, 2020-12-03 (annotated)
- Committer:
- annieluo2
- Date:
- Thu Dec 03 05:07:27 2020 +0000
- Revision:
- 3:3dbf078989b1
- Parent:
- 2:a6e81c45ef48
- Child:
- 4:7974ef1b1e07
- Child:
- 6:d311a95f1e9b
hi
Who changed what in which revision?
User | Revision | Line number | New 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 | |
annieluo2 | 3:3dbf078989b1 | 20 | #define BUFFER_SIZE 64 |
annieluo2 | 1:5dd6801bb0d6 | 21 | |
annieluo2 | 1:5dd6801bb0d6 | 22 | // states for display output |
annieluo2 | 1:5dd6801bb0d6 | 23 | const int on[8][8] = |
annieluo2 | 1:5dd6801bb0d6 | 24 | {1, 1, 1, 1, 1, 1, 1, 1, |
annieluo2 | 1:5dd6801bb0d6 | 25 | 1, 1, 1, 1, 1, 1, 1, 1, |
annieluo2 | 1:5dd6801bb0d6 | 26 | 1, 1, 1, 1, 1, 1, 1, 1, |
annieluo2 | 1:5dd6801bb0d6 | 27 | 1, 1, 1, 1, 1, 1, 1, 1, |
annieluo2 | 1:5dd6801bb0d6 | 28 | 1, 1, 1, 1, 1, 1, 1, 1, |
annieluo2 | 1:5dd6801bb0d6 | 29 | 1, 1, 1, 1, 1, 1, 1, 1, |
annieluo2 | 1:5dd6801bb0d6 | 30 | 1, 1, 1, 1, 1, 1, 1, 1, |
annieluo2 | 1:5dd6801bb0d6 | 31 | 1, 1, 1, 1, 1, 1, 1, 1}; |
annieluo2 | 1:5dd6801bb0d6 | 32 | |
annieluo2 | 1:5dd6801bb0d6 | 33 | const int high[8][8] = |
annieluo2 | 1:5dd6801bb0d6 | 34 | {0, 0, 0, 1, 1, 0, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 35 | 0, 0, 0, 1, 1, 0, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 36 | 0, 0, 1, 1, 1, 1, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 37 | 0, 1, 1, 1, 1, 1, 1, 0, |
annieluo2 | 1:5dd6801bb0d6 | 38 | 0, 1, 1, 1, 1, 1, 1, 0, |
annieluo2 | 1:5dd6801bb0d6 | 39 | 1, 1, 1, 1, 1, 1, 1, 1, |
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 | |
annieluo2 | 1:5dd6801bb0d6 | 43 | const int med[8][8] = |
annieluo2 | 1:5dd6801bb0d6 | 44 | {0, 0, 0, 0, 0, 0, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 45 | 0, 0, 0, 0, 0, 0, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 46 | 0, 0, 0, 1, 1, 0, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 47 | 0, 0, 1, 1, 1, 1, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 48 | 0, 0, 1, 1, 1, 1, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 49 | 0, 1, 1, 1, 1, 1, 1, 0, |
annieluo2 | 1:5dd6801bb0d6 | 50 | 1, 1, 1, 1, 1, 1, 1, 1, |
annieluo2 | 1:5dd6801bb0d6 | 51 | 1, 1, 1, 1, 1, 1, 1, 1}; |
annieluo2 | 1:5dd6801bb0d6 | 52 | |
annieluo2 | 1:5dd6801bb0d6 | 53 | const int low[8][8] = |
annieluo2 | 1:5dd6801bb0d6 | 54 | {0, 0, 0, 0, 0, 0, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 55 | 0, 0, 0, 0, 0, 0, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 56 | 0, 0, 0, 0, 0, 0, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 57 | 0, 0, 0, 0, 0, 0, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 58 | 0, 0, 0, 0, 0, 0, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 59 | 0, 0, 1, 1, 1, 1, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 60 | 0, 1, 1, 1, 1, 1, 1, 0, |
annieluo2 | 1:5dd6801bb0d6 | 61 | 1, 1, 1, 1, 1, 1, 1, 1}; |
annieluo2 | 1:5dd6801bb0d6 | 62 | |
annieluo2 | 1:5dd6801bb0d6 | 63 | const int off[8][8] = |
annieluo2 | 1:5dd6801bb0d6 | 64 | {0, 0, 0, 0, 0, 0, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 65 | 0, 0, 0, 0, 0, 0, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 66 | 0, 0, 0, 0, 0, 0, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 67 | 0, 0, 0, 0, 0, 0, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 68 | 0, 0, 0, 0, 0, 0, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 69 | 0, 0, 0, 0, 0, 0, 0, 0, |
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 | |
dimitryjl23 | 2:a6e81c45ef48 | 73 | |
dimitryjl23 | 2:a6e81c45ef48 | 74 | |
annieluo2 | 3:3dbf078989b1 | 75 | void display_fft(float sample[]) { |
annieluo2 | 1:5dd6801bb0d6 | 76 | |
annieluo2 | 1:5dd6801bb0d6 | 77 | int state = 0; |
annieluo2 | 3:3dbf078989b1 | 78 | float freq = 0; |
annieluo2 | 1:5dd6801bb0d6 | 79 | matrix.begin(0x70); |
annieluo2 | 1:5dd6801bb0d6 | 80 | |
annieluo2 | 3:3dbf078989b1 | 81 | int s = sizeof(sample); |
annieluo2 | 3:3dbf078989b1 | 82 | |
annieluo2 | 3:3dbf078989b1 | 83 | pc.printf("%d\n", s); |
annieluo2 | 3:3dbf078989b1 | 84 | |
annieluo2 | 3:3dbf078989b1 | 85 | for (int a = 0; a < 64; a++) { |
dimitryjl23 | 2:a6e81c45ef48 | 86 | |
annieluo2 | 3:3dbf078989b1 | 87 | freq = sample[a]; |
annieluo2 | 3:3dbf078989b1 | 88 | |
annieluo2 | 3:3dbf078989b1 | 89 | pc.printf("%d\n",freq); |
annieluo2 | 1:5dd6801bb0d6 | 90 | |
annieluo2 | 1:5dd6801bb0d6 | 91 | // determine state based on the FFT output range |
annieluo2 | 3:3dbf078989b1 | 92 | if (freq < 10000) { |
annieluo2 | 1:5dd6801bb0d6 | 93 | state = 5; // OFF |
annieluo2 | 1:5dd6801bb0d6 | 94 | } else if (freq >= 10000 && freq < 25000) { |
annieluo2 | 1:5dd6801bb0d6 | 95 | state = 4; // LOW |
annieluo2 | 1:5dd6801bb0d6 | 96 | } else if (freq >= 25000 && freq < 40000) { |
annieluo2 | 1:5dd6801bb0d6 | 97 | state = 3; // MED |
annieluo2 | 1:5dd6801bb0d6 | 98 | } else if (freq >= 40000 && freq < 55000) { |
annieluo2 | 1:5dd6801bb0d6 | 99 | state = 2; // HIGH |
annieluo2 | 1:5dd6801bb0d6 | 100 | } else if (freq >= 55000) { |
annieluo2 | 3:3dbf078989b1 | 101 | state = 1; // HIGHEST |
annieluo2 | 1:5dd6801bb0d6 | 102 | } |
annieluo2 | 1:5dd6801bb0d6 | 103 | |
annieluo2 | 1:5dd6801bb0d6 | 104 | // switch statement based on the state |
annieluo2 | 1:5dd6801bb0d6 | 105 | switch (state) { |
annieluo2 | 1:5dd6801bb0d6 | 106 | case(1): // CASE HIGHEST |
annieluo2 | 1:5dd6801bb0d6 | 107 | matrix.clear(); |
annieluo2 | 1:5dd6801bb0d6 | 108 | for (int i = 0; i < 8; i++) { |
annieluo2 | 1:5dd6801bb0d6 | 109 | for (int j = 0; j < 8; j++) { |
annieluo2 | 1:5dd6801bb0d6 | 110 | if (on[i][j] == 1) { |
annieluo2 | 1:5dd6801bb0d6 | 111 | matrix.drawPixel(i, j, LED_ON); |
annieluo2 | 1:5dd6801bb0d6 | 112 | } |
annieluo2 | 1:5dd6801bb0d6 | 113 | } |
annieluo2 | 1:5dd6801bb0d6 | 114 | } |
annieluo2 | 1:5dd6801bb0d6 | 115 | matrix.writeDisplay(); |
annieluo2 | 1:5dd6801bb0d6 | 116 | wait(1); |
annieluo2 | 1:5dd6801bb0d6 | 117 | break; |
annieluo2 | 1:5dd6801bb0d6 | 118 | |
annieluo2 | 1:5dd6801bb0d6 | 119 | case(2): // CASE HIGH |
annieluo2 | 1:5dd6801bb0d6 | 120 | matrix.clear(); |
annieluo2 | 1:5dd6801bb0d6 | 121 | for (int i = 0; i < 8; i++) { |
annieluo2 | 1:5dd6801bb0d6 | 122 | for (int j = 0; j < 8; j++) { |
annieluo2 | 1:5dd6801bb0d6 | 123 | if (high[i][j] == 1) { |
annieluo2 | 1:5dd6801bb0d6 | 124 | matrix.drawPixel(i, j, LED_ON); |
annieluo2 | 1:5dd6801bb0d6 | 125 | } |
annieluo2 | 1:5dd6801bb0d6 | 126 | } |
annieluo2 | 1:5dd6801bb0d6 | 127 | } |
annieluo2 | 1:5dd6801bb0d6 | 128 | matrix.writeDisplay(); |
annieluo2 | 1:5dd6801bb0d6 | 129 | wait(1); |
annieluo2 | 1:5dd6801bb0d6 | 130 | |
annieluo2 | 1:5dd6801bb0d6 | 131 | case(3): // CASE MEDIUM |
annieluo2 | 1:5dd6801bb0d6 | 132 | matrix.clear(); |
annieluo2 | 1:5dd6801bb0d6 | 133 | for (int i = 0; i < 8; i++) { |
annieluo2 | 1:5dd6801bb0d6 | 134 | for (int j = 0; j < 8; j++) { |
annieluo2 | 1:5dd6801bb0d6 | 135 | if (med[i][j] == 1) { |
annieluo2 | 1:5dd6801bb0d6 | 136 | matrix.drawPixel(i, j, LED_ON); |
annieluo2 | 1:5dd6801bb0d6 | 137 | } |
annieluo2 | 1:5dd6801bb0d6 | 138 | } |
annieluo2 | 1:5dd6801bb0d6 | 139 | } |
annieluo2 | 1:5dd6801bb0d6 | 140 | matrix.writeDisplay(); |
annieluo2 | 1:5dd6801bb0d6 | 141 | wait(1); |
annieluo2 | 1:5dd6801bb0d6 | 142 | break; |
annieluo2 | 1:5dd6801bb0d6 | 143 | |
annieluo2 | 1:5dd6801bb0d6 | 144 | case(4): // CASE LOW |
annieluo2 | 1:5dd6801bb0d6 | 145 | matrix.clear(); |
annieluo2 | 1:5dd6801bb0d6 | 146 | for (int i = 0; i < 8; i++) { |
annieluo2 | 1:5dd6801bb0d6 | 147 | for (int j = 0; j < 8; j++) { |
annieluo2 | 1:5dd6801bb0d6 | 148 | if (low[i][j] == 1) { |
annieluo2 | 1:5dd6801bb0d6 | 149 | matrix.drawPixel(i, j, LED_ON); |
annieluo2 | 1:5dd6801bb0d6 | 150 | } |
annieluo2 | 1:5dd6801bb0d6 | 151 | } |
annieluo2 | 1:5dd6801bb0d6 | 152 | } |
annieluo2 | 1:5dd6801bb0d6 | 153 | matrix.writeDisplay(); |
annieluo2 | 1:5dd6801bb0d6 | 154 | wait(1); |
annieluo2 | 1:5dd6801bb0d6 | 155 | break; |
annieluo2 | 1:5dd6801bb0d6 | 156 | |
annieluo2 | 1:5dd6801bb0d6 | 157 | case(5): // CASE OFF |
annieluo2 | 1:5dd6801bb0d6 | 158 | matrix.clear(); |
annieluo2 | 1:5dd6801bb0d6 | 159 | for (int i = 0; i < 8; i++) { |
annieluo2 | 1:5dd6801bb0d6 | 160 | for (int j = 0; j < 8; j++) { |
annieluo2 | 1:5dd6801bb0d6 | 161 | if (off[i][j] == 1) { |
annieluo2 | 1:5dd6801bb0d6 | 162 | matrix.drawPixel(i, j, LED_ON); |
annieluo2 | 1:5dd6801bb0d6 | 163 | } |
annieluo2 | 1:5dd6801bb0d6 | 164 | } |
annieluo2 | 1:5dd6801bb0d6 | 165 | } |
annieluo2 | 1:5dd6801bb0d6 | 166 | matrix.writeDisplay(); |
annieluo2 | 1:5dd6801bb0d6 | 167 | wait(1); |
annieluo2 | 1:5dd6801bb0d6 | 168 | break; |
annieluo2 | 1:5dd6801bb0d6 | 169 | |
annieluo2 | 1:5dd6801bb0d6 | 170 | default: |
annieluo2 | 1:5dd6801bb0d6 | 171 | break; |
dimitryjl23 | 2:a6e81c45ef48 | 172 | } |
annieluo2 | 1:5dd6801bb0d6 | 173 | |
annieluo2 | 1:5dd6801bb0d6 | 174 | |
annieluo2 | 1:5dd6801bb0d6 | 175 | } |
annieluo2 | 3:3dbf078989b1 | 176 | |
annieluo2 | 3:3dbf078989b1 | 177 | pc.printf("done"); |
dimitryjl23 | 2:a6e81c45ef48 | 178 | |
dimitryjl23 | 2:a6e81c45ef48 | 179 | } |
dimitryjl23 | 2:a6e81c45ef48 | 180 | |
dimitryjl23 | 2:a6e81c45ef48 | 181 | //Reads SD card and passes value to frequency display |
dimitryjl23 | 2:a6e81c45ef48 | 182 | void display_thread(void const* args) |
dimitryjl23 | 2:a6e81c45ef48 | 183 | { |
dimitryjl23 | 2:a6e81c45ef48 | 184 | FILE *fp = fopen("/sd/africa-toto-array.txt","r"); |
dimitryjl23 | 2:a6e81c45ef48 | 185 | if(fp == NULL) { |
dimitryjl23 | 2:a6e81c45ef48 | 186 | pc.printf("Error opening file\n"); |
dimitryjl23 | 2:a6e81c45ef48 | 187 | return; |
annieluo2 | 0:d6c9b09b4042 | 188 | } |
dimitryjl23 | 2:a6e81c45ef48 | 189 | |
annieluo2 | 3:3dbf078989b1 | 190 | float buffer[BUFFER_SIZE]; |
dimitryjl23 | 2:a6e81c45ef48 | 191 | int buffer_index = 0; |
dimitryjl23 | 2:a6e81c45ef48 | 192 | int value; |
annieluo2 | 3:3dbf078989b1 | 193 | |
dimitryjl23 | 2:a6e81c45ef48 | 194 | while(fscanf(fp,"%d,",&value) == 1){ |
annieluo2 | 3:3dbf078989b1 | 195 | buffer[buffer_index] = (float)value; |
dimitryjl23 | 2:a6e81c45ef48 | 196 | buffer_index++; |
annieluo2 | 3:3dbf078989b1 | 197 | //pc.printf("%d\n",value); |
annieluo2 | 3:3dbf078989b1 | 198 | //buffer_index++; |
dimitryjl23 | 2:a6e81c45ef48 | 199 | if(buffer_index == BUFFER_SIZE) |
dimitryjl23 | 2:a6e81c45ef48 | 200 | { |
dimitryjl23 | 2:a6e81c45ef48 | 201 | //fast fourier tranform function here |
annieluo2 | 3:3dbf078989b1 | 202 | vRealFFT(buffer, 64); |
dimitryjl23 | 2:a6e81c45ef48 | 203 | display_fft(buffer); |
dimitryjl23 | 2:a6e81c45ef48 | 204 | buffer_index = 0; |
dimitryjl23 | 2:a6e81c45ef48 | 205 | memset(buffer,0,sizeof(buffer)); |
dimitryjl23 | 2:a6e81c45ef48 | 206 | |
dimitryjl23 | 2:a6e81c45ef48 | 207 | } |
dimitryjl23 | 2:a6e81c45ef48 | 208 | } |
dimitryjl23 | 2:a6e81c45ef48 | 209 | |
dimitryjl23 | 2:a6e81c45ef48 | 210 | fclose(fp); |
annieluo2 | 0:d6c9b09b4042 | 211 | } |
annieluo2 | 0:d6c9b09b4042 | 212 | |
annieluo2 | 0:d6c9b09b4042 | 213 | void speaker_thread(void const* args) |
annieluo2 | 0:d6c9b09b4042 | 214 | { |
annieluo2 | 0:d6c9b09b4042 | 215 | while(1) |
annieluo2 | 1:5dd6801bb0d6 | 216 | { |
annieluo2 | 1:5dd6801bb0d6 | 217 | // check helper function for new song in GUI |
annieluo2 | 0:d6c9b09b4042 | 218 | // grab file here and put together string |
annieluo2 | 0:d6c9b09b4042 | 219 | // string song_title = "/sd/" + "" + ".wav"; |
annieluo2 | 0:d6c9b09b4042 | 220 | |
dimitryjl23 | 2:a6e81c45ef48 | 221 | |
dimitryjl23 | 2:a6e81c45ef48 | 222 | /* |
annieluo2 | 0:d6c9b09b4042 | 223 | FILE *wave_file; |
annieluo2 | 0:d6c9b09b4042 | 224 | wave_file=fopen("/sd/africa-toto.wav","r"); |
annieluo2 | 0:d6c9b09b4042 | 225 | |
annieluo2 | 0:d6c9b09b4042 | 226 | speaker_lock.lock(); |
annieluo2 | 0:d6c9b09b4042 | 227 | waver.play(wave_file); |
annieluo2 | 0:d6c9b09b4042 | 228 | speaker_lock.unlock(); |
annieluo2 | 0:d6c9b09b4042 | 229 | fclose(wave_file); |
annieluo2 | 0:d6c9b09b4042 | 230 | Thread::wait(100); |
dimitryjl23 | 2:a6e81c45ef48 | 231 | */ |
annieluo2 | 0:d6c9b09b4042 | 232 | } |
annieluo2 | 0:d6c9b09b4042 | 233 | } |
annieluo2 | 0:d6c9b09b4042 | 234 | |
annieluo2 | 0:d6c9b09b4042 | 235 | |
annieluo2 | 0:d6c9b09b4042 | 236 | int main() |
annieluo2 | 0:d6c9b09b4042 | 237 | { |
annieluo2 | 0:d6c9b09b4042 | 238 | |
annieluo2 | 0:d6c9b09b4042 | 239 | Thread th1(speaker_thread); |
annieluo2 | 1:5dd6801bb0d6 | 240 | Thread th2(display_thread); |
annieluo2 | 1:5dd6801bb0d6 | 241 | |
annieluo2 | 0:d6c9b09b4042 | 242 | while(1) { |
annieluo2 | 1:5dd6801bb0d6 | 243 | myled = 1; |
annieluo2 | 1:5dd6801bb0d6 | 244 | wait(0.2); |
annieluo2 | 1:5dd6801bb0d6 | 245 | myled = 0; |
annieluo2 | 1:5dd6801bb0d6 | 246 | wait(0.2); |
annieluo2 | 0:d6c9b09b4042 | 247 | Thread::wait(100); |
annieluo2 | 0:d6c9b09b4042 | 248 | } |
annieluo2 | 0:d6c9b09b4042 | 249 | |
annieluo2 | 0:d6c9b09b4042 | 250 | } |