Very simple cooperative round-robin task scheduler. See examples.

Committer:
AjK
Date:
Fri Mar 04 12:40:14 2011 +0000
Revision:
2:974a420997a9
Parent:
0:564dd7a5d307
Child:
3:95ec5c83c2fe
1.1 See ChangeLog.h

Who changed what in which revision?

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