Maxbotix ultrasonic distance sensor library
Fork of MaxbotixDriver by
Diff: sonar.cpp
- Revision:
- 0:7e65f5077f5a
- Child:
- 1:330989f98a6e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sonar.cpp Wed Nov 21 20:47:05 2012 +0000 @@ -0,0 +1,28 @@ +#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 +} \ No newline at end of file