First commit. Non blocking Led and Buzzer library
Dependents: non_blocking_Led_Buzze_HelloWorld
Revision 0:c18c119011ec, committed 2016-11-21
- Comitter:
- tsungta
- Date:
- Mon Nov 21 06:40:27 2016 +0000
- Commit message:
- Library for non blocking Led and Buzzer controller
Changed in this revision
diff -r 000000000000 -r c18c119011ec Buzzer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Buzzer.cpp Mon Nov 21 06:40:27 2016 +0000 @@ -0,0 +1,75 @@ +/******************** (C) COPYRIGHT 2016 Delta Electronics, Inc. *************** +* +* File Name : Buzzer.cpp +* Authors : Tsungta Wu - CPBG (tsungta.wu@deltaww.com) +* Version : V.1.0.1 +* Date : 2016/Oct/14 +* +*******************************************************************************/ + +#include "Buzzer.h" +#include "mbed.h" + +using namespace mbed; + // constructor + /** Create a Beep object connected to the specified PwmOut pin + * + * @param pin PwmOut pin to connect to + */ + +Buzzer::Buzzer(PinName pin) : _pwm(pin) { + _pwm.write(0.0); // after creating it have to be off +} + +static float Int; +static float* Freq; +static uint16_t Num; +static uint16_t tone_cnt; + + /**next beep instantaneous + * usually not used + */ + +void Buzzer::stopBuzzer() { + _pwm.write(0.0); + tone_cnt = Num; +} + +/** Beep with given frequency and duration. + * + * @param frequency - the frequency of the tone in Hz + * @param time - the duration of the tone in seconds + */ + +void Buzzer::simpleBeep(float freq, float time) { + _pwm.period(1.0/freq); + _pwm.write(0.5); // 50% duty cycle - beep on + tnext.attach(this,&Buzzer::stopBuzzer, time); // time to off +} + +void Buzzer::nextTone() { + if (++tone_cnt < Num) { + if (Freq[tone_cnt] > 0) { + _pwm.period(1.0/Freq[tone_cnt]); + _pwm.write(0.5); // 50% duty cycle - beep on + } else _pwm.write(0.0); + tnext.attach(this,&Buzzer::nextTone, Int); // time to off + } else _pwm.write(0.0); +} + +void Buzzer::playMelody (float* tone_freq, uint16_t tone_num,float tone_time) { + stopBuzzer(); + + Int = tone_time; + Num = tone_num; + Freq = tone_freq; + + tone_cnt = 0; + _pwm.period(1.0/Freq[tone_cnt]); + _pwm.write(0.5); // 50% duty cycle - beep on + tnext.attach(this,&Buzzer::nextTone, Int); // time to off +} + + + +
diff -r 000000000000 -r c18c119011ec Buzzer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Buzzer.h Mon Nov 21 06:40:27 2016 +0000 @@ -0,0 +1,85 @@ +/******************** (C) COPYRIGHT 2016 Delta Electronics, Inc. *************** +* +* File Name : Buzzer.h +* Authors : Tsungta Wu - CPBG (tsungta.wu@deltaww.com) +* Version : V.1.0.1 +* Date : 2016/Nov/14 +* +*******************************************************************************/ + +/** class to make sound and play melody with a buzzer using PWM + * The class use a timer to change tone - it is not blocking while playing melody + * + * Example: + * @code + * #include "mbed.h" + * #include "Buzzer.h" + * + * Buzzer buzzer(p21); + * + *#define tone_num 5 + *float tone[tone_num] = {262, 294, 330, 349, 392}; + + * int main() { + * ... + * buzzer.simpleBeep(1000,0.5); //beep frequency at 1KHz for 0.5 second + * ... + * buzzer.playMelody(tone, tone_num, 0.5); //play do re mi fa so change tone every 0.5 second + * ... + * } + * @endcode + */ + +#ifndef MBED_BUZZER_H +#define MBED_BUZZER_H + +#include "mbed.h" + +namespace mbed { + +/* Class: Buzzer + * A class witch uses PwmOut to play sounds. + * The class use a timer to change tone - it is not blocking + */ +class Buzzer { + +public: + +/** Create a Buzzer object connected to the specified GPIO pin + * + * @param pin GPIO pin to connect to + * @param debug GPIO uart tx pin used to print debug message (reserved) + */ + Buzzer (PinName pin); + +/** Beep with given frequency and duration. + * + * @param frequency - the frequency of the tone in Hz + * @param time - the duration of the tone in seconds + */ + void simpleBeep (float frequency, float time); + +/** Play melody with given frequency and interval. + * + * @param tone_freq - the frequency of each tone in Hz + * @param tone_num - the total number of the given tones + * @param tone_time - the duration of each tone in seconds + */ + void playMelody (float* tone_freq, uint16_t tone_num,float tone_time); + +/** stop the Buzzer instantaneous + * usually not used + */ + void stopBuzzer(); + +private : + + void nextTone(); + PwmOut _pwm; + Timeout tnext; + PinName _debug; +}; + +} +#endif +
diff -r 000000000000 -r c18c119011ec Led.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Led.cpp Mon Nov 21 06:40:27 2016 +0000 @@ -0,0 +1,70 @@ +/******************** (C) COPYRIGHT 2016 Delta Electronics, Inc. *************** +* +* File Name : Led.cpp +* Authors : Tsungta Wu - CPBG (tsungta.wu@deltaww.com) +* Version : V.1.0.1 +* Date : 2016/Nov/14 +* +*******************************************************************************/ + +#include "Led.h" +#include "mbed.h" + +static uint8_t on_logic; + +using namespace mbed; + +Led::Led(PinName pin, uint8_t On_Logic) : _led(pin) { + on_logic = On_Logic; + _led = !on_logic;//turn off when init + +} + +static float Int; +static uint8_t* Toggle; +static uint16_t Num; +static uint16_t toggle_cnt; + + /**turn off led instantaneous + * usually not used + */ + +void Led::offLed() { + _led = !on_logic; + toggle_cnt = Num; +} + +/** Turn on led with given duration. + * @param time - the duration of the tone in seconds + */ + +void Led::simpleBlink(float time) { + _led = on_logic; + tnext.attach(this,&Led::offLed, time); // time to off +} + +void Led::nextToggle() { + if (++toggle_cnt < Num) { + if (Toggle[toggle_cnt] > 0) + _led = on_logic; + else + _led = !on_logic; + tnext.attach(this,&Led::nextToggle, Int); // time to off + } else offLed(); +} + +void Led::toggleLed (uint8_t* toggle_on_off, uint16_t toggle_num, float tonggle_time) { + offLed(); + + Int = tonggle_time; + Num = toggle_num; + Toggle = toggle_on_off; + + toggle_cnt = 0; + if (on_logic) + _led = Toggle[toggle_cnt]; + else + _led = !Toggle[toggle_cnt]; + tnext.attach(this,&Led::nextToggle, Int); // time to off +} +
diff -r 000000000000 -r c18c119011ec Led.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Led.h Mon Nov 21 06:40:27 2016 +0000 @@ -0,0 +1,80 @@ +/******************** (C) COPYRIGHT 2016 Delta Electronics, Inc. *************** +* +* File Name : Led.h +* Authors : Tsungta Wu - CPBG (tsungta.wu@deltaww.com) +* Version : V.1.0.1 +* Date : 2016/Nov/14 +* +*******************************************************************************/ + +/** class to make control LED on/off fashion using GPIO + * The class use a timer to change led on/off - it is not blocking while scheduling led on/off + * + * Example: + * @code + * #include "mbed.h" + * #include "Led.h" + * + * Led led(LED1); + * + *#define schedule_num 7 + *float toggle[schedule_num] = {1, 1, 1, 0, 1, 0, 1}; + + * int main() { + * ... + * led.simpleBlink(0.5); //turn on led for 0.5 second + * ... + * led.toggleLed(toggle, schedule_num, 0.5); //change led to on or off every 0.5 second + * ... + * } + * @endcode + */ + +#ifndef MBED_LED_H +#define MBED_LED_H + +#include "mbed.h" + +namespace mbed { + + +/* Class: Led + * A class witch uses DigitalOut to on/off LED. + * The class use a timer to toggle - it is not blocking + */ +class Led { + +public: + + Led (PinName pin, uint8_t On_Logic = 0); + +/** Turn on LED with given duration. + * + * @param time - the LED on duration in seconds + */ + void simpleBlink (float time); + +/** Toggle LED with given sequence and duration. + * + * @param toggle_on_off - the on/off controlling sequence + * @param toggle_num - the total number of the on/off operation + * @param tonggle_time - the duration of each on/off operation in seconds + */ + void toggleLed (uint8_t* toggle_on_off, uint16_t toggle_num, float tonggle_time); + +/** turn off the Led instantaneous + * usually not used + */ + void offLed(); + +private : + + void nextToggle(); + DigitalOut _led; + Timeout tnext; + PinName _debug; +}; + +} +#endif +