/*############################################################# Program Name : ex4_KEYPAD_DISCO_F429I_PressAndHoldKeys 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. The characters are added to a string and displayed as a string as they are pressed. '*' is used to clear the string. When '#' is held in for +/- 2 seconds, a '$' will be added to the string. 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 int bufpos=0; char k, key; uint32_t holdcounter=0; keypad.Show(1, 1); //Display the keypad, enable the Message and Output Box areas keypad.WriteMsgBoxLine(0, "Press keys..(* clear)"); //display a message in the keypad.WriteMsgBoxLine(1, "(hold # for surprise)"); //Message Box area of the keypad while(1) { k = keypad.ReadKey(); //read the current key pressed if(k != '\0') { //if a key is pressed if(k == '#') { holdcounter++; //increment a counter while the # key is held in if(holdcounter >= 150) { //a nominal value which the counter much reach to indicate the key was held in long enough holdcounter = 0; //reset counter buf[bufpos] = '$'; buf[bufpos+1] = '\0'; bufpos++; keypad.WriteOutBox(buf); } } else { 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 { 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 } } else { holdcounter = 0; //reset counter when keys are released } } }