An interrupt-driven interface to 4x4 keypad.

Dependents:   FYPFinalProgram FYPFinalizeProgram KEYS Proyect_Patric_electronic_door_MSC_Ok_ESP ... more

Committer:
yoonghm
Date:
Wed Jan 01 17:45:53 2014 +0000
Revision:
10:da060f8c03e8
Update Keypad library to be used with RTOS.; Support any combination of keypad size below 4x4.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yoonghm 10:da060f8c03e8 1 /*
yoonghm 10:da060f8c03e8 2 Copyright (c) 2011 Andy Kirkham
yoonghm 10:da060f8c03e8 3
yoonghm 10:da060f8c03e8 4 Permission is hereby granted, free of charge, to any person obtaining a copy
yoonghm 10:da060f8c03e8 5 of this software and associated documentation files (the "Software"), to deal
yoonghm 10:da060f8c03e8 6 in the Software without restriction, including without limitation the rights
yoonghm 10:da060f8c03e8 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
yoonghm 10:da060f8c03e8 8 copies of the Software, and to permit persons to whom the Software is
yoonghm 10:da060f8c03e8 9 furnished to do so, subject to the following conditions:
yoonghm 10:da060f8c03e8 10
yoonghm 10:da060f8c03e8 11 The above copyright notice and this permission notice shall be included in
yoonghm 10:da060f8c03e8 12 all copies or substantial portions of the Software.
yoonghm 10:da060f8c03e8 13
yoonghm 10:da060f8c03e8 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
yoonghm 10:da060f8c03e8 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
yoonghm 10:da060f8c03e8 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
yoonghm 10:da060f8c03e8 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
yoonghm 10:da060f8c03e8 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
yoonghm 10:da060f8c03e8 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
yoonghm 10:da060f8c03e8 20 THE SOFTWARE.
yoonghm 10:da060f8c03e8 21 */
yoonghm 10:da060f8c03e8 22
yoonghm 10:da060f8c03e8 23 #ifndef AJK_FPOINTER_H
yoonghm 10:da060f8c03e8 24 #define AJK_FPOINTER_H
yoonghm 10:da060f8c03e8 25
yoonghm 10:da060f8c03e8 26 namespace AjK {
yoonghm 10:da060f8c03e8 27
yoonghm 10:da060f8c03e8 28 class FPointerDummy;
yoonghm 10:da060f8c03e8 29
yoonghm 10:da060f8c03e8 30 /** FPointer - Adds callbacks that take and return a 32bit uint32_t data type.
yoonghm 10:da060f8c03e8 31 *
yoonghm 10:da060f8c03e8 32 * The Mbed library supplies a callback using the FunctionPointer object as
yoonghm 10:da060f8c03e8 33 * defined in FunctionPointer.h However, this callback system does not allow
yoonghm 10:da060f8c03e8 34 * the caller to pass a value to the callback. Likewise, the callback itself
yoonghm 10:da060f8c03e8 35 * cannot return a value.
yoonghm 10:da060f8c03e8 36 *
yoonghm 10:da060f8c03e8 37 * FPointer operates in the same way but allows the callback function to be
yoonghm 10:da060f8c03e8 38 * passed one arg, a uint32_t value. Additionally, the callback can return
yoonghm 10:da060f8c03e8 39 * a single uint32_t value. The reason for using uint32_t is that the Mbed
yoonghm 10:da060f8c03e8 40 * and the microcontroller (LPC1768) have a natural data size of 32bits and
yoonghm 10:da060f8c03e8 41 * this means we can use the uint32_t as a pointer. See example1.h for more
yoonghm 10:da060f8c03e8 42 * information. This example passes an "int" by passing a pointer to that
yoonghm 10:da060f8c03e8 43 * int as a 32bit value. Using this technique you can pass any value you like.
yoonghm 10:da060f8c03e8 44 * All you have to do is pass a pointer to your value cast to (uint32_t). Your
yoonghm 10:da060f8c03e8 45 * callback can the deference it to get the original value.
yoonghm 10:da060f8c03e8 46 *
yoonghm 10:da060f8c03e8 47 * example2.h shows how to do the same thing but demostrates how to specify
yoonghm 10:da060f8c03e8 48 * the callback into a class object/method.
yoonghm 10:da060f8c03e8 49 *
yoonghm 10:da060f8c03e8 50 * Finally, example3.h shows how to pass multiple values. In this example we
yoonghm 10:da060f8c03e8 51 * define a data structure and in the callback we pass a pointer to that
yoonghm 10:da060f8c03e8 52 * data structure thus allowing the callback to again get the values.
yoonghm 10:da060f8c03e8 53 *
yoonghm 10:da060f8c03e8 54 * Note, when passing pointers to variables to the callback, if the callback
yoonghm 10:da060f8c03e8 55 * function/method changes that variable's value then it will also change the
yoonghm 10:da060f8c03e8 56 * value the caller sees. If C pointers are new to you, you are strongly
yoonghm 10:da060f8c03e8 57 * advised to read up on the subject. It's pointers that often get beginners
yoonghm 10:da060f8c03e8 58 * into trouble when mis-used.
yoonghm 10:da060f8c03e8 59 *
yoonghm 10:da060f8c03e8 60 * @see example1.h
yoonghm 10:da060f8c03e8 61 * @see example2.h
yoonghm 10:da060f8c03e8 62 * @see example3.h
yoonghm 10:da060f8c03e8 63 * @see http://mbed.org/handbook/C-Data-Types
yoonghm 10:da060f8c03e8 64 * @see http://mbed.org/projects/libraries/svn/mbed/trunk/FunctionPointer.h
yoonghm 10:da060f8c03e8 65 */
yoonghm 10:da060f8c03e8 66 class FPointer {
yoonghm 10:da060f8c03e8 67
yoonghm 10:da060f8c03e8 68 protected:
yoonghm 10:da060f8c03e8 69
yoonghm 10:da060f8c03e8 70 //! C callback function pointer.
yoonghm 10:da060f8c03e8 71 uint32_t (*c_callback)(uint32_t);
yoonghm 10:da060f8c03e8 72
yoonghm 10:da060f8c03e8 73 //! C++ callback object/method pointer (the object part).
yoonghm 10:da060f8c03e8 74 FPointerDummy *obj_callback;
yoonghm 10:da060f8c03e8 75
yoonghm 10:da060f8c03e8 76 //! C++ callback object/method pointer (the method part).
yoonghm 10:da060f8c03e8 77 uint32_t (FPointerDummy::*method_callback)(uint32_t);
yoonghm 10:da060f8c03e8 78
yoonghm 10:da060f8c03e8 79 public:
yoonghm 10:da060f8c03e8 80
yoonghm 10:da060f8c03e8 81 /** Constructor
yoonghm 10:da060f8c03e8 82 */
yoonghm 10:da060f8c03e8 83 FPointer() {
yoonghm 10:da060f8c03e8 84 c_callback = NULL;
yoonghm 10:da060f8c03e8 85 obj_callback = NULL;
yoonghm 10:da060f8c03e8 86 method_callback = NULL;
yoonghm 10:da060f8c03e8 87 }
yoonghm 10:da060f8c03e8 88
yoonghm 10:da060f8c03e8 89 /** attach - Overloaded attachment function.
yoonghm 10:da060f8c03e8 90 *
yoonghm 10:da060f8c03e8 91 * Attach a C type function pointer as the callback.
yoonghm 10:da060f8c03e8 92 *
yoonghm 10:da060f8c03e8 93 * Note, the callback function prototype must be:-
yoonghm 10:da060f8c03e8 94 * @code
yoonghm 10:da060f8c03e8 95 * uint32_t myCallbackFunction(uint32_t);
yoonghm 10:da060f8c03e8 96 * @endcode
yoonghm 10:da060f8c03e8 97 * @param A C function pointer to call.
yoonghm 10:da060f8c03e8 98 */
yoonghm 10:da060f8c03e8 99 void attach(uint32_t (*function)(uint32_t) = 0) { c_callback = function; }
yoonghm 10:da060f8c03e8 100
yoonghm 10:da060f8c03e8 101 /** attach - Overloaded attachment function.
yoonghm 10:da060f8c03e8 102 *
yoonghm 10:da060f8c03e8 103 * Attach a C++ type object/method pointer as the callback.
yoonghm 10:da060f8c03e8 104 *
yoonghm 10:da060f8c03e8 105 * Note, the callback method prototype must be:-
yoonghm 10:da060f8c03e8 106 * @code
yoonghm 10:da060f8c03e8 107 * public:
yoonghm 10:da060f8c03e8 108 * uint32_t myCallbackFunction(uint32_t);
yoonghm 10:da060f8c03e8 109 * @endcode
yoonghm 10:da060f8c03e8 110 * @param A C++ object pointer.
yoonghm 10:da060f8c03e8 111 * @param A C++ method within the object to call.
yoonghm 10:da060f8c03e8 112 */
yoonghm 10:da060f8c03e8 113 template<class T>
yoonghm 10:da060f8c03e8 114 void attach(T* item, uint32_t (T::*method)(uint32_t)) {
yoonghm 10:da060f8c03e8 115 obj_callback = (FPointerDummy *)item;
yoonghm 10:da060f8c03e8 116 method_callback = (uint32_t (FPointerDummy::*)(uint32_t))method;
yoonghm 10:da060f8c03e8 117 }
yoonghm 10:da060f8c03e8 118
yoonghm 10:da060f8c03e8 119 /** call - Overloaded callback initiator.
yoonghm 10:da060f8c03e8 120 *
yoonghm 10:da060f8c03e8 121 * call the callback function.
yoonghm 10:da060f8c03e8 122 *
yoonghm 10:da060f8c03e8 123 * @param uint32_t The value to pass to the callback.
yoonghm 10:da060f8c03e8 124 * @return uint32_t The value the callback returns.
yoonghm 10:da060f8c03e8 125 */
yoonghm 10:da060f8c03e8 126 uint32_t call(uint32_t arg) {
yoonghm 10:da060f8c03e8 127 if (c_callback != NULL) {
yoonghm 10:da060f8c03e8 128 return (*c_callback)(arg);
yoonghm 10:da060f8c03e8 129 }
yoonghm 10:da060f8c03e8 130 else {
yoonghm 10:da060f8c03e8 131 if (obj_callback != NULL && method_callback != NULL) {
yoonghm 10:da060f8c03e8 132 return (obj_callback->*method_callback)(arg);
yoonghm 10:da060f8c03e8 133 }
yoonghm 10:da060f8c03e8 134 }
yoonghm 10:da060f8c03e8 135 return (uint32_t)NULL;
yoonghm 10:da060f8c03e8 136 }
yoonghm 10:da060f8c03e8 137
yoonghm 10:da060f8c03e8 138 /** call - Overloaded callback initiator.
yoonghm 10:da060f8c03e8 139 *
yoonghm 10:da060f8c03e8 140 * Call the callback function without passing an argument.
yoonghm 10:da060f8c03e8 141 * The callback itself is passed NULL. Note, the callback
yoonghm 10:da060f8c03e8 142 * prototype should still be <b>uint32_t callback(uint32_t)</b>.
yoonghm 10:da060f8c03e8 143 *
yoonghm 10:da060f8c03e8 144 * @return uint32_t The value the callback returns.
yoonghm 10:da060f8c03e8 145 */
yoonghm 10:da060f8c03e8 146 uint32_t call(void) {
yoonghm 10:da060f8c03e8 147 if (c_callback != NULL) {
yoonghm 10:da060f8c03e8 148 return (*c_callback)((uint32_t)NULL);
yoonghm 10:da060f8c03e8 149 }
yoonghm 10:da060f8c03e8 150 else {
yoonghm 10:da060f8c03e8 151 if (obj_callback != NULL && method_callback != NULL) {
yoonghm 10:da060f8c03e8 152 return (obj_callback->*method_callback)((uint32_t)NULL);
yoonghm 10:da060f8c03e8 153 }
yoonghm 10:da060f8c03e8 154 }
yoonghm 10:da060f8c03e8 155 return (uint32_t)NULL;
yoonghm 10:da060f8c03e8 156 }
yoonghm 10:da060f8c03e8 157 };
yoonghm 10:da060f8c03e8 158
yoonghm 10:da060f8c03e8 159 }; // namespace AjK ends
yoonghm 10:da060f8c03e8 160
yoonghm 10:da060f8c03e8 161 using namespace AjK;
yoonghm 10:da060f8c03e8 162
yoonghm 10:da060f8c03e8 163 #endif