TVZ2022 / Pavetic_BuzzerLib

Files at this revision

API Documentation at this revision

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