-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduleTable.cpp
More file actions
128 lines (116 loc) · 3.07 KB
/
ScheduleTable.cpp
File metadata and controls
128 lines (116 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Schedule tables for Arduino
*
* Copyright Jean-Luc Béchennec 2015
*
* This software is distributed under the GNU Public Licence v2 (GPLv2)
*
* Please read the LICENCE file
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "ScheduleTable.h"
void ScheduleTableActionSlot::print()
{
Serial.println(mOffset);
}
ScheduleTable *ScheduleTable::scheduleTableList = NULL;
void ScheduleTable::insertAction(
unsigned long offset,
ScheduleTableAction *action)
{
ScheduleTableActionSlot point(offset, action);
/* find its place in the table */
for (byte index = 0; index < mSize; index++) {
if (point < mAction[index]) {
ScheduleTableActionSlot tmp = mAction[index];
mAction[index] = point;
point = tmp;
}
}
mAction[mSize++] = point;
}
void ScheduleTable::at(
unsigned long offset,
function action)
{
if (mSize < mMaxSize) {
/* compute the actual offset */
offset *= mTimeBase;
if (offset <= mPeriod) {
insertAction(offset, new FunctionCallAction(action));
}
}
}
void ScheduleTable::at(
unsigned long offset,
ScheduleTableAction& action)
{
if (mSize < mMaxSize) {
/* compute the actual offset */
offset *= mTimeBase;
if (offset <= mPeriod) {
insertAction(offset, &action);
}
}
}
void ScheduleTable::updateIt()
{
if (mState != SCHEDULETABLE_STOPPED) {
unsigned long currentDate = millis();
unsigned long offsetDate = currentDate - mOrigin;
while (mCurrent < mSize &&
mAction[mCurrent].perform(offsetDate, mPeriod)) {
mCurrent++;
}
if (mCurrent == mSize) {
mCurrent = 0;
if (mState == SCHEDULETABLE_PERIODIC_FOREVER ||
(mState == SCHEDULETABLE_PERIODIC && --mHowMuch != 0)) {
mOrigin += mPeriod;
}
else {
mState = SCHEDULETABLE_STOPPED;
}
}
}
}
/* Start the schedule table periodic */
void ScheduleTable::start(unsigned int howMuch)
{
mOrigin = millis();
mCurrent = 0;
mHowMuch = howMuch;
if (howMuch) mState = SCHEDULETABLE_PERIODIC;
else mState = SCHEDULETABLE_PERIODIC_FOREVER;
}
/* Stop the schedule table */
void ScheduleTable::stop()
{
mState = SCHEDULETABLE_STOPPED;
}
void ScheduleTable::setPeriod(unsigned int period)
{
mPeriod = period * mTimeBase;
}
void ScheduleTable::print()
{
Serial.print('['); Serial.print(mSize); Serial.println("]");
for (byte index = 0; index < mSize; index++) {
mAction[index].print();
}
}
void ScheduleTable::update()
{
ScheduleTable *currentScheduleTable = scheduleTableList;
while (currentScheduleTable) {
currentScheduleTable->updateIt();
currentScheduleTable = currentScheduleTable->mNext;
}
}