Maxbotix ultrasonic distance sensor library
Fork of MaxbotixDriver by
sonar.cpp
- Committer:
- SomeRandomBloke
- Date:
- 2020-01-17
- Revision:
- 3:c231deea4d36
- Parent:
- 1:330989f98a6e
- Child:
- 4:95f696f59d94
File content as of revision 3:c231deea4d36:
#include "sonar.h" Sonar::Sonar(PinName input, Timer& t) : interrupt(input), time(t), pulseStartTime(0), range(0) { interrupt.rise(callback(this,&Sonar::pulseStart)); interrupt.fall(callback(this,&Sonar::pulseStop)); } int Sonar::read() { return range; } int Sonar::readCm() { return range / 58; // 58uS per CM } 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 }