OpenMoCo / QEIx4

Dependents:   QEIx4_Example realtimeMM_V3 realtimeMM_V3

Committer:
jocis
Date:
Wed Oct 01 10:23:21 2014 +0000
Revision:
2:c0b87b11b9cd
Parent:
1:ac6b7b1bf6c5
replaced virtual functions by attached functions; added documentation

Who changed what in which revision?

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