Simple example of a very basic cooperative round robin task switching.

Dependencies:   mbed

A very basic demo of how to run a set of tasks in sequence.

Files at this revision

API Documentation at this revision

Comitter:
AndyA
Date:
Thu Mar 19 17:08:55 2015 +0000
Commit message:
First commit.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 301a83d3eeb4 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Mar 19 17:08:55 2015 +0000
@@ -0,0 +1,158 @@
+#include "mbed.h"
+
+Serial pc(USBTX,USBRX);
+
+int taskCount = 0;
+const int maxTasks = 16;
+void *taskList[maxTasks];
+
+typedef void function_t(void);
+
+//runs the next task. Avoid calling from a ticker since that would block all interrupts while running.
+void nextTask(void)
+{
+    static int taskID = 0;
+    if (taskCount) {
+        if (taskID >= taskCount) // a task could have been removed.
+            taskID = 0;
+
+        ((function_t*)taskList[taskID])();
+        taskID++;
+        if (taskID == taskCount)
+            taskID = 0;
+    }
+}
+
+// add a task to the list
+bool addTask(void (*function)(void))
+{
+    if ((taskCount < maxTasks) && function) {
+        taskList[taskCount] = (void *)function;
+        taskCount++;
+        return true;
+    } else
+        return false;
+}
+
+// finds a task in the list or -1 if not in the list. Mainly used for removing tasks.
+int findTask(void (*function)(void))
+{
+    if (!function)
+        return -1;
+    int number = 0;
+    while (number < taskCount) {
+        if (taskList[number] == (void *)function)
+            return number;
+        number++;
+    }
+    return -1;
+}
+
+// remove a task. returns false if the task wasn't in the list.
+bool removeTask(void (*function)(void))
+{
+    int taskToRemove = findTask(function);
+    if (taskToRemove == -1)
+        return false;
+
+    for (int i = taskToRemove; i<(taskCount-1); i++) {
+        taskList[i] = taskList[i+1];
+    }
+    taskCount--;
+    return true;
+}
+
+// simple example task.
+void task1(void)
+{
+    const int NumberOfStates = 5;
+    static int state = 0;
+    switch (state) {
+        case 0:
+            pc.putc('A');
+            break;
+        case 1:
+            pc.putc('B');
+            break;
+        case 2:
+            pc.putc('C');
+            break;
+        case 3:
+            pc.putc('D');
+            break;
+        case 4:
+            pc.putc('E');
+            break;
+        default:
+            state = 0;
+            break;
+    }
+    state++;
+    if (state == NumberOfStates)
+        state = 0;
+}
+
+// simple example task.
+void task2(void)
+{
+    const int NumberOfStates = 2;
+    static int state = 0;
+    switch (state) {
+        case 0:
+            pc.putc('1');
+            break;
+        case 1:
+            pc.putc('2');
+            break;
+        default:
+            state = 0;
+            break;
+    }
+    state++;
+    if (state == NumberOfStates)
+        state = 0;
+}
+
+// simple example task using enum for current state.
+void task3(void)
+{
+    static enum {firstState,secondState} state = firstState;
+    switch (state) {
+        case firstState:
+            pc.putc('X');
+            state = secondState;
+            break;
+        case secondState:
+            pc.putc('Y');
+            state = firstState;
+            break;
+        default:
+            state = firstState;
+            break;
+    }
+}
+
+main ()
+{
+
+    addTask(&task1);
+    addTask(&task2);
+    pc.printf("\r\nTasks 1 & 2\r\n");
+    for (int i = 0; i < 20; i++)
+        nextTask();
+
+
+    addTask(&task1);
+    addTask(&task3);
+    pc.printf("\r\nTasks 1,2,1,3\r\n");
+    for (int i = 0; i < 20; i++)
+        nextTask();
+
+    removeTask(&task1); // removes the first instance of task 1 in the list.
+    pc.printf("\r\nTasks 2,1,3\r\n");
+    for (int i = 0; i < 20; i++)
+        nextTask();
+
+    pc.printf("\r\nDone\r\n");
+
+}
\ No newline at end of file
diff -r 000000000000 -r 301a83d3eeb4 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Mar 19 17:08:55 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/487b796308b0
\ No newline at end of file