Slightly revised version of the SRF02 library created by Craig A. Evans

Fork of SRF02 by Craig Evans

SRF02.h

Committer:
andreykotov91
Date:
2016-05-08
Revision:
4:5e3954a62b23
Parent:
2:d2f687283735

File content as of revision 4:5e3954a62b23:

/**
@file SRF02.h

@brief Header file containing member functions and variables

*/

//header guard
#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 
    * @param sclPin - mbed SCL pin
    * 
    */
    SRF02(PinName sdaPin, PinName sclPin);
    /** Read distance in centimetres
    *
    * @returns distance in centimetres (int)
    * 
    */
    int getDistanceCm();

private:
    /** Called in event of error - flashes LED and hangs
    */
    void error();


private:
    /**Class data member names often have a trailing underscore to make them easily identifiable
    */
    I2C* i2c_;
    DigitalOut* led_;
};

#endif