Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2cBusDevice.h Source File

I2cBusDevice.h

00001 /*
00002  *  I2C device base class
00003  *
00004  *  A base class for all I2C devices.
00005  *  This manages the device address and transfers
00006  *
00007  *  Copyright (c) 2010 Tedd OKANO
00008  *  Released under the MIT License: http://mbed.org/license/mit
00009  *
00010  *  revision 1.0  15-Jan-2010   a. 1st release
00011  *  revision 1.1  23-Jan-2010   a. The word "MBED_I2cBusDevice" is used instead of _I2cBusDevice_ to avoid symbol conflict
00012  *                              b. copyright notice added
00013  */
00014 
00015 #ifndef    MBED_I2cBusDevice
00016 #define    MBED_I2cBusDevice
00017 
00018 #include    "mbed.h"
00019 
00020 class I2cBusDevice {
00021 public:
00022 
00023     I2cBusDevice( I2C *i2c, char dev_address ) {
00024         bus          = i2c;
00025         device       = dev_address;
00026     }
00027 
00028     ~I2cBusDevice() {
00029     }
00030 
00031     int write( char *data, int length ) {
00032         return ( bus->write( device, data, length) );
00033     }
00034 
00035     int read( char *data, int length ) {
00036         return ( bus->read( device, data, length) );
00037     }
00038 
00039     int read( char reg_ptr, char *data, int length ) {
00040         if ( bus->write( device, &reg_ptr, 1 ) )
00041             return ( 1 );
00042 
00043         if ( bus->read( device, data, length ) )
00044             return ( 1 );
00045 
00046         return ( 0 );
00047     }
00048 
00049 protected:
00050     I2C     *bus;
00051     char    device;
00052 
00053 private:
00054     static char    i2c_error;
00055 }
00056 ;
00057 
00058 #endif