forked from prampec/arduino-softtimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlinkTask.cpp
113 lines (96 loc) · 3.3 KB
/
BlinkTask.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
/**
* File: BlinkTask.cpp
* Description:
* SoftTimer library is a lightweight but effective event based timeshare solution for Arduino.
*
* Author: Balazs Kelemen
* Contact: [email protected]
* Copyright: 2012 Balazs Kelemen
* Copying permission statement:
This file is part of SoftTimer.
SoftTimer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http:https://www.gnu.org/licenses/>.
*/
#include "BlinkTask.h"
#include "SoftTimer.h"
void BlinkTask::init(byte outPin, unsigned long onMs, unsigned long offMs, byte count, unsigned long delayMs) {
this->onMs = onMs;
this->offMs = offMs;
this->count = count;
this->delayMs = delayMs;
pinMode(outPin, OUTPUT);
_bitMask = digitalPinToBitMask(outPin);
_portRegister = portOutputRegister(digitalPinToPort(outPin));
this->onLevel = HIGH;
}
BlinkTask::BlinkTask(byte outPin, unsigned long onOffMs) : Task(onOffMs, &(BlinkTask::stepState)) {
this->init(outPin, onOffMs, onOffMs, 0, 0);
}
BlinkTask::BlinkTask(byte outPin, unsigned long onMs, unsigned long offMs) : Task(onMs, &(BlinkTask::stepState)) {
this->init(outPin, onMs, offMs, 0, 0);
}
BlinkTask::BlinkTask(byte outPin, unsigned long onMs,unsigned long offMs, byte count) : Task(onMs, &(BlinkTask::stepState)) {
this->init(outPin, onMs, offMs, count, 0);
this->stop();
}
BlinkTask::BlinkTask(byte outPin, unsigned long onMs, unsigned long offMs, byte count, unsigned long delayMs) : Task(onMs, &(BlinkTask::stepState)) {
this->init(outPin, onMs, offMs, count, delayMs);
}
void BlinkTask::start() {
this->_state = STATE_OFF;
this->_counter = 0;
this->setPeriodMs(0);
SoftTimer.add(this);
}
void BlinkTask::start(byte count) {
this->count = count;
this->start();
this->delayMs = 0;
}
void BlinkTask::stop() {
SoftTimer.remove(this);
}
void BlinkTask::stepState(Task* task) {
BlinkTask* bt = (BlinkTask*)task;
if(bt->_state == STATE_ON) {
// -- Turn off.
if(bt->onLevel == HIGH) {
*bt->_portRegister &= ~bt->_bitMask;
} else {
*bt->_portRegister |= bt->_bitMask;
}
bt->_counter += 1;
bt->_state = STATE_OFF;
bt->setPeriodMs(bt->offMs);
}
else {
// -- state == OFF or WAIT
// -- Turn on.
if(bt->onLevel == HIGH) {
*bt->_portRegister |= bt->_bitMask;
} else {
*bt->_portRegister &= ~bt->_bitMask;
}
bt->_state = STATE_ON;
bt->setPeriodMs(bt->onMs);
}
if((bt->count > 0) && (bt->_counter >= bt->count)) {
// -- Count was defined, and we reached it.
bt->_counter = 0;
if(bt->delayMs > 0) {
// -- delay was defined.
bt->_state = STATE_WAIT;
bt->setPeriodMs(bt->delayMs);
} else {
bt->stop();
}
}
}