MCP23009 is a general IO I2C chip . This lib provide higher level interface ( set pin value, set io modes etc ) without need to have knowledge of the I2C registers

Dependents:   MCP23009tst

Files at this revision

API Documentation at this revision

Comitter:
wbeaumont
Date:
Fri Jun 03 16:01:45 2016 +0000
Parent:
0:d829b4edd564
Commit message:
added more functions , tested with hardware using n FRDM_KL05Z)

Changed in this revision

mcp23009.cpp Show annotated file Show diff for this revision Revisions of this file
mcp23009.h Show annotated file Show diff for this revision Revisions of this file
diff -r d829b4edd564 -r 9146d773a5a3 mcp23009.cpp
--- a/mcp23009.cpp	Fri Jun 03 12:40:49 2016 +0000
+++ b/mcp23009.cpp	Fri Jun 03 16:01:45 2016 +0000
@@ -2,7 +2,7 @@
 //#include "mbed.h"  // should not be in only for printf debug ?
 
 
-#define VERSION_MCP23009_SRC  "0.10"  
+#define VERSION_MCP23009_SRC  "0.20"  
 
 // definition registers 
 
@@ -29,12 +29,20 @@
     _i2c_interface->write(_device_address, data,2);     
 }
 
+int MCP23009::outp_status( int pinnr){
+    return status_gen(pinnr,OLAT);
+}
+
 int MCP23009::status( int pinnr){
+    return status_gen(pinnr,GPIO);
+}    
+
+int MCP23009::status_gen( int pinnr , char reg){
     int result=0;
-    data[0]=GPIO;
+    data[0]=reg;
     result= _i2c_interface->write(_device_address, data,1); // set reg
     if ( result ) return -1;
-    char rb[0];
+    char rb[1];
     result = _i2c_interface->read(_device_address,rb,1); // read the current status 
     if ( result ) return -2;
     if ( pinnr != 8) {
@@ -96,7 +104,7 @@
     if (pinnr < 8 ) {
         result= _i2c_interface->write(_device_address, data,1); // set to ioreg
         if ( result ) return -1;
-        char rb[0];
+        char rb[1];
         result = _i2c_interface->read(_device_address, rb,1); // read the current status 
         if ( result ) return -2;
         data[1]= 1<<pinnr;
diff -r d829b4edd564 -r 9146d773a5a3 mcp23009.h
--- a/mcp23009.h	Fri Jun 03 12:40:49 2016 +0000
+++ b/mcp23009.h	Fri Jun 03 16:01:45 2016 +0000
@@ -9,7 +9,7 @@
 #include "I2CInterface.h" 
 #include "DACInterface.h" 
 
-#define VERSION_MCP23009_HDR "0.1"
+#define VERSION_MCP23009_HDR "0.20"
 
 /** MCP23009 class.
  *  Used for interfacing with a mcp23009 8 bit io expander
@@ -18,7 +18,8 @@
  *  This includes the "virtual" I2CInterface class that is the interface to the I2C device 
  *  An implementation of the I2Cinterface class for the MBED can be found at 
  *  interrupt is not supported (yet )
- *  
+ *  V 0.10  initial version
+ *  V 0.20  added outp_status , tested with hardware. 
  * (C) Wim Beaumont Universiteit Antwerpen 2016
  *  
  */
@@ -53,8 +54,22 @@
      *  @return none zero in case of error 
      */
      int set_as_output(int pinnr ,int pullup,int polarity=-1 );   
-     
+    
+    /** read the status of all IO pins and indicate if the pin, with pinnr is  1 or 0
+     *  if pinnr= 8 the status of all pins is returned
+     *  the status is the level of the port not the "output status" ( open drain so output status can be different) 
+     *  @param pinnr : range 0 .. 8  , if 8 the return value is the status of all pins ,if 0 ..7 it returns 0 or 1 
+     *  @return   0 or 1 if pinnr 0..7 ,  byte ( 0 ..255  ) if pinnr =8  neg value in case of I2C error 
+     */
      int status( int pinnr=8);
+     /** read the status of output register all IO pins and indicate if the pin, with pinnr is  1 or 0
+     *  if pinnr= 8 the status of all pins is returned
+     *  the outp_status is the status of the output latch!! not the actual levels on the IO lines 
+     *  @param pinnr : range 0 .. 8  , if 8 the return value is the status of all pins ,if 0 ..7 it returns 0 or 1 
+     *  @return   0 or 1 if pinnr 0..7 ,  byte ( 0 ..255  ) if pinnr =8  neg value in case of I2C error 
+     */
+     int outp_status( int pinnr=8);
+
      int set( int pinnr , int value);
      protected:
     /** pointer to the I2C interface driver. */
@@ -65,6 +80,7 @@
       char data[5];
       int set_io_pin(bool input ,int pinnr,int pullup,  int polarity  );  
       int pinmanipulation ( bool active , int pinnr );
+      int status_gen( int pinnr , char reg);
 };
 
-#endif
\ No newline at end of file
+#endif