Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: non_blocking_Led_Buzze_HelloWorld
Buzzer.cpp
00001 /******************** (C) COPYRIGHT 2016 Delta Electronics, Inc. *************** 00002 * 00003 * File Name : Buzzer.cpp 00004 * Authors : Tsungta Wu - CPBG (tsungta.wu@deltaww.com) 00005 * Version : V.1.0.1 00006 * Date : 2016/Oct/14 00007 * 00008 *******************************************************************************/ 00009 00010 #include "Buzzer.h" 00011 #include "mbed.h" 00012 00013 using namespace mbed; 00014 // constructor 00015 /** Create a Beep object connected to the specified PwmOut pin 00016 * 00017 * @param pin PwmOut pin to connect to 00018 */ 00019 00020 Buzzer::Buzzer(PinName pin) : _pwm(pin) { 00021 _pwm.write(0.0); // after creating it have to be off 00022 } 00023 00024 static float Int; 00025 static float* Freq; 00026 static uint16_t Num; 00027 static uint16_t tone_cnt; 00028 00029 /**next beep instantaneous 00030 * usually not used 00031 */ 00032 00033 void Buzzer::stopBuzzer() { 00034 _pwm.write(0.0); 00035 tone_cnt = Num; 00036 } 00037 00038 /** Beep with given frequency and duration. 00039 * 00040 * @param frequency - the frequency of the tone in Hz 00041 * @param time - the duration of the tone in seconds 00042 */ 00043 00044 void Buzzer::simpleBeep(float freq, float time) { 00045 _pwm.period(1.0/freq); 00046 _pwm.write(0.5); // 50% duty cycle - beep on 00047 tnext.attach(this,&Buzzer::stopBuzzer, time); // time to off 00048 } 00049 00050 void Buzzer::nextTone() { 00051 if (++tone_cnt < Num) { 00052 if (Freq[tone_cnt] > 0) { 00053 _pwm.period(1.0/Freq[tone_cnt]); 00054 _pwm.write(0.5); // 50% duty cycle - beep on 00055 } else _pwm.write(0.0); 00056 tnext.attach(this,&Buzzer::nextTone, Int); // time to off 00057 } else _pwm.write(0.0); 00058 } 00059 00060 void Buzzer::playMelody (float* tone_freq, uint16_t tone_num,float tone_time) { 00061 stopBuzzer(); 00062 00063 Int = tone_time; 00064 Num = tone_num; 00065 Freq = tone_freq; 00066 00067 tone_cnt = 0; 00068 _pwm.period(1.0/Freq[tone_cnt]); 00069 _pwm.write(0.5); // 50% duty cycle - beep on 00070 tnext.attach(this,&Buzzer::nextTone, Int); // time to off 00071 } 00072 00073 00074 00075
Generated on Mon Jul 25 2022 05:03:58 by
1.7.2