-
Notifications
You must be signed in to change notification settings - Fork 9
/
StatusLed.h
92 lines (80 loc) · 2.24 KB
/
StatusLed.h
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
/*
StatusLed.h -- simple status LED service
Copyright (c) 2017 Alex Shavlovsky. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
//======Status LED service==============
#define STATUS_LED_PIN LED_BUILTIN
#define STATUS_PIN_NOT_SET 255
class StatusLed {
uint8_t pinState=STATUS_PIN_NOT_SET;
void ledOn() {
if(pinState!=0) {
pinState=0;
digitalWrite(STATUS_LED_PIN, LOW);
}
}
void ledOff() {
if(pinState!=1) {
pinState=1;
digitalWrite(STATUS_LED_PIN, HIGH);
}
}
public:
uint8_t mode = 0;
uint32_t interval = 500;
uint8_t onoffratio = 25;
uint32_t timeout = 20;
uint32_t start;
void run() {
if(pinState==STATUS_PIN_NOT_SET) {
pinMode(LED_BUILTIN, OUTPUT);
ledOff();
}
switch(mode) {
case 0:
ledOff();
break;
case 1:
if((!interval)||((millis()-start)%interval<interval*onoffratio/100)) ledOn(); else ledOff();
break;
case 2:
if(millis()-start<timeout) ledOn();
else {
mode=0;
ledOff();
}
break;
}
}
void setOn() {
if(mode==0) {
mode=1;
start=millis();
run();
}
}
void setTimeout() {
if(mode==0) {
mode=2;
start=millis();
run();
}
}
void setOff() {
if(mode==1) {
mode=0;
run();
}
}
};