Library for the SRF05 Ultrasonic Rangefinder

Dependencies:   RangeFinder

Fork of SRF05 by Simon Ford

Committer:
simon
Date:
Fri Nov 19 22:34:56 2010 +0000
Revision:
0:e758665e072c
Revision in new library format (extracted from old cookbook), with doxygen comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:e758665e072c 1 /* mbed SRF05 Ultrasonic Rangefiner Library
simon 0:e758665e072c 2 * Copyright (c) 2007-2010, cstyles, sford
simon 0:e758665e072c 3 *
simon 0:e758665e072c 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
simon 0:e758665e072c 5 * of this software and associated documentation files (the "Software"), to deal
simon 0:e758665e072c 6 * in the Software without restriction, including without limitation the rights
simon 0:e758665e072c 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 0:e758665e072c 8 * copies of the Software, and to permit persons to whom the Software is
simon 0:e758665e072c 9 * furnished to do so, subject to the following conditions:
simon 0:e758665e072c 10 *
simon 0:e758665e072c 11 * The above copyright notice and this permission notice shall be included in
simon 0:e758665e072c 12 * all copies or substantial portions of the Software.
simon 0:e758665e072c 13 *
simon 0:e758665e072c 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 0:e758665e072c 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 0:e758665e072c 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 0:e758665e072c 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 0:e758665e072c 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 0:e758665e072c 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 0:e758665e072c 20 * THE SOFTWARE.
simon 0:e758665e072c 21 */
simon 0:e758665e072c 22
simon 0:e758665e072c 23 #ifndef MBED_SRF05_H
simon 0:e758665e072c 24 #define MBED_SRF05_H
simon 0:e758665e072c 25
simon 0:e758665e072c 26 #include "mbed.h"
simon 0:e758665e072c 27
simon 0:e758665e072c 28 /** Library for the SRF05 Ultrasonic range finder
simon 0:e758665e072c 29 *
simon 0:e758665e072c 30 * Example:
simon 0:e758665e072c 31 * @code
simon 0:e758665e072c 32 * // Print measured distance
simon 0:e758665e072c 33 *
simon 0:e758665e072c 34 * #include "mbed.h"
simon 0:e758665e072c 35 * #include "SRF05.h"
simon 0:e758665e072c 36 *
simon 0:e758665e072c 37 * SRF05 srf(p9,p10);
simon 0:e758665e072c 38 *
simon 0:e758665e072c 39 * int main() {
simon 0:e758665e072c 40 * while(1) {
simon 0:e758665e072c 41 * printf("Measured : %.1f\n", srf.read());
simon 0:e758665e072c 42 * wait(0.2);
simon 0:e758665e072c 43 * }
simon 0:e758665e072c 44 * }
simon 0:e758665e072c 45 * @endcode
simon 0:e758665e072c 46 */
simon 0:e758665e072c 47 class SRF05 {
simon 0:e758665e072c 48 public:
simon 0:e758665e072c 49
simon 0:e758665e072c 50 /** Create a SRF05 object, connected to the specified pins
simon 0:e758665e072c 51 *
simon 0:e758665e072c 52 * @param trigger DigitalOut to the SRF05 trigger
simon 0:e758665e072c 53 * @param echo InterruptIn to measure the return pulse
simon 0:e758665e072c 54 */
simon 0:e758665e072c 55 SRF05(PinName trigger, PinName echo);
simon 0:e758665e072c 56
simon 0:e758665e072c 57 /** A non-blocking function that will return the last measurement
simon 0:e758665e072c 58 *
simon 0:e758665e072c 59 * @returns floating point representation of distance in cm
simon 0:e758665e072c 60 */
simon 0:e758665e072c 61 float read();
simon 0:e758665e072c 62
simon 0:e758665e072c 63 /** A short hand way of using the read function */
simon 0:e758665e072c 64 operator float();
simon 0:e758665e072c 65
simon 0:e758665e072c 66 private :
simon 0:e758665e072c 67 DigitalOut _trigger;
simon 0:e758665e072c 68 InterruptIn _echo;
simon 0:e758665e072c 69 Timer _timer;
simon 0:e758665e072c 70 Ticker _ticker;
simon 0:e758665e072c 71 void _rising (void);
simon 0:e758665e072c 72 void _falling (void);
simon 0:e758665e072c 73 void _startRange (void);
simon 0:e758665e072c 74 float _dist;
simon 0:e758665e072c 75 };
simon 0:e758665e072c 76
simon 0:e758665e072c 77 #endif