-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBlinkLed.cpp
More file actions
199 lines (174 loc) · 4.98 KB
/
BlinkLed.cpp
File metadata and controls
199 lines (174 loc) · 4.98 KB
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
194
195
196
197
198
199
/*
* BlinkLed.cpp
*
* This library let multiple LEDs, connected to any Arduino output, blink non blocking and independently.
* Therefore you must call update continuously in loop()
*
* Copyright (C) 2018-2019 Armin Joachimsmeyer
*
* This file is part of Arduino-Utils https://github.com/ArminJo/Arduino-Utils.
*
* Arduino-Utils 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://www.gnu.org/licenses/gpl.html>.
*/
#include <Arduino.h>
#include "BlinkLed.h"
/*
* The simple blocking variant
*/
void blinkLEDBlocking(uint8_t aLedPin, uint8_t aBlinkCount, uint16_t aDelayMillis) {
for (int i = 0; i < aBlinkCount; ++i) {
digitalWrite(aLedPin, HIGH);
delay(aDelayMillis);
digitalWrite(aLedPin, LOW);
delay(aDelayMillis);
}
}
BlinkLed::BlinkLed(uint8_t aLedPin) {
init(aLedPin, false);
setOnOffTime(1000, 1000);
}
BlinkLed::BlinkLed(uint8_t aLedPin, bool aInitState, unsigned int aOnTimeMillis, unsigned int aOffTimeMillis) {
init(aLedPin, aInitState);
setOnOffTime(aOnTimeMillis, aOffTimeMillis);
}
void BlinkLed::init(uint8_t aLedPin, bool aInitState) {
pin = aLedPin;
pinMode(pin, OUTPUT);
digitalWrite(pin, state);
state = aInitState;
}
/*
* No count specified here, so set to BLINK_LED_FOREVER
*/
void BlinkLed::setOnOffTime(unsigned int aOnTimeMillis, unsigned int aOffTimeMillis) {
onDelayMillis = aOnTimeMillis;
offDelayMillis = aOffTimeMillis;
}
// must be called continuously in loop()
void BlinkLed::update() {
if (!enabled) {
return;
}
if (state) {
// check for on time gone to change state
if ((millis() - lastUpdateMillis >= onDelayMillis)) {
toggle();
// count blinks
if (numberOfBlinks > 0) {
numberOfBlinks--;
if (numberOfBlinks == 0) {
// stop blinking
enabled = false;
}
}
}
} else if ((millis() - lastUpdateMillis >= offDelayMillis)) {
toggle();
}
}
void BlinkLed::start(signed int aBlinkCount, unsigned int aOnTimeMillis, unsigned int aOffTimeMillis) {
onDelayMillis = aOnTimeMillis;
offDelayMillis = aOffTimeMillis;
start(aBlinkCount);
}
/*
* set to 50% duty cycle
*/
void BlinkLed::start(signed int aBlinkCount, unsigned int aPeriod) {
onDelayMillis = offDelayMillis = aPeriod / 2;
start(aBlinkCount);
}
/*
* set to 50% duty cycle
*/
void BlinkLed::start(signed int aBlinkCount) {
numberOfBlinks = aBlinkCount;
start();
}
// Force ON and enable blink
void BlinkLed::start() {
digitalWrite(pin, HIGH);
state = true;
enabled = true;
lastUpdateMillis = millis();
}
/*
* set to 50% duty cycle
*/
void BlinkLed::blink(signed int aBlinkCount, unsigned int aPeriod) {
start(aBlinkCount, aPeriod);
while (numberOfBlinks > 0) {
delay(1);
update();
}
}
/*
* No count specified here, so set to BLINK_LED_FOREVER
*/
void BlinkLed::startWithOnOffTime(unsigned int aOnTimeMillis, unsigned int aOffTimeMillis) {
start(BLINK_LED_FOREVER, aOnTimeMillis, aOffTimeMillis);
}
/*
* set to 50% duty cycle
*/
void BlinkLed::startWithPeriod(unsigned int aPeriod) {
start(BLINK_LED_FOREVER, aPeriod);
}
/*
* set to 50% duty cycle
*/
void BlinkLed::startWithFrequency(float aFrequency) {
offDelayMillis = onDelayMillis = 500.0 / aFrequency;
start(BLINK_LED_FOREVER);
}
void BlinkLed::startWithOnTime(unsigned int aOnTimeMillis) {
onDelayMillis = aOnTimeMillis;
start();
}
void BlinkLed::startWithOffTime(unsigned int aOffTimeMillis) {
offDelayMillis = aOffTimeMillis;
start();
}
// Toggle state and set new timestamp
void BlinkLed::toggle() {
state = !state;
digitalWrite(pin, state);
lastUpdateMillis = millis();
}
// Force ON and disable blink
void BlinkLed::on() {
digitalWrite(pin, HIGH);
state = true;
enabled = false;
}
// Force off and disable blink
void BlinkLed::stop() {
digitalWrite(pin, LOW);
state = false;
enabled = false;
}
// Force off and disable blink - the same as stop
void BlinkLed::off() {
digitalWrite(pin, LOW);
state = false;
enabled = false;
}
/*
* Might be useful if using negative logic i.e. LED is connected to VCC.
* You can also access "enabled" directly, but this is not the recommended way to use a class ;-)
*/
void BlinkLed::setEnabled(bool aIsEnabled) {
enabled = aIsEnabled;
}