Test application for the library MCP23017_I2C

Dependencies:   DebugLibrary MCP23017_I2C mbed

Revision:
1:e981573db063
Parent:
0:1d91b4046326
Child:
2:83b9df729e73
--- a/main.cpp	Wed Jan 07 09:49:59 2015 +0000
+++ b/main.cpp	Fri Jan 09 14:37:46 2015 +0000
@@ -11,6 +11,7 @@
 DigitalOut g_availableLed(LED1); // To verify if program in running
 Ticker g_available; // LED1 will flash with a period of 2s
 char DisplayMenuAndGetChoice();
+void GpioAIntr(); // Interrupt on GPIOA
 
 static CMCP23017_I2C g_gpioExp(p9, p10, 0x27, p15, p16, p17);
 
@@ -18,7 +19,9 @@
     // Launch available indicator
     g_available.attach(&AvailableIndicator, 2.0);
     
-    g_gpioExp.Initialize();
+    g_gpioExp.Initialize(0x01); // GpioA<0> as input port, no interrupt mirroring, Active low  
+    g_gpioExp.setupInterruptPin(GPA0, AbstractGpioExpender::OnFalling); // A switch with pull-up is connected to GPIOA<0>, interrupt on falling mode
+    g_gpioExp.setIntrACallback(GpioAIntr); // Set callback for interrup on GPIOA<0>
 
     while(true) {
         switch (DisplayMenuAndGetChoice()) {
@@ -34,6 +37,18 @@
             case 'd':
                 g_gpioExp.write(GPB2, 0);
                 break;
+            case 'e':
+                g_gpioExp.write(GPB3, 1);
+                break;
+            case 'f':
+                g_gpioExp.write(GPB3, 0);
+                break;
+            case 'g':
+                g_gpioExp.write(GPB7, 1);
+                break;
+            case 'h':
+                g_gpioExp.write(GPB7, 0);
+                break;
             case 'r':
                 g_gpioExp.reset();
                 break;
@@ -56,11 +71,23 @@
     std::cout << "\r" << std::endl << "\r" << std::endl << "MCP23017_I2C v0.1\r" << std::endl;
     std::cout << "\tSet PortB-0   :\t\ta\r" << std::endl;
     std::cout << "\tUnset PortB-0 :\t\tb\r" << std::endl;
-    std::cout << "\tUnset PortB-2 :\t\tc\r" << std::endl;
+    std::cout << "\tSet PortB-2   :\t\tc\r" << std::endl;
     std::cout << "\tUnset PortB-2 :\t\td\r" << std::endl;
+    std::cout << "\tSet PortB-3   :\t\te\r" << std::endl;
+    std::cout << "\tUnset PortB-3 :\t\tf\r" << std::endl;
+    std::cout << "\tSet PortB-7   :\t\tg\r" << std::endl;
+    std::cout << "\tUnset PortB-7 :\t\th\r" << std::endl;
     std::cout << "\tReset device  :\t\tr\r" << std::endl;
     std::cout << "Enter your choice: " << std::flush;
     value = getchar(); 
     std::cout << "\r" << std::endl;
     return value;
 }
+
+void GpioAIntr() {
+    unsigned char gpioId, gpioValue;
+    wait(0.6); // Debounce timer
+    g_gpioExp.getLastInterruptPin(&gpioId, &gpioValue); // Get interrupt info and clear it
+    std::cout << "\r" << std::endl << "GpioAIntr: interrupt on pin #" << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(gpioId) << " - value: " << static_cast<unsigned int>(gpioValue) << "\r" << std::endl;
+}
+