Simple library for scheduling events by polling time, that is, avoiding interrupts.

Dependents:   AVC_20110423 AVC_2012

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Schedule.h Source File

Schedule.h

00001 typedef unsigned int tick;
00002 typedef int value;
00003 typedef char flag;
00004 
00005 /** Simple library for scheduling events by polling time, that is, avoiding interrupts.
00006  *  Can schedule up to 64 events.  The schedule is defined by the number of ticks making
00007  *  up the initial period of the schedule, and either a start and end value or values
00008  *  at specific tick values.
00009  */
00010 class Schedule {
00011     public:
00012         /** Sets the behavior of getNext() after the initial schedule period has exceeded.
00013          *
00014          * @param repeat means the schedule values are repeated
00015          * @param hold means the last schedule value is provided after the schdule is done
00016          */
00017         enum { repeat=0x02, hold=0x04 };
00018 
00019         /** Creates an empty schedule
00020          */
00021         Schedule();
00022         
00023         /** Creates a schedule based on a linear function. See set()
00024          *
00025          *  @param scale
00026          *  @param max the maximum tick value of the clock, sets the period
00027          *  @param start is the value returned at tick == 0 
00028          *  @param stop is the value returned at tick == ticks-1
00029          *  @param m selects the mode / behavior of the schedule when getNext() called after period exceeded
00030          */
00031         Schedule(unsigned int scale, tick max, value start, value stop, flag m);
00032             
00033         /** Sets a ratio of time to ticks. See clockTicked()
00034          *
00035          * @param timePerTick specifies the number of time units per tick
00036          */
00037         void scale(unsigned int scale);
00038         
00039         /** Sets the total number of ticks to run the loop
00040          */
00041         void max(tick max);
00042         
00043         /** Sets behavior of getNext() when called after tickCount exceeded
00044          */
00045         void mode(flag m);
00046         
00047         /** sets the value at the specified tick
00048          *
00049          * @param t specifies the scheduled tick
00050          * @param v specifies the value to return when tick==whichTick
00051          */        
00052         void set(tick t, value v);
00053         
00054         /** Set schedule based on a linear function
00055          *
00056          *  @param ticks total number of ticks over which the schedule is valid
00057          *  @param startValue is the value returned at tick == 0 
00058          *  @param stopValue is the value returned at tick == ticks-1
00059          */
00060         void set(unsigned int scale, tick max, value start, value stop, flag m); 
00061         
00062         /** get the next value for schedule's current time.  Use with ticked()
00063          *
00064          * @returns the value at the current schedule's time
00065          */
00066         value get();
00067         
00068         /** increment the clock and get the next value in the schedule
00069          *
00070          * @returns the value at the schedule's next clock tick
00071          */
00072          value next();
00073          
00074         /** Pass in some unit of time and determine if the 'clock' has ticked.
00075          *  Suppose timePerTick == 20 and you pass in the elapsed time in milliseconds
00076          *  then this function returns true every 20ms.
00077          *
00078          *  @param time the integer corresponding to elapsed time
00079          *  @returns true if the elapsed time % timePerTick == 0
00080          */
00081         bool ticked(unsigned int time);
00082         
00083         /** Are we done with the schedule?
00084          *
00085          * @returns true if schedule is done; see max()
00086          */
00087         bool done();
00088 
00089     private:
00090         unsigned int _scale;
00091         tick _max;
00092         tick _clock;
00093         flag _mode;
00094         int _schedule[64];
00095         bool _validTick(tick t);
00096 };
00097