-
Notifications
You must be signed in to change notification settings - Fork 1
/
Async_Operations.cpp
193 lines (173 loc) · 4.67 KB
/
Async_Operations.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Async_Operations.h library for running time-consuming tasks without blocking the main thread
* Created by Dániel Földi (05. August 2020)
*/
#include "Async_Operations.h"
Async_Operations::Async_Operations(long long *steps, int stepCount) {
this->steps = steps;
this->stepCount = stepCount;
this->initialRepeat = 0;
this->pin = -1;
if (this->pin >= 0) {
pinMode(this->pin, OUTPUT);
}
this->initialState = true;
this->endState = false;
this->running = false;
this->startTime = 0;
this->lastUpdate = 0;
this->stateChangeCallback = 0;
this->loopCallback = 0;
}
Async_Operations::Async_Operations(long long *steps, int stepCount, int repeat) {
this->steps = steps;
this->stepCount = stepCount;
this->initialRepeat = repeat;
this->pin = -1;
if (this->pin >= 0) {
pinMode(this->pin, OUTPUT);
}
this->initialState = true;
this->endState = false;
this->running = false;
this->startTime = 0;
this->lastUpdate = 0;
this->stateChangeCallback = 0;
this->loopCallback = 0;
}
Async_Operations::Async_Operations(long long *steps, int stepCount, int repeat, int pin) {
this->steps = steps;
this->stepCount = stepCount;
this->initialRepeat = repeat;
this->pin = pin;
if (this->pin >= 0) {
pinMode(this->pin, OUTPUT);
}
this->initialState = true;
this->endState = false;
this->running = false;
this->startTime = 0;
this->lastUpdate = 0;
this->stateChangeCallback = 0;
this->loopCallback = 0;
}
Async_Operations::Async_Operations(long long *steps, int stepCount, int repeat, int pin, bool initialState) {
this->steps = steps;
this->stepCount = stepCount;
this->initialRepeat = repeat;
this->pin = pin;
if (this->pin >= 0) {
pinMode(this->pin, OUTPUT);
}
this->initialState = initialState;
this->endState = false;
this->running = false;
this->startTime = 0;
this->lastUpdate = 0;
this->stateChangeCallback = 0;
this->loopCallback = 0;
}
Async_Operations::Async_Operations(long long *steps, int stepCount, int repeat, int pin, bool initialState, bool endState) {
this->steps = steps;
this->stepCount = stepCount;
this->initialRepeat = repeat;
this->pin = pin;
if (this->pin >= 0) {
pinMode(this->pin, OUTPUT);
}
this->initialState = initialState;
this->endState = endState;
this->running = false;
this->startTime = 0;
this->lastUpdate = 0;
this->stateChangeCallback = 0;
this->loopCallback = 0;
}
void Async_Operations::start() {
this->startTime = millis();
if (this->pin >= 0) {
digitalWrite(this->pin, this->state);
}
this->lastUpdate = this->startTime;
this->step = 0;
this->remaining = this->steps[this->step];
this->running = true;
this->state = this->initialState;
this->repeat = this->initialRepeat;
}
void Async_Operations::stop() {
this->running = false;
this->step = 0;
this->repeat = this->initialRepeat;
}
void Async_Operations::pause() {
//this->running = false;
//this->remaining = this->steps[this->step] - millis() + this->start_time;
}
void Async_Operations::play() {
//this->running = true;
//this->start_time = millis() - this->steps[this->step] + this->remaining;
}
void Async_Operations::restart() {
this->stop();
this->start();
}
bool Async_Operations::getPaused() {
return !this->running;
}
bool Async_Operations::getRunning() {
return this->running;
}
void Async_Operations::update() {
if (!this->running) {
return;
}
long long currentTime = millis();
while (this->lastUpdate + this->remaining < currentTime) {
this->step++;
if (this->step >= this->stepCount) {
this->step = 0;
if (this->repeat > 0) {
this->repeat--;
}
if (this->repeat == 0) {
this->remaining = 0;
this->running = false;
if (this->pin >= 0) {
digitalWrite(this->pin, this->endState);
}
return;
}
if (this->loopCallback) {
this->loopCallback();
}
}
this->lastUpdate += this->remaining;
this->remaining = this->steps[this->step];
this->state = !this->state;
if (this->pin >= 0) {
digitalWrite(this->pin, this->state);
}
if (this->stateChangeCallback) {
this->stateChangeCallback();
}
}
}
int Async_Operations::getRepeatCount() {
return this->repeat;
}
bool Async_Operations::getState() {
return this->state;
}
void Async_Operations::setStateChangeCallback(void (*stateChangeCallback)(void)) {
this->stateChangeCallback = stateChangeCallback;
}
void Async_Operations::deleteStateChangeCallback() {
this->stateChangeCallback = 0;
}
void Async_Operations::setLoopCallback(void (*loopCallback)(void)) {
this->loopCallback = loopCallback;
}
void Async_Operations::deleteLoopCallback() {
this->loopCallback = 0;
}