VFD modular clock firmware
Dependencies: DipCortex-EEprom RTC flw mbed
beep.cpp
00001 #include "beep.h" 00002 #include "mbed.h" 00003 00004 /** class to make sound with a buzzer, based on a PwmOut 00005 * The class use a timeout to switch off the sound - it is not blocking while making noise 00006 * 00007 * Example: 00008 * @code 00009 * // Beep with 1Khz for 0.5 seconds 00010 * #include 'mbed.h' 00011 * #include 'beep.h' 00012 * 00013 * Beep buzzer(p21); 00014 * 00015 * int main() { 00016 * ... 00017 * buzzer.beep(1000,0.5); 00018 * ... 00019 * } 00020 * @endcode 00021 */ 00022 00023 //using namespace mbed; 00024 // constructor 00025 /** Create a Beep object connected to the specified PwmOut pin 00026 * 00027 * @param pin PwmOut pin to connect to 00028 */ 00029 00030 Beep::Beep(PinName pin) : _pwm(pin) { 00031 _pwm.write(0.0); // after creating it have to be off 00032 } 00033 00034 /** stop the beep instantaneous 00035 * usually not used 00036 */ 00037 void Beep::nobeep() { 00038 _pwm.write(0.0); 00039 } 00040 00041 /** Beep with given frequency and duration. 00042 * 00043 * @param frequency - the frequency of the tone in Hz 00044 * @param time - the duration of the tone in seconds 00045 */ 00046 00047 void Beep::beep(float freq, float time) { 00048 00049 _pwm.period(1.0/freq); 00050 _pwm.write(0.5); // 50% duty cycle - beep on 00051 toff.attach(this,&Beep::nobeep, time); // time to off 00052 } 00053 00054 #define NOTE_GB 0 00055 #define NOTE_EB 1 00056 #define NOTE_FS 2 00057 00058 void Beep::play(char note) { 00059 if (note=='a') { 00060 beep(880,0.1); 00061 } 00062 if (note=='b') { 00063 beep(987,0.1); 00064 } 00065 if (note=='c') { 00066 beep(1024,0.1); 00067 } 00068 if (note=='d') { 00069 beep(1175,0.1); 00070 } 00071 if (note=='e') { 00072 beep(1319,0.1); 00073 } 00074 if (note=='f') { 00075 beep(1397,0.1); 00076 } 00077 if (note=='g') { 00078 beep(1568,0.1); 00079 } 00080 00081 if (note==NOTE_GB) { 00082 beep(830,0.1); 00083 } 00084 if (note==NOTE_EB) { 00085 beep(659,0.1); 00086 } 00087 if (note==NOTE_FS) { 00088 beep(1480,0.1); 00089 } 00090 if (note=='w') { 00091 wait(0.05); 00092 } 00093 wait (0.2); //wait while the note plays. 00094 } 00095 00096 void Beep::popcorn() { 00097 00098 play('e'); 00099 play('d'); 00100 play('e'); 00101 play('c'); 00102 play(NOTE_GB); 00103 play('c'); 00104 play(NOTE_EB); 00105 play('w'); 00106 play('e'); 00107 play('d'); 00108 play('e'); 00109 play('c'); 00110 play(NOTE_GB); 00111 play('c'); 00112 play(NOTE_EB); 00113 play('w'); 00114 play('e'); 00115 play(NOTE_FS); 00116 play('g'); 00117 play(NOTE_FS); 00118 play('g'); 00119 play('e'); 00120 play(NOTE_FS); 00121 play('e'); 00122 play(NOTE_FS); 00123 play('d'); 00124 play('e'); 00125 play('d'); 00126 play('e'); 00127 play('d'); 00128 play('e'); 00129 play('w'); 00130 play('e'); 00131 play('d'); 00132 play('e'); 00133 play('c'); 00134 play(NOTE_GB); 00135 play('c'); 00136 play(NOTE_EB); 00137 play('w'); 00138 play('e'); 00139 play('d'); 00140 play('e'); 00141 play('c'); 00142 play(NOTE_GB); 00143 play('c'); 00144 play(NOTE_EB); 00145 play('w'); 00146 play('e'); 00147 play(NOTE_FS); 00148 play('g'); 00149 play(NOTE_FS); 00150 play('g'); 00151 play('e'); 00152 play(NOTE_FS); 00153 play('e'); 00154 play(NOTE_FS); 00155 play('d'); 00156 play('e'); 00157 play('d'); 00158 play('e'); 00159 play('d'); 00160 play('g'); 00161 00162 } 00163
Generated on Wed Jul 13 2022 06:11:41 by 1.7.2