Simple PWM / beep generation library. Supports timed-duration beeps or infinite length tone generation.
Dependents: PseudoTheremin NSDL_HelloWorld_WiFi UbloxModemNanoServiceClient IOT-NSDL_HelloWorld ... more
Beep.cpp
00001 #include "mbed.h" 00002 #include "Beep.h" 00003 00004 /** Create a Beep object connected to the specified PwmOut pin 00005 * 00006 * @param pin PwmOut pin to connect to 00007 */ 00008 Beep::Beep(PinName pin) : _pwm(pin) { 00009 _pwm.write(0.0); // after creating it have to be off 00010 } 00011 00012 /** Stop the beep instantaneously. 00013 */ 00014 void Beep::nobeep() { 00015 _pwm.write(0.0); 00016 } 00017 00018 /** Beep with given frequency and duration. 00019 * 00020 * @param frequency - the frequency of the tone in Hz 00021 * @param time - the duration of the tone in seconds, 0 runs indefinitely 00022 */ 00023 void Beep::beep(float freq, float time) { 00024 00025 _pwm.period(1.0/freq); 00026 _pwm.write(0.5); // 50% duty cycle - beep on 00027 if (time > 0) 00028 toff.attach(this,&Beep::nobeep, time); // time to off 00029 }
Generated on Thu Jul 14 2022 01:13:48 by
