-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMtasks.cpp
170 lines (158 loc) · 4.02 KB
/
Mtasks.cpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
Mtasks.cpp - Library for Multi-Tasking based on millis().
Created by Georgi Dobrishinov, October 17, 2019.
Mtasks is licensed under the MIT License
*/
#include "Mtasks.h"
/**
* Constructor
*/
Mtasks::Mtasks()
{
_init();
}
/**
* Mtasks();
* Input: unsigned long interval
* Output: void
* Descrtiption: Constructor with one input parameter -
* interval of the task (Every 1s, Every 2s..., etc.)
*/
Mtasks::Mtasks(unsigned long interval)
{
_init();
runTask(interval);
}
/**
* Mtasks();
* Input: unsigned long interval
* Output: void
* Descrtiption: Constructor with two input parameter -
* interval of the task and duration of the task
* (Every 1s and 1 run, Every 2s and 10 runs..., etc.)
*/
Mtasks::Mtasks(unsigned long interval, unsigned int durationCount)
{
_init();
runTask(interval, durationCount);
}
/**
* Destructors
*/
Mtasks::~Mtasks() {}
/**
* Initialization: Set some default values on the tasks.
*/
void Mtasks::_init()
{
this->_lastTime = 0;
this->_interval = 0;
this->_durationCount = -1;
this->_isRunning = false;
}
/**
* runTask();
* Input: unsigned long interval
* Output: void
* Descrtiption: Run some task which is stopped.
* We can pass the interval of the task.
* (Every 1s, Every 2s..., etc.)
*/
void Mtasks::runTask(unsigned long interval)
{
this->_interval = interval;
this->_isRunning = true;
}
/**
* runTask();
* Input: unsigned long interval, unsigned int durationCount
* Output: void
* Descrtiption: Run some task which is stopped.
* We can pass the interval and duration of the task.
* (Every 1s and 1 run, Every 2s and 10 runs..., etc.)
*/
void Mtasks::runTask(unsigned long interval, unsigned int durationCount)
{
this->_interval = interval;
this->_durationCount = durationCount;
this->_isRunning = true;
}
/**
* isTaskReady();
* Input: void
* Output: bool
* Descrtiption: This method returns true or false when the task is running,
* use the method in IF condition in the loop context to execute some logic.
*/
bool Mtasks::isTaskReady()
{
return isTaskReady(millis());
}
/**
* isTaskReady();
* Input: unsigned long currentTime
* Output: bool
* Descrtiption: This method returns true or false when the task is running,
* use the method in IF condition in the loop context to execute some logic.
* We can pass one current time reference to the method,
* so if we use many tasks we can pass the same reference of the current time.
*/
bool Mtasks::isTaskReady(unsigned long currentTime)
{
return _taskProcessor(currentTime, this->_interval);
}
/**
* _taskProcessor();
* Input: unsigned long currentTime, unsigned long interval
* Output: bool
* Descrtiption: Calculation of the task times.
*/
bool Mtasks::_taskProcessor(unsigned long currentTime, unsigned long interval)
{
if ((unsigned long)currentTime - _lastTime >= interval)
{
if (isRunning())
{
if (_durationCount == 0)
{
cleanTask();
return false;
}
else if (_durationCount > 0)
{
_lastTime = currentTime;
_durationCount -= 1;
return true;
}
else
{
_lastTime = currentTime;
return true;
}
}
}
return false;
}
/**
* cleanTask();
* Input: void
* Output: void
* Descrtiption: Reset and stop current task.
* Disable duration of the task if exist and stop the task.
*/
void Mtasks::cleanTask()
{
this->_durationCount = -1;
this->_isRunning = false;
}
/**
* isRunning();
* Input: void
* Output: bool
* Descrtiption: Returns true or false when some task is running,
* we can use it in one task to check the status of the other task.
*/
bool Mtasks::isRunning()
{
return _isRunning;
}