5#include <boost/thread.hpp>
21RpiEcho::RpiEcho(
int triggerpin,
int echopin):
22 mTriggerPin(triggerpin),
28 pinMode(mTriggerPin, OUTPUT);
29 pinMode(mEchoPin, INPUT);
30 digitalWrite(mTriggerPin, LOW);
40 mThread = boost::thread( boost::bind(&RpiEcho::threadLoop,
this));
53 digitalWrite(mTriggerPin, LOW);
57long RpiEcho::pulseIn(
int timeout)
60 long startTime = micros();
63 while (digitalRead(mEchoPin) == LOW && mState)
65 if ((micros() - startTime) > timeout)
71 while (digitalRead(mEchoPin) == HIGH && mState)
74 if ((stopTime - startTime) > timeout)
78 return stopTime - startTime;
84void RpiEcho::threadLoop()
89 unsigned int nbacc = 0;
90 unsigned int spent = 0;
97 for (nbacc = 0; nbacc < 10;)
99 digitalWrite(mTriggerPin, LOW);
101 digitalWrite(mTriggerPin, HIGH);
102 delayMicroseconds(10);
103 digitalWrite(mTriggerPin, LOW);
105 spent = pulseIn(timeout);
114 boost::mutex::scoped_lock l(mMutex);
116 mValue = (avg / nbacc) / 58;
122int RpiEcho::ReadValue()
124 boost::mutex::scoped_lock l(mMutex);