圧電ブザーをwaitなしで好きなように鳴らすライブラリ

Dependents:   Tourobo2022_TBCMotorDriver

Revision:
0:97f383c12e42
Child:
1:96bd2135c3bf
diff -r 000000000000 -r 97f383c12e42 buzzer.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buzzer.cpp	Thu Oct 03 09:34:57 2019 +0000
@@ -0,0 +1,39 @@
+#include "buzzer.h"
+
+buzzer::buzzer(PinName buzzerPin) : _buzzer(buzzerPin) {
+    _buzzer = 0;
+    _counter = 0;
+    _setCount = 0;
+    _timerFlag = 0;
+}
+
+void buzzer::output(unsigned int count,float period) {
+    if(!(_timerFlag)) {
+        _timer.attach(this,&buzzer::timerFunction,period);
+        _timerFlag = true;
+        _setCount = count;
+    }
+}
+
+void buzzer::output(bool buzzerStates) {
+    _timer.detach();
+    _timerFlag = false;
+    _buzzer = buzzerStates;
+}
+
+void buzzer::stop() {
+    _timer.detach();
+    _timerFlag = false;
+    _buzzer = 0;
+}
+
+void buzzer::timerFunction() {
+    if(_counter < _setCount * 2) {
+        _buzzer =! _buzzer;
+        _counter++;
+    } else {
+        _buzzer = 0;
+        _timer.detach();
+        _timerFlag = false;
+    }   
+}
\ No newline at end of file