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

Dependents:   AVC_20110423 AVC_2012

Committer:
shimniok
Date:
Wed Apr 27 18:00:45 2011 +0000
Revision:
0:03d36a9a088b
Initial revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shimniok 0:03d36a9a088b 1 typedef unsigned int tick;
shimniok 0:03d36a9a088b 2 typedef int value;
shimniok 0:03d36a9a088b 3 typedef char flag;
shimniok 0:03d36a9a088b 4
shimniok 0:03d36a9a088b 5 /** Simple library for scheduling events by polling time, that is, avoiding interrupts.
shimniok 0:03d36a9a088b 6 * Can schedule up to 64 events. The schedule is defined by the number of ticks making
shimniok 0:03d36a9a088b 7 * up the initial period of the schedule, and either a start and end value or values
shimniok 0:03d36a9a088b 8 * at specific tick values.
shimniok 0:03d36a9a088b 9 */
shimniok 0:03d36a9a088b 10 class Schedule {
shimniok 0:03d36a9a088b 11 public:
shimniok 0:03d36a9a088b 12 /** Sets the behavior of getNext() after the initial schedule period has exceeded.
shimniok 0:03d36a9a088b 13 *
shimniok 0:03d36a9a088b 14 * @param repeat means the schedule values are repeated
shimniok 0:03d36a9a088b 15 * @param hold means the last schedule value is provided after the schdule is done
shimniok 0:03d36a9a088b 16 */
shimniok 0:03d36a9a088b 17 enum { repeat=0x02, hold=0x04 };
shimniok 0:03d36a9a088b 18
shimniok 0:03d36a9a088b 19 /** Creates an empty schedule
shimniok 0:03d36a9a088b 20 */
shimniok 0:03d36a9a088b 21 Schedule();
shimniok 0:03d36a9a088b 22
shimniok 0:03d36a9a088b 23 /** Creates a schedule based on a linear function. See set()
shimniok 0:03d36a9a088b 24 *
shimniok 0:03d36a9a088b 25 * @param scale
shimniok 0:03d36a9a088b 26 * @param max the maximum tick value of the clock, sets the period
shimniok 0:03d36a9a088b 27 * @param start is the value returned at tick == 0
shimniok 0:03d36a9a088b 28 * @param stop is the value returned at tick == ticks-1
shimniok 0:03d36a9a088b 29 * @param m selects the mode / behavior of the schedule when getNext() called after period exceeded
shimniok 0:03d36a9a088b 30 */
shimniok 0:03d36a9a088b 31 Schedule(unsigned int scale, tick max, value start, value stop, flag m);
shimniok 0:03d36a9a088b 32
shimniok 0:03d36a9a088b 33 /** Sets a ratio of time to ticks. See clockTicked()
shimniok 0:03d36a9a088b 34 *
shimniok 0:03d36a9a088b 35 * @param timePerTick specifies the number of time units per tick
shimniok 0:03d36a9a088b 36 */
shimniok 0:03d36a9a088b 37 void scale(unsigned int scale);
shimniok 0:03d36a9a088b 38
shimniok 0:03d36a9a088b 39 /** Sets the total number of ticks to run the loop
shimniok 0:03d36a9a088b 40 */
shimniok 0:03d36a9a088b 41 void max(tick max);
shimniok 0:03d36a9a088b 42
shimniok 0:03d36a9a088b 43 /** Sets behavior of getNext() when called after tickCount exceeded
shimniok 0:03d36a9a088b 44 */
shimniok 0:03d36a9a088b 45 void mode(flag m);
shimniok 0:03d36a9a088b 46
shimniok 0:03d36a9a088b 47 /** sets the value at the specified tick
shimniok 0:03d36a9a088b 48 *
shimniok 0:03d36a9a088b 49 * @param t specifies the scheduled tick
shimniok 0:03d36a9a088b 50 * @param v specifies the value to return when tick==whichTick
shimniok 0:03d36a9a088b 51 */
shimniok 0:03d36a9a088b 52 void set(tick t, value v);
shimniok 0:03d36a9a088b 53
shimniok 0:03d36a9a088b 54 /** Set schedule based on a linear function
shimniok 0:03d36a9a088b 55 *
shimniok 0:03d36a9a088b 56 * @param ticks total number of ticks over which the schedule is valid
shimniok 0:03d36a9a088b 57 * @param startValue is the value returned at tick == 0
shimniok 0:03d36a9a088b 58 * @param stopValue is the value returned at tick == ticks-1
shimniok 0:03d36a9a088b 59 */
shimniok 0:03d36a9a088b 60 void set(unsigned int scale, tick max, value start, value stop, flag m);
shimniok 0:03d36a9a088b 61
shimniok 0:03d36a9a088b 62 /** get the next value for schedule's current time. Use with ticked()
shimniok 0:03d36a9a088b 63 *
shimniok 0:03d36a9a088b 64 * @returns the value at the current schedule's time
shimniok 0:03d36a9a088b 65 */
shimniok 0:03d36a9a088b 66 value get();
shimniok 0:03d36a9a088b 67
shimniok 0:03d36a9a088b 68 /** increment the clock and get the next value in the schedule
shimniok 0:03d36a9a088b 69 *
shimniok 0:03d36a9a088b 70 * @returns the value at the schedule's next clock tick
shimniok 0:03d36a9a088b 71 */
shimniok 0:03d36a9a088b 72 value next();
shimniok 0:03d36a9a088b 73
shimniok 0:03d36a9a088b 74 /** Pass in some unit of time and determine if the 'clock' has ticked.
shimniok 0:03d36a9a088b 75 * Suppose timePerTick == 20 and you pass in the elapsed time in milliseconds
shimniok 0:03d36a9a088b 76 * then this function returns true every 20ms.
shimniok 0:03d36a9a088b 77 *
shimniok 0:03d36a9a088b 78 * @param time the integer corresponding to elapsed time
shimniok 0:03d36a9a088b 79 * @returns true if the elapsed time % timePerTick == 0
shimniok 0:03d36a9a088b 80 */
shimniok 0:03d36a9a088b 81 bool ticked(unsigned int time);
shimniok 0:03d36a9a088b 82
shimniok 0:03d36a9a088b 83 /** Are we done with the schedule?
shimniok 0:03d36a9a088b 84 *
shimniok 0:03d36a9a088b 85 * @returns true if schedule is done; see max()
shimniok 0:03d36a9a088b 86 */
shimniok 0:03d36a9a088b 87 bool done();
shimniok 0:03d36a9a088b 88
shimniok 0:03d36a9a088b 89 private:
shimniok 0:03d36a9a088b 90 unsigned int _scale;
shimniok 0:03d36a9a088b 91 tick _max;
shimniok 0:03d36a9a088b 92 tick _clock;
shimniok 0:03d36a9a088b 93 flag _mode;
shimniok 0:03d36a9a088b 94 int _schedule[64];
shimniok 0:03d36a9a088b 95 bool _validTick(tick t);
shimniok 0:03d36a9a088b 96 };
shimniok 0:03d36a9a088b 97