AirsoftTimer software based on mbed

Dependencies:   mbed TextLCD keypad

Committer:
sillevl
Date:
Thu Feb 09 12:28:56 2017 +0000
Revision:
27:f29805113454
Parent:
18:abcebc4d0da0
ARCHIVE WIP, not working (crashes, cause unknown)

Who changed what in which revision?

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