''
buzzer.cpp@0:01eac446ef9f, 2016-05-05 (annotated)
- Committer:
- Reniboy
- Date:
- Thu May 05 00:28:41 2016 +0000
- Revision:
- 0:01eac446ef9f
Working version 1.0;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Reniboy | 0:01eac446ef9f | 1 | #include "buzzer.h" |
Reniboy | 0:01eac446ef9f | 2 | #include "mbed.h" |
Reniboy | 0:01eac446ef9f | 3 | |
Reniboy | 0:01eac446ef9f | 4 | /** class to make sound with a buzzer, based on a PwmOut |
Reniboy | 0:01eac446ef9f | 5 | * The class use a timeout to switch off the sound - it is not blocking while making noise |
Reniboy | 0:01eac446ef9f | 6 | * |
Reniboy | 0:01eac446ef9f | 7 | * Example: |
Reniboy | 0:01eac446ef9f | 8 | * @code |
Reniboy | 0:01eac446ef9f | 9 | * // Beep with 1Khz for 0.5 seconds |
Reniboy | 0:01eac446ef9f | 10 | * #include "mbed.h" |
Reniboy | 0:01eac446ef9f | 11 | * #include "beep.h" |
Reniboy | 0:01eac446ef9f | 12 | * |
Reniboy | 0:01eac446ef9f | 13 | * Beep buzzer(PTC2); |
Reniboy | 0:01eac446ef9f | 14 | * |
Reniboy | 0:01eac446ef9f | 15 | * int main() { |
Reniboy | 0:01eac446ef9f | 16 | * ... |
Reniboy | 0:01eac446ef9f | 17 | * buzzer.beep(1000,0.5); |
Reniboy | 0:01eac446ef9f | 18 | * ... |
Reniboy | 0:01eac446ef9f | 19 | * } |
Reniboy | 0:01eac446ef9f | 20 | * @endcode |
Reniboy | 0:01eac446ef9f | 21 | */ |
Reniboy | 0:01eac446ef9f | 22 | |
Reniboy | 0:01eac446ef9f | 23 | using namespace mbed; |
Reniboy | 0:01eac446ef9f | 24 | // constructor |
Reniboy | 0:01eac446ef9f | 25 | /** Create a Beep object connected to the specified PwmOut pin |
Reniboy | 0:01eac446ef9f | 26 | * |
Reniboy | 0:01eac446ef9f | 27 | * @param pin PwmOut pin to connect to |
Reniboy | 0:01eac446ef9f | 28 | */ |
Reniboy | 0:01eac446ef9f | 29 | |
Reniboy | 0:01eac446ef9f | 30 | Beep::Beep(PinName pin) : _pwm(pin) { |
Reniboy | 0:01eac446ef9f | 31 | _pwm.write(0.0); // after creating it have to be off |
Reniboy | 0:01eac446ef9f | 32 | } |
Reniboy | 0:01eac446ef9f | 33 | |
Reniboy | 0:01eac446ef9f | 34 | /** stop the beep instantaneous |
Reniboy | 0:01eac446ef9f | 35 | * usually not used |
Reniboy | 0:01eac446ef9f | 36 | */ |
Reniboy | 0:01eac446ef9f | 37 | void Beep::nobeep() { |
Reniboy | 0:01eac446ef9f | 38 | _pwm.write(0.0); |
Reniboy | 0:01eac446ef9f | 39 | } |
Reniboy | 0:01eac446ef9f | 40 | |
Reniboy | 0:01eac446ef9f | 41 | /** Beep with given frequency and duration. |
Reniboy | 0:01eac446ef9f | 42 | * |
Reniboy | 0:01eac446ef9f | 43 | * @param frequency - the frequency of the tone in Hz |
Reniboy | 0:01eac446ef9f | 44 | * @param time - the duration of the tone in seconds |
Reniboy | 0:01eac446ef9f | 45 | */ |
Reniboy | 0:01eac446ef9f | 46 | |
Reniboy | 0:01eac446ef9f | 47 | void Beep::beep(float freq, float time) { |
Reniboy | 0:01eac446ef9f | 48 | |
Reniboy | 0:01eac446ef9f | 49 | _pwm.period(1.0/freq); |
Reniboy | 0:01eac446ef9f | 50 | _pwm.write(0.5); // 50% duty cycle - beep on |
Reniboy | 0:01eac446ef9f | 51 | toff.attach(this,&Beep::nobeep, time); // time to off |
Reniboy | 0:01eac446ef9f | 52 | } |