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

Dependencies:   FPointer

Committer:
d34d
Date:
Sun Jul 27 19:12:34 2014 +0000
Revision:
0:99ee7c741e9d
Child:
1:72fbcb01291d
Initial commit

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