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

Committer:
AjK
Date:
Fri Mar 04 12:15:38 2011 +0000
Revision:
1:f043501c4bed
1.0 See ChangeLog.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 1:f043501c4bed 1 /*
AjK 1:f043501c4bed 2 Copyright (c) 2011 Andy Kirkham
AjK 1:f043501c4bed 3
AjK 1:f043501c4bed 4 Permission is hereby granted, free of charge, to any person obtaining a copy
AjK 1:f043501c4bed 5 of this software and associated documentation files (the "Software"), to deal
AjK 1:f043501c4bed 6 in the Software without restriction, including without limitation the rights
AjK 1:f043501c4bed 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
AjK 1:f043501c4bed 8 copies of the Software, and to permit persons to whom the Software is
AjK 1:f043501c4bed 9 furnished to do so, subject to the following conditions:
AjK 1:f043501c4bed 10
AjK 1:f043501c4bed 11 The above copyright notice and this permission notice shall be included in
AjK 1:f043501c4bed 12 all copies or substantial portions of the Software.
AjK 1:f043501c4bed 13
AjK 1:f043501c4bed 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
AjK 1:f043501c4bed 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
AjK 1:f043501c4bed 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AjK 1:f043501c4bed 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
AjK 1:f043501c4bed 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AjK 1:f043501c4bed 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
AjK 1:f043501c4bed 20 THE SOFTWARE.
AjK 1:f043501c4bed 21
AjK 1:f043501c4bed 22 @file example2.h
AjK 1:f043501c4bed 23 @purpose Simple round-robin cooperative scheduler example
AjK 1:f043501c4bed 24 @date Mar 2011
AjK 1:f043501c4bed 25 @author Andy Kirkham
AjK 1:f043501c4bed 26 */
AjK 1:f043501c4bed 27
AjK 1:f043501c4bed 28 /* This example shows how to call tasks defined as C++ class methods. */
AjK 1:f043501c4bed 29
AjK 1:f043501c4bed 30 #include "mbed.h"
AjK 1:f043501c4bed 31 #include "SimpleScheduler.h"
AjK 1:f043501c4bed 32
AjK 1:f043501c4bed 33 // Task class example.
AjK 1:f043501c4bed 34 class Foo {
AjK 1:f043501c4bed 35 protected:
AjK 1:f043501c4bed 36 DigitalOut *_do;
AjK 1:f043501c4bed 37 public:
AjK 1:f043501c4bed 38 Foo(DigitalOut *d) { _do = d; }
AjK 1:f043501c4bed 39 void func(SimpleTask *task) { *_do = !*_do; }
AjK 1:f043501c4bed 40 };
AjK 1:f043501c4bed 41
AjK 1:f043501c4bed 42 DigitalOut led1(LED1);
AjK 1:f043501c4bed 43 DigitalOut led2(LED2);
AjK 1:f043501c4bed 44 DigitalOut led3(LED3);
AjK 1:f043501c4bed 45 DigitalOut led4(LED4);
AjK 1:f043501c4bed 46
AjK 1:f043501c4bed 47 Foo foo1(&led1);
AjK 1:f043501c4bed 48 Foo foo2(&led2);
AjK 1:f043501c4bed 49 Foo foo3(&led3);
AjK 1:f043501c4bed 50 Foo foo4(&led4);
AjK 1:f043501c4bed 51
AjK 1:f043501c4bed 52 SimpleScheduler *scheduler;
AjK 1:f043501c4bed 53
AjK 1:f043501c4bed 54 int main() {
AjK 1:f043501c4bed 55
AjK 1:f043501c4bed 56 scheduler = new SimpleScheduler;
AjK 1:f043501c4bed 57
AjK 1:f043501c4bed 58 scheduler
AjK 1:f043501c4bed 59 ->addTask( new SimpleTask(0, &foo1, &Foo::func) )
AjK 1:f043501c4bed 60 ->addTask( new SimpleTask(200, &foo2, &Foo::func) )
AjK 1:f043501c4bed 61 ->addTask( new SimpleTask(300, &foo3, &Foo::func) )
AjK 1:f043501c4bed 62 ->addTask( new SimpleTask(0.4, &foo4, &Foo::func) )
AjK 1:f043501c4bed 63 ;
AjK 1:f043501c4bed 64
AjK 1:f043501c4bed 65 scheduler->run();
AjK 1:f043501c4bed 66 }