Jatin Sharma
/
RaheeNew
RaheeNew
Fork of Adafruit9-DOf by
Header/I2C_base.h@1:c3381056a1c6, 2015-11-28 (annotated)
- Committer:
- jatinsha
- Date:
- Sat Nov 28 10:06:40 2015 +0000
- Revision:
- 1:c3381056a1c6
- Parent:
- 0:772bf4786416
js
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bmanga95 | 0:772bf4786416 | 1 | #ifndef I2C_BASE_H |
bmanga95 | 0:772bf4786416 | 2 | #define I2C_BASE_H |
bmanga95 | 0:772bf4786416 | 3 | |
bmanga95 | 0:772bf4786416 | 4 | #define _MBED_ |
bmanga95 | 0:772bf4786416 | 5 | |
bmanga95 | 0:772bf4786416 | 6 | //Base class to extend I2C support to multiple platforms |
bmanga95 | 0:772bf4786416 | 7 | typedef char byte; |
bmanga95 | 0:772bf4786416 | 8 | |
bmanga95 | 0:772bf4786416 | 9 | |
bmanga95 | 0:772bf4786416 | 10 | class I2C_base |
bmanga95 | 0:772bf4786416 | 11 | { |
bmanga95 | 0:772bf4786416 | 12 | public: |
bmanga95 | 0:772bf4786416 | 13 | virtual void frequency (int frequency) = 0; //set the communication frequency |
bmanga95 | 0:772bf4786416 | 14 | virtual bool read (int address, byte* data, int length) = 0; //read data from the bus |
bmanga95 | 0:772bf4786416 | 15 | virtual bool write (int address, const byte* data, int length) = 0; //write data to the bus |
bmanga95 | 0:772bf4786416 | 16 | virtual bool writeByte (int address, byte data) = 0; //write byte to the bus |
bmanga95 | 0:772bf4786416 | 17 | |
bmanga95 | 0:772bf4786416 | 18 | private: |
bmanga95 | 0:772bf4786416 | 19 | int _frequency; |
bmanga95 | 0:772bf4786416 | 20 | }; |
bmanga95 | 0:772bf4786416 | 21 | |
bmanga95 | 0:772bf4786416 | 22 | extern I2C_base* i2c; |
bmanga95 | 0:772bf4786416 | 23 | |
bmanga95 | 0:772bf4786416 | 24 | |
bmanga95 | 0:772bf4786416 | 25 | //MBED programming environmnet |
bmanga95 | 0:772bf4786416 | 26 | #ifdef _MBED_ |
bmanga95 | 0:772bf4786416 | 27 | #include "mbed.h" |
bmanga95 | 0:772bf4786416 | 28 | //define SDA and SCL if not already defined |
bmanga95 | 0:772bf4786416 | 29 | #ifndef SDA |
bmanga95 | 0:772bf4786416 | 30 | #define SDA p28 |
bmanga95 | 0:772bf4786416 | 31 | #endif |
bmanga95 | 0:772bf4786416 | 32 | |
bmanga95 | 0:772bf4786416 | 33 | #ifndef SCL |
bmanga95 | 0:772bf4786416 | 34 | #define SCL p27 |
bmanga95 | 0:772bf4786416 | 35 | #endif |
bmanga95 | 0:772bf4786416 | 36 | //******************************************* |
bmanga95 | 0:772bf4786416 | 37 | |
bmanga95 | 0:772bf4786416 | 38 | |
bmanga95 | 0:772bf4786416 | 39 | /* should be placed somewhere else, just debug code*/ |
bmanga95 | 0:772bf4786416 | 40 | |
bmanga95 | 0:772bf4786416 | 41 | |
bmanga95 | 0:772bf4786416 | 42 | |
bmanga95 | 0:772bf4786416 | 43 | int millis(); |
bmanga95 | 0:772bf4786416 | 44 | |
bmanga95 | 0:772bf4786416 | 45 | /***************************************************/ |
bmanga95 | 0:772bf4786416 | 46 | |
bmanga95 | 0:772bf4786416 | 47 | class I2C_MBED: public I2C_base |
bmanga95 | 0:772bf4786416 | 48 | { |
bmanga95 | 0:772bf4786416 | 49 | public: |
bmanga95 | 0:772bf4786416 | 50 | I2C_MBED (PinName pSDA, PinName pSCL): _i2c(pSDA,pSCL){} |
bmanga95 | 0:772bf4786416 | 51 | virtual void frequency (int hz) |
bmanga95 | 0:772bf4786416 | 52 | { |
bmanga95 | 0:772bf4786416 | 53 | _i2c.frequency(hz); |
bmanga95 | 0:772bf4786416 | 54 | } |
bmanga95 | 0:772bf4786416 | 55 | virtual bool read(int address, byte* data, int length) |
bmanga95 | 0:772bf4786416 | 56 | { |
bmanga95 | 0:772bf4786416 | 57 | return !_i2c.read(address<<1,data,length); |
bmanga95 | 0:772bf4786416 | 58 | } |
bmanga95 | 0:772bf4786416 | 59 | virtual bool write(int address, const byte* data, int length) |
bmanga95 | 0:772bf4786416 | 60 | { |
bmanga95 | 0:772bf4786416 | 61 | return !_i2c.write(address<<1,data,length); |
bmanga95 | 0:772bf4786416 | 62 | } |
bmanga95 | 0:772bf4786416 | 63 | virtual bool writeByte(int address, byte data) |
bmanga95 | 0:772bf4786416 | 64 | { |
bmanga95 | 0:772bf4786416 | 65 | return write(address, &data, 1); |
bmanga95 | 0:772bf4786416 | 66 | } |
bmanga95 | 0:772bf4786416 | 67 | |
bmanga95 | 0:772bf4786416 | 68 | private: |
bmanga95 | 0:772bf4786416 | 69 | I2C _i2c; |
bmanga95 | 0:772bf4786416 | 70 | }; |
bmanga95 | 0:772bf4786416 | 71 | |
bmanga95 | 0:772bf4786416 | 72 | |
bmanga95 | 0:772bf4786416 | 73 | |
bmanga95 | 0:772bf4786416 | 74 | #endif //_MBED_ |
bmanga95 | 0:772bf4786416 | 75 | |
bmanga95 | 0:772bf4786416 | 76 | |
bmanga95 | 0:772bf4786416 | 77 | #endif //I2C_BASE_H |