blabla

Revision:
0:8ae0145ae26e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sonar.cpp	Thu Jun 21 11:39:58 2018 +0000
@@ -0,0 +1,50 @@
+#include "sonar.h"
+
+Sonar::Sonar(PinName trig, PinName echo)
+    :_trig(trig)
+    ,_echo(echo)
+{
+    _timer.start();
+    _echo.rise(callback(this, &Sonar::echoBegin));
+    _echo.fall(callback(this, &Sonar::echoEnd));
+    _pinged = false;
+}
+
+void Sonar::echoBegin()
+{
+    _timer.reset();
+    _begin = _timer.read_us();}
+
+void Sonar::echoEnd()
+{
+    _end = _timer.read_us();
+    _distance = (_end - _begin)/58.3;
+    _pinged = true;
+}
+
+float Sonar::getDistance()
+{
+    _trig = 1;
+    wait_us(10);
+    _trig = 0;
+    while(!_pinged){printf("");}
+    _pinged = false;
+    return _distance;
+}
+
+void Sonar::pulse()
+{
+    _trig = 1;
+    wait_us(10);
+    _trig = 0;
+}
+
+void Sonar::autoPulse(bool on_off, float frequency_ms /*=1*/)
+{
+    if (on_off) {
+        _ticker.attach(callback(this, &Sonar::pulse), frequency_ms/1000);
+    }
+    else {
+        _ticker.detach();
+    }
+}
\ No newline at end of file