Beep using PWM and speaker

Dependents:   20180621_FT813

Fork of beep by Pallavi Prasad

Committer:
JackB
Date:
Mon Jul 23 12:24:59 2018 +0000
Revision:
1:cfc88789e54f
Parent:
beep.h@0:080ef5594cce
Beep

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pprasad7 0:080ef5594cce 1 #ifndef MBED_BEEP_H
pprasad7 0:080ef5594cce 2 #define MBED_BEEP_H
pprasad7 0:080ef5594cce 3
pprasad7 0:080ef5594cce 4 #include "mbed.h"
pprasad7 0:080ef5594cce 5
pprasad7 0:080ef5594cce 6 /** class to make sound with a buzzer, based on a PwmOut
pprasad7 0:080ef5594cce 7 * The class use a timeout to switch off the sound - it is not blocking while making noise
pprasad7 0:080ef5594cce 8 *
pprasad7 0:080ef5594cce 9 * Example:
pprasad7 0:080ef5594cce 10 * @code
pprasad7 0:080ef5594cce 11 * // Beep with 1Khz for 0.5 seconds
pprasad7 0:080ef5594cce 12 * #include "mbed.h"
pprasad7 0:080ef5594cce 13 * #include "beep.h"
pprasad7 0:080ef5594cce 14 *
pprasad7 0:080ef5594cce 15 * Beep buzzer(p21);
pprasad7 0:080ef5594cce 16 *
pprasad7 0:080ef5594cce 17 * int main() {
pprasad7 0:080ef5594cce 18 * ...
pprasad7 0:080ef5594cce 19 * buzzer.beep(1000,0.5);
pprasad7 0:080ef5594cce 20 * ...
pprasad7 0:080ef5594cce 21 * }
pprasad7 0:080ef5594cce 22 * @endcode
pprasad7 0:080ef5594cce 23 */
pprasad7 0:080ef5594cce 24
pprasad7 0:080ef5594cce 25
pprasad7 0:080ef5594cce 26 namespace mbed {
pprasad7 0:080ef5594cce 27
pprasad7 0:080ef5594cce 28 /* Class: Beep
pprasad7 0:080ef5594cce 29 * A class witch uses pwm to controle a beeper to generate sounds.
pprasad7 0:080ef5594cce 30 */
pprasad7 0:080ef5594cce 31 class Beep {
pprasad7 0:080ef5594cce 32
pprasad7 0:080ef5594cce 33 public:
pprasad7 0:080ef5594cce 34
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 Beep (PinName pin);
pprasad7 0:080ef5594cce 40
pprasad7 0:080ef5594cce 41 /** Beep with given frequency and duration.
pprasad7 0:080ef5594cce 42 *
pprasad7 0:080ef5594cce 43 * @param frequency - the frequency of the tone in Hz
pprasad7 0:080ef5594cce 44 * @param time - the duration of the tone in seconds
pprasad7 0:080ef5594cce 45 */
pprasad7 0:080ef5594cce 46 void beep (float frequency, float time);
pprasad7 0:080ef5594cce 47
pprasad7 0:080ef5594cce 48 /** stop the beep instantaneous
pprasad7 0:080ef5594cce 49 * usually not used
pprasad7 0:080ef5594cce 50 */
pprasad7 0:080ef5594cce 51 void nobeep();
pprasad7 0:080ef5594cce 52
pprasad7 0:080ef5594cce 53 private :
pprasad7 0:080ef5594cce 54 PwmOut _pwm;
pprasad7 0:080ef5594cce 55 Timeout toff;
pprasad7 0:080ef5594cce 56 };
pprasad7 0:080ef5594cce 57
pprasad7 0:080ef5594cce 58 }
pprasad7 0:080ef5594cce 59 #endif