This library provides simplified I2C access to a Microchip MCP23x17 GPIO expender device, including a general interface for any GPIO expender

Dependents:   MCP23017App

Revision:
1:ec9e770173d5
Parent:
0:ebd3a7cc9b92
Child:
2:3bea48e1505c
--- a/MCP23017_I2C.cpp	Fri Jan 09 14:37:42 2015 +0000
+++ b/MCP23017_I2C.cpp	Fri Jan 09 15:35:40 2015 +0000
@@ -1,4 +1,4 @@
-/* mbed simplified access to Microchip MCP28x17 GPIO expender devices (I2C)
+/* mbed simplified access to Microchip MCP23x17 GPIO expender devices (I2C)
  * Copyright (c) 2010-2012 ygarcia, MIT License
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
@@ -405,6 +405,9 @@
         }
         DEBUG("CMCP23017_I2C::read: gpioRegisterValue=%02x", gpioRegisterValue)
         
+        *p_value = (isBitSet(gpioRegisterValue, gpioBit)) ? 0x01 : 0x00;
+        DEBUG("CMCP23017_I2C::read: p_value=%02x", *p_value)
+        
         DEBUG_LEAVE("CMCP23017_I2C::read: 0")
         return 0;
     }
@@ -480,12 +483,47 @@
         _buses.erase(p_busId);
     }
 
-    int CMCP23017_I2C::busRead(const unsigned char p_busId) {
-        return -1;
+    int CMCP23017_I2C::busRead(const unsigned char p_busId, unsigned short * p_value) {
+        // Sanity checks
+        if (_buses.size() == 0) {
+            return -1;
+        }
+        std::map<unsigned char, std::list<unsigned char> >::iterator result = _buses.find(p_busId);
+        if (result == _buses.end()) { // Invalid bus identifier
+            return -1;
+        }
+        
+        std::list<unsigned char>::reverse_iterator rit;
+        for (std::list<unsigned char>::reverse_iterator rit = result->second.rbegin(); rit != result->second.rend(); ++rit) {
+            unsigned char regvalue;
+            if (read(*rit, &regvalue) == 0) {
+                *p_value = (*p_value | regvalue) << 1;
+            } else {
+                *p_value <<= 1;
+            }
+        } // End of 'for' statement
+        
+        return 0;
     }
 
-    int CMCP23017_I2C::busWrite(const unsigned char p_busId, const unsigned char p_value) {
-        return -1;
+    int CMCP23017_I2C::busWrite(const unsigned char p_busId, const unsigned short p_value) {
+        // Sanity checks
+        if (_buses.size() == 0) {
+            return -1;
+        }
+        std::map<unsigned char, std::list<unsigned char> >::iterator result = _buses.find(p_busId);
+        if (result == _buses.end()) { // Invalid bus identifier
+            return -1;
+        }
+        
+        std::list<unsigned char>::reverse_iterator rit;
+        unsigned short value = p_value;
+        for (std::list<unsigned char>::reverse_iterator rit = result->second.rbegin(); rit != result->second.rend(); ++rit) {
+            write(*rit, value & 0x01);
+            value >>= 1;
+        } // End of 'for' statement
+        
+        return 0;
     }
 
     bool CMCP23017_I2C::writeRegister(const unsigned char p_registerId, const unsigned char p_value) {