Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Hotboards_keypad mbed
main.cpp
00001 00002 #include "mbed.h" 00003 #include "Hotboards_keypad.h" 00004 #include <ctype.h> 00005 00006 // Define the keymaps. The blank spot (lower left) is the space character. 00007 char alphaKeys[ 4 ][ 4 ] = 00008 { 00009 { 'a' , 'd' , 'g' }, 00010 { 'j' , 'm' , 'p' }, 00011 { 's' , 'v' , 'y' }, 00012 { ' ' , '.' , '#' } 00013 }; 00014 00015 char numberKeys[ 4 ][ 4 ] = 00016 { 00017 { '1' , '2' , '3' }, 00018 { '4' , '5' , '6' }, 00019 { '7' , '8' , '9' }, 00020 { ' ' , '0' , '#' } 00021 }; 00022 00023 bool alpha = false; // Start with the numeric keypad. 00024 00025 // Defines the pins connected to the rows 00026 DigitalInOut rowPins[ 4 ] = { PA_6 , PA_7 , PB_6 , PC_7 }; 00027 // Defines the pins connected to the cols 00028 DigitalInOut colPins[ 4 ] = { PA_8 , PB_10 , PB_4 , PB_5 }; 00029 00030 // Create two new keypads, one is a number pad and the other is a letter pad. 00031 Keypad numpad( makeKeymap( numberKeys ) , rowPins , colPins , 4 , 4 ); 00032 Keypad ltrpad( makeKeymap( alphaKeys ) , rowPins , colPins , 4 , 4 ); 00033 00034 int startTime; 00035 // For this example we will use the Nucleo LED1 on pin PA_5 00036 DigitalOut led1( LED1 ); 00037 00038 // Configures the serial port 00039 Serial pc( USBTX , USBRX ); 00040 00041 // Configures a timer 00042 Timer t; 00043 00044 char key; 00045 static char virtKey = NO_KEY; // Stores the last virtual key press. (Alpha keys only) 00046 static char physKey = NO_KEY; // Stores the last physical key press. (Alpha keys only) 00047 static char buildStr[ 12 ]; 00048 static uint8_t buildCount; 00049 static uint8_t pressCount; 00050 static uint8_t kpadState; 00051 00052 // Take care of some special events. 00053 00054 void swOnState( char key ) 00055 { 00056 switch( kpadState ) 00057 { 00058 case PRESSED: 00059 if( isalpha( key ) ) // This is a letter key so we're using the letter keymap. 00060 { 00061 if( physKey != key ) // New key so start with the first of 3 characters. 00062 { 00063 pressCount = 0; 00064 virtKey = key; 00065 physKey = key; 00066 } 00067 else // Pressed the same key again... 00068 { 00069 virtKey ++; // so select the next character on that key. 00070 pressCount ++; // Tracks how many times we press the same key. 00071 } 00072 if( pressCount > 2 ) // Last character reached so cycle back to start. 00073 { 00074 pressCount = 0; 00075 virtKey = key; 00076 } 00077 pc.printf( "%c" , virtKey ); // Used for testing. 00078 if( isdigit( key ) || key == ' ' || key == '.' ) 00079 { 00080 pc.printf( "%c" , key ); 00081 } 00082 if( key == '#' ) 00083 { 00084 pc.printf( "\n\r" ); 00085 } 00086 break; 00087 } 00088 case HOLD: 00089 if( key == '#' ) // Toggle between keymaps. 00090 { 00091 if( alpha == true ) // We are currently using a keymap with letters 00092 { 00093 alpha = false; // Now we want a keymap with numbers. 00094 led1 = 0; 00095 } 00096 else // We are currently using a keymap with numbers 00097 { 00098 alpha = true; // Now we want a keymap with letters. 00099 } 00100 } 00101 else // Some key other than '#' was pressed. 00102 { 00103 buildStr[ buildCount ++ ] = ( isalpha( key ) ) ? virtKey : key; 00104 buildStr[ buildCount ] = '\0'; 00105 pc.printf( "\n\r" ); 00106 pc.printf( buildStr ); 00107 } 00108 break; 00109 case RELEASED: 00110 if( buildCount >= sizeof( buildStr ) ) // Our string is full. Start fresh. 00111 { 00112 buildCount = 0; 00113 } 00114 break; 00115 } 00116 } 00117 00118 void keypadEvent_ltr( KeypadEvent key ) 00119 { 00120 // in here when in alpha mode. 00121 kpadState = ltrpad.getState( ); 00122 swOnState( key ); 00123 } 00124 00125 void keypadEvent_num( KeypadEvent key ) 00126 { 00127 // in here when using number keypad 00128 kpadState = numpad.getState( ); 00129 swOnState( key ); 00130 } 00131 00132 int main() 00133 { 00134 // Starts the timer 00135 t.start( ); 00136 led1 = 0; // Turns the LED off. 00137 ltrpad.begin( makeKeymap( alphaKeys ) ); 00138 numpad.begin( makeKeymap( numberKeys ) ); 00139 ltrpad.addEventListener( keypadEvent_ltr ); // Add an event listener. 00140 ltrpad.setHoldTime( 500 ); // Default is 1000mS 00141 numpad.addEventListener( keypadEvent_num ); // Add an event listener. 00142 numpad.setHoldTime( 500 ); // Default is 1000mS 00143 00144 while(1) 00145 { 00146 if( alpha ) 00147 { 00148 key = ltrpad.getKey( ); 00149 } 00150 else 00151 { 00152 key = numpad.getKey( ); 00153 } 00154 if( alpha && t.read_ms( ) - startTime > 100 ) 00155 { 00156 // Flash the LED if we are using the letter keymap. 00157 led1 = !led1; 00158 startTime = t.read_ms( ); 00159 } 00160 } 00161 } 00162
Generated on Mon Jul 18 2022 23:32:29 by
1.7.2