An improved version of mbed's I2C class.

Dependents:   acd52832_Humidity_Temp_Example BB acnSensa_LIS aconnoCellularGnss ... more

Revision:
2:3c0eab894a4b
Parent:
1:5ae1807e3902
Child:
3:b9f3eef1fad9
diff -r 5ae1807e3902 -r 3c0eab894a4b aconno_i2c.cpp
--- a/aconno_i2c.cpp	Fri Sep 22 16:48:28 2017 +0000
+++ b/aconno_i2c.cpp	Tue Sep 26 12:03:09 2017 +0000
@@ -9,9 +9,10 @@
 
 #include "aconno_i2c.h"
 
-aconno_i2c::aconno_i2c(PinName data, PinName clock, char address) : I2C(data, clock){
+aconno_i2c::aconno_i2c(I2C *i2c, char address){
+    this->i2c = i2c;    
     i2cAddress = address;
-} 
+}
 
 uint8_t aconno_i2c::writeToReg(char regAddress, char *data, int len){
     uint8_t success; /* '0' - NAK was received '1' - ACK was received, '2' - timeout */
@@ -19,7 +20,7 @@
     
     dataToSend[0] = regAddress;
     dataToSend[1] = *data;
-    success = write(i2cAddress & 0xFE, dataToSend, len + 1); // R/W bit is set low for a write command
+    success = i2c->write(i2cAddress & 0xFE, dataToSend, len + 1); // R/W bit is set low for a write command
     return success;
 }
 
@@ -27,8 +28,8 @@
     uint8_t success;    /* 0 on success (ack), non-0 on failure (nack) */
     char regAddr = regAddress;
     
-    write(i2cAddress & 0xFE, &regAddr, 1);               // R/W bit is set low for a write command
-    success = read(i2cAddress | 0x01, dataBuffer, len);     // R/W bit is set high for a read command
+    i2c->write(i2cAddress & 0xFE, &regAddr, 1);               // R/W bit is set low for a write command
+    success = i2c->read(i2cAddress | 0x01, dataBuffer, len);     // R/W bit is set high for a read command
     return success;
 }
 
@@ -39,7 +40,7 @@
 uint8_t aconno_i2c::readBus(char *dataBuffer, int len){
     uint8_t success;    /* 0 on success (ack), non-0 on failure (nack) */
     
-    success = read(i2cAddress | 0x01, dataBuffer, len);     // R/W bit is set high for a read command
+    success = i2c->read(i2cAddress | 0x01, dataBuffer, len);     // R/W bit is set high for a read command
     return success;
 }
 
@@ -50,15 +51,15 @@
 uint8_t aconno_i2c::sendCommand(char *command, uint8_t len, char *response, uint8_t responseLen){
     uint8_t success;    /* 0 on success (ack), non-0 on failure (nack) */
     
-    write(i2cAddress & 0xFE, command, len);               // R/W bit is set low for a write command
-    success = read(i2cAddress | 0x01, response, responseLen);     // R/W bit is set high for a read command
+    i2c->write(i2cAddress & 0xFE, command, len);               // R/W bit is set low for a write command
+    success = i2c->read(i2cAddress | 0x01, response, responseLen);     // R/W bit is set high for a read command
     return success;
 }
 
 uint8_t aconno_i2c::sendCommand(char *command, uint8_t len){
     uint8_t success;    /* 0 on success (ack), non-0 on failure (nack) */
     
-    success = write(i2cAddress & 0xFE, command, len);
+    success = i2c->write(i2cAddress & 0xFE, command, len);
     return success;
 }