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

Dependents:   Garage_Control

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers example3.h Source File

example3.h

00001 /*
00002     Copyright (c) 2011 Andy Kirkham
00003  
00004     Permission is hereby granted, free of charge, to any person obtaining a copy
00005     of this software and associated documentation files (the "Software"), to deal
00006     in the Software without restriction, including without limitation the rights
00007     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008     copies of the Software, and to permit persons to whom the Software is
00009     furnished to do so, subject to the following conditions:
00010  
00011     The above copyright notice and this permission notice shall be included in
00012     all copies or substantial portions of the Software.
00013  
00014     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020     THE SOFTWARE.
00021     
00022     @file          example3.h 
00023     @purpose       Simple round-robin cooperative scheduler example
00024     @date          Mar 2011
00025     @author        Andy Kirkham
00026 */
00027 
00028 /*
00029     SimpleScheduler is a cooperative scheduler. What that means is YOU, the
00030     programmer MUST cooperate with it. SimpleScheduler will call your task
00031     functions based on the schedule you provide. However, it's not a guarenteed
00032     scheduled time. It's on a "best effort". However, no tasks are called until
00033     the currently executing task completes. What that means is your task function
00034     must return before another task can be called.
00035     
00036     Your tasks therefore should execute as fast as possible and return as soon as
00037     they can. But when Mbed's wait API is used you stall your entire system. This
00038     example shows how you can wait() in a task without actually waiting at all. In
00039     order to do this we use the "Ton" (Timer ON) library. You will need to import
00040     this library into your project to use this example:-
00041     
00042     http://mbed.org/users/AjK/libraries/Ton/latest
00043     
00044     What follows is basically example1. However, we are going to flash LED1 by 
00045     "waiting" 1second in the task. 
00046     
00047 */
00048 
00049 #include "mbed.h"
00050 #include "SimpleScheduler.h"
00051 #include "Ton.h"
00052 
00053 Ton t1(1000); // Timer ON delay of one second.
00054 
00055 DigitalOut led1(LED1);
00056 DigitalOut led2(LED2);
00057 DigitalOut led3(LED3);
00058 DigitalOut led4(LED4);
00059 
00060 void f1(SimpleTask *task) {
00061     // wait(1);     
00062     if (!t1.isRunning()) {
00063         t1 = Ton::On;
00064         // Code that should execute once before the wait() goes here.
00065         // ...
00066     }        
00067     if (t1) {
00068         // This code will be executed once the timer times out.
00069         led1 = !led1; 
00070         t1.reset();
00071     }
00072 }
00073 void f2(SimpleTask *task) { led2 = !led2; }
00074 void f3(SimpleTask *task) { led3 = !led3; }
00075 void f4(SimpleTask *task) { led4 = !led4; }
00076 
00077 SimpleScheduler *scheduler;
00078 
00079 int main() {
00080 
00081     scheduler = new SimpleScheduler;
00082     
00083     scheduler
00084         ->addTask( new SimpleTask(0,   f1) )
00085         ->addTask( new SimpleTask(200, f2) )
00086         ->addTask( new SimpleTask(300, f3) )
00087         ->addTask( new SimpleTask(0.4, f4) )
00088     ;
00089     
00090     scheduler->run();    
00091 }
00092