11

Dependents:   Program_R11

Revision:
0:2df66331c109
Child:
1:b5eee44fa8a2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/keypad.h	Mon Jan 30 09:41:47 2012 +0000
@@ -0,0 +1,109 @@
+/* mbed Keypad library, using user-defined interrupt callback
+ * Copyright (c) 2012 Yoong Hor Meng
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE
+ */
+
+#ifndef KEYPAD_H
+#define KEYPAD_H
+
+#include "mbed.h"
+#include "FPointer.h"
+
+/**
+ * An interrupt-based interface to 4x4 keypad. 
+ *
+ * On each key pressed on a keypad, the index of the key is passed to a
+ * user-defined function. User is free to define what to be done with the
+ * input.
+ *
+ *
+ * @code
+ *
+* #include "mbed.h"
+* #include "keypad.h"
+* 
+* // Define your own keypad values
+* char Keytable[] = { '1', '2', '3', 'A',
+*                     '4', '5', '6', 'B',
+*                     '7', '8', '9', 'C',
+*                     '*', '0', '#', 'D'
+*                   };
+* 
+* uint32_t cbAfterInput(uint32_t index) {
+*     printf("Index:%d => Key:%c\n", key, Keytable[index]);
+*     return 0;
+* }
+* 
+* int main() {
+*     Keypad keypad(p25, p26, p27, p28, p21, p22, p23, p24);
+*     keypad.CallAfterInput(&cbAfterInput);
+*     keypad.Start();
+* 
+*     while (1) {
+*         wait_ms(100);
+*     }
+* }
+* @endcode
+*/
+class Keypad {
+public:
+    /** Create a Keypad interface
+     *
+     *  @param row<3..0>     Row data lines
+     *  @param col<3..0>     Column data lines
+     *  @param debounce_ms   Debounce in ms (Default to 20ms)
+     */
+    Keypad(PinName row3, PinName row2, PinName row1, PinName row0,
+           PinName col3, PinName col2, PinName col1, PinName col0,
+           int debounce_ms = 20);
+
+    /** Start the keypad interrupt routines
+     */
+    void Start(void);
+
+    /** Stop the keypad interrupt routines
+     */
+    void Stop(void);
+
+    /** User-defined function that to be called when a key is pressed
+     *  @param fptr           A function pointer takes a uint32_t and
+     *                        returns uint32_t
+     */
+    void CallAfterInput(uint32_t (*fptr)(uint32_t));
+
+protected:
+    InterruptIn      _row0;
+    InterruptIn      _row1;
+    InterruptIn      _row2;
+    InterruptIn      _row3;
+    BusOut           _cols;
+    int              _debounce;
+    FPointer         _input; // Called after each input
+
+    void _callback(int row, InterruptIn &therow);
+    void _cbRow0Rise(void);
+    void _cbRow1Rise(void);
+    void _cbRow2Rise(void);
+    void _cbRow3Rise(void);
+    void _setupRiseTrigger(void);
+    void _dummy(void) { };
+};
+
+#endif // KEYPAD_H
\ No newline at end of file