Simple library for scheduling events by polling time, that is, avoiding interrupts.
Dependents: AVC_20110423 AVC_2012
Schedule.cpp
00001 #include "Schedule.h" 00002 00003 Schedule::Schedule(): _scale(0), _max(0), _clock(0) 00004 { 00005 for (int i=0; i < 64; i++) { 00006 _schedule[i] = 0; 00007 } 00008 } 00009 00010 Schedule::Schedule(unsigned int scale, tick max, value start, value stop, flag m): _scale(scale), _max(max), _clock(0) 00011 { 00012 Schedule::set(scale, max, start, stop, m); 00013 } 00014 00015 void Schedule::scale(unsigned int scale) 00016 { 00017 _scale = scale; 00018 } 00019 00020 00021 void Schedule::max(tick max) 00022 { 00023 if (_validTick(max)) 00024 _max = max; 00025 } 00026 00027 00028 void Schedule::mode(flag m) 00029 { 00030 _mode = m; 00031 } 00032 00033 00034 void Schedule::set(unsigned int scale, tick max, value start, value stop, flag m) 00035 { 00036 if (_validTick(max)) { 00037 _scale = scale; 00038 _max = max; 00039 _mode = m; 00040 float slope = ((float) stop - (float) start) / (float) max; 00041 for (int i=0; i <= max; i++) { 00042 _schedule[i] = ((int) (slope*(float)i)) + start; 00043 } 00044 } 00045 } 00046 00047 00048 void Schedule::set(tick t, value v) 00049 { 00050 if (_validTick(t)) { 00051 _schedule[t] = v; 00052 if (t > _max) _max = t; 00053 } 00054 } 00055 00056 00057 value Schedule::get() 00058 { 00059 if (done()) { 00060 if (_mode == repeat) 00061 _clock %= _max+1; 00062 else if (_mode == hold) 00063 _clock = _max; 00064 else 00065 return 0; 00066 } 00067 00068 return _schedule[_clock]; 00069 } 00070 00071 00072 value Schedule::next() 00073 { 00074 _clock++; 00075 return get(); 00076 } 00077 00078 00079 bool Schedule::ticked(unsigned int time) 00080 { 00081 bool result = false; 00082 00083 if ((time % _scale) == 0) { 00084 _clock++; 00085 result = true; 00086 } 00087 00088 return result; 00089 } 00090 00091 bool Schedule::done() 00092 { 00093 return (_clock > _max); 00094 } 00095 00096 bool Schedule::_validTick(tick t) 00097 { 00098 return (t < 64); // unsigned int, always > 0; if wraps around will be > 64 00099 }
Generated on Wed Jul 13 2022 10:41:54 by 1.7.2