Library for interfacing with the Parallax Ping))) sensor.

Dependencies:   FPointer

Committer:
d34d
Date:
Sun Jul 27 19:45:01 2014 +0000
Revision:
1:72fbcb01291d
Parent:
0:99ee7c741e9d
Child:
3:0c337c262d84
Use ticker to delay continuous readings.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
d34d 0:99ee7c741e9d 1 #ifndef PING_H
d34d 0:99ee7c741e9d 2 #define PING_H
d34d 0:99ee7c741e9d 3
d34d 0:99ee7c741e9d 4 #include "mbed.h"
d34d 0:99ee7c741e9d 5 #include "FPointer.h"
d34d 0:99ee7c741e9d 6
d34d 1:72fbcb01291d 7 #define MEASUREMENT_DELAY_US 200
d34d 1:72fbcb01291d 8
d34d 0:99ee7c741e9d 9 /**
d34d 0:99ee7c741e9d 10 * Class for interfacing with the Parallax Ping))) Ultrasonic Sensor
d34d 0:99ee7c741e9d 11 *
d34d 0:99ee7c741e9d 12 * See the Ping)) documentation for more information.
d34d 0:99ee7c741e9d 13 * http://www.parallax.com/sites/default/files/downloads/28015-PING-Documentation-v1.6.pdf
d34d 0:99ee7c741e9d 14 */
d34d 0:99ee7c741e9d 15 class Ping {
d34d 0:99ee7c741e9d 16 public:
d34d 0:99ee7c741e9d 17 /**
d34d 0:99ee7c741e9d 18 * @param pin Digital I/O pin used for communicating with the Ping)))
d34d 0:99ee7c741e9d 19 */
d34d 0:99ee7c741e9d 20 Ping(PinName pin);
d34d 0:99ee7c741e9d 21
d34d 0:99ee7c741e9d 22 /**
d34d 0:99ee7c741e9d 23 * Starts a one shot reading.
d34d 0:99ee7c741e9d 24 *
d34d 0:99ee7c741e9d 25 * @return True if the reading was successfuly started, false if not.
d34d 0:99ee7c741e9d 26 */
d34d 0:99ee7c741e9d 27 bool startReading();
d34d 0:99ee7c741e9d 28
d34d 0:99ee7c741e9d 29 /**
d34d 0:99ee7c741e9d 30 * Starts a reading.
d34d 0:99ee7c741e9d 31 *
d34d 0:99ee7c741e9d 32 * @param continuous Continuous readings if true, one-shot if false.
d34d 0:99ee7c741e9d 33 * @return True if the reading was successfuly started, false if not.
d34d 0:99ee7c741e9d 34 */
d34d 0:99ee7c741e9d 35 bool startReading(bool continuous);
d34d 0:99ee7c741e9d 36
d34d 0:99ee7c741e9d 37 /**
d34d 0:99ee7c741e9d 38 * Returns whether the sensor is busy getting a reading
d34d 0:99ee7c741e9d 39 *
d34d 0:99ee7c741e9d 40 * @return True if busy, false if not.
d34d 0:99ee7c741e9d 41 */
d34d 0:99ee7c741e9d 42 bool isBusy() { return mBusy; }
d34d 0:99ee7c741e9d 43
d34d 0:99ee7c741e9d 44 /**
d34d 0:99ee7c741e9d 45 * Returns whether the valid reading is available
d34d 0:99ee7c741e9d 46 *
d34d 0:99ee7c741e9d 47 * @return True if valid, false if not.
d34d 0:99ee7c741e9d 48 */
d34d 0:99ee7c741e9d 49 bool isValid() { return mValid; }
d34d 0:99ee7c741e9d 50
d34d 0:99ee7c741e9d 51 /**
d34d 0:99ee7c741e9d 52 * Returns the raw reading from the sensor.
d34d 0:99ee7c741e9d 53 *
d34d 0:99ee7c741e9d 54 * @return The time in uS that the sound travelled from the sensor to an object.
d34d 0:99ee7c741e9d 55 */
d34d 0:99ee7c741e9d 56 uint32_t getRawReading() { return mRawReading; }
d34d 0:99ee7c741e9d 57
d34d 0:99ee7c741e9d 58 /**
d34d 0:99ee7c741e9d 59 * Attach a callback function that will be called when a valid reading is available.
d34d 0:99ee7c741e9d 60 *
d34d 0:99ee7c741e9d 61 * @param function Function pointer for callback.
d34d 0:99ee7c741e9d 62 */
d34d 0:99ee7c741e9d 63 void attach(uint32_t (*function)(uint32_t) = 0) { mCallback.attach(function); }
d34d 0:99ee7c741e9d 64
d34d 0:99ee7c741e9d 65 /**
d34d 0:99ee7c741e9d 66 * Attach a class method that will be called when a valid reading is available.
d34d 0:99ee7c741e9d 67 *
d34d 0:99ee7c741e9d 68 * @param item Class object that contains the method to call.
d34d 0:99ee7c741e9d 69 * @param method Method to call.
d34d 0:99ee7c741e9d 70 */
d34d 0:99ee7c741e9d 71 template<class T>
d34d 0:99ee7c741e9d 72 void attach(T* item, uint32_t (T::*method)(uint32_t)) { mCallback.attach(item, method); }
d34d 0:99ee7c741e9d 73
d34d 0:99ee7c741e9d 74 private:
d34d 0:99ee7c741e9d 75 DigitalInOut mSignalIo;
d34d 0:99ee7c741e9d 76 InterruptIn mEvent;
d34d 0:99ee7c741e9d 77
d34d 0:99ee7c741e9d 78 bool mBusy;
d34d 0:99ee7c741e9d 79 bool mValid;
d34d 0:99ee7c741e9d 80 bool mContinuous;
d34d 0:99ee7c741e9d 81
d34d 0:99ee7c741e9d 82 uint32_t mStartTime;
d34d 0:99ee7c741e9d 83 Timer mTimer;
d34d 1:72fbcb01291d 84 Ticker mMeasureDelayTicker;
d34d 0:99ee7c741e9d 85
d34d 0:99ee7c741e9d 86 uint32_t mRawReading;
d34d 0:99ee7c741e9d 87
d34d 0:99ee7c741e9d 88 FPointer mCallback;
d34d 0:99ee7c741e9d 89
d34d 0:99ee7c741e9d 90 void start();
d34d 0:99ee7c741e9d 91 void stop();
d34d 1:72fbcb01291d 92 void nextMeasurement();
d34d 0:99ee7c741e9d 93 };
d34d 0:99ee7c741e9d 94
d34d 0:99ee7c741e9d 95 #endif