/*############################################################# Program Name : ex5_KEYPAD_DISCO_F429I_ConvertToInt Author : Grant Phillips Date Modified : 10/05/2016 Compiler : ARMmbed Tested On : STM32F4-Discovery Description : Example program that demonstrates the use of the KEYPAD_DISCO_F429I class to implement a 4x4 keypad on the STM32F429I-Discovery board. Allows a user to enter a value with the keypad and then converts the string to an integer value after the '#' key is pressed. Requirements : * STM32F4-Discovery Board Circuit : * NONE ##############################################################*/ // #include "mbed.h" #include "TS_DISCO_F429ZI.h" #include "LCD_DISCO_F429ZI.h" #include "KEYPAD_DISCO_F429ZI.h" KEYPAD_DISCO_F429ZI keypad; int main() { char buf[20]; //string variable char temp[20]; int bufpos=0; char k, key; int val; keypad.Show(1, 1); //Display the keypad, enable the Message and Output Box areas keypad.WriteMsgBoxLine(0, "Enter a value ... "); //display a message in the keypad.WriteMsgBoxLine(1, "(* clear; # accept)"); //Message Box area of the keypad while(1) { k = keypad.ReadKey(); //read the current key pressed if(k != '\0') { //if a key is pressed key = k; //store the key in a variable do //read until key is released k = keypad.ReadKey(); while(k != '\0'); if(key == '*') { buf[0] = '\0'; //'clear' the string bufpos = 0; //start at character 0 keypad.WriteOutBox(" "); //clear the line } else if(key == '#') { val = atoi(buf); //convert the buffer string to an integer value sprintf(temp, "%d", val); keypad.WriteMsgBoxLine(0, " "); //clear line 0 keypad.WriteMsgBoxLine(1, " "); //clear line 1 keypad.WriteMsgBoxLine(0, "You entered a value: "); keypad.WriteMsgBoxLine(1, temp); buf[0] = '\0'; //'clear' the string bufpos = 0; //start at character 0 keypad.WriteOutBox(" "); //clear the line } else { buf[bufpos] = key; //add current keypressed to the string buf[bufpos+1] = '\0'; //add NULL to end of string bufpos++; } keypad.WriteOutBox(buf); //display result in Output Box area of keypad } } }