FPointer - A callback system that allows for 32bit unsigned ints to be passed to and from the callback.
Dependents: FYPFinalProgram FYPFinalizeProgram KEYS SaveKeypad ... more
Revision 0:fcfb13f40846, committed 2011-01-18
- Comitter:
- AjK
- Date:
- Tue Jan 18 17:31:19 2011 +0000
- Child:
- 1:d7803001a259
- Commit message:
- 1.0 Initial release
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ChangeLog.h Tue Jan 18 17:31:19 2011 +0000 @@ -0,0 +1,26 @@ +/* + Copyright (c) 2010 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + + + 1.0 18/Jan/2011 + Initial release + +*/ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/FPointer.h Tue Jan 18 17:31:19 2011 +0000 @@ -0,0 +1,137 @@ +/* + Copyright (c) 2011 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef AJK_FPOINTER_H +#define AJK_FPOINTER_H + +namespace AjK { + +class FPointerDummy; + +/** FPointer - Adds callbacks that take and return a 32bit uint32_t data type. + * + * The Mbed library supplies a callback using the FunctionPointer object as + * defined in FunctionPointer.h However, this callback system does not allow + * the caller to pass a value to the callback. Likewise, the callback itself + * cannot return a value. + * + * FPointer operates in the same way but allows the callback function to be + * passed one arg, a uint32_t value. Additionally, the callback can return + * a single uint32_t value. The reason for using uint32_t is that the Mbed + * and the microcontroller (LPC1768) have a natural data size of 32bits and + * this means we can use the uint32_t as a pointer. See example1.h for more + * information. This example passes an "int" by passing a pointer to that + * int as a 32bit value. Using this technique you can pass any value you like. + * All you have to do is pass a pointer to your value cast to (uint32_t). Your + * callback can the deference it to get the original value. + * + * example2.h shows how to do the same thing but demostrates how to specify + * the callback into a class object/method. + * + * Finally, example3.h shows how to pass multiple values. In this example we + * define a data structure and in the callback we pass a pointer to that + * data structure thus allowing the callback to again get the values. + * + * Note, when passing pointers to variables to the callback, if the callback + * function/method changes that variables value then it will also change the + * value the caller sees. If C pointers are new to you, you are strongly + * advised to read up on the subject. It's pointers that often get beginners + * into trouble when mis-used. + * + * @see example1.h + * @see example2.h + * @see example3.h + * @see http://mbed.org/handbook/C-Data-Types + * @see http://mbed.org/projects/libraries/svn/mbed/trunk/FunctionPointer.h + */ +class FPointer { + +protected: + + // C callback function pointer for to pass read data to. + uint32_t (*c_callback)(uint32_t); + + // C++ callback object/method pointer for to pass read data to. + FPointerDummy *obj_callback; + uint32_t (FPointerDummy::*method_callback)(uint32_t); + +public: + + /** Constructor + */ + FPointer() { + c_callback = NULL; + obj_callback = NULL; + method_callback = NULL; + } + + /** attach - Overloaded attachment function. + * + * Attach a C type function pointer as the callback. + * + * Note, the callback function prototype must be:- + * @code + * uint32_t myCallbackFunction(uint32_t); + * @endcode + * @param A C function pointer to call. + */ + void attach(uint32_t (*function)(uint32_t) = 0) { c_callback = function; } + + /** attach - Overloaded attachment function. + * + * Attach a C++ type object/method pointer as the callback. + * + * Note, the callback method prototype must be:- + * @code + * public: + * uint32_t myCallbackFunction(uint32_t); + * @endcode + * @param A C++ object pointer. + * @param A C++ method within the object to call. + */ + template<class T> + void attach(T* item, uint32_t (T::*method)(uint32_t)) { + obj_callback = (FPointerDummy *)item; + method_callback = (uint32_t (FPointerDummy::*)(uint32_t))method; + } + + /** call + * + * call the callback function. + * @param uint32_t The value to pass to the callback. + * @return uint32_t The value the callback returns. + */ + uint32_t call(uint32_t arg) { + if (c_callback != NULL) return (*c_callback)(arg); + else { + if (obj_callback != NULL && method_callback != NULL) + return (obj_callback->*method_callback)(arg); + } + return 0; + } +}; + +}; // namespace AjK ends + +using namespace AjK; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example1.h Tue Jan 18 17:31:19 2011 +0000 @@ -0,0 +1,71 @@ +/* + Copyright (c) 2010 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifdef AJK_COMPILE_EXAMPLE1 + +#include "mbed.h" +#include "FPointer.h" + +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); + +uint32_t myCallback(uint32_t value) { + // Get the value of the count in main + // (deference it) so we know what it is. + int i = *((int *)value); + + // Then display the bottom four bits of + // the count value on the LEDs. + led4 = (i & 1) ? 1 : 0; + led3 = (i & 2) ? 1 : 0; + led2 = (i & 4) ? 1 : 0; + led1 = (i & 8) ? 1 : 0; + + // What we return doesn't matter as it's + // not used in this example but we return + // "something" (zero in this case) to keep + // teh compiler happy as it expects us to + // return something. + return 0; +} + +int main() { + FPointer myPointer; + int count = 0; + + // Attach a C function pointer as the callback. + myPointer.attach(&myCallback); + + while(1) { + wait(0.5); + + // Make the callback passing a pointer + // to the int count variable. + myPointer.call((uint32_t)&count); + + count++; + } +} + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example2.h Tue Jan 18 17:31:19 2011 +0000 @@ -0,0 +1,74 @@ +/* + Copyright (c) 2010 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifdef AJK_COMPILE_EXAMPLE2 + +#include "mbed.h" +#include "FPointer.h" + +class FOO { +protected: + DigitalOut *led1; + DigitalOut *led2; + DigitalOut *led3; + DigitalOut *led4; + +public: + FOO() { + led1 = new DigitalOut(LED1); + led2 = new DigitalOut(LED2); + led3 = new DigitalOut(LED3); + led4 = new DigitalOut(LED4); + } + + uint32_t myCallback(uint32_t value) { + int i = *((int *)value); + led4->write( (i & 1) ? 1 : 0 ); + led3->write( (i & 2) ? 1 : 0 ); + led2->write( (i & 4) ? 1 : 0 ); + led1->write( (i & 8) ? 1 : 0 ); + return 0; + } +}; + +// Create an instance of FOO called foo. +FOO foo; + +int main() { + FPointer myPointer; + int count = 0; + + // Attach a C++ object/method pointer as the callback. + myPointer.attach(&foo, &FOO::myCallback); + + while(1) { + wait(0.5); + + // Make the callback passing a pointer + // to the int count variable. + myPointer.call((uint32_t)&count); + + count++; + } +} + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example3.h Tue Jan 18 17:31:19 2011 +0000 @@ -0,0 +1,92 @@ +/* + Copyright (c) 2010 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifdef AJK_COMPILE_EXAMPLE3 + +#include "mbed.h" +#include "FPointer.h" + +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); + +// Create a data structure that matches what you need. +// In this example the structure just holds an int and +// a float. Your own data structure can be constructed +// however you need. +typedef struct { + int i_value; + float f_value; +} MYDATA; + +// The call back function. +uint32_t myCallback(uint32_t value) { + + // Get the value of the count in main + // (deference it) so we know what it is. + // Note, Mbed/LPC17xx pointers are 32bit + // values. So we can "cast" a value back + // into a pointer. + MYDATA *q = (MYDATA *)value; + + // Then display the bottom four bits of + // the i_value on the LEDs. + led4 = (q->i_value & 1) ? 1 : 0; + led3 = (q->i_value & 2) ? 1 : 0; + led2 = (q->i_value & 4) ? 1 : 0; + led1 = (q->i_value & 8) ? 1 : 0; + + // Note, f_value isn't used but we could access + // it's value with q->f_value if we needed to. + + // What we return doesn't matter as it's + // not used in this example but we return + // "something" (zero in this case) to keep + // teh compiler happy as it expects us to + // return something. + return 0; +} + +int main() { + FPointer myPointer; + MYDATA data; + + data.i_value = 0; + data.f_value = 0.0; + + // Attach a C function pointer as the callback. + myPointer.attach(&myCallback); + + while(1) { + wait(0.5); + + // Make the callback passing a pointer + // to the data structure "data". + myPointer.call((uint32_t)&data); + + // Increment the i_value by one. + data.i_value++; + } +} + +#endif