-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleds.cpp
More file actions
120 lines (103 loc) · 3.57 KB
/
leds.cpp
File metadata and controls
120 lines (103 loc) · 3.57 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
/*
* This file is part of LynxGradProject.
*
* LynxGradProject 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.
*
* LynxGradProject 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 LynxGradProject. If not, see <http://www.gnu.org/licenses/>.
*/
#include "leds.h"
#include <ChibiOS_AVR.h>
bool LEDs::s_Initialized = false;
LEDs::BlinkType LEDs::s_BlinkType[c_LEDCount] = {BlinkOff, BlinkOff};
unsigned long LEDs::s_BlinkTime[c_LEDCount][4] = {{0, 0, 0, 0}, {0, 0, 0, 0}};
bool LEDs::s_State[c_LEDCount] = {false, false};
byte LEDs::s_BlinkStage[c_LEDCount] = {0, 0};
const byte LEDs::c_nLEDPin[c_LEDCount] = {10, 8};
LEDs::LEDs()
{
if (s_Initialized == false) {
s_Initialized = true;
for (byte index = 0; index < c_LEDCount; index++) {
pinMode(c_nLEDPin[index], OUTPUT);
turnOff((LEDColor)index);
}
}
}
LEDs::~LEDs()
{
}
void LEDs::setPattern(LEDs::LEDColor color, LEDs::BlinkType blinkType)
{
if (blinkType == BlinkOff) {
s_State[color] = false;
} else if (blinkType == BlinkNone) {
s_State[color] = true;
} else if (blinkType == Blink2Short) {
s_BlinkTime[color][0] = c_Blink2ShortOnTime1;
s_BlinkTime[color][1] = c_Blink2ShortOffTime1;
s_BlinkTime[color][2] = c_Blink2ShortOnTime2;
s_BlinkTime[color][3] = c_Blink2ShortOffTime2;
s_State[color] = true;
} else if (blinkType == Blink50DC) {
s_BlinkTime[color][0] = c_Blink50DCOnTime1;
s_BlinkTime[color][1] = c_Blink50DCOffTime1;
s_BlinkTime[color][2] = c_Blink50DCOnTime1;
s_BlinkTime[color][3] = c_Blink50DCOffTime1;
s_State[color] = true;
} else if (blinkType == Blink2ShortStop) {
s_BlinkTime[color][0] = c_Blink2ShortStopOnTime1;
s_BlinkTime[color][1] = c_Blink2ShortStopOffTime1;
s_BlinkTime[color][2] = c_Blink2ShortStopOnTime2;
s_BlinkTime[color][3] = c_Blink2ShortStopOffTime2;
s_State[color] = true;
} else {
return;
}
s_BlinkType[color] = blinkType;
s_BlinkStage[color] = 0;
digitalWrite(c_nLEDPin[color], !s_State[color]);
}
void LEDs::turnOn(LEDs::LEDColor color)
{
setPattern(color, BlinkNone);
}
void LEDs::turnOff(LEDs::LEDColor color)
{
setPattern(color, BlinkOff);
}
void LEDs::run(LEDs::LEDColor color)
{
while (1) {
if (s_BlinkType[color] == BlinkNone) {
s_State[color] = true;
digitalWrite(c_nLEDPin[color], !s_State[color]);
chThdSleepMilliseconds(50);
continue;
}
if (s_BlinkType[color] == BlinkOff) {
s_State[color] = false;
digitalWrite(c_nLEDPin[color], !s_State[color]);
chThdSleepMilliseconds(50);
continue;
}
chThdSleepMilliseconds(s_BlinkTime[color][s_BlinkStage[color]]);
s_State[color] = !s_State[color];
s_BlinkStage[color]++;
if (s_BlinkStage[color] > 3) {
if (s_BlinkType[color] == Blink2ShortStop) {
turnOff((LEDColor)color);
}
s_BlinkStage[color] = 0;
}
digitalWrite(c_nLEDPin[color], !s_State[color]);
}
}