jednostavni buzzer
Dependents: konstrukcijski_zadatak
Revision 0:9f85ba530763, committed 2021-11-30
- Comitter:
- pkresic
- Date:
- Tue Nov 30 17:00:10 2021 +0000
- Commit message:
- Buzzer, prva verzija
Changed in this revision
buzzer.cpp | Show annotated file Show diff for this revision Revisions of this file |
buzzer.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 9f85ba530763 buzzer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/buzzer.cpp Tue Nov 30 17:00:10 2021 +0000 @@ -0,0 +1,27 @@ +#include "buzzer.h" +#include "mbed.h" + +using namespace mbed; + +Beep::Beep(PinName pin) : _pwm(pin) +{ + _pwm.write(0.0); +} + +void Beep::nobeep() +{ + _pwm.write(0.0); +} + +/** 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 Beep::beep(float freq, float time) +{ + + _pwm.period(1.0/freq); + _pwm.write(0.5); + toff.attach(this,&Beep::nobeep, time); +} \ No newline at end of file
diff -r 000000000000 -r 9f85ba530763 buzzer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/buzzer.h Tue Nov 30 17:00:10 2021 +0000 @@ -0,0 +1,33 @@ +#ifndef MBED_BEEP_H +#define MBED_BEEP_H + +#include "mbed.h" + +namespace mbed { + +/* Class: Beep + * A class which uses pwm to controle a beeper to generate sounds. + */ +class Beep { + +public: + + Beep (PinName pin);//Specified PwmOut pin for buzzer + +/** 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 beep (float frequency, float time); + + void nobeep(); + +private : + PwmOut _pwm; + Timeout toff; +}; + +} +#endif + \ No newline at end of file