CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

pTask - priority (out of order) task manager (edit)

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
mbradley



Joined: 11 Jul 2009
Posts: 118
Location: California, USA

View user's profile Send private message Visit poster's website

pTask - priority (out of order) task manager (edit)
PostPosted: Tue Jan 12, 2010 12:25 am     Reply with quote

Hi everyone,

I wrote this to solve a few issues, and have been using it for about a month now, and all seems to work well. I also wrapped this up all in macros so it is nice and small and clean.

What it does, is allow you to group several tasks together, and have each one run more often than others.

Lets say you have a task that checks a button, well, you might not want to run that task every ms, so it can run with a larger number, thus run less often. What about a task that checks the size of a serial buffer, you might want to run that every iteration, so a number of 1.

Ok, its easier than it sounds, take a look at the code below

EDIT: forgot to show while forever loop, its now added

Code:

/*
          file: ptask.h
   description: priority (out of order) task manager
   written by : Michael Bradley

   This will allow you to run several tasks out of order, with the important
   tasks running more often than the others.

   Give each task a number from 1 to 255, the lower the number,
   the higher the priority the task has. (more often it runs)

   Basicaly, each task gets run the N'th time the task manager is entered

   example:

   // global define
   mpDefineTask mainTasks;

   void main(void)
   {

     // loop forever
     while(true)
       {
       mpTaskManager(mainTasks)
         {
         mpTask(mainTasks,  1, task_procGPS() );    // task runs every time
         mpTask(mainTasks,  2, task_procMode() );   // task runs every other time
         mpTask(mainTasks,  5, task_procTarget() ); // task runs every 5th time
         mpTask(mainTasks, 12, task_termUpdate() ); // task runs every 12th time
         }
       }
   }

*/



#ifndef __PTASK_MANAGER
   #define __PTASK_MANAGER


#define mpDefineTask             unsigned int8
#define mpTaskManager(wtask)     wtask++;
#define mpTask(wtask,pri,fn)     if ( (wtask - ( (wtask/pri) * pri)) == 0 ) { fn; }

#endif


_________________
Michael Bradley
www.mculabs.com
Open Drivers and Projects
MikeW



Joined: 15 Sep 2003
Posts: 184
Location: Warrington UK

View user's profile Send private message

PostPosted: Sun Jan 17, 2010 2:06 am     Reply with quote

Since no one else has commented.

I would like to say that this looks really useful, and is very elegant.

Mike
mbradley



Joined: 11 Jul 2009
Posts: 118
Location: California, USA

View user's profile Send private message Visit poster's website

PostPosted: Sun Jan 17, 2010 2:42 am     Reply with quote

Thank you very much.

I have found that I use it alot in LCD display applications, where I dont want to update the LCD so often that it just blinks and flashes. I rather use that time to process other items.

I generaly use a 200 or so for the lcd tasks.
_________________
Michael Bradley
www.mculabs.com
Open Drivers and Projects
MikeW



Joined: 15 Sep 2003
Posts: 184
Location: Warrington UK

View user's profile Send private message

PostPosted: Sun Jan 17, 2010 4:13 am     Reply with quote

Michael,

I usually have a 1msec or 10 msec timer interrupt, and then set flags for different times, so

in the interrrupt routine have

100mSec flag
200msec flag
500 msec flag
1 second flag
etc etc.

Then in the main loop test the flag, and go off and run the function required.

Your approach looks better.

Question, if I have several functions, which I want to run say every 1 tick, but have them run sequentially on the next tick, could you modify your solution for me.

example,
functionA
functionB
functionC

That I want to run say every 5 ticks.

So that after 5 ticks, functionA runs, then 5+1 ticks later functionB runs, then after 5+2 ticks, functionC runs.

I appreciate I could have a loop counter in the task_procTarget() in your example. mpTask(mainTasks, 5, task_procTarget()

Any thoughts ?

Mike
mbradley



Joined: 11 Jul 2009
Posts: 118
Location: California, USA

View user's profile Send private message Visit poster's website

PostPosted: Sun Jan 17, 2010 2:30 pm     Reply with quote

Hi Mike,

In general I try to save my timers and interrupts, I use them alot for timmings, so I try not to use them becouse I never seem to have enough of them.

I do have another set of macros for doing just what you ask. That is, a "in order" processing of a list of functions. called slice.

I had written it before pTask, but I needed more flexability, so today, I actualy use several pTasks and slices in an application.

So, as you said, mpTask(mainTasks, 5, task_procTarget() );

take task_procTarget(), and put a slice manager in it, then it would function as you asked.

Code:

void  task_procTarget(void)
{
  // manage 5 slices of the pie
  mSliceManager(myPie,5)
    {
    // define the order of the slices, (constant, function)
    mOrderSlice(1, slice_item1() );
    mOrderSlice(2, slice_item2() );
    mOrderSlice(3, slice_item2() );
    mOrderSlice(4, slice_item2() );
    mOrderSlice(5, slice_item2() );
    }
}


I dont want to post another driver source in this thread, so here is a link to the source in CCS C: http://www.mculabs.com/snippets/slice.html
_________________
Michael Bradley
www.mculabs.com
Open Drivers and Projects
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group