Audio Reactive Infinity Mirror
Dependencies: mbed-dsp mbed-rtos mbed
Infinity Mirror Project Page https://developer.mbed.org/users/mgolino/notebook/infinity-mirror/
main.cpp@0:85385a11b2da, 2015-12-12 (annotated)
- Committer:
- mgolino
- Date:
- Sat Dec 12 01:22:30 2015 +0000
- Revision:
- 0:85385a11b2da
Audio Reactive Infinity Mirror
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mgolino | 0:85385a11b2da | 1 | // ECE 4180 Final Project |
mgolino | 0:85385a11b2da | 2 | // Fall 2015 |
mgolino | 0:85385a11b2da | 3 | // Matthew Ashcraft 4th year EE |
mgolino | 0:85385a11b2da | 4 | // Matthew Golino 5th year CE |
mgolino | 0:85385a11b2da | 5 | // |
mgolino | 0:85385a11b2da | 6 | // Bluetooth Controlled Audio Reactive Infinity Mirror |
mgolino | 0:85385a11b2da | 7 | |
mgolino | 0:85385a11b2da | 8 | #include "mbed.h" |
mgolino | 0:85385a11b2da | 9 | #include "ble_mini.h" |
mgolino | 0:85385a11b2da | 10 | #include <iostream> |
mgolino | 0:85385a11b2da | 11 | #include "fftstuff.h" |
mgolino | 0:85385a11b2da | 12 | |
mgolino | 0:85385a11b2da | 13 | //////////////////////////////////////////////////////////////////////////////// |
mgolino | 0:85385a11b2da | 14 | // MBED PIN DESIGNATION |
mgolino | 0:85385a11b2da | 15 | // DO NOT change unless accompanying hardware is altered |
mgolino | 0:85385a11b2da | 16 | //////////////////////////////////////////////////////////////////////////////// |
mgolino | 0:85385a11b2da | 17 | // Analog |
mgolino | 0:85385a11b2da | 18 | //AnalogIn mic(p15); // Analog input for the microphone |
mgolino | 0:85385a11b2da | 19 | //AnalogIn aux(p20); // Analog input for the DC offset aux cable input |
mgolino | 0:85385a11b2da | 20 | // PWM outputs for RGB LEDs |
mgolino | 0:85385a11b2da | 21 | PwmOut gled(p23); |
mgolino | 0:85385a11b2da | 22 | PwmOut rled(p22); |
mgolino | 0:85385a11b2da | 23 | PwmOut bled(p21); |
mgolino | 0:85385a11b2da | 24 | // Serial Interface for Mbed to PC |
mgolino | 0:85385a11b2da | 25 | Serial pc(USBTX, USBRX); |
mgolino | 0:85385a11b2da | 26 | // Serial Interface for Bluetooth Module to Mbed |
mgolino | 0:85385a11b2da | 27 | Serial device(p9,p10); |
mgolino | 0:85385a11b2da | 28 | // END MBED PIN DESIGNATION |
mgolino | 0:85385a11b2da | 29 | |
mgolino | 0:85385a11b2da | 30 | //////////////////////////////////////////////////////////////////////////////// |
mgolino | 0:85385a11b2da | 31 | // GLOBAL VARIABLES |
mgolino | 0:85385a11b2da | 32 | // Variables used across multiple functions, simpler than passing to functions |
mgolino | 0:85385a11b2da | 33 | //////////////////////////////////////////////////////////////////////////////// |
mgolino | 0:85385a11b2da | 34 | |
mgolino | 0:85385a11b2da | 35 | char buf[4]; // Char buffer for bluetooth commands |
mgolino | 0:85385a11b2da | 36 | // Boolean flags for audio sampling |
mgolino | 0:85385a11b2da | 37 | bool AuxIn = false; |
mgolino | 0:85385a11b2da | 38 | bool MicIn = false; |
mgolino | 0:85385a11b2da | 39 | bool inputSelect = false; //false = aux --- true = mic |
mgolino | 0:85385a11b2da | 40 | // Float variable used to write to LED PWM outputs |
mgolino | 0:85385a11b2da | 41 | float redVal = -1; |
mgolino | 0:85385a11b2da | 42 | float greenVal = -1; |
mgolino | 0:85385a11b2da | 43 | float blueVal = -1; |
mgolino | 0:85385a11b2da | 44 | // Float used to "save" previous values for manual writing |
mgolino | 0:85385a11b2da | 45 | float redHold = -1; |
mgolino | 0:85385a11b2da | 46 | float greenHold = -1; |
mgolino | 0:85385a11b2da | 47 | float blueHold = -1; |
mgolino | 0:85385a11b2da | 48 | //strobe values |
mgolino | 0:85385a11b2da | 49 | float strobeVal = -1; |
mgolino | 0:85385a11b2da | 50 | float strobeHold = 0; |
mgolino | 0:85385a11b2da | 51 | // END GLOBAL VARIABLES |
mgolino | 0:85385a11b2da | 52 | |
mgolino | 0:85385a11b2da | 53 | |
mgolino | 0:85385a11b2da | 54 | //////////////////////////////////////////////////////////////////////////////// |
mgolino | 0:85385a11b2da | 55 | // LED WRITING FUNCTIONS |
mgolino | 0:85385a11b2da | 56 | //////////////////////////////////////////////////////////////////////////////// |
mgolino | 0:85385a11b2da | 57 | // Write stored RGB values to PWM outputs |
mgolino | 0:85385a11b2da | 58 | void writeColors(float redVal, float greenVal, float blueVal, float strobeVal) |
mgolino | 0:85385a11b2da | 59 | { |
mgolino | 0:85385a11b2da | 60 | if (strobeVal > 0) { |
mgolino | 0:85385a11b2da | 61 | rled = 0; |
mgolino | 0:85385a11b2da | 62 | gled = 0; |
mgolino | 0:85385a11b2da | 63 | bled = 0; |
mgolino | 0:85385a11b2da | 64 | wait(strobeVal); |
mgolino | 0:85385a11b2da | 65 | } |
mgolino | 0:85385a11b2da | 66 | if ((redVal >= 0) || (greenVal >= 0) || (blueVal >= 0)) { |
mgolino | 0:85385a11b2da | 67 | rled = redVal; |
mgolino | 0:85385a11b2da | 68 | gled = greenVal; |
mgolino | 0:85385a11b2da | 69 | bled = blueVal; |
mgolino | 0:85385a11b2da | 70 | } |
mgolino | 0:85385a11b2da | 71 | } |
mgolino | 0:85385a11b2da | 72 | // END LED WRITING FUNCTIONS |
mgolino | 0:85385a11b2da | 73 | |
mgolino | 0:85385a11b2da | 74 | int main() { |
mgolino | 0:85385a11b2da | 75 | // Initialize frequency window size |
mgolino | 0:85385a11b2da | 76 | // Set BLE mini baud rate |
mgolino | 0:85385a11b2da | 77 | device.baud(57600); |
mgolino | 0:85385a11b2da | 78 | // Floats used to "hold" RGB values |
mgolino | 0:85385a11b2da | 79 | // float redHold = -1; |
mgolino | 0:85385a11b2da | 80 | // float greenHold = -1; |
mgolino | 0:85385a11b2da | 81 | // float blueHold = -1; |
mgolino | 0:85385a11b2da | 82 | // Floats used to set and "hold" strobe setting |
mgolino | 0:85385a11b2da | 83 | |
mgolino | 0:85385a11b2da | 84 | while(1) { |
mgolino | 0:85385a11b2da | 85 | if(device.readable()) { |
mgolino | 0:85385a11b2da | 86 | // Read in Bluetooth commands |
mgolino | 0:85385a11b2da | 87 | for(int i = 0; i < 4; i++){ |
mgolino | 0:85385a11b2da | 88 | buf[i]=device.getc(); |
mgolino | 0:85385a11b2da | 89 | pc.printf("%x ",buf[i]); |
mgolino | 0:85385a11b2da | 90 | } |
mgolino | 0:85385a11b2da | 91 | pc.printf("\n\r"); |
mgolino | 0:85385a11b2da | 92 | pc.printf("done\n"); |
mgolino | 0:85385a11b2da | 93 | |
mgolino | 0:85385a11b2da | 94 | // Check cases for all different switch options based on buf[0]; |
mgolino | 0:85385a11b2da | 95 | char whichInput = buf[0]; |
mgolino | 0:85385a11b2da | 96 | // Check for Bluetooth Connectivity |
mgolino | 0:85385a11b2da | 97 | if(whichInput == BLE_CONNECTED){ |
mgolino | 0:85385a11b2da | 98 | pc.printf("case0\n\n"); |
mgolino | 0:85385a11b2da | 99 | //pretty lights for connect |
mgolino | 0:85385a11b2da | 100 | rled = 255; |
mgolino | 0:85385a11b2da | 101 | wait(0.25); |
mgolino | 0:85385a11b2da | 102 | rled=0; |
mgolino | 0:85385a11b2da | 103 | gled=255; |
mgolino | 0:85385a11b2da | 104 | wait(0.25); |
mgolino | 0:85385a11b2da | 105 | gled=0; |
mgolino | 0:85385a11b2da | 106 | bled=255; |
mgolino | 0:85385a11b2da | 107 | wait(0.25); |
mgolino | 0:85385a11b2da | 108 | bled=0; |
mgolino | 0:85385a11b2da | 109 | } |
mgolino | 0:85385a11b2da | 110 | else if(whichInput == LIGHTS_OFF_SWITCH){ // Manually turn off lights |
mgolino | 0:85385a11b2da | 111 | pc.printf("Turning All Lights Off\n\n"); |
mgolino | 0:85385a11b2da | 112 | strobeVal = -1; |
mgolino | 0:85385a11b2da | 113 | if(buf[1]==0x01){ |
mgolino | 0:85385a11b2da | 114 | redVal = 0.0f; |
mgolino | 0:85385a11b2da | 115 | greenVal = 0.0f; |
mgolino | 0:85385a11b2da | 116 | blueVal = 0.0f; |
mgolino | 0:85385a11b2da | 117 | } |
mgolino | 0:85385a11b2da | 118 | else if(buf[1]==0x00){ |
mgolino | 0:85385a11b2da | 119 | redVal = -1.0f; |
mgolino | 0:85385a11b2da | 120 | greenVal = -1.0f; |
mgolino | 0:85385a11b2da | 121 | blueVal = -1.0f; |
mgolino | 0:85385a11b2da | 122 | } |
mgolino | 0:85385a11b2da | 123 | } |
mgolino | 0:85385a11b2da | 124 | else if(whichInput == IR_SWITCH){ // React to IR remote |
mgolino | 0:85385a11b2da | 125 | pc.printf("Listening to IR Remote\n\n"); |
mgolino | 0:85385a11b2da | 126 | strobeVal = -1; |
mgolino | 0:85385a11b2da | 127 | //TBD |
mgolino | 0:85385a11b2da | 128 | |
mgolino | 0:85385a11b2da | 129 | |
mgolino | 0:85385a11b2da | 130 | } |
mgolino | 0:85385a11b2da | 131 | |
mgolino | 0:85385a11b2da | 132 | else if(whichInput == MIC_SWITCH){ // React to microphone audio input |
mgolino | 0:85385a11b2da | 133 | pc.printf("Listening to Microphone for FFT\n\n"); |
mgolino | 0:85385a11b2da | 134 | strobeVal = -1; |
mgolino | 0:85385a11b2da | 135 | if(buf[1]==0x00){ //switched off |
mgolino | 0:85385a11b2da | 136 | redVal = 0; |
mgolino | 0:85385a11b2da | 137 | greenVal = 0; |
mgolino | 0:85385a11b2da | 138 | blueVal = 0; |
mgolino | 0:85385a11b2da | 139 | } |
mgolino | 0:85385a11b2da | 140 | else if (buf[1]==0x01){ //switched on |
mgolino | 0:85385a11b2da | 141 | //MicIn = true; //FALSE = MIC CONTROLS -- REMEMBER TO FIX IN CODE |
mgolino | 0:85385a11b2da | 142 | inputSelect = true; |
mgolino | 0:85385a11b2da | 143 | float colors[3] = {0.0f,0.0f,0.0f}; |
mgolino | 0:85385a11b2da | 144 | |
mgolino | 0:85385a11b2da | 145 | while(!device.readable()){ // done to improve speed => bypass long if statement |
mgolino | 0:85385a11b2da | 146 | runFFT(inputSelect, colors); |
mgolino | 0:85385a11b2da | 147 | redVal = colors[0]; |
mgolino | 0:85385a11b2da | 148 | greenVal = colors[1]; |
mgolino | 0:85385a11b2da | 149 | blueVal = colors[2];; |
mgolino | 0:85385a11b2da | 150 | writeColors(redVal, greenVal, blueVal, strobeVal); |
mgolino | 0:85385a11b2da | 151 | } |
mgolino | 0:85385a11b2da | 152 | } |
mgolino | 0:85385a11b2da | 153 | redVal = -1; |
mgolino | 0:85385a11b2da | 154 | greenVal = -1; |
mgolino | 0:85385a11b2da | 155 | blueVal = -1; |
mgolino | 0:85385a11b2da | 156 | } |
mgolino | 0:85385a11b2da | 157 | else if(whichInput == AUX_SWITCH){ // React to aux cable audio input |
mgolino | 0:85385a11b2da | 158 | pc.printf("Listening to Aux for FFT\n\n"); |
mgolino | 0:85385a11b2da | 159 | strobeVal = -1; |
mgolino | 0:85385a11b2da | 160 | if(buf[1]==0x00){ //switched off |
mgolino | 0:85385a11b2da | 161 | redVal = 0; |
mgolino | 0:85385a11b2da | 162 | greenVal = 0; |
mgolino | 0:85385a11b2da | 163 | blueVal = 0; |
mgolino | 0:85385a11b2da | 164 | } |
mgolino | 0:85385a11b2da | 165 | else if (buf[1]==0x01){ //switched on |
mgolino | 0:85385a11b2da | 166 | inputSelect = false; |
mgolino | 0:85385a11b2da | 167 | float colors[3] = {0.0f,0.0f,0.0f}; |
mgolino | 0:85385a11b2da | 168 | |
mgolino | 0:85385a11b2da | 169 | while(!device.readable()){ // done to improve speed => bypass long if statement |
mgolino | 0:85385a11b2da | 170 | runFFT(inputSelect, colors); |
mgolino | 0:85385a11b2da | 171 | redVal = colors[0]; |
mgolino | 0:85385a11b2da | 172 | greenVal = colors[1]; |
mgolino | 0:85385a11b2da | 173 | blueVal = colors[2];; |
mgolino | 0:85385a11b2da | 174 | writeColors(redVal, greenVal, blueVal, strobeVal); |
mgolino | 0:85385a11b2da | 175 | } |
mgolino | 0:85385a11b2da | 176 | } |
mgolino | 0:85385a11b2da | 177 | redVal = -1; |
mgolino | 0:85385a11b2da | 178 | greenVal = -1; |
mgolino | 0:85385a11b2da | 179 | blueVal = -1; |
mgolino | 0:85385a11b2da | 180 | } |
mgolino | 0:85385a11b2da | 181 | else if(whichInput == MANUAL_SWITCH){ // Manual color control |
mgolino | 0:85385a11b2da | 182 | pc.printf("Colors are Being Controlled Manually\n\n"); |
mgolino | 0:85385a11b2da | 183 | strobeVal = strobeHold; |
mgolino | 0:85385a11b2da | 184 | if ((redVal < 0) && (redHold < 0)){ |
mgolino | 0:85385a11b2da | 185 | redVal = 128.0f/255; |
mgolino | 0:85385a11b2da | 186 | greenVal = 128.0f/255; |
mgolino | 0:85385a11b2da | 187 | blueVal = 128.0f/255; |
mgolino | 0:85385a11b2da | 188 | } |
mgolino | 0:85385a11b2da | 189 | else{ |
mgolino | 0:85385a11b2da | 190 | redVal = redHold; |
mgolino | 0:85385a11b2da | 191 | greenVal = greenHold; |
mgolino | 0:85385a11b2da | 192 | blueVal = blueHold; |
mgolino | 0:85385a11b2da | 193 | } |
mgolino | 0:85385a11b2da | 194 | |
mgolino | 0:85385a11b2da | 195 | if(buf[1] == 0x00){ |
mgolino | 0:85385a11b2da | 196 | redVal = -1; |
mgolino | 0:85385a11b2da | 197 | greenVal = -1; |
mgolino | 0:85385a11b2da | 198 | blueVal = -1; |
mgolino | 0:85385a11b2da | 199 | } |
mgolino | 0:85385a11b2da | 200 | } |
mgolino | 0:85385a11b2da | 201 | else if(whichInput == STROBE_SPEED){ // Strobe feature for manual control |
mgolino | 0:85385a11b2da | 202 | pc.printf("Strobe Speed Changed\n\n"); |
mgolino | 0:85385a11b2da | 203 | float strobeSpeed = buf[1]; // Value 0-255 to be used in determining strobe speed |
mgolino | 0:85385a11b2da | 204 | //determine how to control strobe |
mgolino | 0:85385a11b2da | 205 | //pc.printf("Strobe rate: %f", strobeSpeed); |
mgolino | 0:85385a11b2da | 206 | strobeVal = 0.35 - (strobeSpeed*0.35/255); |
mgolino | 0:85385a11b2da | 207 | if(strobeSpeed == 0){ |
mgolino | 0:85385a11b2da | 208 | strobeVal=0; |
mgolino | 0:85385a11b2da | 209 | } |
mgolino | 0:85385a11b2da | 210 | strobeHold = strobeVal; |
mgolino | 0:85385a11b2da | 211 | } |
mgolino | 0:85385a11b2da | 212 | else if(whichInput == COLOR_SELECT){ |
mgolino | 0:85385a11b2da | 213 | pc.printf("case7\n\n"); |
mgolino | 0:85385a11b2da | 214 | // Convert from hex to float |
mgolino | 0:85385a11b2da | 215 | redVal = buf[1]; |
mgolino | 0:85385a11b2da | 216 | greenVal = buf[2]; |
mgolino | 0:85385a11b2da | 217 | blueVal = buf[3]; |
mgolino | 0:85385a11b2da | 218 | |
mgolino | 0:85385a11b2da | 219 | // Put in range from 0-1 |
mgolino | 0:85385a11b2da | 220 | redVal=redVal/255; |
mgolino | 0:85385a11b2da | 221 | greenVal=greenVal/255; |
mgolino | 0:85385a11b2da | 222 | blueVal=blueVal/255; |
mgolino | 0:85385a11b2da | 223 | |
mgolino | 0:85385a11b2da | 224 | // Store for holding purposes |
mgolino | 0:85385a11b2da | 225 | redHold = redVal; |
mgolino | 0:85385a11b2da | 226 | greenHold = greenVal; |
mgolino | 0:85385a11b2da | 227 | blueHold = blueVal; |
mgolino | 0:85385a11b2da | 228 | } |
mgolino | 0:85385a11b2da | 229 | else{ |
mgolino | 0:85385a11b2da | 230 | pc.printf("CASE CONFUSED\n\n"); |
mgolino | 0:85385a11b2da | 231 | } |
mgolino | 0:85385a11b2da | 232 | } // End readable |
mgolino | 0:85385a11b2da | 233 | |
mgolino | 0:85385a11b2da | 234 | else { |
mgolino | 0:85385a11b2da | 235 | //if we can detect BT or no BT |
mgolino | 0:85385a11b2da | 236 | //if(//conditions for aux mode) |
mgolino | 0:85385a11b2da | 237 | //else if (conditions for mic mode) |
mgolino | 0:85385a11b2da | 238 | //else IR mode |
mgolino | 0:85385a11b2da | 239 | |
mgolino | 0:85385a11b2da | 240 | } // End !readable |
mgolino | 0:85385a11b2da | 241 | |
mgolino | 0:85385a11b2da | 242 | if(redVal < 0){ |
mgolino | 0:85385a11b2da | 243 | writeColors(0,0,0,strobeVal); |
mgolino | 0:85385a11b2da | 244 | } |
mgolino | 0:85385a11b2da | 245 | else{ |
mgolino | 0:85385a11b2da | 246 | writeColors(redVal, greenVal, blueVal, strobeVal); |
mgolino | 0:85385a11b2da | 247 | } |
mgolino | 0:85385a11b2da | 248 | |
mgolino | 0:85385a11b2da | 249 | wait(0.1); |
mgolino | 0:85385a11b2da | 250 | }// End while(1) loop |
mgolino | 0:85385a11b2da | 251 | }// End main |
mgolino | 0:85385a11b2da | 252 |