-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduleTable.h
More file actions
149 lines (123 loc) · 3.63 KB
/
ScheduleTable.h
File metadata and controls
149 lines (123 loc) · 3.63 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* 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.
*/
#ifndef __ScheduleTable_h__
#define __ScheduleTable_h__
#include "Arduino.h"
typedef void (*function)();
class ScheduleTableAction
{
public:
/* action to be performed */
virtual void action() = 0;
};
class ScheduleTableActionSlot
{
private:
unsigned long mOffset;
ScheduleTableAction *mAction;
public:
ScheduleTableActionSlot() : mOffset(0), mAction(NULL) {}
ScheduleTableActionSlot(unsigned long offset, ScheduleTableAction *action) :
mOffset(offset), mAction(action) {}
bool perform(unsigned long offsetDate, unsigned long period) {
if (offsetDate <= period && offsetDate >= mOffset) {
mAction->action();
return true;
}
return false;
}
virtual ScheduleTableActionSlot& operator=(
const ScheduleTableActionSlot& actionSlot)
{
mOffset = actionSlot.mOffset;
mAction = actionSlot.mAction;
return *this;
}
bool operator<(const ScheduleTableActionSlot& actionSlot) {
return (mOffset < actionSlot.mOffset);
}
bool operator<=(const ScheduleTableActionSlot& actionSlot) {
return (mOffset <= actionSlot.mOffset);
}
bool operator>(const ScheduleTableActionSlot& actionSlot) {
return (mOffset > actionSlot.mOffset);
}
bool operator>=(const ScheduleTableActionSlot& actionSlot) {
return (mOffset >= actionSlot.mOffset);
}
void print();
};
class FunctionCallAction : public ScheduleTableAction
{
private:
function mCallback;
public:
FunctionCallAction(function callback) : mCallback(callback) {}
virtual void action() { if (mCallback) mCallback(); }
};
enum {
SCHEDULETABLE_STOPPED,
SCHEDULETABLE_PERIODIC,
SCHEDULETABLE_PERIODIC_FOREVER
};
class ScheduleTable
{
private:
unsigned long mTimeBase;
unsigned long mOrigin;
unsigned long mPeriod;
ScheduleTableActionSlot *mAction;
unsigned int mHowMuch;
byte mSize;
byte mMaxSize;
byte mCurrent;
byte mState;
ScheduleTable *mNext;
static ScheduleTable *scheduleTableList;
/* Look for action to perform */
void updateIt();
/* insert an action in the table */
void insertAction(unsigned long offset, ScheduleTableAction *action);
public:
/* Constructor. Does the initialization of the Schedule Table */
ScheduleTable(byte size, unsigned long period, unsigned long timeBase = 1) :
mTimeBase(timeBase),
mPeriod(period * timeBase),
mSize(0),
mMaxSize(size),
mCurrent(0),
mState(SCHEDULETABLE_STOPPED)
{
mAction = new ScheduleTableActionSlot[size];
mNext = scheduleTableList;
scheduleTableList = this;
}
/* Schedule a new action */
void at(unsigned long offset, function action);
void at(unsigned long offset, ScheduleTableAction& action);
/* Start the schedule table periodic or one shot */
void start(unsigned int howMuch = 0);
/* Stop the schedule table */
void stop();
/* Set the period of the schedule table */
void setPeriod(unsigned int period);
/* Print the whole schedule table for debugging purpose */
void print();
static void update();
};
#endif