/*############################################################# Program Name : ex3_Keypad_KeyHoldIn 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 and exit a menu type application by holding in the '*' key. 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) uint32_t holdcounter = 0; int InMenuFlag = 0; int EnterMenuFlag = 0; SWO.printf("\nHold in * to enter the menu ..."); 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 == '*') { //if * is pressed holdcounter++; //increment a counter while the * key is held in if(holdcounter >= 200) { //a nominal value which the counter much reach to indicate the key was held in long enough (+/- 2 sec) holdcounter = 0; if(InMenuFlag == 0) { //if not in the menu ... InMenuFlag = 1; //now go into the menu EnterMenuFlag = 1; //display the menu } else { //if in the menu... InMenuFlag = 0; //now go out of the menu SWO.printf("\n\n\n\n\n\n\nHold in * to enter the menu ..."); } } } else { if(InMenuFlag == 1) { //if any other key is pressed AND in the menu SWO.printf("%c\n", key); //print the character for the key pressed switch(key) { case '1': SWO.printf("You selected Popcorn\n\n"); EnterMenuFlag = 1; break; case '2': SWO.printf("You selected Chocolate\n\n"); EnterMenuFlag = 1; break; case '3': SWO.printf("You selected Milkshake\n\n"); EnterMenuFlag = 1; break; case '4': SWO.printf("You selected Cooldrink\n\n"); EnterMenuFlag = 1; break; default: SWO.printf("Incorrect choice!\n\n"); EnterMenuFlag = 1; } released = 0; //clear the flag to indicate that key is still pressed } } } if(EnterMenuFlag == 1) { //display the menu SWO.printf("\n\n\nMENU\n"); SWO.printf("====\n"); SWO.printf("1. Popcorn\n"); SWO.printf("2. Chocolate\n"); SWO.printf("3. Milkshake\n"); SWO.printf("4. Cooldrink\n"); SWO.printf("Make your selection (hold * to exit): "); EnterMenuFlag = 0; } } }