forked from DaniFoldi/Async_Operations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsync_Operations.cpp
146 lines (124 loc) · 3.52 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
/*
* 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;
}
Async_Operations::Async_Operations(long long *steps, int stepCount, int repeat) {
this->steps = steps;
this->stepCount = stepCount;
this->initialRepeat = repeat;
}
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;
}
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;
this->initialState = initialState;
}
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;
this->initialState = initialState;
this->endState = endState;
}
void Async_Operations::start() {
this->startTime = millis();
if (this->pin >= 0) {
pinMode(this->pin, OUTPUT);
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) {
if (this->loopCallback) {
this->loopCallback();
}
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;
}
}
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;
}