A Library to control I2C SRF02 sensor

Dependents:   LPC1768BagSensor

Committer:
bobboteck
Date:
Fri Dec 16 14:09:37 2011 +0000
Revision:
1:6b978ea41787
Parent:
0:8ac34d529a3b
Modified Oxygen tag

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bobboteck 0:8ac34d529a3b 1 /* mbed SRF02 Ultrasonic Ranger Sensor Library
bobboteck 0:8ac34d529a3b 2 * Created by bobboteck at 16/11/2011
bobboteck 0:8ac34d529a3b 3 *
bobboteck 0:8ac34d529a3b 4 * Based on: Chris Styles [http://mbed.org/users/chris/] Library for SRF08
bobboteck 0:8ac34d529a3b 5 * "http://mbed.org/users/chris/programs/SRF08/603nk/docs/"
bobboteck 0:8ac34d529a3b 6 *
bobboteck 0:8ac34d529a3b 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
bobboteck 0:8ac34d529a3b 8 * of this software and associated documentation files (the "Software"), to deal
bobboteck 0:8ac34d529a3b 9 * in the Software without restriction, including without limitation the rights
bobboteck 0:8ac34d529a3b 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
bobboteck 0:8ac34d529a3b 11 * copies of the Software, and to permit persons to whom the Software is
bobboteck 0:8ac34d529a3b 12 * furnished to do so, subject to the following conditions:
bobboteck 0:8ac34d529a3b 13 *
bobboteck 0:8ac34d529a3b 14 * The above copyright notice and this permission notice shall be included in
bobboteck 0:8ac34d529a3b 15 * all copies or substantial portions of the Software.
bobboteck 0:8ac34d529a3b 16 *
bobboteck 0:8ac34d529a3b 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
bobboteck 0:8ac34d529a3b 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
bobboteck 0:8ac34d529a3b 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
bobboteck 0:8ac34d529a3b 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
bobboteck 0:8ac34d529a3b 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
bobboteck 0:8ac34d529a3b 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
bobboteck 0:8ac34d529a3b 23 * THE SOFTWARE.
bobboteck 0:8ac34d529a3b 24 */
bobboteck 0:8ac34d529a3b 25
bobboteck 0:8ac34d529a3b 26 #ifndef SRF02_H
bobboteck 0:8ac34d529a3b 27 #define SRF02_H
bobboteck 0:8ac34d529a3b 28
bobboteck 0:8ac34d529a3b 29 #include "mbed.h"
bobboteck 0:8ac34d529a3b 30
bobboteck 0:8ac34d529a3b 31 #define INCHES_RESULT 0x50 // For result in inches
bobboteck 0:8ac34d529a3b 32 #define CENTIMETERS_RESULT 0x51 // For result in centimeters
bobboteck 0:8ac34d529a3b 33 #define MICROSECONDS_RESULT 0x52 // For result in micro-seconds
bobboteck 1:6b978ea41787 34 /** Library for the SRF02 Ultrasonic Ranger Sensor, using the I2C bus for the
bobboteck 0:8ac34d529a3b 35 * comunication. Remeber that sensor can function in serial mode if MODE pin
bobboteck 0:8ac34d529a3b 36 * is connected to the ground, but this libary not support this mode.
bobboteck 0:8ac34d529a3b 37 *
bobboteck 0:8ac34d529a3b 38 * Example:
bobboteck 0:8ac34d529a3b 39 * @code
bobboteck 0:8ac34d529a3b 40 * // Continuously measuring range
bobboteck 0:8ac34d529a3b 41 * #include "mbed.h"
bobboteck 0:8ac34d529a3b 42 * #include "SRF02.h"
bobboteck 0:8ac34d529a3b 43 * // Create instance of class SRF02 for device at address 0xE0 and take mesure in cm
bobboteck 0:8ac34d529a3b 44 * SRF02 srf02(p28, p27, 0xE0, 1);
bobboteck 0:8ac34d529a3b 45 * DigitalOut led1(LED1);
bobboteck 0:8ac34d529a3b 46 *
bobboteck 0:8ac34d529a3b 47 * int main()
bobboteck 0:8ac34d529a3b 48 * {
bobboteck 0:8ac34d529a3b 49 * led1=1;
bobboteck 0:8ac34d529a3b 50 * while(1)
bobboteck 0:8ac34d529a3b 51 * {
bobboteck 0:8ac34d529a3b 52 * printf("Measured range : %.2f cm\n\r",srf02.read());
bobboteck 0:8ac34d529a3b 53 * wait(0.5);
bobboteck 0:8ac34d529a3b 54 * led1=!led1;
bobboteck 0:8ac34d529a3b 55 * }
bobboteck 0:8ac34d529a3b 56 * }
bobboteck 0:8ac34d529a3b 57 * @endcode
bobboteck 0:8ac34d529a3b 58 */
bobboteck 0:8ac34d529a3b 59 class SRF02
bobboteck 0:8ac34d529a3b 60 {
bobboteck 0:8ac34d529a3b 61 public:
bobboteck 0:8ac34d529a3b 62 enum measure_type{INCHES=0x050,CENTIMETERS=0x51,MICROSECONDS=0x52};
bobboteck 1:6b978ea41787 63 /** Creates an instance of class. Setting the pin used for I2C, the address of device and the measure range type.
bobboteck 0:8ac34d529a3b 64 *
bobboteck 0:8ac34d529a3b 65 * @param sda A pin used for SDA I2C signal.
bobboteck 0:8ac34d529a3b 66 * @param scl A pin used for SCL I2C signal.
bobboteck 0:8ac34d529a3b 67 * @param addr The address of I2C SRF02 device.
bobboteck 0:8ac34d529a3b 68 * @param measure_type The of mesure response (0-inches,1-centimeters,2-micro-seconds).
bobboteck 0:8ac34d529a3b 69 */
bobboteck 0:8ac34d529a3b 70 SRF02(PinName sda, PinName scl, int addr, char type);
bobboteck 0:8ac34d529a3b 71
bobboteck 0:8ac34d529a3b 72 /* Destroyer of class instance. */
bobboteck 0:8ac34d529a3b 73 ~SRF02();
bobboteck 0:8ac34d529a3b 74
bobboteck 1:6b978ea41787 75 /** Start and return the range measure.
bobboteck 0:8ac34d529a3b 76 *
bobboteck 0:8ac34d529a3b 77 * @returns The value of measure.
bobboteck 0:8ac34d529a3b 78 */
bobboteck 0:8ac34d529a3b 79 float read();
bobboteck 0:8ac34d529a3b 80
bobboteck 0:8ac34d529a3b 81 private:
bobboteck 0:8ac34d529a3b 82 I2C _i2c; // I2C object to comunicate with SRF02
bobboteck 0:8ac34d529a3b 83 int _addr; // Address of sensor
bobboteck 1:6b978ea41787 84 char _typem; // Type of mesure
bobboteck 0:8ac34d529a3b 85 };
bobboteck 0:8ac34d529a3b 86
bobboteck 0:8ac34d529a3b 87 #endif