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:
Tue Jun 17 14:03:36 2014 +0000
Revision:
0:b8c9dffbbe7e
Child:
1:7421267b0777
Simplified Tony Dicola Code - directly drives the on-board LED. Needs constant writing to serial port in order to work.

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