-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLEDController.cpp
211 lines (156 loc) · 5.33 KB
/
LEDController.cpp
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
200
201
202
203
204
205
206
207
208
209
210
211
#include "LEDController.h"
LEDController::LEDController() {}
LEDController::~LEDController() {
if (led) delete led;
}
void LEDController::setup() {
#ifdef DIGITAL_LED
led = new DigitalLED(LED_PIN_R, LED_PIN_G, LED_PIN_B, LED_ANODE);
led->setup();
return;
#endif // ifdef DIGITAL_LED
#ifdef RGB_LED
led = new LEDController::AnalogRGBLED(LED_PIN_R, LED_PIN_G, LED_PIN_B, LED_MODE_BRIGHTNESS, LED_ANODE);
led->setup();
return;
#endif // ifdef RGB_LED
#ifdef NEOPIXEL_LED
led = new LEDController::NeopixelLED(LED_NEOPIXEL_NUM, LED_NEOPIXEL_PIN, LED_MODE_BRIGHTNESS);
led->setup();
return;
#endif // ifdef NEOPIXEL_LED
prntln(L_NOT_CONFIGURED);
}
void LEDController::update() {
if (!tempEnabled || !led) return;
if (!settings.getLedEnabled() && tempEnabled) tempDisable();
if (scan.isScanning() && (scan.deauths < settings.getMinDeauths())) setMode(LED_MODE::SCAN, false);
else if (scan.deauths >= settings.getMinDeauths()) setMode(LED_MODE::DEAUTH, false);
else if (attack.isRunning()) setMode(LED_MODE::ATTACK, false);
else setMode(LED_MODE::IDLE, false);
}
void LEDController::setMode(uint8_t mode, bool force) {
if (!led) return;
if ((mode != LEDController::mode) || force) {
LEDController::mode = mode;
switch (mode) {
case LED_MODE::OFF:
led->setColor(0, 0, 0);
break;
case LED_MODE::SCAN:
led->setColor(0, 0, 255);
break;
case LED_MODE::ATTACK:
led->setColor(255, 255, 0);
break;
case LED_MODE::DEAUTH:
led->setColor(255, 0, 0);
break;
case LED_MODE::IDLE:
led->setColor(0, 255, 0);
break;
}
}
}
void LEDController::setColor(uint8_t r, uint8_t g, uint8_t b, bool output) {
// debug output
if (output) {
char s[30];
sprintf_P(s, L_OUTPUT, r, g, b);
prnt(String(s));
}
led->setColor(r, g, b);
}
void LEDController::setColor(uint8_t r, uint8_t g, uint8_t b, uint8_t brightness, bool output) {
led->setBrightness(brightness);
setColor(r, g, b, output);
}
void LEDController::tempEnable() {
tempEnabled = true;
prntln(L_ENABLED);
}
void LEDController::tempDisable() {
tempEnabled = false;
prntln(L_DISABLED);
}
bool LEDController::getTempEnabled() {
return tempEnabled;
}
// ===== DigitalLED ===== //
LEDController::DigitalLED::DigitalLED(uint8_t rPin, uint8_t gPin, uint8_t bPin, bool anode) {
LEDController::DigitalLED::anode = anode;
LEDController::DigitalLED::rPin = rPin;
LEDController::DigitalLED::gPin = gPin;
LEDController::DigitalLED::bPin = bPin;
}
LEDController::DigitalLED::~DigitalLED() {}
void LEDController::DigitalLED::setup() {
if (rPin < 255) pinMode(rPin, OUTPUT);
if (gPin < 255) pinMode(gPin, OUTPUT);
if (bPin < 255) pinMode(bPin, OUTPUT);
}
void LEDController::DigitalLED::setColor(uint8_t r, uint8_t g, uint8_t b) {
if (anode) {
if (rPin < 255) digitalWrite(rPin, r > 0);
if (gPin < 255) digitalWrite(gPin, g > 0);
if (bPin < 255) digitalWrite(bPin, b > 0);
} else {
if (rPin < 255) digitalWrite(rPin, r == 0);
if (gPin < 255) digitalWrite(gPin, g == 0);
if (bPin < 255) digitalWrite(bPin, b == 0);
}
}
void LEDController::DigitalLED::setBrightness(uint8_t brightness) {}
// ===== AnalogRGBLED ===== //
LEDController::AnalogRGBLED::AnalogRGBLED(uint8_t rPin, uint8_t gPin, uint8_t bPin, uint8_t brightness, bool anode) {
LEDController::AnalogRGBLED::anode = anode;
LEDController::AnalogRGBLED::rPin = rPin;
LEDController::AnalogRGBLED::gPin = gPin;
LEDController::AnalogRGBLED::bPin = bPin;
setBrightness(brightness);
}
LEDController::AnalogRGBLED::~AnalogRGBLED() {}
void LEDController::AnalogRGBLED::setup() {
analogWriteRange(0xff);
if (rPin < 255) pinMode(rPin, OUTPUT);
if (gPin < 255) pinMode(gPin, OUTPUT);
if (bPin < 255) pinMode(bPin, OUTPUT);
}
void LEDController::AnalogRGBLED::setColor(uint8_t r, uint8_t g, uint8_t b) {
if ((r > 0) && (brightness < 100)) r = r * brightness / 100;
if ((g > 0) && (brightness < 100)) g = g * brightness / 100;
if ((b > 0) && (brightness < 100)) b = b * brightness / 100;
if (anode) {
r = 255 - r;
g = 255 - g;
b = 255 - b;
}
analogWrite(rPin, r);
analogWrite(gPin, g);
analogWrite(bPin, b);
}
void LEDController::AnalogRGBLED::setBrightness(uint8_t brightness) {
if (brightness > 100) brightness = 100;
LEDController::AnalogRGBLED::brightness = brightness;
}
// ===== NeopixelLED ===== //
LEDController::NeopixelLED::NeopixelLED(int num, uint8_t dataPin, uint8_t brightness) {
strip = new Adafruit_NeoPixel(num, dataPin, LED_NEOPIXEL_MODE);
setBrightness(brightness);
}
LEDController::NeopixelLED::~NeopixelLED() {
delete strip;
}
void LEDController::NeopixelLED::setup() {
strip->begin();
strip->show();
}
void LEDController::NeopixelLED::setColor(uint8_t r, uint8_t g, uint8_t b) {
int num = strip->numPixels();
for (uint16_t i = 0; i < num; i++) strip->setPixelColor(i, strip->Color(r, g, b));
strip->show();
}
void LEDController::NeopixelLED::setBrightness(uint8_t brightness) {
if (brightness > 100) brightness = 100;
strip->setBrightness(brightness);
}