Stanislaus Eichstaedt / Mbed 2 deprecated Scheduler

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers scheduler.cpp Source File

scheduler.cpp

00001 /*! \file scheduler.c \brief Simple Task Scheduler. */
00002 //*****************************************************************************
00003 //
00004 // File Name    : 'scheduler.c'
00005 // Title        : Simple Task Scheduler
00006 // Author       : Pascal Stang - Copyright (C) 2006
00007 // Created      : 2006.05.24
00008 // Revised      : 2006.05.26
00009 // Version      : 0.1
00010 // Target MCU   : Atmel AVR Series
00011 // Editor Tabs  : 4
00012 //
00013 // NOTE: This code is currently below version 1.0, and therefore is considered
00014 // to be lacking in some functionality or documentation, or may not be fully
00015 // tested.  Nonetheless, you can expect most functions to work.
00016 //
00017 // This code is distributed under the GNU Public License
00018 //      which can be found at http://www.gnu.org/licenses/gpl.txt
00019 //
00020 //*****************************************************************************
00021  
00022 #include "scheduler.h"
00023 
00024 // the task list
00025 struct task TaskList[TASKLIST_SIZE];
00026  
00027 // functions
00028 void schedulerInit(void)
00029 {
00030      int i;
00031      
00032      // reset task list
00033      for(i=0; i<TASKLIST_SIZE; i++)
00034      {
00035          // initialize task list
00036          TaskList[i].runtime = 0;
00037          TaskList[i].funcptr = 0;
00038      }
00039  }
00040  
00041 void schedulerRun(int systime)
00042 {
00043      int i;
00044      
00045      // locate active task slots
00046      for(i=0; i<TASKLIST_SIZE; i++)
00047      {
00048          if(TaskList[i].funcptr)
00049          {
00050              // check systime against runtime
00051              if(systime >= TaskList[i].runtime)
00052              {
00053                  // run task, then update it
00054                  TaskList[i].funcptr(i,systime);
00055                  
00056                  if(TaskList[i].nrepeat)
00057                  {
00058                      TaskList[i].runtime += TaskList[i].interval;
00059                      TaskList[i].nrepeat--;
00060                      // task has expired, remove it now
00061                      if(!TaskList[i].nrepeat)
00062                          schedulerRemoveTask(i);
00063                  }
00064                  else
00065                  {
00066                      // nrepeat==0 means run forever
00067                      TaskList[i].runtime += TaskList[i].interval;
00068                  }
00069                  
00070              }
00071          }
00072      }
00073  
00074      // increment scheduler timer
00075      //SchedulerTimer++;
00076 }
00077  
00078 int schedulerAddTask(int runtime, int nrepeat, int interval, taskfuncptr taskfunc)
00079 {
00080     int i;
00081     
00082     // locate empty scheduler slot
00083     for(i=0; i<TASKLIST_SIZE; i++)
00084     {
00085         //CRITICAL_SECTION_BEGIN;
00086         if(!TaskList[i].funcptr)
00087         {
00088             // setup entry
00089             TaskList[i].funcptr = taskfunc;
00090             TaskList[i].runtime = runtime;
00091             TaskList[i].nrepeat = nrepeat;
00092             TaskList[i].interval = interval;
00093             
00094             // return task handle
00095             return i;
00096         }
00097         //CRITICAL_SECTION_END;
00098     }
00099     // error - no task slots left
00100     return -1;
00101 }
00102 
00103 int schedulerRemoveTask(int taskhandle)
00104 {
00105     // clear the task entry
00106     TaskList[taskhandle].runtime = 0;
00107     TaskList[taskhandle].interval = 0;
00108     TaskList[taskhandle].nrepeat = 0;
00109     TaskList[taskhandle].funcptr = 0;
00110     
00111     return taskhandle;
00112 }
00113  
00114 int schedulerRemoveTaskFunc(taskfuncptr funcptr)
00115 {
00116     int i;
00117     int taskhandle = -1;
00118     
00119     // locate the task
00120     for(i=0; i<TASKLIST_SIZE; i++)
00121     {
00122         // on match, disable the task
00123         if(TaskList[i].funcptr == funcptr) taskhandle = schedulerRemoveTask(i);
00124     }
00125     return taskhandle;
00126 }