this hurts
Dependencies: FFT
main.cpp@1:5dd6801bb0d6, 2020-12-02 (annotated)
- Committer:
- annieluo2
- Date:
- Wed Dec 02 23:39:20 2020 +0000
- Revision:
- 1:5dd6801bb0d6
- Parent:
- 0:d6c9b09b4042
- Child:
- 2:a6e81c45ef48
added display options
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; |
annieluo2 | 0:d6c9b09b4042 | 17 | |
annieluo2 | 0:d6c9b09b4042 | 18 | DigitalOut myled(LED1); // mbed LED |
annieluo2 | 0:d6c9b09b4042 | 19 | |
annieluo2 | 1:5dd6801bb0d6 | 20 | // delete later |
annieluo2 | 1:5dd6801bb0d6 | 21 | int sample[12][8] = { |
annieluo2 | 1:5dd6801bb0d6 | 22 | 34085,32666,29810,29823,27514,24175,24468,32559, |
annieluo2 | 1:5dd6801bb0d6 | 23 | 36401,33075,29037,21933,20189,21751,20975,20087, |
annieluo2 | 1:5dd6801bb0d6 | 24 | 17530,27723,30891,21128,19982,15446,12413,13597, |
annieluo2 | 1:5dd6801bb0d6 | 25 | 15776,14233,15622,16334,15485,17136,16389,17954, |
annieluo2 | 1:5dd6801bb0d6 | 26 | 20349,18486,23377,34447,34400,34681,33107,31171, |
annieluo2 | 1:5dd6801bb0d6 | 27 | 30007,33640,37007,38136,39187,38169,51831,52041, |
annieluo2 | 1:5dd6801bb0d6 | 28 | 47831,48634,44189,43878,44743,46468,43170,47172, |
annieluo2 | 1:5dd6801bb0d6 | 29 | 53518,52294,54825,51992,49619,48147,47901,48413, |
annieluo2 | 1:5dd6801bb0d6 | 30 | 46049,49090,47590,46154,45423,41223,41782,54695, |
annieluo2 | 1:5dd6801bb0d6 | 31 | 57452,51210,52462,59096,56711,52268,49364,42865, |
annieluo2 | 1:5dd6801bb0d6 | 32 | 41483,37964,39215,35908,31570,28332,24492,23121, |
annieluo2 | 1:5dd6801bb0d6 | 33 | 22874,18154,15541,15263,12463,12198,13301,9951 |
annieluo2 | 1:5dd6801bb0d6 | 34 | }; |
annieluo2 | 1:5dd6801bb0d6 | 35 | |
annieluo2 | 1:5dd6801bb0d6 | 36 | // states for display output |
annieluo2 | 1:5dd6801bb0d6 | 37 | const int on[8][8] = |
annieluo2 | 1:5dd6801bb0d6 | 38 | {1, 1, 1, 1, 1, 1, 1, 1, |
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 | 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 | |
annieluo2 | 1:5dd6801bb0d6 | 47 | const int high[8][8] = |
annieluo2 | 1:5dd6801bb0d6 | 48 | {0, 0, 0, 1, 1, 0, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 49 | 0, 0, 0, 1, 1, 0, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 50 | 0, 0, 1, 1, 1, 1, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 51 | 0, 1, 1, 1, 1, 1, 1, 0, |
annieluo2 | 1:5dd6801bb0d6 | 52 | 0, 1, 1, 1, 1, 1, 1, 0, |
annieluo2 | 1:5dd6801bb0d6 | 53 | 1, 1, 1, 1, 1, 1, 1, 1, |
annieluo2 | 1:5dd6801bb0d6 | 54 | 1, 1, 1, 1, 1, 1, 1, 1, |
annieluo2 | 1:5dd6801bb0d6 | 55 | 1, 1, 1, 1, 1, 1, 1, 1}; |
annieluo2 | 1:5dd6801bb0d6 | 56 | |
annieluo2 | 1:5dd6801bb0d6 | 57 | const int med[8][8] = |
annieluo2 | 1:5dd6801bb0d6 | 58 | {0, 0, 0, 0, 0, 0, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 59 | 0, 0, 0, 0, 0, 0, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 60 | 0, 0, 0, 1, 1, 0, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 61 | 0, 0, 1, 1, 1, 1, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 62 | 0, 0, 1, 1, 1, 1, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 63 | 0, 1, 1, 1, 1, 1, 1, 0, |
annieluo2 | 1:5dd6801bb0d6 | 64 | 1, 1, 1, 1, 1, 1, 1, 1, |
annieluo2 | 1:5dd6801bb0d6 | 65 | 1, 1, 1, 1, 1, 1, 1, 1}; |
annieluo2 | 1:5dd6801bb0d6 | 66 | |
annieluo2 | 1:5dd6801bb0d6 | 67 | const int low[8][8] = |
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 | 0, 0, 0, 0, 0, 0, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 73 | 0, 0, 1, 1, 1, 1, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 74 | 0, 1, 1, 1, 1, 1, 1, 0, |
annieluo2 | 1:5dd6801bb0d6 | 75 | 1, 1, 1, 1, 1, 1, 1, 1}; |
annieluo2 | 1:5dd6801bb0d6 | 76 | |
annieluo2 | 1:5dd6801bb0d6 | 77 | const int off[8][8] = |
annieluo2 | 1:5dd6801bb0d6 | 78 | {0, 0, 0, 0, 0, 0, 0, 0, |
annieluo2 | 1:5dd6801bb0d6 | 79 | 0, 0, 0, 0, 0, 0, 0, 0, |
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 | |
annieluo2 | 1:5dd6801bb0d6 | 87 | void display_thread(void const* args) { |
annieluo2 | 1:5dd6801bb0d6 | 88 | |
annieluo2 | 1:5dd6801bb0d6 | 89 | int state = 0; |
annieluo2 | 1:5dd6801bb0d6 | 90 | matrix.begin(0x70); |
annieluo2 | 1:5dd6801bb0d6 | 91 | |
annieluo2 | 0:d6c9b09b4042 | 92 | while(1) { |
annieluo2 | 1:5dd6801bb0d6 | 93 | for (int a = 0; a < 12; a++) { |
annieluo2 | 1:5dd6801bb0d6 | 94 | for (int b = 0; b < 8; b++) { |
annieluo2 | 1:5dd6801bb0d6 | 95 | |
annieluo2 | 1:5dd6801bb0d6 | 96 | // grab the FFT frequencies; |
annieluo2 | 1:5dd6801bb0d6 | 97 | int freq = sample[a][b]; |
annieluo2 | 1:5dd6801bb0d6 | 98 | |
annieluo2 | 1:5dd6801bb0d6 | 99 | // determine state based on the FFT output range |
annieluo2 | 1:5dd6801bb0d6 | 100 | if (freq >= 0 && freq < 10000) { |
annieluo2 | 1:5dd6801bb0d6 | 101 | state = 5; // OFF |
annieluo2 | 1:5dd6801bb0d6 | 102 | } else if (freq >= 10000 && freq < 25000) { |
annieluo2 | 1:5dd6801bb0d6 | 103 | state = 4; // LOW |
annieluo2 | 1:5dd6801bb0d6 | 104 | } else if (freq >= 25000 && freq < 40000) { |
annieluo2 | 1:5dd6801bb0d6 | 105 | state = 3; // MED |
annieluo2 | 1:5dd6801bb0d6 | 106 | } else if (freq >= 40000 && freq < 55000) { |
annieluo2 | 1:5dd6801bb0d6 | 107 | state = 2; // HIGH |
annieluo2 | 1:5dd6801bb0d6 | 108 | } else if (freq >= 55000) { |
annieluo2 | 1:5dd6801bb0d6 | 109 | state = 1; // ON |
annieluo2 | 1:5dd6801bb0d6 | 110 | } |
annieluo2 | 1:5dd6801bb0d6 | 111 | |
annieluo2 | 1:5dd6801bb0d6 | 112 | // switch statement based on the state |
annieluo2 | 1:5dd6801bb0d6 | 113 | switch (state) { |
annieluo2 | 1:5dd6801bb0d6 | 114 | case(1): // CASE HIGHEST |
annieluo2 | 1:5dd6801bb0d6 | 115 | matrix.clear(); |
annieluo2 | 1:5dd6801bb0d6 | 116 | for (int i = 0; i < 8; i++) { |
annieluo2 | 1:5dd6801bb0d6 | 117 | for (int j = 0; j < 8; j++) { |
annieluo2 | 1:5dd6801bb0d6 | 118 | if (on[i][j] == 1) { |
annieluo2 | 1:5dd6801bb0d6 | 119 | matrix.drawPixel(i, j, LED_ON); |
annieluo2 | 1:5dd6801bb0d6 | 120 | } |
annieluo2 | 1:5dd6801bb0d6 | 121 | } |
annieluo2 | 1:5dd6801bb0d6 | 122 | } |
annieluo2 | 1:5dd6801bb0d6 | 123 | matrix.writeDisplay(); |
annieluo2 | 1:5dd6801bb0d6 | 124 | wait(1); |
annieluo2 | 1:5dd6801bb0d6 | 125 | break; |
annieluo2 | 1:5dd6801bb0d6 | 126 | |
annieluo2 | 1:5dd6801bb0d6 | 127 | case(2): // CASE HIGH |
annieluo2 | 1:5dd6801bb0d6 | 128 | matrix.clear(); |
annieluo2 | 1:5dd6801bb0d6 | 129 | for (int i = 0; i < 8; i++) { |
annieluo2 | 1:5dd6801bb0d6 | 130 | for (int j = 0; j < 8; j++) { |
annieluo2 | 1:5dd6801bb0d6 | 131 | if (high[i][j] == 1) { |
annieluo2 | 1:5dd6801bb0d6 | 132 | matrix.drawPixel(i, j, LED_ON); |
annieluo2 | 1:5dd6801bb0d6 | 133 | } |
annieluo2 | 1:5dd6801bb0d6 | 134 | } |
annieluo2 | 1:5dd6801bb0d6 | 135 | } |
annieluo2 | 1:5dd6801bb0d6 | 136 | matrix.writeDisplay(); |
annieluo2 | 1:5dd6801bb0d6 | 137 | wait(1); |
annieluo2 | 1:5dd6801bb0d6 | 138 | |
annieluo2 | 1:5dd6801bb0d6 | 139 | case(3): // CASE MEDIUM |
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 (med[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); |
annieluo2 | 1:5dd6801bb0d6 | 150 | break; |
annieluo2 | 1:5dd6801bb0d6 | 151 | |
annieluo2 | 1:5dd6801bb0d6 | 152 | case(4): // CASE LOW |
annieluo2 | 1:5dd6801bb0d6 | 153 | matrix.clear(); |
annieluo2 | 1:5dd6801bb0d6 | 154 | for (int i = 0; i < 8; i++) { |
annieluo2 | 1:5dd6801bb0d6 | 155 | for (int j = 0; j < 8; j++) { |
annieluo2 | 1:5dd6801bb0d6 | 156 | if (low[i][j] == 1) { |
annieluo2 | 1:5dd6801bb0d6 | 157 | matrix.drawPixel(i, j, LED_ON); |
annieluo2 | 1:5dd6801bb0d6 | 158 | } |
annieluo2 | 1:5dd6801bb0d6 | 159 | } |
annieluo2 | 1:5dd6801bb0d6 | 160 | } |
annieluo2 | 1:5dd6801bb0d6 | 161 | matrix.writeDisplay(); |
annieluo2 | 1:5dd6801bb0d6 | 162 | wait(1); |
annieluo2 | 1:5dd6801bb0d6 | 163 | break; |
annieluo2 | 1:5dd6801bb0d6 | 164 | |
annieluo2 | 1:5dd6801bb0d6 | 165 | case(5): // CASE OFF |
annieluo2 | 1:5dd6801bb0d6 | 166 | matrix.clear(); |
annieluo2 | 1:5dd6801bb0d6 | 167 | for (int i = 0; i < 8; i++) { |
annieluo2 | 1:5dd6801bb0d6 | 168 | for (int j = 0; j < 8; j++) { |
annieluo2 | 1:5dd6801bb0d6 | 169 | if (off[i][j] == 1) { |
annieluo2 | 1:5dd6801bb0d6 | 170 | matrix.drawPixel(i, j, LED_ON); |
annieluo2 | 1:5dd6801bb0d6 | 171 | } |
annieluo2 | 1:5dd6801bb0d6 | 172 | } |
annieluo2 | 1:5dd6801bb0d6 | 173 | } |
annieluo2 | 1:5dd6801bb0d6 | 174 | matrix.writeDisplay(); |
annieluo2 | 1:5dd6801bb0d6 | 175 | wait(1); |
annieluo2 | 1:5dd6801bb0d6 | 176 | break; |
annieluo2 | 1:5dd6801bb0d6 | 177 | |
annieluo2 | 1:5dd6801bb0d6 | 178 | default: |
annieluo2 | 1:5dd6801bb0d6 | 179 | break; |
annieluo2 | 1:5dd6801bb0d6 | 180 | } |
annieluo2 | 1:5dd6801bb0d6 | 181 | |
annieluo2 | 1:5dd6801bb0d6 | 182 | |
annieluo2 | 1:5dd6801bb0d6 | 183 | } |
annieluo2 | 1:5dd6801bb0d6 | 184 | } |
annieluo2 | 0:d6c9b09b4042 | 185 | } |
annieluo2 | 0:d6c9b09b4042 | 186 | } |
annieluo2 | 0:d6c9b09b4042 | 187 | |
annieluo2 | 0:d6c9b09b4042 | 188 | void speaker_thread(void const* args) |
annieluo2 | 0:d6c9b09b4042 | 189 | { |
annieluo2 | 0:d6c9b09b4042 | 190 | while(1) |
annieluo2 | 1:5dd6801bb0d6 | 191 | { |
annieluo2 | 1:5dd6801bb0d6 | 192 | // check helper function for new song in GUI |
annieluo2 | 0:d6c9b09b4042 | 193 | // grab file here and put together string |
annieluo2 | 0:d6c9b09b4042 | 194 | // string song_title = "/sd/" + "" + ".wav"; |
annieluo2 | 0:d6c9b09b4042 | 195 | |
annieluo2 | 0:d6c9b09b4042 | 196 | FILE *wave_file; |
annieluo2 | 0:d6c9b09b4042 | 197 | wave_file=fopen("/sd/africa-toto.wav","r"); |
annieluo2 | 0:d6c9b09b4042 | 198 | |
annieluo2 | 0:d6c9b09b4042 | 199 | speaker_lock.lock(); |
annieluo2 | 0:d6c9b09b4042 | 200 | waver.play(wave_file); |
annieluo2 | 0:d6c9b09b4042 | 201 | speaker_lock.unlock(); |
annieluo2 | 0:d6c9b09b4042 | 202 | fclose(wave_file); |
annieluo2 | 0:d6c9b09b4042 | 203 | Thread::wait(100); |
annieluo2 | 0:d6c9b09b4042 | 204 | |
annieluo2 | 0:d6c9b09b4042 | 205 | } |
annieluo2 | 0:d6c9b09b4042 | 206 | } |
annieluo2 | 0:d6c9b09b4042 | 207 | |
annieluo2 | 0:d6c9b09b4042 | 208 | |
annieluo2 | 0:d6c9b09b4042 | 209 | int main() |
annieluo2 | 0:d6c9b09b4042 | 210 | { |
annieluo2 | 0:d6c9b09b4042 | 211 | |
annieluo2 | 0:d6c9b09b4042 | 212 | Thread th1(speaker_thread); |
annieluo2 | 1:5dd6801bb0d6 | 213 | Thread th2(display_thread); |
annieluo2 | 1:5dd6801bb0d6 | 214 | |
annieluo2 | 0:d6c9b09b4042 | 215 | while(1) { |
annieluo2 | 1:5dd6801bb0d6 | 216 | myled = 1; |
annieluo2 | 1:5dd6801bb0d6 | 217 | wait(0.2); |
annieluo2 | 1:5dd6801bb0d6 | 218 | myled = 0; |
annieluo2 | 1:5dd6801bb0d6 | 219 | wait(0.2); |
annieluo2 | 0:d6c9b09b4042 | 220 | Thread::wait(100); |
annieluo2 | 0:d6c9b09b4042 | 221 | } |
annieluo2 | 0:d6c9b09b4042 | 222 | |
annieluo2 | 0:d6c9b09b4042 | 223 | } |