example project to explain how to write a class library

test_LM75B.h

Committer:
okano
Date:
2014-11-03
Revision:
6:ab79d1157026
Parent:
5:863659ef0231
Child:
7:9a7235e5fe27

File content as of revision 6:ab79d1157026:

/** A sample code for class library development
 *
 *  @author  Tedd OKANO
 *  @version 1.0
 *  @date    22-Oct-2014
 *
 *  This code explains steps of the class code development. 
 *  The each steps are saved in code repository. 
 *  A I2C temperature sensor LM75B is used as a target device. 
 *
 *  About LM75B:
 *    http://www.nxp.com/documents/data_sheet/LM75B.pdf
 */

#include "mbed.h"

/** Default slave address */
#define     ADDRESS_LM75B   0x90

/** LM75B register name ans addresses */
#define     LM75B_Conf      0x01
#define     LM75B_Temp      0x00
#define     LM75B_Tos       0x03
#define     LM75B_Thyst     0x02

/** test_LM75B class library
 *
 *  Class library to provide very simple interface for mbed
 *
 *  Example:
 *  @code
 *  #include "mbed.h"
 *  #include "test_LM75B.h"
 *  
 *  test_LM75B  temp0( p28, p27 );
 *  
 *  I2C         i2c( p28, p27 );
 *  test_LM75B  temp1( i2c );
 *  
 *  
 *  int main()
 *  {
 *      float   t0;
 *      float   t1;
 *  
 *      i2c.frequency( 400 * 1000 );
 *  
 *      while(1) {
 *          t0   = temp0;
 *          t1   = temp1;
 *          printf( "temp = %7.3f, %7.3f\r\n", t0, t1 );
 *          wait( 1 );
 *      }
 *  }
 *  @endcode
 */
class test_LM75B
{
public:

    /** Create a test_LM75B instance connected to specified I2C pins with specified address
     *
     * @param sda I2C-bus SDA pin
     * @param scl I2C-bus SCL pin
     * @param address (option) I2C-bus slave address (default: 0x90)
     */
    test_LM75B( PinName sda, PinName scl, char address = ADDRESS_LM75B );

    /** Create a test_LM75B instance connected to specified I2C pins with specified address
     *
     * @param i2c_obj I2C object (instance)
     * @param address (option) I2C-bus slave address (default: 0x90)
     */
    test_LM75B( I2C &i2c_obj, char address = ADDRESS_LM75B );
    
    /** Destractor
     */
    ~test_LM75B();
    
    /** Initialization
     */    
    void    init( void );

    /** Read temperature
     * 
     *  @return value of degree Celsius (in float) 
     */
    float   read( void );

    /** Read temperature
     * 
     *  @return the object returns the read value 
     */
    operator float( void );
    
private:
    I2C     *i2c_p;
    I2C     &i2c;
    char    adr;
};