Create a beeping alarm that starts and stops when you want it to. Created for OspreyQuad project and intended for use with a piezo buzzer.
Diff: alarm.cpp
- Revision:
- 0:60cd02d169ea
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/alarm.cpp Sun Jan 19 13:03:30 2014 +0000 @@ -0,0 +1,35 @@ +#include "mbed.h" +#include "alarm.h" + +using namespace mbed; + +Alarm :: Alarm(PinName pin) : _pwm(pin) +{ + _pwm.period(1.0/1000); + _pwm.write(0.0); + active = false; +} + +void Alarm :: startBeep(void) +{ + active = true; + beepOn(); +} + +void Alarm :: stopBeep(void) +{ + active = false; +} + +void Alarm :: beepOn(void) +{ + _pwm.write(0.5); + beepTimer.attach(this, &Alarm::beepOff, 0.1); +} + +void Alarm :: beepOff(void) +{ + _pwm.write(0.0); + if(active) + beepTimer.attach(this,&Alarm::beepOn, 0.05); +} \ No newline at end of file