/*############################################################# Program Name : ex3_MCP23008_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 MCP23008_Keypad class to use when a 4x4 keypad is connected to a MCP23008 I2C IO Expander. 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 : * 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) 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; } } }