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.
Revision 0:f867aece57de, committed 2022-11-27
- Comitter:
- dpavetic
- Date:
- Sun Nov 27 10:33:00 2022 +0000
- Commit message:
- Initial lib version
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 |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Buzzer.cpp Sun Nov 27 10:33:00 2022 +0000
@@ -0,0 +1,21 @@
+#include "Buzzer.h"
+
+using namespace mbed;
+
+Buzzer::Buzzer(PinName pin) : _pwm(pin) { _pwm.write(0.0); }
+
+void Buzzer::silence() { _pwm.write(0.0); }
+void empty() {
+}
+
+/**
+ * Buzzer with given frequency and rithm.
+ *
+ * @param frequency - the frequency of the tone in Hz
+ * @param rithm - rithm of the song
+ */
+void Buzzer::buzz(float freq, float rithm) {
+ _pwm.period(1.0 / freq);
+ _pwm.write(0.5f);
+ ThisThread::sleep_for(Kernel::Clock::duration_u32((int)(rithm * 500)));
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Buzzer.h Sun Nov 27 10:33:00 2022 +0000
@@ -0,0 +1,36 @@
+#ifndef MBED_BUZZER_H
+#define MBED_BUZZER_H
+
+#include "mbed.h"
+
+namespace mbed {
+
+/**
+ * Class: Buzzer
+ * A class which uses PWM to create sounds on given PWM pin.
+ */
+class Buzzer {
+
+public:
+ Buzzer(PinName pin); // Specified PwmOut pin for buzzer
+
+ /**
+ * Buzzer with given frequency and duration.
+ *
+ * @param frequency - the frequency of the tone in Hz
+ * @param time - the duration of the tone in seconds
+ */
+ void buzz(float frequency, float time);
+
+ /**
+ * Silences the buzzer.
+ */
+ void silence();
+
+private:
+ PwmOut _pwm;
+ Timeout toff;
+};
+
+} // namespace mbed
+#endif
\ No newline at end of file