Simple library for reading distance from SRF02 Ultrasonic Sensor in I2C mode.
Fork of SRF02 by
Revision 2:43910fab41ca, committed 2016-05-05
- Comitter:
- el14rirr
- Date:
- Thu May 05 01:55:58 2016 +0000
- Parent:
- 1:8e6587d88773
- Commit message:
- no changes!
Changed in this revision
SRF02.cpp | Show annotated file Show diff for this revision Revisions of this file |
SRF02.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/SRF02.cpp Sun Mar 08 14:21:40 2015 +0000 +++ b/SRF02.cpp Thu May 05 01:55:58 2016 +0000 @@ -1,20 +1,20 @@ /** @file SRF02.cpp - + @brief Member functions implementations - + */ #include "mbed.h" #include "SRF02.h" - + SRF02::SRF02(PinName sdaPin, PinName sclPin) { i2c = new I2C(sdaPin,sclPin); // create new I2C instance and initialise i2c->frequency(400000); // I2C Fast Mode - 400kHz leds = new BusOut(LED4,LED3,LED2,LED1); - + } - + int SRF02::getDistanceCm() { char data[2]; @@ -39,11 +39,12 @@ ack = i2c->read(SRF02_R_ADD,data,2); if (ack) error(); // if we don't receive acknowledgement, flash error message - + // high byte is first, then low byte, so combine into 16-bit value return (data[0] << 8) | data[1]; } + void SRF02::error() { while(1) {
--- a/SRF02.h Sun Mar 08 14:21:40 2015 +0000 +++ b/SRF02.h Thu May 05 01:55:58 2016 +0000 @@ -1,63 +1,63 @@ /** @file SRF02.h - + @brief Header file containing member functions and variables - + */ - + #ifndef SRF02_H #define SRF02_H - + // addresses #define SRF02_R_ADD 0xE1 #define SRF02_W_ADD 0xE0 - + // registers #define CMD_REG 0x00 #define RANGE_H_REG 0x02 #define RANGE_L_REG 0x03 - + // commands #define INCH_CMD 0x50 #define CM_CMD 0x51 #define US_CMD 0x52 - + #include "mbed.h" - + /** @brief Library for interfacing with SRF02 Ultrasonic Sensor in I2C @see http://www.robot-electronics.co.uk/htm/srf02tech.htm - + @brief Revision 1.0 - + @author Craig A. Evans @date March 2014 * * Example: * @code - + #include "mbed.h" #include "SRF02.h" - + int main() { - + while(1) { - + // read sensor distance in cm and print over serial port int distance = sensor.getDistanceCm(); serial.printf("Distance = %d cm\n",distance); // short delay before next measurement wait(0.5); - + } } * @endcode */ - + class SRF02 { public: - + /** Create a SRF02 object connected to the specified I2C pins * * @param sdaPin - mbed SDA pin @@ -71,17 +71,17 @@ * */ int getDistanceCm(); - + private: /** Hangs in infinite loop flashing 'blue lights of death' * */ void error(); - - + + private: // private variables I2C* i2c; BusOut* leds; }; - + #endif \ No newline at end of file