/*############################################################# Program Name : ex2_MCP23008_Keypad_ConvertToInt Author : Grant Phillips Date Modified : 05/04/2016 Compiler : ARMmbed Tested On : STM32F4-Discovery Description : Example program that demonstrates the use of the MCP23008_Keypad class to use when a 4x4 keypad is connected to a MCP23008 I2C IO Expander. Allows a user to enter a value with the keypad and then converts the string to an integer value after the '*' key is pressed. The output is displayed on a PC using the Serial Wire Viewer (SWV). See the SWV examples for an explanation of the use of this feature. Requirements : * STM32F4-Discovery Board Circuit : * a MCP23008 connected to the STM32F4-Discovery as follows: MCP23008 SDA pin - PB9 MCP23008 SCL pin - PB6 * the 4x4 keypad must be connected to the MCP23008 as follows: MCP23008 Keypad -------- ------ GP7 ROW4 GP6 ROW3 GP5 ROW2 GP4 ROW1 GP3 COL4 GP2 COL3 GP1 COL2 GP0 COL1 * A wire link between PB3 and pin6 of the SWD connector (CN2) as explained in the SWV examples section ##############################################################*/ #include "mbed.h" #include "MCP23008_Keypad.h" #include "SWO.h" MCP23008_Keypad kpad(PB_9, PB_6, 0x42); //SDA, SCL, MCP23008 I2C address SWO_Channel SWO; int main() { char key; int released = 1; //flag for indicating if a key was released (=1) or not (=0) int val; int bufcounter = 0; char buffer[20]; //string for storing the characters from the keypad while(1) { key = kpad.ReadKey(); //read the current key pressed, but don't wait for a key to be pressed if(key == '\0') //if no key is pressed released = 1; //set the flag when all keys are released if((key != '\0') && (released == 1)) { //if a key is pressed AND previous key was released if(key == '*') { buffer[bufcounter] = '\0'; //add NULL to terminate the string bufcounter = 0; //reset counter val = atoi(buffer); //convert the buffer string to an integer value SWO.printf("\nThe value is %d\n->", val); //display the integer value } else { SWO.printf("%c", key); //print the character for the key pressed buffer[bufcounter] = key; //add the character to the string buffer bufcounter++; //increment the buffer counter } released = 0; //clear the flag to indicate that key is still pressed } } }