Manages the sending and reading of pulses via separate trigger and echo pins.

Dependents:   HCSR04

Files at this revision

API Documentation at this revision

Comitter:
aagnone3
Date:
Mon Aug 03 18:26:05 2015 +0000
Commit message:
Initial revision.

Changed in this revision

PulseManager.cpp Show annotated file Show diff for this revision Revisions of this file
PulseManager.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 67bcc374fa16 PulseManager.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PulseManager.cpp	Mon Aug 03 18:26:05 2015 +0000
@@ -0,0 +1,142 @@
+#include "PulseManager.h"
+
+PulseManager::PulseManager(PinName trigger, PinName echo):
+        startval(0), pulsetime(), runtime(), trigger(trigger), echo(echo)    {}
+
+
+PulseManager::~PulseManager() {}
+
+void PulseManager::write(int val) {
+    trigger = val;
+}
+
+void PulseManager::write_us(int val, int time)   {
+    // Write the complement of the high/low signal for a clean desired signal
+    trigger = !val;
+    wait_us(4);
+    
+    // Now write the desired signal for the given amount of time in us
+    trigger = val;
+    wait_us(time);
+    
+    // Write the complement again to end the signal write
+    trigger = !val;
+}
+
+int PulseManager::read_us()  {
+    // Reset the pulse timer
+    pulsetime.reset();
+    
+    // Wait for edge
+    startval = echo;
+    while (echo == startval)   {}
+    
+    // Measure pulse width
+    pulsetime.start();
+    while (echo != startval)  {}
+    pulsetime.stop();
+    
+    return pulsetime.read_us();
+}
+
+int PulseManager::read_us(int timeout)   {
+    // Reset timers, start runtime timer to detect timeout
+    runtime.reset();
+    runtime.start();
+    pulsetime.reset();
+    
+    // Wait for edge
+    startval = echo;
+    while (echo == startval)  {
+        if (runtime.read_us() > timeout)   return -1;
+    }
+    
+    // Measure pulse width
+    pulsetime.start();
+    while (echo != startval)   {
+        if (runtime.read_us() > timeout)   return -1;
+    }
+    pulsetime.stop();
+    
+    return pulsetime.read_us();
+}
+
+int PulseManager::read_high_us()  {
+    // Reset timer
+    pulsetime.reset();
+    
+    // Wait for next edge of echo signal
+    while (echo == 1) {}
+    while (echo == 0) {}
+    
+    // Measure pulse width of the echo pulse
+    pulsetime.start();
+    while (echo == 1) {}
+    pulsetime.stop();
+    
+    return pulsetime.read_us();
+}
+
+int PulseManager::read_high_us(int timeout)  {
+    // Reset runtime timer for timeout detection
+    runtime.reset();
+    runtime.start();
+    
+    // Reset pulseTimer
+    pulsetime.reset();
+    
+    // Wait for next edge of echo signal
+    while (echo == 1) {
+        if (runtime.read_us() > timeout)   return -1;
+    }
+    while (echo == 0) {
+        if (runtime.read_us() > timeout)   return -1;
+    }
+    
+    // Measure pulse width of the echo pulse
+    pulsetime.start();
+    while (echo == 1) {
+        if (runtime.read_us() > timeout)   return -1;    
+    }
+    pulsetime.stop();
+    
+    return pulsetime.read_us();
+}
+
+int PulseManager::read_low_us()   {
+    // Wait for next edge of echo signal
+    while (echo == 0) {
+    }
+    while (echo == 1) {
+    }
+    
+    // Measure pulse width of the echo pulse
+    pulsetime.start();
+    while (echo == 0) {}
+    pulsetime.stop();
+    
+    return pulsetime.read_us();
+}
+
+int PulseManager::read_low_us(int timeout)   {
+    // Reset runtime timer for timeout detection
+    runtime.reset();
+    runtime.start();
+    
+    // Wait for next edge of echo signal
+    while (echo == 0) {
+        if (runtime.read_us() > timeout)   return -1;
+    }
+    while (echo == 1) {
+        if (runtime.read_us() > timeout)   return -1;
+    }
+    
+    // Measure pulse width of the echo pulse
+    pulsetime.start();
+    while (echo == 0) {
+        if (runtime.read_us() > timeout)   return -1;    
+    }
+    pulsetime.stop();
+    
+    return pulsetime.read_us();
+}
diff -r 000000000000 -r 67bcc374fa16 PulseManager.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PulseManager.h	Mon Aug 03 18:26:05 2015 +0000
@@ -0,0 +1,80 @@
+#ifndef MBED_PULSEMANAGER_H
+#define MBED_PULSEMANAGER_H
+
+#include "mbed.h"
+
+class PulseManager   {
+    public:
+        /** 
+        * Create a PulseManager object connected to the specified pin
+        * @param triggerPin output pin to use for generating a trigger signal
+        * @param echoPin input pin to use for measuring an echo signal
+        */
+        PulseManager(PinName triggerPin, PinName echoPin);
+        
+        /**
+        * Destructor to free allocated memory
+        */
+        ~PulseManager();
+        
+        /** 
+        * Set the value of the trigger pin
+        * @param val Value to set, 0 for LOW, otherwise HIGH
+        */
+        void write(int val);
+        
+        /** 
+        * Send a pulse of a given value for a specified time
+        * @param val Value to set, 0 for LOW, otherwise HIGH
+        * @param time Length of pulse in microseconds
+        */
+        void write_us(int val, int time);
+        
+        /** 
+        * Return the length of the next HIGH pulse in microsconds
+        */
+        int read_high_us();
+        
+        /** 
+        * Return the length of the next HIGH pulse in microseconds or -1 if longer than timeout
+        * @param timeout Time before pulse reading aborts and returns -1, in microseconds
+        */
+        int read_high_us(int timeout);
+        
+        /** 
+        * Return the length of the next LOW pulse in microsconds
+        */
+        int read_low_us();
+        
+        /** 
+        * Return the length of the next LOW pulse in microseconds or -1 if longer than timeout
+        * @param timeout Time before pulse reading aborts and returns -1, in microseconds
+        */
+        int read_low_us(int timeout);
+        
+        /** 
+        * Return the length of the next pulse in microsconds
+        */
+        int read_us();
+        
+        /** 
+        * Return the length of the next pulse in microseconds or -1 if longer than timeout
+        * @param timeout Time before pulse reading aborts and returns -1, in microseconds
+        */
+        int read_us(int timeout);
+        
+    private:
+    
+        /** Start value for echo pin to use for detecting a change */
+        int startval;
+        /** Timer for measuring pulse width */
+        Timer pulsetime;
+        /** Timer for detecting a timeout */
+        Timer runtime;
+        /** Pin handle for trigger pin */
+        DigitalOut trigger;
+        /** Pin handle for echo pin */
+        DigitalIn  echo;
+};
+
+#endif
\ No newline at end of file