Very simple cooperative round-robin task scheduler. See examples.
Examples/example3.h@1:f043501c4bed, 2011-03-04 (annotated)
- Committer:
- AjK
- Date:
- Fri Mar 04 12:15:38 2011 +0000
- Revision:
- 1:f043501c4bed
- Child:
- 4:49652acb6806
1.0 See ChangeLog.h
Who changed what in which revision?
User | Revision | Line number | New 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 example3.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 | /* |
AjK | 1:f043501c4bed | 29 | SimpleScheduler is a cooperative scheduler. What that means is YOU, the |
AjK | 1:f043501c4bed | 30 | programmer MUST cooperate with it. SimpleScheduler will call your task |
AjK | 1:f043501c4bed | 31 | functions based on the schedule you provide. However, it's not a guarenteed |
AjK | 1:f043501c4bed | 32 | scheduled time. It's on a "best effort". However, no tasks are called until |
AjK | 1:f043501c4bed | 33 | the currently executing task completes. What that means is your task function |
AjK | 1:f043501c4bed | 34 | must return before another task can be called. |
AjK | 1:f043501c4bed | 35 | |
AjK | 1:f043501c4bed | 36 | Your tasks therefore should execute as fast as possible and return as soon as |
AjK | 1:f043501c4bed | 37 | they can. But when Mbed's wait API is used you stall your entire system. This |
AjK | 1:f043501c4bed | 38 | example shows how you can wait() in a task without actually waiting at all. In |
AjK | 1:f043501c4bed | 39 | order to do this we use the "Ton" (Timer ON) library. You will need to import |
AjK | 1:f043501c4bed | 40 | this library into your project to use this example:- |
AjK | 1:f043501c4bed | 41 | |
AjK | 1:f043501c4bed | 42 | http://mbed.org/users/AjK/libraries/Ton/latest |
AjK | 1:f043501c4bed | 43 | |
AjK | 1:f043501c4bed | 44 | What follows is basically example1. However, we are going to flash LED1 by |
AjK | 1:f043501c4bed | 45 | "waiting" 1second in the task. |
AjK | 1:f043501c4bed | 46 | |
AjK | 1:f043501c4bed | 47 | */ |
AjK | 1:f043501c4bed | 48 | |
AjK | 1:f043501c4bed | 49 | #include "mbed.h" |
AjK | 1:f043501c4bed | 50 | #include "SimpleScheduler.h" |
AjK | 1:f043501c4bed | 51 | #include "Ton.h" |
AjK | 1:f043501c4bed | 52 | |
AjK | 1:f043501c4bed | 53 | Ton t1(1000); // Timer ON delay of one second. |
AjK | 1:f043501c4bed | 54 | |
AjK | 1:f043501c4bed | 55 | DigitalOut led1(LED1); |
AjK | 1:f043501c4bed | 56 | DigitalOut led2(LED2); |
AjK | 1:f043501c4bed | 57 | DigitalOut led3(LED3); |
AjK | 1:f043501c4bed | 58 | DigitalOut led4(LED4); |
AjK | 1:f043501c4bed | 59 | |
AjK | 1:f043501c4bed | 60 | void f1(SimpleTask *task) { |
AjK | 1:f043501c4bed | 61 | // wait(1); |
AjK | 1:f043501c4bed | 62 | t1 = Ton::On; |
AjK | 1:f043501c4bed | 63 | if (t1) { |
AjK | 1:f043501c4bed | 64 | led1 = !led1; |
AjK | 1:f043501c4bed | 65 | t1.reset(); |
AjK | 1:f043501c4bed | 66 | } |
AjK | 1:f043501c4bed | 67 | } |
AjK | 1:f043501c4bed | 68 | void f2(SimpleTask *task) { led2 = !led2; } |
AjK | 1:f043501c4bed | 69 | void f3(SimpleTask *task) { led3 = !led3; } |
AjK | 1:f043501c4bed | 70 | void f4(SimpleTask *task) { led4 = !led4; } |
AjK | 1:f043501c4bed | 71 | |
AjK | 1:f043501c4bed | 72 | SimpleScheduler *scheduler; |
AjK | 1:f043501c4bed | 73 | |
AjK | 1:f043501c4bed | 74 | int main() { |
AjK | 1:f043501c4bed | 75 | |
AjK | 1:f043501c4bed | 76 | scheduler = new SimpleScheduler; |
AjK | 1:f043501c4bed | 77 | |
AjK | 1:f043501c4bed | 78 | scheduler |
AjK | 1:f043501c4bed | 79 | ->addTask( new SimpleTask(0, f1) ) |
AjK | 1:f043501c4bed | 80 | ->addTask( new SimpleTask(200, f2) ) |
AjK | 1:f043501c4bed | 81 | ->addTask( new SimpleTask(300, f3) ) |
AjK | 1:f043501c4bed | 82 | ->addTask( new SimpleTask(0.4, f4) ) |
AjK | 1:f043501c4bed | 83 | ; |
AjK | 1:f043501c4bed | 84 | |
AjK | 1:f043501c4bed | 85 | scheduler->run(); |
AjK | 1:f043501c4bed | 86 | } |
AjK | 1:f043501c4bed | 87 |