-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBlinker.cpp
More file actions
66 lines (61 loc) · 1.7 KB
/
Blinker.cpp
File metadata and controls
66 lines (61 loc) · 1.7 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
// Modbus bridge device V3
// Copyright 2020 by miq1@gmx.de
#include "Blinker.h"
// Constructor: takes GPIO of LED to handle
Blinker::Blinker(uint8_t port, bool onState) :
B_port(port),
B_onState(onState) {
pinMode(port, OUTPUT);
stop();
}
// start: in interval steps, loop over blinking pattern
uint32_t Blinker::start(uint16_t pattern, uint32_t interval) {
B_interval = interval;
B_pattern = pattern;
B_pLength = 16;
// Shift B_pattern left until the first '1' bit is found
while (B_pLength && !(B_pattern & 0x8000)) {
B_pattern <<= 1;
B_pLength --;
}
B_pWork = B_pattern;
B_counter = 0;
B_lastTick = millis();
return B_lastTick + B_interval;
}
// stop: stop blinking
void Blinker::stop() {
B_lastTick = 0;
B_interval = 0;
B_pattern = 0;
B_counter = 0;
digitalWrite(B_port, !B_onState);
}
// update: check if the blinking pattern needs to be advanced a step
void Blinker::update() {
// Do we have a valid interval?
if (B_interval) {
// Yes. Has it passed?
if (millis() - B_lastTick > B_interval) {
// Yes. get the current state of the LED pin
bool state = digitalRead(B_port);
// Does the pattern require an ON?
if (B_pWork & 0x8000) {
// Yes. switch LED ON, if was OFF
if (state != B_onState) digitalWrite(B_port, B_onState);
} else {
// No, OFF requested. switch LED OFF, if was ON
if (state == B_onState) digitalWrite(B_port, !B_onState);
}
// Advance pattern
B_pWork <<= 1;
// Overflow?
if (++B_counter == B_pLength) {
// Yes. Start again with the pattern
B_counter = 0;
B_pWork = B_pattern;
}
B_lastTick = millis();
}
}
}