Simple library for scheduling events by polling time, that is, avoiding interrupts.
Dependents: AVC_20110423 AVC_2012
Revision 0:03d36a9a088b, committed 2011-04-27
- Comitter:
- shimniok
- Date:
- Wed Apr 27 18:00:45 2011 +0000
- Commit message:
- Initial revision
Changed in this revision
Schedule.cpp | Show annotated file Show diff for this revision Revisions of this file |
Schedule.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 03d36a9a088b Schedule.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Schedule.cpp Wed Apr 27 18:00:45 2011 +0000 @@ -0,0 +1,99 @@ +#include "Schedule.h" + +Schedule::Schedule(): _scale(0), _max(0), _clock(0) +{ + for (int i=0; i < 64; i++) { + _schedule[i] = 0; + } +} + +Schedule::Schedule(unsigned int scale, tick max, value start, value stop, flag m): _scale(scale), _max(max), _clock(0) +{ + Schedule::set(scale, max, start, stop, m); +} + +void Schedule::scale(unsigned int scale) +{ + _scale = scale; +} + + +void Schedule::max(tick max) +{ + if (_validTick(max)) + _max = max; +} + + +void Schedule::mode(flag m) +{ + _mode = m; +} + + +void Schedule::set(unsigned int scale, tick max, value start, value stop, flag m) +{ + if (_validTick(max)) { + _scale = scale; + _max = max; + _mode = m; + float slope = ((float) stop - (float) start) / (float) max; + for (int i=0; i <= max; i++) { + _schedule[i] = ((int) (slope*(float)i)) + start; + } + } +} + + +void Schedule::set(tick t, value v) +{ + if (_validTick(t)) { + _schedule[t] = v; + if (t > _max) _max = t; + } +} + + +value Schedule::get() +{ + if (done()) { + if (_mode == repeat) + _clock %= _max+1; + else if (_mode == hold) + _clock = _max; + else + return 0; + } + + return _schedule[_clock]; +} + + +value Schedule::next() +{ + _clock++; + return get(); +} + + +bool Schedule::ticked(unsigned int time) +{ + bool result = false; + + if ((time % _scale) == 0) { + _clock++; + result = true; + } + + return result; +} + +bool Schedule::done() +{ + return (_clock > _max); +} + +bool Schedule::_validTick(tick t) +{ + return (t < 64); // unsigned int, always > 0; if wraps around will be > 64 +} \ No newline at end of file
diff -r 000000000000 -r 03d36a9a088b Schedule.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Schedule.h Wed Apr 27 18:00:45 2011 +0000 @@ -0,0 +1,97 @@ +typedef unsigned int tick; +typedef int value; +typedef char flag; + +/** Simple library for scheduling events by polling time, that is, avoiding interrupts. + * Can schedule up to 64 events. The schedule is defined by the number of ticks making + * up the initial period of the schedule, and either a start and end value or values + * at specific tick values. + */ +class Schedule { + public: + /** Sets the behavior of getNext() after the initial schedule period has exceeded. + * + * @param repeat means the schedule values are repeated + * @param hold means the last schedule value is provided after the schdule is done + */ + enum { repeat=0x02, hold=0x04 }; + + /** Creates an empty schedule + */ + Schedule(); + + /** Creates a schedule based on a linear function. See set() + * + * @param scale + * @param max the maximum tick value of the clock, sets the period + * @param start is the value returned at tick == 0 + * @param stop is the value returned at tick == ticks-1 + * @param m selects the mode / behavior of the schedule when getNext() called after period exceeded + */ + Schedule(unsigned int scale, tick max, value start, value stop, flag m); + + /** Sets a ratio of time to ticks. See clockTicked() + * + * @param timePerTick specifies the number of time units per tick + */ + void scale(unsigned int scale); + + /** Sets the total number of ticks to run the loop + */ + void max(tick max); + + /** Sets behavior of getNext() when called after tickCount exceeded + */ + void mode(flag m); + + /** sets the value at the specified tick + * + * @param t specifies the scheduled tick + * @param v specifies the value to return when tick==whichTick + */ + void set(tick t, value v); + + /** Set schedule based on a linear function + * + * @param ticks total number of ticks over which the schedule is valid + * @param startValue is the value returned at tick == 0 + * @param stopValue is the value returned at tick == ticks-1 + */ + void set(unsigned int scale, tick max, value start, value stop, flag m); + + /** get the next value for schedule's current time. Use with ticked() + * + * @returns the value at the current schedule's time + */ + value get(); + + /** increment the clock and get the next value in the schedule + * + * @returns the value at the schedule's next clock tick + */ + value next(); + + /** Pass in some unit of time and determine if the 'clock' has ticked. + * Suppose timePerTick == 20 and you pass in the elapsed time in milliseconds + * then this function returns true every 20ms. + * + * @param time the integer corresponding to elapsed time + * @returns true if the elapsed time % timePerTick == 0 + */ + bool ticked(unsigned int time); + + /** Are we done with the schedule? + * + * @returns true if schedule is done; see max() + */ + bool done(); + + private: + unsigned int _scale; + tick _max; + tick _clock; + flag _mode; + int _schedule[64]; + bool _validTick(tick t); +}; + \ No newline at end of file