|
31 | 31 | #define BLINK_STATE_ON 1
|
32 | 32 | #define BLINK_STATE_WAIT 2
|
33 | 33 |
|
34 |
| -void BlinkTask::init(byte outPin, unsigned long onMs, unsigned long offMs, byte count, unsigned long delayMs) { |
| 34 | +void BlinkTask::setupProperties(byte outPin, unsigned long onMs, unsigned long offMs, byte count, unsigned long delayMs) { |
| 35 | + this->_outPin = outPin; |
35 | 36 | this->onMs = onMs;
|
36 | 37 | this->offMs = offMs;
|
37 | 38 | this->count = count;
|
38 | 39 | this->delayMs = delayMs;
|
| 40 | + this->onLevel = HIGH; |
| 41 | +} |
39 | 42 |
|
40 |
| - pinMode(outPin, OUTPUT); |
41 |
| - this->_bitMask = digitalPinToBitMask(outPin); |
42 |
| - this->_portRegister = portOutputRegister(digitalPinToPort(outPin)); |
| 43 | +void BlinkTask::init() { |
| 44 | + pinMode(this->_outPin, OUTPUT); |
| 45 | + this->_bitMask = digitalPinToBitMask(this->_outPin); |
| 46 | + this->_portRegister = portOutputRegister(digitalPinToPort(this->_outPin)); |
43 | 47 |
|
44 |
| - this->onLevel = HIGH; |
| 48 | + // -- Turn off. |
| 49 | + if(this->onLevel == HIGH) { |
| 50 | + *this->_portRegister &= ~this->_bitMask; |
| 51 | + } else { |
| 52 | + *this->_portRegister |= this->_bitMask; |
| 53 | + } |
| 54 | + Task::init(); |
45 | 55 | }
|
46 | 56 |
|
47 | 57 | BlinkTask::BlinkTask(byte outPin, unsigned long onOffMs) : Task(onOffMs, &(BlinkTask::stepState)) {
|
48 |
| - this->init(outPin, onOffMs, onOffMs, 0, 0); |
| 58 | + this->setupProperties(outPin, onOffMs, onOffMs, 0, 0); |
49 | 59 | }
|
50 | 60 |
|
51 | 61 | BlinkTask::BlinkTask(byte outPin, unsigned long onMs, unsigned long offMs) : Task(onMs, &(BlinkTask::stepState)) {
|
52 |
| - this->init(outPin, onMs, offMs, 0, 0); |
| 62 | + this->setupProperties(outPin, onMs, offMs, 0, 0); |
53 | 63 | }
|
54 | 64 |
|
55 | 65 | BlinkTask::BlinkTask(byte outPin, unsigned long onMs,unsigned long offMs, byte count) : Task(onMs, &(BlinkTask::stepState)) {
|
56 |
| - this->init(outPin, onMs, offMs, count, 0); |
57 |
| - this->stop(); |
| 66 | + this->setupProperties(outPin, onMs, offMs, count, 0); |
58 | 67 | }
|
59 | 68 |
|
60 |
| -BlinkTask::BlinkTask(byte outPin, unsigned long onMs, unsigned long offMs, byte count, unsigned long delayMs) : Task(onMs, &(BlinkTask::stepState)) { |
61 |
| - this->init(outPin, onMs, offMs, count, delayMs); |
| 69 | +BlinkTask::BlinkTask(byte outPin, unsigned long onMs, unsigned long offMs, byte count, unsigned long delayMs) |
| 70 | + : Task(onMs, &(BlinkTask::stepState)) { |
| 71 | + this->setupProperties(outPin, onMs, offMs, count, delayMs); |
62 | 72 | }
|
63 | 73 |
|
64 | 74 | void BlinkTask::start() {
|
| 75 | + this->init(); |
65 | 76 | this->_state = BLINK_STATE_OFF;
|
66 | 77 | this->_counter = 0;
|
67 | 78 | this->setPeriodMs(0);
|
|
0 commit comments