Samuel M.H. 's technological blog

Sunday, September 2, 2012

SMH_Scheduler

I've just uploaded to the repo a new library I coded this morning. It's called SMH_Scheduler and it allows the programmer to execute functions every x seconds.

It's not concurrency nor realtime because what it only does is checking it's time to call a function.

The way it should be used is the following.
  1. Create the scheduler object.
  2. Add the desired functions and the period between conecuent calls.
  3. Call the run method on your loop or whenever you'd like to call the function if it was time.

Features:
  • Call a function every x milliseconds.
  • Delay the first execution of a function x milliseconds.
  • It's possible to pass data to the function.
  • Easy to use.

Cons:
  •  I'm afraid it'll work properly only till the max value of the millis() function.
  • If a function never ends, the scheduler cannot recover the control.
  • Not realtime, a function is called at time or later.
  • If two functions should execute at the same time, takes priority the lastest added one.


Here I leave the example. It prints through the serial port at specific intervals:
1:  /*  
2:   * AUTHOR:  Samuel M.H. <samuel.mh@gmail.com>  
3:   * LAST REV: 2-Sep-2012  
4:   * DESCRIPTION:  
5:   *  Example for the SMH_Scheduler Arduino library.  
6:   *  It shows how it's possible to schedule functions  
7:   *  over the time.  
8:   *  
9:   *  The arduino calls functions at defined times  
10:   *  that print strings through the serial port.  
11:   *  
12:   * WARNING:  
13:   * - It does NOT provide REALTIME executions.  
14:   * - Executions are NEVER done BEFORE the time set (period).  
15:   * - If a function never returns, the other functions  
16:   *   will never be called.  
17:   *  
18:   * LICENSE: GPL V3 <http://www.gnu.org/licenses/gpl.html>  
19:  */  
20:  #include <SMH_Scheduler.h>  
21:  //Construct the object  
22:  SMH_Scheduler sched;  
23:  //Print Hello  
24:  void hello(void *data){  
25:   Serial.println("Hello");  
26:  }  
27:  //Print World  
28:  void world(void *data){  
29:   Serial.println("World\n");  
30:  }  
31:  //Print a String taken from data  
32:  void print(void *data){  
33:   Serial.println((char*)data);  
34:  }  
35:  void setup(){  
36:   Serial.begin(9600);  
37:   /*  
38:   * Here we configure our task list.  
39:   * WARNING: take into consideration the tasks are executed in reversed order,  
40:   * so if there are 2 tasks that should be executed at the same time, the last one  
41:   * you added will be executed first.  
42:   */  
43:   //Print Hello every second  
44:   sched.addTask(&hello, 1000);  
45:   //Print World every second but delay the start 0,5 seconds  
46:   sched.addTask(&world, 1000, 500, NULL);  
47:   //Print Bazinga! every 2 seconds by calling a function with a parameter.  
48:   sched.addTask(&print, 2000, 0, (void*)"Bazinga!\n");  
49:  }  
50:  void loop(){  
51:   //Call to the scheduler  
52:   sched.run();  
53:  }  

No comments:

Post a Comment

Copyright © Samuel M.H. All rights reserved. Powered by Blogger.