smoothie port to mbed online compiler (smoothieware.org)

Dependencies:   mbed

For documentation, license, ..., please check http://smoothieware.org/

This version has been tested with a 3 axis machine

Committer:
scachat
Date:
Tue Jul 31 21:11:18 2012 +0000
Revision:
0:31e91bb0ef3c
smoothie port to mbed online compiler

Who changed what in which revision?

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