blabla
sonar.cpp
- Committer:
- twjfransen
- Date:
- 2018-06-21
- Revision:
- 0:8ae0145ae26e
File content as of revision 0:8ae0145ae26e:
#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(); } }