Maxbotix library
Fork of MaxbotixDriver by
sonar.cpp
- Committer:
- joe4465
- Date:
- 2015-02-22
- Revision:
- 1:24d9d6d213aa
- Parent:
- 0:7e65f5077f5a
File content as of revision 1:24d9d6d213aa:
#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
}
Joseph Roberts
