
Hello code for "Switch Science mbed LPC824"
Fork of SwitchSciencembedLPC824_test by
I2cBusDevice.h@9:e2333773718f, 2014-12-18 (annotated)
- Committer:
- asagin
- Date:
- Thu Dec 18 03:20:52 2014 +0000
- Revision:
- 9:e2333773718f
Erased comment
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
asagin | 9:e2333773718f | 1 | /* |
asagin | 9:e2333773718f | 2 | * I2C device base class |
asagin | 9:e2333773718f | 3 | * |
asagin | 9:e2333773718f | 4 | * A base class for all I2C devices. |
asagin | 9:e2333773718f | 5 | * This manages the device address and transfers |
asagin | 9:e2333773718f | 6 | * |
asagin | 9:e2333773718f | 7 | * Copyright (c) 2010 Tedd OKANO |
asagin | 9:e2333773718f | 8 | * Released under the MIT License: http://mbed.org/license/mit |
asagin | 9:e2333773718f | 9 | * |
asagin | 9:e2333773718f | 10 | * revision 1.0 15-Jan-2010 a. 1st release |
asagin | 9:e2333773718f | 11 | * revision 1.1 23-Jan-2010 a. The word "MBED_I2cBusDevice" is used instead of _I2cBusDevice_ to avoid symbol conflict |
asagin | 9:e2333773718f | 12 | * b. copyright notice added |
asagin | 9:e2333773718f | 13 | */ |
asagin | 9:e2333773718f | 14 | |
asagin | 9:e2333773718f | 15 | #ifndef MBED_I2cBusDevice |
asagin | 9:e2333773718f | 16 | #define MBED_I2cBusDevice |
asagin | 9:e2333773718f | 17 | |
asagin | 9:e2333773718f | 18 | #include "mbed.h" |
asagin | 9:e2333773718f | 19 | |
asagin | 9:e2333773718f | 20 | class I2cBusDevice { |
asagin | 9:e2333773718f | 21 | public: |
asagin | 9:e2333773718f | 22 | |
asagin | 9:e2333773718f | 23 | I2cBusDevice( I2C *i2c, char dev_address ) { |
asagin | 9:e2333773718f | 24 | bus = i2c; |
asagin | 9:e2333773718f | 25 | device = dev_address; |
asagin | 9:e2333773718f | 26 | } |
asagin | 9:e2333773718f | 27 | |
asagin | 9:e2333773718f | 28 | ~I2cBusDevice() { |
asagin | 9:e2333773718f | 29 | } |
asagin | 9:e2333773718f | 30 | |
asagin | 9:e2333773718f | 31 | int write( char *data, int length ) { |
asagin | 9:e2333773718f | 32 | return ( bus->write( device, data, length) ); |
asagin | 9:e2333773718f | 33 | } |
asagin | 9:e2333773718f | 34 | |
asagin | 9:e2333773718f | 35 | int read( char *data, int length ) { |
asagin | 9:e2333773718f | 36 | return ( bus->read( device, data, length) ); |
asagin | 9:e2333773718f | 37 | } |
asagin | 9:e2333773718f | 38 | |
asagin | 9:e2333773718f | 39 | int read( char reg_ptr, char *data, int length ) { |
asagin | 9:e2333773718f | 40 | if ( bus->write( device, ®_ptr, 1 ) ) |
asagin | 9:e2333773718f | 41 | return ( 1 ); |
asagin | 9:e2333773718f | 42 | |
asagin | 9:e2333773718f | 43 | if ( bus->read( device, data, length ) ) |
asagin | 9:e2333773718f | 44 | return ( 1 ); |
asagin | 9:e2333773718f | 45 | |
asagin | 9:e2333773718f | 46 | return ( 0 ); |
asagin | 9:e2333773718f | 47 | } |
asagin | 9:e2333773718f | 48 | |
asagin | 9:e2333773718f | 49 | protected: |
asagin | 9:e2333773718f | 50 | I2C *bus; |
asagin | 9:e2333773718f | 51 | char device; |
asagin | 9:e2333773718f | 52 | |
asagin | 9:e2333773718f | 53 | private: |
asagin | 9:e2333773718f | 54 | static char i2c_error; |
asagin | 9:e2333773718f | 55 | } |
asagin | 9:e2333773718f | 56 | ; |
asagin | 9:e2333773718f | 57 | |
asagin | 9:e2333773718f | 58 | #endif |