Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of HCSR04 by
Diff: hcsr04.cpp
- Revision:
- 2:0acb6ade091c
- Parent:
- 1:093521f56089
- Child:
- 3:e6795cb9439c
diff -r 093521f56089 -r 0acb6ade091c hcsr04.cpp
--- a/hcsr04.cpp Sat Mar 30 17:32:04 2013 +0000
+++ b/hcsr04.cpp Sat Mar 30 18:41:34 2013 +0000
@@ -21,25 +21,30 @@
#include "hcsr04.h"
-DistMeasure::DistMeasure(PinName TrigPin,PinName EchoPin,unsigned int maxtime):
- trigger(TrigPin), echo(EchoPin), timeout(maxtime)
+DistMeasure::DistMeasure(PinName TrigPin,PinName EchoPin):
+ trigger(TrigPin), echo(EchoPin)
{
pulsetime.stop();
pulsetime.reset();
echo.rise(this,&DistMeasure::isr_rise);
- echo.fall(this,&DistMeasure::isr_fall);
+ echo.fall(this,&DistMeasure::isr_fall);
+ trigger=0;
}
-
DistMeasure::~DistMeasure()
{
}
-
void DistMeasure::isr_rise(void)
{
pulsetime.start();
}
+void DistMeasure::start_measurement(void)
+{
+ trigger=1;
+ wait_us(10);
+ trigger=0;
+}
void DistMeasure::isr_fall(void)
{
@@ -48,6 +53,7 @@
distance= (pulsedur*343)/20000;
pulsetime.reset();
}
+
void DistMeasure::rise (void (*fptr)(void))
{
echo.rise(fptr);
@@ -57,11 +63,11 @@
echo.fall(fptr);
}
-unsigned int DistMeasure::get_distance_cm()
+unsigned int DistMeasure::get_dist_cm()
{
- if(distance > 65530) {
- return -1;
- } else {
- return distance;
- }
+ return distance;
}
+unsigned int DistMeasure::get_pulse_us()
+{
+ return pulsedur;
+}
