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

Dependents:   Garage_Control

Revision:
0:564dd7a5d307
Child:
2:974a420997a9
diff -r 000000000000 -r 564dd7a5d307 STcallback.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/STcallback.h	Fri Mar 04 10:04:17 2011 +0000
@@ -0,0 +1,143 @@
+/*
+    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_STCALLBACK_H
+#define AJK_STCALLBACK_H
+
+namespace AjK {
+
+class SimpleTask;
+class STcallbackDummy;
+
+/** STcallback - Adds callbacks that take SimpleTask * pointer 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.
+ *
+ * Note, when passing pointers to variables to the callback, if the callback
+ * function/method changes that variable's 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 http://mbed.org/handbook/C-Data-Types
+ * @see http://mbed.org/projects/libraries/svn/mbed/trunk/FunctionPointer.h
+ * @see http://mbed.org/cookbook/FunctionPointer
+ * @see http://mbed.org/cookbook/FPointer
+ */
+class STcallback {
+
+protected:
+
+    //! C callback function pointer.
+    void (*c_callback)(SimpleTask *); 
+    
+    //! C++ callback object/method pointer (the object part).
+    STcallbackDummy *obj_callback;
+    
+    //! C++ callback object/method pointer (the method part).
+    void (STcallbackDummy::*method_callback)(SimpleTask *);
+
+public:
+    
+    /** Constructor
+     */
+    STcallback() {
+        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(void (*function)(SimpleTask *) = 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 = 0, void (T::*method)(SimpleTask *) = 0) { 
+        obj_callback    = (STcallbackDummy *)item; 
+        method_callback = (void (STcallbackDummy::*)(SimpleTask *))method; 
+    }
+
+    /** call - Overloaded callback initiator.
+     *
+     * call the callback function.
+     *
+     * @return uint32_t The value the callback returns.
+     */
+    void call(SimpleTask *arg) {
+        if (c_callback != 0) {
+            (*c_callback)(arg);
+        }
+        else {
+            if (obj_callback != 0 && method_callback != 0) {
+                (obj_callback->*method_callback)(arg);
+            }
+        }
+    }
+    
+    /** call - Overloaded callback initiator.
+     *
+     * Call the callback function without passing an argument.
+     * The callback itself is passed NULL. Note, the callback
+     * prototype should still be <b>uint32_t callback(uint32_t)</b>.
+     *
+     * @return uint32_t The value the callback returns.
+     */
+    void call(void) {
+        if (c_callback != NULL) {
+            (*c_callback)((SimpleTask *)NULL);
+        }
+        else {
+            if (obj_callback  != NULL && method_callback != NULL) {
+                (obj_callback->*method_callback)((SimpleTask *)NULL);
+            }
+        }
+    }    
+};
+
+}; // namespace AjK ends
+
+using namespace AjK;
+
+#endif