Library for setting up the Parallax X-Band Motion Detector.

Dependents:   Parallax_XBAND_Demo Motion_Detection_Parallax

Revision:
2:6e294a3a74f8
Parent:
1:860badad8be9
Child:
3:b9e54783e0c5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Parallax_X-Band.h	Sun Mar 08 20:01:37 2015 +0000
@@ -0,0 +1,87 @@
+#include "mbed.h"
+
+/**
+ * Wrapper for the Parallax X-Band Motion Detector
+ */
+class xband
+{
+public:
+
+    /** 
+    * Initialize the xband motion detector and setup interrupts on specified pins.
+    * Default is to be disabled.
+    *
+    * @param pin_enable Enable pin. Connected to EN on xband.
+    * @param pin_input Data pin. Connected to OUT on xband.
+    */ 
+    xband(PinName pin_enable, PinName pin_input);
+    
+    /**
+    * Destructor
+    */
+    ~xband();
+    
+    /** 
+    * Subroutine to enable and disable the detector.
+    *
+    * @param enable True to enable, False to disable.
+    */ 
+    void enable(bool enable);
+
+    /** 
+    * Get count value and return it as an integer.
+    *
+    * @return Integer value of current count.
+    */ 
+    int read_count();
+    
+    /** 
+    * Resets the count variable to 0.
+    */ 
+    void reset_count();
+    
+    /** 
+    * Checks if a new velocity value has been calculated.
+    *
+    * @return True if a new velocity value is avaliable. False if the value has
+    * already been read.
+    */ 
+    bool velocityack();
+    
+    /** 
+    * Get current velocity reading. Sets a flag to false to indicate a stale value after reading.
+    *
+    * @return Float value of currently measured velocity.
+    */ 
+    float read_velocity();
+    
+private:    
+    /** 
+    * Interrupt service routine to collect data on the input pin.
+    */ 
+    void xband_RiseISR();
+    
+    /** 
+    * Interrupt service routine to calculate the velocity of the moving object.
+    */ 
+    void xband_velocityISR();
+
+    // Setup pin input types.
+    DigitalOut _pin_enable;
+    InterruptIn _pin_input;
+
+    // Stores count value.
+    volatile int _count;
+    // Old count value.
+    volatile int _lastcount;
+    // Store calculated velocity.
+    float _velocity;
+    // Velocity read status flag.
+    bool _velocityflag;
+
+    // Timer for differentiation
+    Timer _sampletime;
+    
+    // Interrupt to calculate velocity.
+    Ticker _samplevelocity;
+};
\ No newline at end of file