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.
Fork of MaxbotixDriver by
sonar.cpp
- Committer:
- DanielC
- Date:
- 2012-11-21
- Revision:
- 0:7e65f5077f5a
- Child:
- 1:330989f98a6e
File content as of revision 0:7e65f5077f5a:
#include "sonar.h"
Sonar::Sonar(PinName input, Timer& t) :
interrupt(input),
time(t),
pulseStartTime(0),
range(0) {
interrupt.rise(this, &Sonar::pulseStart);
interrupt.fall(this, &Sonar::pulseStop);
}
int Sonar::read() {
return range;
}
Sonar::operator int() {
return read();
}
void Sonar::pulseStart() {
pulseStartTime = time.read_us();
}
void Sonar::pulseStop() {
int endTime = time.read_us();
if (endTime < pulseStartTime) return; // Escape if there's been a roll over
range = (endTime - pulseStartTime) / 58; // 58uS per CM
}
