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@0:3eb39e374fc3, 2013-07-31 (annotated)
- Committer:
- shimniok
- Date:
- Wed Jul 31 23:05:44 2013 +0000
- Revision:
- 0:3eb39e374fc3
Working version with dual sharp rangers and A2D I2C adapters
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shimniok | 0:3eb39e374fc3 | 1 | #include "mbed.h" |
shimniok | 0:3eb39e374fc3 | 2 | #include "Beep.h" |
shimniok | 0:3eb39e374fc3 | 3 | |
shimniok | 0:3eb39e374fc3 | 4 | /** Create a Beep object connected to the specified PwmOut pin |
shimniok | 0:3eb39e374fc3 | 5 | * |
shimniok | 0:3eb39e374fc3 | 6 | * @param pin PwmOut pin to connect to |
shimniok | 0:3eb39e374fc3 | 7 | */ |
shimniok | 0:3eb39e374fc3 | 8 | Beep::Beep(PinName pin) : _pwm(pin) { |
shimniok | 0:3eb39e374fc3 | 9 | _pwm.write(0.0); // after creating it have to be off |
shimniok | 0:3eb39e374fc3 | 10 | } |
shimniok | 0:3eb39e374fc3 | 11 | |
shimniok | 0:3eb39e374fc3 | 12 | /** Stop the beep instantaneously. |
shimniok | 0:3eb39e374fc3 | 13 | */ |
shimniok | 0:3eb39e374fc3 | 14 | void Beep::nobeep() { |
shimniok | 0:3eb39e374fc3 | 15 | _pwm.write(0.0); |
shimniok | 0:3eb39e374fc3 | 16 | } |
shimniok | 0:3eb39e374fc3 | 17 | |
shimniok | 0:3eb39e374fc3 | 18 | /** Beep with given frequency and duration. |
shimniok | 0:3eb39e374fc3 | 19 | * |
shimniok | 0:3eb39e374fc3 | 20 | * @param frequency - the frequency of the tone in Hz |
shimniok | 0:3eb39e374fc3 | 21 | * @param time - the duration of the tone in seconds, 0 runs indefinitely |
shimniok | 0:3eb39e374fc3 | 22 | */ |
shimniok | 0:3eb39e374fc3 | 23 | void Beep::beep(float freq, float time) { |
shimniok | 0:3eb39e374fc3 | 24 | |
shimniok | 0:3eb39e374fc3 | 25 | _pwm.period(1.0/freq); |
shimniok | 0:3eb39e374fc3 | 26 | _pwm.write(0.5); // 50% duty cycle - beep on |
shimniok | 0:3eb39e374fc3 | 27 | if (time > 0) |
shimniok | 0:3eb39e374fc3 | 28 | toff.attach(this,&Beep::nobeep, time); // time to off |
shimniok | 0:3eb39e374fc3 | 29 | } |