Yann Garcia / Mbed 2 deprecated MCP23017App

Dependencies:   DebugLibrary MCP23017_I2C mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <string>
00002 #include <iostream>
00003 #include <iomanip>
00004 
00005 #include "MCP23017_I2C.h"
00006 
00007 /*
00008  * Declare functions
00009  */
00010 void AvailableIndicator(); // LED1 flashing for program while program is alive
00011 DigitalOut g_availableLed(LED1); // To verify if program in running
00012 Ticker g_available; // LED1 will flash with a period of 2s
00013 char DisplayMenuAndGetChoice();
00014 void GpioAIntr(); // Interrupt on GPIOA
00015 
00016 static CMCP23017_I2C g_gpioExp(p9, p10, 0x27, p15, p16, p17);
00017 
00018 int main() {
00019     // Launch available indicator
00020     g_available.attach(&AvailableIndicator, 2.0);
00021     
00022     g_gpioExp.Initialize(0x01); // GpioA<0> as input port, no interrupt mirroring, Active low  
00023     g_gpioExp.setupInterruptPin(GPA0, AbstractGpioExpender::OnFalling); // A switch with pull-up is connected to GPIOA<0>, interrupt on falling mode
00024     g_gpioExp.setIntrACallback(GpioAIntr); // Set callback for interrup on GPIOA<0>
00025 
00026     while(true) {
00027         switch (DisplayMenuAndGetChoice()) {
00028             case 'a':
00029                 g_gpioExp.write(GPB0, 1);
00030                 break;
00031             case 'b':
00032                 g_gpioExp.write(GPB0, 0);
00033                 break;
00034             case 'c':
00035                 g_gpioExp.write(GPB2, 1);
00036                 break;
00037             case 'd':
00038                 g_gpioExp.write(GPB2, 0);
00039                 break;
00040             case 'e':
00041                 g_gpioExp.write(GPB3, 1);
00042                 break;
00043             case 'f':
00044                 g_gpioExp.write(GPB3, 0);
00045                 break;
00046             case 'g':
00047                 g_gpioExp.write(GPB4, 1);
00048                 break;
00049             case 'h':
00050                 g_gpioExp.write(GPB4, 0);
00051                 break;
00052             case 'i':
00053                 g_gpioExp.write(GPB7, 1);
00054                 break;
00055             case 'j':
00056                 g_gpioExp.write(GPB7, 0);
00057                 break;
00058             case 'r':
00059                 g_gpioExp.reset();
00060                 break;
00061             case 's': {
00062                     std::list<unsigned char> lcdBus;
00063                     lcdBus.push_back(GPA7); // First item in the list
00064                     lcdBus.push_back(GPA6);
00065                     lcdBus.push_back(GPA5);
00066                     lcdBus.push_back(GPA4);
00067                     unsigned char busId = g_gpioExp.createBus(lcdBus);
00068                     g_gpioExp.busWrite(busId, 0x000A); // GPA7=1, GPA6=0, GPA5=1, GPA4=0 - LSB is GPB4, MSB is GPA7
00069                     wait_us(20);
00070                     g_gpioExp.busWrite(busId, 0x0000); // GPA7=1, GPA6=0, GPA5=1, GPA4=0
00071                     wait_us(20);
00072                     g_gpioExp.deleteBus(busId);
00073                 }
00074                 break;
00075             default:
00076                 std::cout << "Invalid user choice\r" << std::endl;
00077                 break;
00078         } // End of 'switch' statement
00079         
00080     } // End of 'while' loop
00081 }
00082 
00083 void AvailableIndicator()
00084 {
00085     g_availableLed = !g_availableLed;
00086 } // End of AvailableIndicator
00087 
00088 char DisplayMenuAndGetChoice()
00089 {
00090     char value;
00091     std::cout << "\r" << std::endl << "\r" << std::endl << "MCP23017_I2C v0.1\r" << std::endl;
00092     std::cout << "\tSet MUX_EN1 (PortB-0)     :\t\ta\r" << std::endl;
00093     std::cout << "\tUnset MUX_EN1 (PortB-0)   :\t\tb\r" << std::endl;
00094     std::cout << "\tSet MUX_ADDR0 (PortB-2)   :\t\tc\r" << std::endl;
00095     std::cout << "\tUnset MUX_ADDR0 (PortB-2) :\t\td\r" << std::endl;
00096     std::cout << "\tSet MUX_ADDR1 (PortB-3)   :\t\te\r" << std::endl;
00097     std::cout << "\tUnset MUX_ADDR1 (PortB-3) :\t\tf\r" << std::endl;
00098     std::cout << "\tSet MUX_ADDR2 (PortB-4)   :\t\tg\r" << std::endl;
00099     std::cout << "\tUnset MUX_ADDR2 (PortB-4) :\t\th\r" << std::endl;
00100     std::cout << "\tSet BOARD_EN (PortB-7)    :\t\ti\r" << std::endl;
00101     std::cout << "\tUnset BOARD_EN (PortB-7)  :\t\tj\r" << std::endl;
00102     std::cout << "\tReset device              :\t\tr\r" << std::endl;
00103     std::cout << "\tBus test                  :\t\ts\r" << std::endl;
00104     std::cout << "Enter your choice: " << std::flush;
00105     value = getchar(); 
00106     std::cout << "\r" << std::endl;
00107     return value;
00108 }
00109 
00110 void GpioAIntr() {
00111     unsigned char gpioId, gpioValue;
00112     wait(0.6); // Debounce timer
00113     g_gpioExp.getLastInterruptPinAndValue(&gpioId, &gpioValue); // Get interrupt info and clear it
00114     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;
00115 }
00116