How to call functions when a key changes it's state

Dependencies:   Hotboards_keypad mbed

Files at this revision

API Documentation at this revision

Comitter:
RomanValenciaP
Date:
Tue Mar 08 20:46:03 2016 +0000
Commit message:
first release

Changed in this revision

Hotboards_keypad.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r a2b95a8cb722 Hotboards_keypad.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Hotboards_keypad.lib	Tue Mar 08 20:46:03 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/Hotboards/code/Hotboards_keypad/#e870110f753b
diff -r 000000000000 -r a2b95a8cb722 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Mar 08 20:46:03 2016 +0000
@@ -0,0 +1,78 @@
+
+#include "mbed.h"
+#include "Hotboards_keypad.h"
+
+// Defines the keys array with it's respective number of rows & cols,
+// and with the value of each key
+char keys[ 4 ][ 4 ] =
+{
+    { '1' , '2' , '3' , 'A' },
+    { '4' , '5' , '6' , 'B' },
+    { '7' , '8' , '9' , 'C' },
+    { '*' , '0' , '#' , 'D' }
+};
+
+// Defines the pins connected to the rows
+DigitalInOut rowPins[ 4 ] = { PA_6 , PA_7 , PB_6 , PC_7 };
+// Defines the pins connected to the cols
+DigitalInOut colPins[ 4 ] = { PA_8 , PB_10 , PB_4 , PB_5 };
+
+// Creates a new keyboard with the values entered before
+Keypad kpd( makeKeymap( keys ) , rowPins , colPins , 4 , 4 );
+
+// Configures the serial port
+Serial pc( USBTX , USBRX );
+
+// For this example we will use the Nucleo LED1 on pin PA_5
+DigitalOut led1( LED1 );
+bool blink = false;
+bool ledPin_state;
+
+
+// Taking care of some special events.
+void kpdEvent( KeypadEvent key )
+{
+    switch( kpd.getState( ) )
+    {
+        case PRESSED:
+            if( key == '#' )
+            {
+                led1 = !led1;
+                ledPin_state = led1; // Remember LED state, lit or unlit.
+            }
+            break;
+        case RELEASED:
+            if( key == '*' )
+            {
+                led1 = ledPin_state;
+                blink = false; // Restore LED state from before it started blinking.
+            }
+            break;
+        case HOLD:
+            if( key == '*' )
+            {
+                blink = true; // Blink the LED when holding the * key.
+            }
+            break;
+    }
+}
+
+int main()
+{
+    led1 = 1; // Turn the LED on.
+    ledPin_state = led1; // Store initial LED state. HIGH when LED is on.
+    kpd.addEventListener( kpdEvent ); // Add an event listener for this keypad
+    while(1)
+    {
+        char key = kpd.getKey( );
+        if( key )
+        {
+            pc.printf( "%c" , key );
+        }
+        if( blink )
+        {
+            led1 = !led1; // Change the ledPin from Hi2Lo or Lo2Hi
+            wait_ms( 100 );
+        }
+    }
+}
diff -r 000000000000 -r a2b95a8cb722 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Mar 08 20:46:03 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/87f2f5183dfb
\ No newline at end of file