Depot Pertamina / keypad
Committer:
irsanjul
Date:
Fri Jul 05 10:33:24 2019 +0000
Revision:
0:372ef8c791a1
hhhh

Who changed what in which revision?

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