Changing the keyboard layout

Dependencies:   Hotboards_keypad mbed

Committer:
RomanValenciaP
Date:
Tue Mar 08 20:46:33 2016 +0000
Revision:
0:d8190262fb61
first release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RomanValenciaP 0:d8190262fb61 1
RomanValenciaP 0:d8190262fb61 2 #include "mbed.h"
RomanValenciaP 0:d8190262fb61 3 #include "Hotboards_keypad.h"
RomanValenciaP 0:d8190262fb61 4 #include <ctype.h>
RomanValenciaP 0:d8190262fb61 5
RomanValenciaP 0:d8190262fb61 6 // Define the keymaps. The blank spot (lower left) is the space character.
RomanValenciaP 0:d8190262fb61 7 char alphaKeys[ 4 ][ 4 ] =
RomanValenciaP 0:d8190262fb61 8 {
RomanValenciaP 0:d8190262fb61 9 { 'a' , 'd' , 'g' },
RomanValenciaP 0:d8190262fb61 10 { 'j' , 'm' , 'p' },
RomanValenciaP 0:d8190262fb61 11 { 's' , 'v' , 'y' },
RomanValenciaP 0:d8190262fb61 12 { ' ' , '.' , '#' }
RomanValenciaP 0:d8190262fb61 13 };
RomanValenciaP 0:d8190262fb61 14
RomanValenciaP 0:d8190262fb61 15 char numberKeys[ 4 ][ 4 ] =
RomanValenciaP 0:d8190262fb61 16 {
RomanValenciaP 0:d8190262fb61 17 { '1' , '2' , '3' },
RomanValenciaP 0:d8190262fb61 18 { '4' , '5' , '6' },
RomanValenciaP 0:d8190262fb61 19 { '7' , '8' , '9' },
RomanValenciaP 0:d8190262fb61 20 { ' ' , '0' , '#' }
RomanValenciaP 0:d8190262fb61 21 };
RomanValenciaP 0:d8190262fb61 22
RomanValenciaP 0:d8190262fb61 23 bool alpha = false; // Start with the numeric keypad.
RomanValenciaP 0:d8190262fb61 24
RomanValenciaP 0:d8190262fb61 25 // Defines the pins connected to the rows
RomanValenciaP 0:d8190262fb61 26 DigitalInOut rowPins[ 4 ] = { PA_6 , PA_7 , PB_6 , PC_7 };
RomanValenciaP 0:d8190262fb61 27 // Defines the pins connected to the cols
RomanValenciaP 0:d8190262fb61 28 DigitalInOut colPins[ 4 ] = { PA_8 , PB_10 , PB_4 , PB_5 };
RomanValenciaP 0:d8190262fb61 29
RomanValenciaP 0:d8190262fb61 30 // Create two new keypads, one is a number pad and the other is a letter pad.
RomanValenciaP 0:d8190262fb61 31 Keypad numpad( makeKeymap( numberKeys ) , rowPins , colPins , 4 , 4 );
RomanValenciaP 0:d8190262fb61 32 Keypad ltrpad( makeKeymap( alphaKeys ) , rowPins , colPins , 4 , 4 );
RomanValenciaP 0:d8190262fb61 33
RomanValenciaP 0:d8190262fb61 34 int startTime;
RomanValenciaP 0:d8190262fb61 35 // For this example we will use the Nucleo LED1 on pin PA_5
RomanValenciaP 0:d8190262fb61 36 DigitalOut led1( LED1 );
RomanValenciaP 0:d8190262fb61 37
RomanValenciaP 0:d8190262fb61 38 // Configures the serial port
RomanValenciaP 0:d8190262fb61 39 Serial pc( USBTX , USBRX );
RomanValenciaP 0:d8190262fb61 40
RomanValenciaP 0:d8190262fb61 41 // Configures a timer
RomanValenciaP 0:d8190262fb61 42 Timer t;
RomanValenciaP 0:d8190262fb61 43
RomanValenciaP 0:d8190262fb61 44 char key;
RomanValenciaP 0:d8190262fb61 45 static char virtKey = NO_KEY; // Stores the last virtual key press. (Alpha keys only)
RomanValenciaP 0:d8190262fb61 46 static char physKey = NO_KEY; // Stores the last physical key press. (Alpha keys only)
RomanValenciaP 0:d8190262fb61 47 static char buildStr[ 12 ];
RomanValenciaP 0:d8190262fb61 48 static uint8_t buildCount;
RomanValenciaP 0:d8190262fb61 49 static uint8_t pressCount;
RomanValenciaP 0:d8190262fb61 50 static uint8_t kpadState;
RomanValenciaP 0:d8190262fb61 51
RomanValenciaP 0:d8190262fb61 52 // Take care of some special events.
RomanValenciaP 0:d8190262fb61 53
RomanValenciaP 0:d8190262fb61 54 void swOnState( char key )
RomanValenciaP 0:d8190262fb61 55 {
RomanValenciaP 0:d8190262fb61 56 switch( kpadState )
RomanValenciaP 0:d8190262fb61 57 {
RomanValenciaP 0:d8190262fb61 58 case PRESSED:
RomanValenciaP 0:d8190262fb61 59 if( isalpha( key ) ) // This is a letter key so we're using the letter keymap.
RomanValenciaP 0:d8190262fb61 60 {
RomanValenciaP 0:d8190262fb61 61 if( physKey != key ) // New key so start with the first of 3 characters.
RomanValenciaP 0:d8190262fb61 62 {
RomanValenciaP 0:d8190262fb61 63 pressCount = 0;
RomanValenciaP 0:d8190262fb61 64 virtKey = key;
RomanValenciaP 0:d8190262fb61 65 physKey = key;
RomanValenciaP 0:d8190262fb61 66 }
RomanValenciaP 0:d8190262fb61 67 else // Pressed the same key again...
RomanValenciaP 0:d8190262fb61 68 {
RomanValenciaP 0:d8190262fb61 69 virtKey ++; // so select the next character on that key.
RomanValenciaP 0:d8190262fb61 70 pressCount ++; // Tracks how many times we press the same key.
RomanValenciaP 0:d8190262fb61 71 }
RomanValenciaP 0:d8190262fb61 72 if( pressCount > 2 ) // Last character reached so cycle back to start.
RomanValenciaP 0:d8190262fb61 73 {
RomanValenciaP 0:d8190262fb61 74 pressCount = 0;
RomanValenciaP 0:d8190262fb61 75 virtKey = key;
RomanValenciaP 0:d8190262fb61 76 }
RomanValenciaP 0:d8190262fb61 77 pc.printf( "%c" , virtKey ); // Used for testing.
RomanValenciaP 0:d8190262fb61 78 if( isdigit( key ) || key == ' ' || key == '.' )
RomanValenciaP 0:d8190262fb61 79 {
RomanValenciaP 0:d8190262fb61 80 pc.printf( "%c" , key );
RomanValenciaP 0:d8190262fb61 81 }
RomanValenciaP 0:d8190262fb61 82 if( key == '#' )
RomanValenciaP 0:d8190262fb61 83 {
RomanValenciaP 0:d8190262fb61 84 pc.printf( "\n\r" );
RomanValenciaP 0:d8190262fb61 85 }
RomanValenciaP 0:d8190262fb61 86 break;
RomanValenciaP 0:d8190262fb61 87 }
RomanValenciaP 0:d8190262fb61 88 case HOLD:
RomanValenciaP 0:d8190262fb61 89 if( key == '#' ) // Toggle between keymaps.
RomanValenciaP 0:d8190262fb61 90 {
RomanValenciaP 0:d8190262fb61 91 if( alpha == true ) // We are currently using a keymap with letters
RomanValenciaP 0:d8190262fb61 92 {
RomanValenciaP 0:d8190262fb61 93 alpha = false; // Now we want a keymap with numbers.
RomanValenciaP 0:d8190262fb61 94 led1 = 0;
RomanValenciaP 0:d8190262fb61 95 }
RomanValenciaP 0:d8190262fb61 96 else // We are currently using a keymap with numbers
RomanValenciaP 0:d8190262fb61 97 {
RomanValenciaP 0:d8190262fb61 98 alpha = true; // Now we want a keymap with letters.
RomanValenciaP 0:d8190262fb61 99 }
RomanValenciaP 0:d8190262fb61 100 }
RomanValenciaP 0:d8190262fb61 101 else // Some key other than '#' was pressed.
RomanValenciaP 0:d8190262fb61 102 {
RomanValenciaP 0:d8190262fb61 103 buildStr[ buildCount ++ ] = ( isalpha( key ) ) ? virtKey : key;
RomanValenciaP 0:d8190262fb61 104 buildStr[ buildCount ] = '\0';
RomanValenciaP 0:d8190262fb61 105 pc.printf( "\n\r" );
RomanValenciaP 0:d8190262fb61 106 pc.printf( buildStr );
RomanValenciaP 0:d8190262fb61 107 }
RomanValenciaP 0:d8190262fb61 108 break;
RomanValenciaP 0:d8190262fb61 109 case RELEASED:
RomanValenciaP 0:d8190262fb61 110 if( buildCount >= sizeof( buildStr ) ) // Our string is full. Start fresh.
RomanValenciaP 0:d8190262fb61 111 {
RomanValenciaP 0:d8190262fb61 112 buildCount = 0;
RomanValenciaP 0:d8190262fb61 113 }
RomanValenciaP 0:d8190262fb61 114 break;
RomanValenciaP 0:d8190262fb61 115 }
RomanValenciaP 0:d8190262fb61 116 }
RomanValenciaP 0:d8190262fb61 117
RomanValenciaP 0:d8190262fb61 118 void keypadEvent_ltr( KeypadEvent key )
RomanValenciaP 0:d8190262fb61 119 {
RomanValenciaP 0:d8190262fb61 120 // in here when in alpha mode.
RomanValenciaP 0:d8190262fb61 121 kpadState = ltrpad.getState( );
RomanValenciaP 0:d8190262fb61 122 swOnState( key );
RomanValenciaP 0:d8190262fb61 123 }
RomanValenciaP 0:d8190262fb61 124
RomanValenciaP 0:d8190262fb61 125 void keypadEvent_num( KeypadEvent key )
RomanValenciaP 0:d8190262fb61 126 {
RomanValenciaP 0:d8190262fb61 127 // in here when using number keypad
RomanValenciaP 0:d8190262fb61 128 kpadState = numpad.getState( );
RomanValenciaP 0:d8190262fb61 129 swOnState( key );
RomanValenciaP 0:d8190262fb61 130 }
RomanValenciaP 0:d8190262fb61 131
RomanValenciaP 0:d8190262fb61 132 int main()
RomanValenciaP 0:d8190262fb61 133 {
RomanValenciaP 0:d8190262fb61 134 // Starts the timer
RomanValenciaP 0:d8190262fb61 135 t.start( );
RomanValenciaP 0:d8190262fb61 136 led1 = 0; // Turns the LED off.
RomanValenciaP 0:d8190262fb61 137 ltrpad.begin( makeKeymap( alphaKeys ) );
RomanValenciaP 0:d8190262fb61 138 numpad.begin( makeKeymap( numberKeys ) );
RomanValenciaP 0:d8190262fb61 139 ltrpad.addEventListener( keypadEvent_ltr ); // Add an event listener.
RomanValenciaP 0:d8190262fb61 140 ltrpad.setHoldTime( 500 ); // Default is 1000mS
RomanValenciaP 0:d8190262fb61 141 numpad.addEventListener( keypadEvent_num ); // Add an event listener.
RomanValenciaP 0:d8190262fb61 142 numpad.setHoldTime( 500 ); // Default is 1000mS
RomanValenciaP 0:d8190262fb61 143
RomanValenciaP 0:d8190262fb61 144 while(1)
RomanValenciaP 0:d8190262fb61 145 {
RomanValenciaP 0:d8190262fb61 146 if( alpha )
RomanValenciaP 0:d8190262fb61 147 {
RomanValenciaP 0:d8190262fb61 148 key = ltrpad.getKey( );
RomanValenciaP 0:d8190262fb61 149 }
RomanValenciaP 0:d8190262fb61 150 else
RomanValenciaP 0:d8190262fb61 151 {
RomanValenciaP 0:d8190262fb61 152 key = numpad.getKey( );
RomanValenciaP 0:d8190262fb61 153 }
RomanValenciaP 0:d8190262fb61 154 if( alpha && t.read_ms( ) - startTime > 100 )
RomanValenciaP 0:d8190262fb61 155 {
RomanValenciaP 0:d8190262fb61 156 // Flash the LED if we are using the letter keymap.
RomanValenciaP 0:d8190262fb61 157 led1 = !led1;
RomanValenciaP 0:d8190262fb61 158 startTime = t.read_ms( );
RomanValenciaP 0:d8190262fb61 159 }
RomanValenciaP 0:d8190262fb61 160 }
RomanValenciaP 0:d8190262fb61 161 }
RomanValenciaP 0:d8190262fb61 162