/*############################################################# Program Name : ex2_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 Keypad class to use when a 4x4 keypad using a polling method. 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 : * 4x4 Keypad connected as follows: COL1 (pin1) - PE15 COL2 (pin2) - PE14 COL3 (pin3) - PE13 COL4 (pin4) - PE12 ROW1 (pin5) - PE11 ROW2 (pin6) - PE10 ROW3 (pin7) - PE9 ROW4 (pin8) - PE8 * A wire link between PB3 and pin6 of the SWD connector (CN2) as explained in the SWV examples section ##############################################################*/ #include "mbed.h" #include "Keypad.h" #include "SWO.h" Keypad kpad(PE_15, PE_14, PE_13, PE_12, PE_11, PE_10, PE_9, PE_8); //col1, col2, col3, col4, row1, row2, row3, row4 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 } } }