2017-10-15 10:30:15 +00:00
|
|
|
#include "distancethread.h"
|
|
|
|
#include <wiringPi.h>
|
|
|
|
|
|
|
|
volatile unsigned int timestampHigh = 0;
|
2017-10-15 11:11:46 +00:00
|
|
|
volatile unsigned int pulseLength = 0;
|
2017-10-15 10:30:15 +00:00
|
|
|
|
|
|
|
DistanceThread::DistanceThread()
|
|
|
|
{
|
|
|
|
wiringPiSetup();
|
|
|
|
pinMode(m_triggerPin, OUTPUT);
|
|
|
|
pinMode(m_echoPin, INPUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DistanceThread::run()
|
|
|
|
{
|
|
|
|
wiringPiISR(m_echoPin, INT_EDGE_BOTH, []
|
|
|
|
{
|
|
|
|
if (digitalRead(29) == HIGH)
|
|
|
|
{
|
|
|
|
timestampHigh = micros();
|
|
|
|
}
|
2017-10-15 11:11:46 +00:00
|
|
|
else if (timestampHigh != 0)
|
2017-10-15 10:30:15 +00:00
|
|
|
{
|
2017-10-15 11:11:46 +00:00
|
|
|
pulseLength = micros() - timestampHigh;
|
|
|
|
timestampHigh = 0;
|
2017-10-15 10:30:15 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
digitalWrite(m_triggerPin, HIGH);
|
|
|
|
delayMicroseconds(10);
|
|
|
|
digitalWrite(m_triggerPin, LOW);
|
|
|
|
delayMicroseconds(40);
|
|
|
|
|
2017-10-15 11:11:46 +00:00
|
|
|
if (pulseLength < 25e3)
|
2017-10-15 10:30:15 +00:00
|
|
|
{
|
2017-10-15 11:11:46 +00:00
|
|
|
emit distanceUpdated(pulseLength / 0.58);
|
2017-10-15 10:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delay(100);
|
|
|
|
}
|
|
|
|
}
|