Delta / non_blocking_Led_Buzzer

Dependents:   non_blocking_Led_Buzze_HelloWorld

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Buzzer.h Source File

Buzzer.h

00001 /******************** (C) COPYRIGHT 2016 Delta Electronics, Inc. ***************
00002 *
00003 * File Name : Buzzer.h
00004 * Authors   : Tsungta Wu - CPBG (tsungta.wu@deltaww.com)
00005 * Version   : V.1.0.1
00006 * Date      : 2016/Nov/14
00007 *
00008 *******************************************************************************/
00009 
00010 /** class to make sound and play melody with a buzzer using PWM
00011  *   The class use a timer to change tone  - it is not blocking while playing melody
00012  *
00013  * Example:
00014  * @code
00015  * #include "mbed.h"
00016  * #include "Buzzer.h"
00017  * 
00018  * Buzzer buzzer(p21);
00019  * 
00020  *#define tone_num   5
00021  *float tone[tone_num] = {262, 294, 330, 349, 392};
00022 
00023  * int main() {
00024  *       ...
00025  *   buzzer.simpleBeep(1000,0.5);   //beep frequency at 1KHz for 0.5 second    
00026  *       ...
00027  *   buzzer.playMelody(tone, tone_num, 0.5);    //play do re mi fa so change tone every 0.5 second
00028  *       ...
00029  * }
00030  * @endcode
00031  */
00032 
00033 #ifndef MBED_BUZZER_H
00034 #define MBED_BUZZER_H
00035 
00036 #include "mbed.h"
00037 
00038 namespace mbed {
00039 
00040 /* Class: Buzzer
00041  *  A class witch uses PwmOut to play sounds.
00042  *  The class use a timer to change tone  - it is not blocking
00043  */
00044 class Buzzer {
00045 
00046 public:
00047 
00048 /** Create a Buzzer object connected to the specified GPIO pin
00049  *
00050  * @param pin GPIO pin to connect to
00051  * @param debug GPIO uart tx pin used to print debug message (reserved)  
00052  */
00053     Buzzer (PinName pin);
00054 
00055 /** Beep with given frequency and duration.
00056  *
00057  * @param frequency - the frequency of the tone in Hz
00058  * @param time - the duration of the tone in seconds
00059  */
00060     void simpleBeep (float frequency, float time);
00061 
00062 /** Play melody with given frequency and interval.
00063  *
00064  * @param tone_freq - the frequency of each tone in Hz
00065  * @param tone_num - the total number of the given tones
00066  * @param tone_time - the duration of each tone in seconds
00067  */
00068     void playMelody (float* tone_freq, uint16_t tone_num,float tone_time);
00069     
00070 /** stop the Buzzer instantaneous 
00071  * usually not used 
00072  */
00073     void stopBuzzer();
00074     
00075 private :
00076 
00077     void nextTone();
00078     PwmOut _pwm;
00079     Timeout tnext;
00080     PinName _debug;
00081 };
00082 
00083 }
00084 #endif
00085