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