ver 1.0
Dependencies: FATFileSystem SDFileSystem mbed-rtos mbed wave_player
Fork of LED_effects_w_sound by
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 #include "SDFileSystem.h" 00004 #include "wave_player.h" 00005 00006 00007 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card 00008 00009 //LED lighting effects with sound using the RTOS 00010 //Plays the wave file "sample.wav" on the USB flash drive 00011 //using waveplayer code library 00012 //Class D style audio output on P26 or 00013 //use the Audio Out jack for connection to a set of amplified PC speakers (at higher volume) 00014 //Needs a USB flash drive inserted with the *.wav files on it to run with audio effects 00015 //Pins are setup for mbed LPC1768 on mbed application board 00016 //Analog Audio Out Jack for PC Speakers- Sparkfun also has a breakout 00017 //For more info see: 00018 //http://developer.mbed.org/users/4180_1/notebook/led-lighting-effects-for-modelers/ 00019 AnalogOut DACout(p18); 00020 //On Board Speaker on Application board but very low volume using PWM 00021 PwmOut PWMout(p26); 00022 //LEDs used in demo 00023 PwmOut myled2(LED2); 00024 PwmOut myled4(LED4); 00025 //Use a PWM output to enable dimming 00026 //1.0 is full on, 0.0 off, 0.5 50% on 00027 00028 inline float random_number() 00029 { 00030 return (rand()/(float(RAND_MAX))); 00031 } 00032 void beacon(void const *args) 00033 { 00034 while(1) { 00035 //LED warm up effect using PWM 00036 for(int i=0; i<50; i++) { 00037 myled2 = i/50.0; 00038 Thread::wait(1000.0*0.02); 00039 } 00040 //LED at full brightness level 00041 myled2 = 1.0; 00042 Thread::wait(1000.0*0.25); 00043 //LED cool down effect using PWM 00044 for(int i=49; i>0; i--) { 00045 myled2 = i/50.0; 00046 Thread::wait(1000.0*0.02); 00047 } 00048 //LED off 00049 myled2 = 0.0; 00050 Thread::wait(1000.0*1.5); 00051 } 00052 } 00053 void welding(void const *args) 00054 { 00055 float x = 0.0; 00056 while(1) { 00057 //get a new random number for PWM 00058 x = random_number(); 00059 //add some exponential brightness scaling 00060 //for more of a fast flash effect 00061 myled2 = x*x*x; 00062 //fast update rate for welding flashes 00063 Thread::wait(1000.0*0.02); 00064 //add a random pause between welds 00065 if (random_number()>0.995) { 00066 myled2 = 0.0; 00067 Thread::wait(1000.0*random_number()); 00068 } 00069 } 00070 } 00071 void fire(void const *args) 00072 { 00073 while(1) { 00074 //get a new random number for PWM 00075 myled2 = random_number(); 00076 //a bit slower time delay can be used for fire 00077 wait(0.04); 00078 } 00079 } 00080 void police(void const *args) 00081 { 00082 while(1) { 00083 //flash three times on LED1 00084 for(int i=0; i<3; i++) { 00085 //ramp up brightness level 00086 for(double x = 0.0; x <= 1.0; x = x+0.2) { 00087 myled2 = x*x; 00088 wait(.02); 00089 } 00090 } 00091 myled2=0.0; //LED1 off 00092 //flash three times on LED2 00093 for(int i=0; i<3; i++) { 00094 //ramp up brightness level 00095 for(double x = 0.0; x <= 1.0; x = x+0.2) { 00096 myled4 = x*x; 00097 wait(.02); 00098 } 00099 } 00100 myled4=0.0; //LED2 off 00101 } 00102 } 00103 void lighthouse(void const *args) 00104 { 00105 float y=0.0; 00106 00107 while(1) { 00108 for(double x=0.0; x <= 3.14159; x = x + 0.0314159) { 00109 y = sin(x); //nice periodic function 0..1..0 00110 myled2 = y*y*y;//exponential effect - needs a sharp peak 00111 Thread::wait(1000.0*.025); 00112 } 00113 myled2 = 0.0; 00114 //most lighthouses have a 5 second delay - so add another 2.5 00115 Thread::wait(1000.0*2.5); 00116 } 00117 } 00118 00119 00120 //function to simulate incandescent bulb warm up 00121 //and cool down cycles 00122 void incandescent_bulb(PwmOut led, float on_time = 0.1, 00123 float warm_time = 0.2) 00124 { 00125 //LED warm up effect using PWM 00126 for(int i=0; i<10; i++) { 00127 led = i/10.0; 00128 Thread::wait(1000.0*warm_time/10.0); 00129 } 00130 //LED at full brightness level 00131 led = 1.0; 00132 Thread::wait(1000.0*on_time); 00133 //LED cool down effect using PWM 00134 for(int i=9; i>0; i--) { 00135 led = i/10.0; 00136 Thread::wait(1000.0*warm_time/10.0); 00137 } 00138 //LED off 00139 led = 0.0; 00140 } 00141 //LED Railroad Crossing Lighting Effect 00142 void rrcrossing(void const *args) 00143 { 00144 while(1) { 00145 incandescent_bulb(myled2); 00146 incandescent_bulb(myled4); 00147 } 00148 } 00149 00150 //wave player plays a *.wav file to D/A 00151 wave_player waver(&DACout); 00152 00153 int main() 00154 { 00155 00156 FILE *wave_file; 00157 while(1) { 00158 { 00159 //Lighthouse demo with foghorn 00160 Thread thread(lighthouse); //Start LED effect thread 00161 for(int i=0; i<4; ++i) { 00162 //open wav file and play it 00163 wave_file=fopen("/sd/foghorn.wav","r"); 00164 waver.play(wave_file); //Plays (*.wav file 00165 fclose(wave_file); 00166 Thread::wait(1925); 00167 } 00168 //end of lighthouse demo; 00169 thread.terminate(); //kills LED effect thread whan audio stops 00170 00171 } //code block forces thread out of scope reclaims mem-can recreate thread object 00172 { 00173 //Arc Welding with sound 00174 Thread thread(welding); 00175 //open wav file and play it 00176 wave_file=fopen("/sd/welding.wav","r"); 00177 waver.play(wave_file); 00178 fclose(wave_file); 00179 //end of welding demo; 00180 thread.terminate(); 00181 } 00182 { 00183 //Police Lights with siren 00184 //code block forces thread out of scope 00185 Thread thread(police); 00186 //open wav file and play it 00187 wave_file=fopen("/sd/emgsiren.wav","r"); 00188 waver.play(wave_file); 00189 fclose(wave_file); 00190 //end of police light demo; 00191 thread.terminate(); 00192 } 00193 { 00194 //Fire with sound 00195 Thread thread(fire); 00196 //open wav file and play it 00197 wave_file=fopen("/sd/fire.wav","r"); 00198 waver.play(wave_file); 00199 fclose(wave_file); 00200 //end of fire demo; 00201 thread.terminate(); 00202 } 00203 00204 { 00205 //RR Crossing Lights with Signal Bells 00206 Thread thread(rrcrossing); 00207 //open wav file and play it 00208 wave_file=fopen("/sd/SignalBell.wav","r"); 00209 waver.play(wave_file); 00210 fclose(wave_file); 00211 //end of railroad crossing demo; 00212 thread.terminate(); 00213 } 00214 } 00215 }
Generated on Wed Jul 13 2022 20:36:27 by 1.7.2