Matthew Golino / Mbed 2 deprecated FinalProject

Dependencies:   mbed-dsp mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // ECE 4180 Final Project
00002 // Fall 2015
00003 // Matthew Ashcraft 4th year EE
00004 // Matthew Golino   5th year CE
00005 //
00006 // Bluetooth Controlled Audio Reactive Infinity Mirror
00007  
00008 #include "mbed.h"
00009 #include "ble_mini.h"
00010 #include <iostream>
00011 #include "fftstuff.h"
00012 
00013 ////////////////////////////////////////////////////////////////////////////////
00014 // MBED PIN DESIGNATION
00015 // DO NOT change unless accompanying hardware is altered
00016 ////////////////////////////////////////////////////////////////////////////////
00017 // Analog
00018 //AnalogIn mic(p15);      // Analog input for the microphone
00019 //AnalogIn aux(p20);      // Analog input for the DC offset aux cable input
00020 // PWM outputs for RGB LEDs
00021 PwmOut gled(p23);
00022 PwmOut rled(p22);
00023 PwmOut bled(p21);
00024 // Serial Interface for Mbed to PC
00025 Serial pc(USBTX, USBRX);
00026 // Serial Interface for Bluetooth Module to Mbed
00027 Serial device(p9,p10);
00028 // END MBED PIN DESIGNATION
00029 
00030 ////////////////////////////////////////////////////////////////////////////////
00031 // GLOBAL VARIABLES
00032 // Variables used across multiple functions, simpler than passing to functions
00033 ////////////////////////////////////////////////////////////////////////////////
00034 
00035 char buf[4];            // Char buffer for bluetooth commands
00036 // Boolean flags for audio sampling
00037 bool AuxIn = false;
00038 bool MicIn = false;
00039 bool inputSelect = false; //false = aux --- true = mic
00040 // Float variable used to write to LED PWM outputs
00041 float redVal = -1;
00042 float greenVal = -1;
00043 float blueVal = -1;
00044 // Float used to "save" previous values for manual writing
00045 float redHold = -1;
00046 float greenHold = -1;
00047 float blueHold = -1;
00048 //strobe values
00049 float strobeVal = -1;
00050 float strobeHold = 0;
00051 // END GLOBAL VARIABLES  
00052 
00053 
00054 ////////////////////////////////////////////////////////////////////////////////
00055 // LED WRITING FUNCTIONS
00056 ////////////////////////////////////////////////////////////////////////////////
00057 // Write stored RGB values to PWM outputs
00058 void writeColors(float redVal, float greenVal, float blueVal, float strobeVal)
00059 {
00060     if (strobeVal > 0) {
00061         rled = 0;
00062         gled = 0;
00063         bled = 0;
00064         wait(strobeVal);     
00065     }
00066     if ((redVal >= 0) || (greenVal >= 0) || (blueVal >= 0)) {
00067         rled = redVal;
00068         gled = greenVal;
00069         bled = blueVal;
00070     }
00071 }
00072 // END LED WRITING FUNCTIONS
00073 
00074 int main() {
00075     // Initialize frequency window size
00076     // Set BLE mini baud rate   
00077     device.baud(57600);
00078     // Floats used to "hold" RGB values
00079 //    float redHold = -1;
00080 //    float greenHold = -1;
00081 //    float blueHold = -1;
00082     // Floats used to set and "hold" strobe setting
00083 
00084     while(1) {
00085         if(device.readable()) {
00086             // Read in Bluetooth commands
00087             for(int i = 0; i < 4; i++){
00088                 buf[i]=device.getc();
00089                 pc.printf("%x  ",buf[i]);
00090             }
00091             pc.printf("\n\r");
00092             pc.printf("done\n");
00093             
00094             // Check cases for all different switch options based on buf[0];
00095             char whichInput = buf[0];
00096             // Check for Bluetooth Connectivity
00097             if(whichInput == BLE_CONNECTED){        
00098                 pc.printf("case0\n\n");
00099                 //pretty lights for connect
00100                 rled = 255;
00101                 wait(0.25);
00102                 rled=0;
00103                 gled=255;
00104                 wait(0.25);
00105                 gled=0;
00106                 bled=255;
00107                 wait(0.25);
00108                 bled=0;
00109             }
00110             else if(whichInput == LIGHTS_OFF_SWITCH){ // Manually turn off lights
00111                 pc.printf("Turning All Lights Off\n\n");
00112                 strobeVal = -1;
00113                 if(buf[1]==0x01){
00114                     redVal = 0.0f;
00115                     greenVal = 0.0f;
00116                     blueVal = 0.0f;
00117                 }
00118                 else if(buf[1]==0x00){
00119                     redVal = -1.0f;
00120                     greenVal = -1.0f;
00121                     blueVal = -1.0f;
00122                 }
00123             }
00124             else if(whichInput == IR_SWITCH){       // React to IR remote
00125                 pc.printf("Listening to IR Remote\n\n");
00126                 strobeVal = -1;
00127                 //TBD
00128                 
00129                 
00130             }
00131             
00132             else if(whichInput == MIC_SWITCH){      // React to microphone audio input
00133                 pc.printf("Listening to Microphone for FFT\n\n");
00134                 strobeVal = -1;
00135                 if(buf[1]==0x00){ //switched off
00136                     redVal = 0;
00137                     greenVal = 0;
00138                     blueVal = 0;
00139                 }
00140                 else if (buf[1]==0x01){ //switched on
00141                     //MicIn = true; //FALSE = MIC CONTROLS -- REMEMBER TO FIX IN CODE
00142                     inputSelect = true;
00143                     float colors[3] = {0.0f,0.0f,0.0f};
00144 
00145                     while(!device.readable()){ // done to improve speed => bypass long if statement
00146                             runFFT(inputSelect, colors); 
00147                             redVal = colors[0];
00148                             greenVal = colors[1];
00149                             blueVal = colors[2];;
00150                             writeColors(redVal, greenVal, blueVal, strobeVal);
00151                     }
00152                 }    
00153                 redVal = -1;
00154                 greenVal = -1;
00155                 blueVal = -1;
00156             }
00157             else if(whichInput == AUX_SWITCH){      // React to aux cable audio input
00158                 pc.printf("Listening to Aux for FFT\n\n");
00159                 strobeVal = -1;
00160                 if(buf[1]==0x00){ //switched off
00161                     redVal = 0;
00162                     greenVal = 0;
00163                     blueVal = 0;
00164                 }
00165                 else if (buf[1]==0x01){ //switched on
00166                     inputSelect = false;
00167                     float colors[3] = {0.0f,0.0f,0.0f};
00168 
00169                     while(!device.readable()){ // done to improve speed => bypass long if statement
00170                             runFFT(inputSelect, colors); 
00171                             redVal = colors[0];
00172                             greenVal = colors[1];
00173                             blueVal = colors[2];;
00174                             writeColors(redVal, greenVal, blueVal, strobeVal);
00175                     }
00176                 }    
00177                 redVal = -1;
00178                 greenVal = -1;
00179                 blueVal = -1;
00180             }
00181             else if(whichInput == MANUAL_SWITCH){   // Manual color control
00182                 pc.printf("Colors are Being Controlled Manually\n\n");
00183                 strobeVal = strobeHold;
00184                 if ((redVal < 0) && (redHold < 0)){
00185                     redVal   = 128.0f/255;
00186                     greenVal = 128.0f/255;
00187                     blueVal  = 128.0f/255;
00188                 }
00189                 else{
00190                     redVal = redHold;
00191                     greenVal = greenHold;
00192                     blueVal = blueHold;    
00193                 }
00194                 
00195                 if(buf[1] == 0x00){
00196                     redVal = -1;
00197                     greenVal = -1;
00198                     blueVal = -1;
00199                 }
00200             }
00201             else if(whichInput == STROBE_SPEED){ // Strobe feature for manual control
00202                 pc.printf("Strobe Speed Changed\n\n");
00203                 float strobeSpeed = buf[1]; // Value 0-255 to be used in determining strobe speed
00204                 //determine how to control strobe               
00205                 //pc.printf("Strobe rate: %f", strobeSpeed);
00206                 strobeVal = 0.35 - (strobeSpeed*0.35/255);
00207                 if(strobeSpeed == 0){
00208                     strobeVal=0;
00209                 }
00210                 strobeHold = strobeVal;
00211             }
00212             else if(whichInput == COLOR_SELECT){
00213                 pc.printf("case7\n\n");
00214                 // Convert from hex to float
00215                 redVal = buf[1];
00216                 greenVal = buf[2];
00217                 blueVal = buf[3];
00218                 
00219                 // Put in range from 0-1
00220                 redVal=redVal/255;
00221                 greenVal=greenVal/255;
00222                 blueVal=blueVal/255;
00223                 
00224                 // Store for holding purposes
00225                 redHold = redVal;
00226                 greenHold = greenVal;
00227                 blueHold = blueVal;                
00228             }
00229             else{
00230                 pc.printf("CASE CONFUSED\n\n");
00231             }
00232         } // End readable
00233     
00234         else {
00235             //if we can detect BT or no BT
00236             //if(//conditions for aux mode)
00237             //else if (conditions for mic mode)
00238             //else IR mode
00239         
00240         } // End !readable
00241         
00242         if(redVal < 0){
00243             writeColors(0,0,0,strobeVal);    
00244         }
00245         else{
00246             writeColors(redVal, greenVal, blueVal, strobeVal);    
00247         }    
00248          
00249         wait(0.1);
00250     }// End while(1) loop
00251 }// End main
00252