Dependencies: I2CMotor I2CServo Robot XBee
Diff: I2CDevice.h
- Revision:
- 1:01d881dc1170
- Child:
- 2:6c60a3006881
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/I2CDevice.h Fri Aug 21 04:51:59 2015 +0000 @@ -0,0 +1,41 @@ +#ifndef INCLUDED_I2CDEVICE_H +#define INCLUDED_I2CDEVICE_H + +#include "mbed.h" + +class I2CDevice{ +public: + static void initI2C( PinName sda, PinName scl ){ + mI2C = new I2C( sda, scl ); + } + + I2CDevice( char address ) : mAddress( address ){ + } + + virtual void write() = 0; + virtual void read() = 0; + + void write( char* data, size_t len ) const{ + mI2C->write( mAddress, data, len ); + } + void read( char* data, size_t len ) const{ + mI2C->read( mAddress, data, len ); + } + + void writeReg( char devAddr, char regAddr, char data ){ + char d[ 2 ] = { regAddr, data }; + mI2C->write( devAddr, d, 2 ); + } + + void readReg( char devAddr, char regAddr, char* data, size_t len ){ + regAddr |= 0x80; + mI2C->write( devAddr, ®Addr, 1, true ); + mI2C->read( devAddr | 1, data, len ); + } + + static I2C* mI2C; +protected: + const char mAddress; +}; + +#endif