Make noise with a piezo buzzer. Use a pwm pin.
Fork of beep by
Embed:
(wiki syntax)
Show/hide line numbers
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 00055 00056
Generated on Sun Jul 17 2022 05:13:32 by 1.7.2