Simplified version of FFT code - drives on-board LED as a "Colour Organ".

Dependencies:   FastAnalogIn NVIC_set_all_priorities mbed-dsp mbed

Fork of KL25Z_FFT_Demo_tony by Tony Abbey

Committer:
tony1tf
Date:
Fri Jul 11 17:03:53 2014 +0000
Revision:
2:aa24865dfef5
Parent:
1:7c7539fba82b
various improvements and external LED

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tony1tf 0:b8c9dffbbe7e 1 // Audio Spectrum Display
tony1tf 0:b8c9dffbbe7e 2 // Copyright 2013 Tony DiCola (tony@tonydicola.com)
tony1tf 0:b8c9dffbbe7e 3 // Code ported from the guide at http://learn.adafruit.com/fft-fun-with-fourier-transforms?view=all
tony1tf 0:b8c9dffbbe7e 4 // mods by Tony Abbey to simplify code to drive tri-colour LED as a "colour organ"
tony1tf 0:b8c9dffbbe7e 5
tony1tf 0:b8c9dffbbe7e 6 #include "mbed.h"
tony1tf 0:b8c9dffbbe7e 7 #include "NVIC_set_all_priorities.h"
tony1tf 0:b8c9dffbbe7e 8 #include <ctype.h>
tony1tf 0:b8c9dffbbe7e 9 #include "arm_math.h"
tony1tf 0:b8c9dffbbe7e 10 #include "arm_const_structs.h"
tony1tf 0:b8c9dffbbe7e 11 #include "FastAnalogIn.h"
tony1tf 0:b8c9dffbbe7e 12
tony1tf 0:b8c9dffbbe7e 13 Serial pc(USBTX, USBRX);
tony1tf 0:b8c9dffbbe7e 14
tony1tf 0:b8c9dffbbe7e 15 FastAnalogIn Audio(PTC2);
tony1tf 0:b8c9dffbbe7e 16
tony1tf 2:aa24865dfef5 17 // #define RGBW_ext // Disable this line when you want to use the KL25Z on-board RGB LED.
tony1tf 0:b8c9dffbbe7e 18
tony1tf 0:b8c9dffbbe7e 19
tony1tf 0:b8c9dffbbe7e 20 #ifndef RGBW_ext
tony1tf 0:b8c9dffbbe7e 21 // RGB direct output to PWM channels - on-board RGB LED
tony1tf 0:b8c9dffbbe7e 22 PwmOut gled(LED_GREEN);
tony1tf 0:b8c9dffbbe7e 23 PwmOut rled(LED_RED);
tony1tf 0:b8c9dffbbe7e 24 PwmOut bled(LED_BLUE);
tony1tf 0:b8c9dffbbe7e 25 #else
tony1tf 2:aa24865dfef5 26 PwmOut gled(D6);
tony1tf 2:aa24865dfef5 27 PwmOut rled(D5);
tony1tf 2:aa24865dfef5 28 PwmOut bled(D7);
tony1tf 2:aa24865dfef5 29
tony1tf 0:b8c9dffbbe7e 30 #endif
tony1tf 0:b8c9dffbbe7e 31
tony1tf 0:b8c9dffbbe7e 32 // Dummy ISR for disabling NMI on PTA4 - !! DO NOT REMOVE THIS !!
tony1tf 0:b8c9dffbbe7e 33 // More info at https://mbed.org/questions/1387/How-can-I-access-the-FTFA_FOPT-register-/
tony1tf 0:b8c9dffbbe7e 34 extern "C" void NMI_Handler() {
tony1tf 0:b8c9dffbbe7e 35 DigitalIn test(PTA4);
tony1tf 0:b8c9dffbbe7e 36 }
tony1tf 0:b8c9dffbbe7e 37
tony1tf 0:b8c9dffbbe7e 38
tony1tf 0:b8c9dffbbe7e 39 ////////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 40 // CONFIGURATION
tony1tf 0:b8c9dffbbe7e 41 // These values can be changed to alter the behavior of the spectrum display.
tony1tf 0:b8c9dffbbe7e 42 // KL25Z limitations
tony1tf 0:b8c9dffbbe7e 43 // -----------------
tony1tf 0:b8c9dffbbe7e 44 // - When used with the Spectrogram python script :
tony1tf 0:b8c9dffbbe7e 45 // There is a substantial time lag between the music and the screen output.
tony1tf 0:b8c9dffbbe7e 46 // Max allowed SAMPLE_RATE_HZ is 40000
tony1tf 0:b8c9dffbbe7e 47 // Max allowed FFT_SIZE is 64
tony1tf 0:b8c9dffbbe7e 48 ////////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 49
tony1tf 2:aa24865dfef5 50 int SAMPLE_RATE_HZ = 8000; // Sample rate of the audio in hertz - note was 20000
tony1tf 2:aa24865dfef5 51 float SPECTRUM_MIN_DB = 33.0; // Audio intensity (in decibels) that maps to low LED brightness.
tony1tf 2:aa24865dfef5 52 float SPECTRUM_MAX_DB = 60.0; // Audio intensity (in decibels) that maps to high LED brightness.
tony1tf 0:b8c9dffbbe7e 53 int LEDS_ENABLED = 1; // Control if the LED's should display the spectrum or not. 1 is true, 0 is false.
tony1tf 0:b8c9dffbbe7e 54 // Useful for turning the LED display on and off with commands from the serial port.
tony1tf 0:b8c9dffbbe7e 55 const int FFT_SIZE = 64; // Size of the FFT.
tony1tf 0:b8c9dffbbe7e 56 const int PIXEL_COUNT = 3; // Number of pixels (RGB LED). You should be able to increase this without
tony1tf 0:b8c9dffbbe7e 57 // any other changes to the program.
tony1tf 0:b8c9dffbbe7e 58 const int MAX_CHARS = 65; // Max size of the input command buffer
tony1tf 1:7c7539fba82b 59 int PRINT_ON = 0; // flag to send chars continually to serial output
tony1tf 0:b8c9dffbbe7e 60
tony1tf 0:b8c9dffbbe7e 61 ////////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 62 // INTERNAL STATE
tony1tf 0:b8c9dffbbe7e 63 // These shouldn't be modified unless you know what you're doing.
tony1tf 0:b8c9dffbbe7e 64 ////////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 65 const static arm_cfft_instance_f32 *S;
tony1tf 0:b8c9dffbbe7e 66 Ticker samplingTimer;
tony1tf 0:b8c9dffbbe7e 67 float samples[FFT_SIZE*2];
tony1tf 0:b8c9dffbbe7e 68 float magnitudes[FFT_SIZE];
tony1tf 0:b8c9dffbbe7e 69 int sampleCounter = 0;
tony1tf 0:b8c9dffbbe7e 70 char commandBuffer[MAX_CHARS];
tony1tf 0:b8c9dffbbe7e 71 float frequencyWindow[PIXEL_COUNT+1];
tony1tf 0:b8c9dffbbe7e 72 float hues[PIXEL_COUNT];
tony1tf 2:aa24865dfef5 73 float oldhues[PIXEL_COUNT];
tony1tf 2:aa24865dfef5 74 float huescount = 1;
tony1tf 0:b8c9dffbbe7e 75 bool commandRecv = 0;
tony1tf 0:b8c9dffbbe7e 76 ////////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 77 // UTILITY FUNCTIONS
tony1tf 0:b8c9dffbbe7e 78 ////////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 79
tony1tf 0:b8c9dffbbe7e 80 void rxisr() {
tony1tf 0:b8c9dffbbe7e 81 char c = pc.getc();
tony1tf 0:b8c9dffbbe7e 82 // Add any characters that aren't the end of a command (semicolon) to the input buffer.
tony1tf 0:b8c9dffbbe7e 83 if (c != ';') {
tony1tf 0:b8c9dffbbe7e 84 c = toupper(c);
tony1tf 0:b8c9dffbbe7e 85 strncat(commandBuffer, &c, 1);
tony1tf 0:b8c9dffbbe7e 86 } else {
tony1tf 0:b8c9dffbbe7e 87 // Parse the command because an end of command token was encountered.
tony1tf 0:b8c9dffbbe7e 88 commandRecv = 1;
tony1tf 0:b8c9dffbbe7e 89 }
tony1tf 0:b8c9dffbbe7e 90 }
tony1tf 0:b8c9dffbbe7e 91
tony1tf 0:b8c9dffbbe7e 92 // Compute the average magnitude of a target frequency window vs. all other frequencies.
tony1tf 0:b8c9dffbbe7e 93 void windowMean(float* magnitudes, int lowBin, int highBin, float* windowMean, float* otherMean)
tony1tf 0:b8c9dffbbe7e 94 {
tony1tf 0:b8c9dffbbe7e 95 *windowMean = 0;
tony1tf 0:b8c9dffbbe7e 96 *otherMean = 0;
tony1tf 0:b8c9dffbbe7e 97 // Notice the first magnitude bin is skipped because it represents the
tony1tf 0:b8c9dffbbe7e 98 // average power of the signal.
tony1tf 0:b8c9dffbbe7e 99 for (int i = 1; i < FFT_SIZE/2; ++i) {
tony1tf 0:b8c9dffbbe7e 100 if (i >= lowBin && i <= highBin) {
tony1tf 0:b8c9dffbbe7e 101 *windowMean += magnitudes[i];
tony1tf 0:b8c9dffbbe7e 102 } else {
tony1tf 0:b8c9dffbbe7e 103 *otherMean += magnitudes[i];
tony1tf 0:b8c9dffbbe7e 104 }
tony1tf 0:b8c9dffbbe7e 105 }
tony1tf 0:b8c9dffbbe7e 106 *windowMean /= (highBin - lowBin) + 1;
tony1tf 0:b8c9dffbbe7e 107 *otherMean /= (FFT_SIZE / 2 - (highBin - lowBin));
tony1tf 0:b8c9dffbbe7e 108 }
tony1tf 0:b8c9dffbbe7e 109
tony1tf 0:b8c9dffbbe7e 110 // Convert a frequency to the appropriate FFT bin it will fall within.
tony1tf 0:b8c9dffbbe7e 111 int frequencyToBin(float frequency)
tony1tf 0:b8c9dffbbe7e 112 {
tony1tf 0:b8c9dffbbe7e 113 float binFrequency = float(SAMPLE_RATE_HZ) / float(FFT_SIZE);
tony1tf 0:b8c9dffbbe7e 114 return int(frequency / binFrequency);
tony1tf 0:b8c9dffbbe7e 115 }
tony1tf 0:b8c9dffbbe7e 116
tony1tf 0:b8c9dffbbe7e 117
tony1tf 0:b8c9dffbbe7e 118 ////////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 119 // SPECTRUM DISPLAY FUNCTIONS
tony1tf 0:b8c9dffbbe7e 120 ///////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 121
tony1tf 0:b8c9dffbbe7e 122 void spectrumSetup()
tony1tf 0:b8c9dffbbe7e 123 {
tony1tf 0:b8c9dffbbe7e 124 // Set the frequency window values by evenly dividing the possible frequency
tony1tf 0:b8c9dffbbe7e 125 // spectrum across the number of neo pixels.
tony1tf 0:b8c9dffbbe7e 126 float windowSize = (SAMPLE_RATE_HZ / 2.0) / float(PIXEL_COUNT);
tony1tf 0:b8c9dffbbe7e 127 for (int i = 0; i < PIXEL_COUNT+1; ++i) {
tony1tf 0:b8c9dffbbe7e 128 frequencyWindow[i] = i*windowSize;
tony1tf 0:b8c9dffbbe7e 129 }
tony1tf 0:b8c9dffbbe7e 130
tony1tf 0:b8c9dffbbe7e 131 }
tony1tf 0:b8c9dffbbe7e 132
tony1tf 0:b8c9dffbbe7e 133 void spectrumLoop()
tony1tf 0:b8c9dffbbe7e 134 {
tony1tf 0:b8c9dffbbe7e 135 // Update each LED based on the intensity of the audio
tony1tf 0:b8c9dffbbe7e 136 // in the associated frequency window.
tony1tf 2:aa24865dfef5 137 static int SLpixcnt = 0;
tony1tf 2:aa24865dfef5 138 float intensity, otherMean;
tony1tf 2:aa24865dfef5 139 int SLpixend = PIXEL_COUNT;
tony1tf 0:b8c9dffbbe7e 140 for (int i = SLpixcnt; i < SLpixend; ++i) {
tony1tf 0:b8c9dffbbe7e 141 windowMean(magnitudes,
tony1tf 0:b8c9dffbbe7e 142 frequencyToBin(frequencyWindow[i]),
tony1tf 0:b8c9dffbbe7e 143 frequencyToBin(frequencyWindow[i+1]),
tony1tf 0:b8c9dffbbe7e 144 &intensity,
tony1tf 0:b8c9dffbbe7e 145 &otherMean);
tony1tf 0:b8c9dffbbe7e 146 // Convert intensity to decibels.
tony1tf 0:b8c9dffbbe7e 147 intensity = 20.0*log10(intensity);
tony1tf 0:b8c9dffbbe7e 148 // Scale the intensity and clamp between 0 and 1.0.
tony1tf 0:b8c9dffbbe7e 149 intensity -= SPECTRUM_MIN_DB;
tony1tf 0:b8c9dffbbe7e 150 intensity = intensity < 0.0 ? 0.0 : intensity;
tony1tf 0:b8c9dffbbe7e 151 intensity /= (SPECTRUM_MAX_DB-SPECTRUM_MIN_DB);
tony1tf 2:aa24865dfef5 152 //intensity = intensity > 1.0 ? 1.0 : intensity;
tony1tf 2:aa24865dfef5 153 hues[i]=(intensity+oldhues[i]) / huescount; // averaging function
tony1tf 2:aa24865dfef5 154 oldhues[i]=hues[i];
tony1tf 2:aa24865dfef5 155 huescount += 1;
tony1tf 2:aa24865dfef5 156 if (huescount > 9) huescount = 1;
tony1tf 0:b8c9dffbbe7e 157 }
tony1tf 0:b8c9dffbbe7e 158 rled=1.0-hues[0] ; // onboard LED is common anode so inversion needed
tony1tf 0:b8c9dffbbe7e 159 gled=1.0-hues[1];
tony1tf 0:b8c9dffbbe7e 160 bled=1.0-hues[2];
tony1tf 0:b8c9dffbbe7e 161 }
tony1tf 0:b8c9dffbbe7e 162
tony1tf 0:b8c9dffbbe7e 163
tony1tf 0:b8c9dffbbe7e 164 ////////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 165 // SAMPLING FUNCTIONS
tony1tf 0:b8c9dffbbe7e 166 ////////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 167
tony1tf 0:b8c9dffbbe7e 168 void samplingCallback()
tony1tf 0:b8c9dffbbe7e 169 {
tony1tf 0:b8c9dffbbe7e 170 // Read from the ADC and store the sample data
tony1tf 2:aa24865dfef5 171 samples[sampleCounter] = (1024 * Audio) - 511.0f;
tony1tf 2:aa24865dfef5 172 // samples[sampleCounter] = Audio ; // just to see what this call actually produces
tony1tf 0:b8c9dffbbe7e 173 // Complex FFT functions require a coefficient for the imaginary part of the input.
tony1tf 0:b8c9dffbbe7e 174 // Since we only have real data, set this coefficient to zero.
tony1tf 0:b8c9dffbbe7e 175 samples[sampleCounter+1] = 0.0;
tony1tf 0:b8c9dffbbe7e 176 // Update sample buffer position and stop after the buffer is filled
tony1tf 0:b8c9dffbbe7e 177 sampleCounter += 2;
tony1tf 0:b8c9dffbbe7e 178 if (sampleCounter >= FFT_SIZE*2) {
tony1tf 0:b8c9dffbbe7e 179 samplingTimer.detach();
tony1tf 0:b8c9dffbbe7e 180 }
tony1tf 0:b8c9dffbbe7e 181 }
tony1tf 0:b8c9dffbbe7e 182
tony1tf 0:b8c9dffbbe7e 183 void samplingBegin()
tony1tf 0:b8c9dffbbe7e 184 {
tony1tf 0:b8c9dffbbe7e 185 // Reset sample buffer position and start callback at necessary rate.
tony1tf 0:b8c9dffbbe7e 186 sampleCounter = 0;
tony1tf 0:b8c9dffbbe7e 187 samplingTimer.attach_us(&samplingCallback, 1000000/SAMPLE_RATE_HZ);
tony1tf 0:b8c9dffbbe7e 188 }
tony1tf 0:b8c9dffbbe7e 189
tony1tf 0:b8c9dffbbe7e 190 bool samplingIsDone()
tony1tf 0:b8c9dffbbe7e 191 {
tony1tf 0:b8c9dffbbe7e 192 return sampleCounter >= FFT_SIZE*2;
tony1tf 0:b8c9dffbbe7e 193 }
tony1tf 0:b8c9dffbbe7e 194
tony1tf 0:b8c9dffbbe7e 195
tony1tf 0:b8c9dffbbe7e 196 ////////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 197 // COMMAND PARSING FUNCTIONS
tony1tf 0:b8c9dffbbe7e 198 // These functions allow parsing simple commands input on the serial port.
tony1tf 0:b8c9dffbbe7e 199 // Commands allow reading and writing variables that control the device.
tony1tf 0:b8c9dffbbe7e 200 //
tony1tf 0:b8c9dffbbe7e 201 // All commands must end with a semicolon character.
tony1tf 0:b8c9dffbbe7e 202 //
tony1tf 0:b8c9dffbbe7e 203 // Example commands are:
tony1tf 0:b8c9dffbbe7e 204 // GET SAMPLE_RATE_HZ;
tony1tf 0:b8c9dffbbe7e 205 // - Get the sample rate of the device.
tony1tf 0:b8c9dffbbe7e 206 // SET SAMPLE_RATE_HZ 400;
tony1tf 0:b8c9dffbbe7e 207 // - Set the sample rate of the device to 400 hertz.
tony1tf 0:b8c9dffbbe7e 208 //
tony1tf 0:b8c9dffbbe7e 209 ////////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 210
tony1tf 0:b8c9dffbbe7e 211 void parseCommand(char* command)
tony1tf 1:7c7539fba82b 212 {
tony1tf 0:b8c9dffbbe7e 213 if (strcmp(command, "GET MAGNITUDES") == 0) {
tony1tf 2:aa24865dfef5 214 for (int i = 1; i < FFT_SIZE; ++i) {
tony1tf 2:aa24865dfef5 215 printf("%4.2f,", magnitudes[i]);
tony1tf 0:b8c9dffbbe7e 216 }
tony1tf 0:b8c9dffbbe7e 217 } else if (strcmp(command, "GET SAMPLES") == 0) {
tony1tf 0:b8c9dffbbe7e 218 for (int i = 0; i < FFT_SIZE*2; i+=2) {
tony1tf 2:aa24865dfef5 219 printf("%f,", samples[i]);
tony1tf 0:b8c9dffbbe7e 220 }
tony1tf 0:b8c9dffbbe7e 221 } else if (strcmp(command, "GET FFT_SIZE") == 0) {
tony1tf 0:b8c9dffbbe7e 222 printf("%d\r\n", FFT_SIZE);
tony1tf 0:b8c9dffbbe7e 223 } else if (strcmp(command, "GET SAMPLE_RATE_HZ") == 0) {
tony1tf 0:b8c9dffbbe7e 224 printf("%d\r\n", SAMPLE_RATE_HZ);
tony1tf 0:b8c9dffbbe7e 225 } else if (strstr(command, "SET SAMPLE_RATE_HZ") != NULL) {
tony1tf 0:b8c9dffbbe7e 226 SAMPLE_RATE_HZ = (typeof(SAMPLE_RATE_HZ)) atof(command+(sizeof("SET SAMPLE_RATE_HZ")-1));
tony1tf 0:b8c9dffbbe7e 227 } else if (strcmp(command, "GET LEDS_ENABLED") == 0) {
tony1tf 0:b8c9dffbbe7e 228 printf("%d\r\n", LEDS_ENABLED);
tony1tf 0:b8c9dffbbe7e 229 } else if (strstr(command, "SET LEDS_ENABLED") != NULL) {
tony1tf 0:b8c9dffbbe7e 230 LEDS_ENABLED = (typeof(LEDS_ENABLED)) atof(command+(sizeof("SET LEDS_ENABLED")-1));
tony1tf 0:b8c9dffbbe7e 231 } else if (strcmp(command, "GET SPECTRUM_MIN_DB") == 0) {
tony1tf 0:b8c9dffbbe7e 232 printf("%f\r\n", SPECTRUM_MIN_DB);
tony1tf 0:b8c9dffbbe7e 233 } else if (strstr(command, "SET SPECTRUM_MIN_DB") != NULL) {
tony1tf 0:b8c9dffbbe7e 234 SPECTRUM_MIN_DB = (typeof(SPECTRUM_MIN_DB)) atof(command+(sizeof("SET SPECTRUM_MIN_DB")-1));
tony1tf 0:b8c9dffbbe7e 235 } else if (strcmp(command, "GET SPECTRUM_MAX_DB") == 0) {
tony1tf 0:b8c9dffbbe7e 236 printf("%f\r\n", SPECTRUM_MAX_DB);
tony1tf 0:b8c9dffbbe7e 237 } else if (strstr(command, "SET SPECTRUM_MAX_DB") != NULL) {
tony1tf 0:b8c9dffbbe7e 238 SPECTRUM_MAX_DB = (typeof(SPECTRUM_MAX_DB)) atof(command+(sizeof("SET SPECTRUM_MAX_DB")-1));
tony1tf 1:7c7539fba82b 239 } else if (strcmp(command, "GET HUES") == 0) {
tony1tf 1:7c7539fba82b 240 for (int i = 0; i < PIXEL_COUNT; ++i) {
tony1tf 1:7c7539fba82b 241 printf("%f\r\n", hues[i]);
tony1tf 1:7c7539fba82b 242 }
tony1tf 1:7c7539fba82b 243 } else if (strcmp(command, "GET FREQUENCIES") == 0) {
tony1tf 1:7c7539fba82b 244 for (int i = 0; i < PIXEL_COUNT; ++i) {
tony1tf 1:7c7539fba82b 245 printf("%f\r\n", frequencyWindow[i]);
tony1tf 1:7c7539fba82b 246 }
tony1tf 1:7c7539fba82b 247 } else if (strcmp(command, "PRINT_ON") == 0) {
tony1tf 1:7c7539fba82b 248 PRINT_ON = 1;
tony1tf 1:7c7539fba82b 249 } else if (strcmp(command, "PRINT_OFF") == 0) {
tony1tf 1:7c7539fba82b 250 PRINT_ON = 0;
tony1tf 1:7c7539fba82b 251 }
tony1tf 0:b8c9dffbbe7e 252 // Update spectrum display values if sample rate was changed.
tony1tf 0:b8c9dffbbe7e 253 if (strstr(command, "SET SAMPLE_RATE_HZ ") != NULL) {
tony1tf 0:b8c9dffbbe7e 254 spectrumSetup();
tony1tf 0:b8c9dffbbe7e 255 }
tony1tf 0:b8c9dffbbe7e 256
tony1tf 0:b8c9dffbbe7e 257
tony1tf 0:b8c9dffbbe7e 258 // Turn off the LEDs if the state changed.
tony1tf 0:b8c9dffbbe7e 259 if (LEDS_ENABLED == 0) {
tony1tf 0:b8c9dffbbe7e 260 }
tony1tf 0:b8c9dffbbe7e 261 }
tony1tf 0:b8c9dffbbe7e 262
tony1tf 0:b8c9dffbbe7e 263 void parserLoop()
tony1tf 0:b8c9dffbbe7e 264 {
tony1tf 0:b8c9dffbbe7e 265 // Process any incoming characters from the serial port
tony1tf 0:b8c9dffbbe7e 266 while (pc.readable()) {
tony1tf 0:b8c9dffbbe7e 267 char c = pc.getc();
tony1tf 0:b8c9dffbbe7e 268 // (doesnt work!) printf("%c",c); // echo characters typed
tony1tf 0:b8c9dffbbe7e 269 // Add any characters that aren't the end of a command (semicolon) to the input buffer.
tony1tf 0:b8c9dffbbe7e 270 if (c != ';') {
tony1tf 0:b8c9dffbbe7e 271 c = toupper(c);
tony1tf 0:b8c9dffbbe7e 272 strncat(commandBuffer, &c, 1);
tony1tf 0:b8c9dffbbe7e 273 } else {
tony1tf 0:b8c9dffbbe7e 274 // Parse the command because an end of command token was encountered.
tony1tf 0:b8c9dffbbe7e 275 parseCommand(commandBuffer);
tony1tf 0:b8c9dffbbe7e 276 // Clear the input buffer
tony1tf 0:b8c9dffbbe7e 277 memset(commandBuffer, 0, sizeof(commandBuffer));
tony1tf 0:b8c9dffbbe7e 278 }
tony1tf 0:b8c9dffbbe7e 279 }
tony1tf 0:b8c9dffbbe7e 280 }
tony1tf 0:b8c9dffbbe7e 281
tony1tf 0:b8c9dffbbe7e 282 ////////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 283 // MAIN FUNCTION
tony1tf 0:b8c9dffbbe7e 284 ////////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 285
tony1tf 0:b8c9dffbbe7e 286 int main()
tony1tf 0:b8c9dffbbe7e 287 {
tony1tf 0:b8c9dffbbe7e 288 NVIC_set_all_irq_priorities(1);
tony1tf 0:b8c9dffbbe7e 289 NVIC_SetPriority(UART0_IRQn, 0);
tony1tf 0:b8c9dffbbe7e 290 // Set up serial port.
tony1tf 0:b8c9dffbbe7e 291 pc.baud (38400);
tony1tf 0:b8c9dffbbe7e 292 pc.attach(&rxisr);
tony1tf 0:b8c9dffbbe7e 293
tony1tf 0:b8c9dffbbe7e 294 // Clear the input command buffer
tony1tf 0:b8c9dffbbe7e 295 memset(commandBuffer, 0, sizeof(commandBuffer));
tony1tf 0:b8c9dffbbe7e 296
tony1tf 0:b8c9dffbbe7e 297 // Initialize spectrum display
tony1tf 0:b8c9dffbbe7e 298 spectrumSetup();
tony1tf 0:b8c9dffbbe7e 299
tony1tf 0:b8c9dffbbe7e 300 // Begin sampling audio
tony1tf 0:b8c9dffbbe7e 301 samplingBegin();
tony1tf 0:b8c9dffbbe7e 302
tony1tf 0:b8c9dffbbe7e 303 // Init arm_ccft_32
tony1tf 0:b8c9dffbbe7e 304 switch (FFT_SIZE)
tony1tf 0:b8c9dffbbe7e 305 {
tony1tf 0:b8c9dffbbe7e 306 case 16:
tony1tf 0:b8c9dffbbe7e 307 S = & arm_cfft_sR_f32_len16;
tony1tf 0:b8c9dffbbe7e 308 break;
tony1tf 0:b8c9dffbbe7e 309 case 32:
tony1tf 0:b8c9dffbbe7e 310 S = & arm_cfft_sR_f32_len32;
tony1tf 0:b8c9dffbbe7e 311 break;
tony1tf 0:b8c9dffbbe7e 312 case 64:
tony1tf 0:b8c9dffbbe7e 313 S = & arm_cfft_sR_f32_len64;
tony1tf 0:b8c9dffbbe7e 314 break;
tony1tf 0:b8c9dffbbe7e 315 case 128:
tony1tf 0:b8c9dffbbe7e 316 S = & arm_cfft_sR_f32_len128;
tony1tf 0:b8c9dffbbe7e 317 break;
tony1tf 0:b8c9dffbbe7e 318 case 256:
tony1tf 0:b8c9dffbbe7e 319 S = & arm_cfft_sR_f32_len256;
tony1tf 0:b8c9dffbbe7e 320 break;
tony1tf 0:b8c9dffbbe7e 321 case 512:
tony1tf 0:b8c9dffbbe7e 322 S = & arm_cfft_sR_f32_len512;
tony1tf 0:b8c9dffbbe7e 323 break;
tony1tf 0:b8c9dffbbe7e 324 case 1024:
tony1tf 0:b8c9dffbbe7e 325 S = & arm_cfft_sR_f32_len1024;
tony1tf 0:b8c9dffbbe7e 326 break;
tony1tf 0:b8c9dffbbe7e 327 case 2048:
tony1tf 0:b8c9dffbbe7e 328 S = & arm_cfft_sR_f32_len2048;
tony1tf 0:b8c9dffbbe7e 329 break;
tony1tf 0:b8c9dffbbe7e 330 case 4096:
tony1tf 0:b8c9dffbbe7e 331 S = & arm_cfft_sR_f32_len4096;
tony1tf 0:b8c9dffbbe7e 332 break;
tony1tf 0:b8c9dffbbe7e 333 }
tony1tf 0:b8c9dffbbe7e 334
tony1tf 0:b8c9dffbbe7e 335 while(1) {
tony1tf 0:b8c9dffbbe7e 336 // Calculate FFT if a full sample is available.
tony1tf 0:b8c9dffbbe7e 337 if (samplingIsDone()) {
tony1tf 0:b8c9dffbbe7e 338 // Run FFT on sample data.
tony1tf 0:b8c9dffbbe7e 339 arm_cfft_f32(S, samples, 0, 1);
tony1tf 0:b8c9dffbbe7e 340 // Calculate magnitude of complex numbers output by the FFT.
tony1tf 0:b8c9dffbbe7e 341 arm_cmplx_mag_f32(samples, magnitudes, FFT_SIZE);
tony1tf 0:b8c9dffbbe7e 342
tony1tf 0:b8c9dffbbe7e 343 if (LEDS_ENABLED == 1) {
tony1tf 0:b8c9dffbbe7e 344 spectrumLoop();
tony1tf 0:b8c9dffbbe7e 345 }
tony1tf 1:7c7539fba82b 346 wait_ms(10);
tony1tf 0:b8c9dffbbe7e 347 // Restart audio sampling.
tony1tf 0:b8c9dffbbe7e 348 samplingBegin();
tony1tf 1:7c7539fba82b 349 if (PRINT_ON == 1) {
tony1tf 1:7c7539fba82b 350 for (int i = 0; i < PIXEL_COUNT; ++i) {
tony1tf 1:7c7539fba82b 351 printf("%f\r\n", hues[i]);
tony1tf 1:7c7539fba82b 352 }
tony1tf 1:7c7539fba82b 353 }
tony1tf 1:7c7539fba82b 354 }
tony1tf 0:b8c9dffbbe7e 355
tony1tf 0:b8c9dffbbe7e 356 // Parse any pending commands.
tony1tf 0:b8c9dffbbe7e 357 if(commandRecv) {
tony1tf 0:b8c9dffbbe7e 358 // pc.attach(NULL);
tony1tf 0:b8c9dffbbe7e 359 parseCommand(commandBuffer);
tony1tf 0:b8c9dffbbe7e 360 commandRecv = 0;
tony1tf 0:b8c9dffbbe7e 361 // Clear the input buffer
tony1tf 0:b8c9dffbbe7e 362 memset(commandBuffer, 0, sizeof(commandBuffer));
tony1tf 0:b8c9dffbbe7e 363 // pc.attach(&rxisr);
tony1tf 0:b8c9dffbbe7e 364 }
tony1tf 0:b8c9dffbbe7e 365 }
tony1tf 0:b8c9dffbbe7e 366 }