Buzzzer
Dependents: ishtendo-vI-IX-IX-V Oled_bme280
Fork of beep by
beep.cpp@1:e29e9889cfb4, 2016-05-04 (annotated)
- Committer:
- Ihsianmulla
- Date:
- Wed May 04 13:00:51 2016 +0000
- Revision:
- 1:e29e9889cfb4
- Parent:
- 0:080ef5594cce
Buzzer Library using PWM
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ihsianmulla | 1:e29e9889cfb4 | 1 | /** Piezo Buzzer Functions |
Ihsianmulla | 1:e29e9889cfb4 | 2 | * Used for creating an audible alert when starting and to indicate failure of task |
Ihsianmulla | 1:e29e9889cfb4 | 3 | @file beep.cpp |
Ihsianmulla | 1:e29e9889cfb4 | 4 | @brief PiezoBuzzer header file containing functions uses PWM |
Ihsianmulla | 1:e29e9889cfb4 | 5 | @brief write stuff ehre |
Ihsianmulla | 1:e29e9889cfb4 | 6 | @author Pallavi Prasad |
Ihsianmulla | 1:e29e9889cfb4 | 7 | @date April2016 |
Ihsianmulla | 1:e29e9889cfb4 | 8 | */ |
Ihsianmulla | 1:e29e9889cfb4 | 9 | |
Ihsianmulla | 1:e29e9889cfb4 | 10 | |
pprasad7 | 0:080ef5594cce | 11 | #include "beep.h" |
pprasad7 | 0:080ef5594cce | 12 | #include "mbed.h" |
pprasad7 | 0:080ef5594cce | 13 | |
pprasad7 | 0:080ef5594cce | 14 | /** class to make sound with a buzzer, based on a PwmOut |
pprasad7 | 0:080ef5594cce | 15 | * The class use a timeout to switch off the sound - it is not blocking while making noise |
pprasad7 | 0:080ef5594cce | 16 | * |
pprasad7 | 0:080ef5594cce | 17 | * Example: |
pprasad7 | 0:080ef5594cce | 18 | * @code |
pprasad7 | 0:080ef5594cce | 19 | * // Beep with 1Khz for 0.5 seconds |
pprasad7 | 0:080ef5594cce | 20 | * #include "mbed.h" |
pprasad7 | 0:080ef5594cce | 21 | * #include "beep.h" |
pprasad7 | 0:080ef5594cce | 22 | * |
pprasad7 | 0:080ef5594cce | 23 | * Beep buzzer(p21); |
pprasad7 | 0:080ef5594cce | 24 | * |
pprasad7 | 0:080ef5594cce | 25 | * int main() { |
pprasad7 | 0:080ef5594cce | 26 | * ... |
pprasad7 | 0:080ef5594cce | 27 | * buzzer.beep(1000,0.5); |
pprasad7 | 0:080ef5594cce | 28 | * ... |
pprasad7 | 0:080ef5594cce | 29 | * } |
pprasad7 | 0:080ef5594cce | 30 | * @endcode |
pprasad7 | 0:080ef5594cce | 31 | */ |
pprasad7 | 0:080ef5594cce | 32 | |
pprasad7 | 0:080ef5594cce | 33 | using namespace mbed; |
pprasad7 | 0:080ef5594cce | 34 | // constructor |
pprasad7 | 0:080ef5594cce | 35 | /** Create a Beep object connected to the specified PwmOut pin |
pprasad7 | 0:080ef5594cce | 36 | * |
pprasad7 | 0:080ef5594cce | 37 | * @param pin PwmOut pin to connect to |
pprasad7 | 0:080ef5594cce | 38 | */ |
pprasad7 | 0:080ef5594cce | 39 | |
pprasad7 | 0:080ef5594cce | 40 | Beep::Beep(PinName pin) : _pwm(pin) { |
pprasad7 | 0:080ef5594cce | 41 | _pwm.write(0.0); // after creating it have to be off |
pprasad7 | 0:080ef5594cce | 42 | } |
pprasad7 | 0:080ef5594cce | 43 | |
pprasad7 | 0:080ef5594cce | 44 | /** stop the beep instantaneous |
pprasad7 | 0:080ef5594cce | 45 | * usually not used |
pprasad7 | 0:080ef5594cce | 46 | */ |
pprasad7 | 0:080ef5594cce | 47 | void Beep::nobeep() { |
pprasad7 | 0:080ef5594cce | 48 | _pwm.write(0.0); |
pprasad7 | 0:080ef5594cce | 49 | } |
pprasad7 | 0:080ef5594cce | 50 | |
pprasad7 | 0:080ef5594cce | 51 | /** Beep with given frequency and duration. |
pprasad7 | 0:080ef5594cce | 52 | * |
pprasad7 | 0:080ef5594cce | 53 | * @param frequency - the frequency of the tone in Hz |
pprasad7 | 0:080ef5594cce | 54 | * @param time - the duration of the tone in seconds |
pprasad7 | 0:080ef5594cce | 55 | */ |
pprasad7 | 0:080ef5594cce | 56 | |
pprasad7 | 0:080ef5594cce | 57 | void Beep::beep(float freq, float time) { |
pprasad7 | 0:080ef5594cce | 58 | |
pprasad7 | 0:080ef5594cce | 59 | _pwm.period(1.0/freq); |
pprasad7 | 0:080ef5594cce | 60 | _pwm.write(0.5); // 50% duty cycle - beep on |
pprasad7 | 0:080ef5594cce | 61 | toff.attach(this,&Beep::nobeep, time); // time to off |
pprasad7 | 0:080ef5594cce | 62 | } |
pprasad7 | 0:080ef5594cce | 63 | |
pprasad7 | 0:080ef5594cce | 64 | |
pprasad7 | 0:080ef5594cce | 65 | |
pprasad7 | 0:080ef5594cce | 66 |