Updated LED class, config for color modes

- No more led.tempDisable()
- Brightness is fixed and can't be changed
- led.setColor is now private
- setColor with brightness removed
- Removed unused strings from lang

Color modes can be adjusted in A_config now.
Color and Brightness are fixed and can not be changed over CLI anymore.
This commit is contained in:
Stefan Kremser
2019-05-10 13:11:37 +02:00
parent b498f03787
commit 46ffccb6bc
4 changed files with 711 additions and 714 deletions

View File

@@ -179,6 +179,10 @@
// ===== LED ===== // // ===== LED ===== //
#define LED_MY92 #define LED_MY92
#define LED_MODE_OFF 0, 0, 0
#define LED_MODE_SCAN 0, 0, 255
#define LED_MODE_ATTACK 255, 0, 0
#define LED_MODE_IDLE 0, 255, 0
#define LED_MODE_BRIGHTNESS 10 #define LED_MODE_BRIGHTNESS 10
#define LED_MY92_NUM 1 #define LED_MY92_NUM 1
@@ -195,6 +199,10 @@
// ===== LED ===== // // ===== LED ===== //
#define LED_MY92 #define LED_MY92
#define LED_MODE_OFF 0, 0, 0
#define LED_MODE_SCAN 0, 0, 255
#define LED_MODE_ATTACK 255, 0, 0
#define LED_MODE_IDLE 0, 255, 0
#define LED_MODE_BRIGHTNESS 10 #define LED_MODE_BRIGHTNESS 10
#define LED_MY92_NUM 1 #define LED_MY92_NUM 1
@@ -248,6 +256,22 @@
#define LED_ANODE false #define LED_ANODE false
#endif /* ifndef LED_ANODE */ #endif /* ifndef LED_ANODE */
#ifndef LED_MODE_OFF
#define LED_MODE_OFF 0, 0, 0
#endif /* ifndef LED_MODE_OFF */
#ifndef LED_MODE_SCAN
#define LED_MODE_SCAN 0, 0, 255
#endif /* ifndef LED_MODE_SCAN */
#ifndef LED_MODE_ATTACK
#define LED_MODE_ATTACK 255, 0, 0
#endif /* ifndef LED_MODE_ATTACK */
#ifndef LED_MODE_IDLE
#define LED_MODE_IDLE 0, 255, 0
#endif /* ifndef LED_MODE_IDLE */
#ifndef LED_MODE_BRIGHTNESS #ifndef LED_MODE_BRIGHTNESS
#define LED_MODE_BRIGHTNESS 10 #define LED_MODE_BRIGHTNESS 10
#endif /* ifndef LED_MODE_BRIGHTNESS */ #endif /* ifndef LED_MODE_BRIGHTNESS */
@@ -333,7 +357,11 @@
#define LED_NEOPIXEL_NUM 1 #define LED_NEOPIXEL_NUM 1
#define LED_NEOPIXEL_PIN 255 #define LED_NEOPIXEL_PIN 255
#define LED_MODE_BRIGHTNESS 100 #define LED_MODE_OFF 0,0,0
#define LED_MODE_SCAN 0,0,255
#define LED_MODE_ATTACK 255,0,0
#define LED_MODE_IDLE 0,255,0
#define LED_MODE_BRIGHTNESS 10
#define LED_MY92_NUM 1 #define LED_MY92_NUM 1
#define LED_MY92_DATA 4 #define LED_MY92_DATA 4
@@ -370,4 +398,9 @@
*/ */
// ========== ERROR CHECKS ========== //
#if LED_MODE_BRIGHTNESS == 0
#error LED_MODE_BRIGHTNESS must not be zero!
#endif /* if LED_MODE_BRIGHTNESS == 0 */
#endif /* ifndef config_h */ #endif /* ifndef config_h */

View File

@@ -1,25 +1,91 @@
#include "LED.h" #include "LED.h"
// Strings used in printColor and tempDisable // ===== [Includes] ===== //
#include "language.h" // used for update()
// For Update()
#include "Settings.h" #include "Settings.h"
#include "Attack.h" #include "Attack.h"
#include "Scan.h" #include "Scan.h"
// ===== [External] ===== //
// used for update()
extern Settings settings; extern Settings settings;
extern Attack attack; extern Attack attack;
extern Scan scan; extern Scan scan;
void LED::update() { void LED::setColor(uint8_t r, uint8_t g, uint8_t b, bool output) {
if (!tempEnabled) return; if (output) {
char s[30];
if (!settings.getLedEnabled() && tempEnabled) { sprintf_P(s, L_OUTPUT, r, g, b);
tempDisable(); prnt(String(s));
} }
if (scan.isScanning() && (scan.deauths < settings.getMinDeauths())) { #if defined(LED_DIGITAL)
if (LED_ANODE) {
if (LED_PIN_R < 255) digitalWrite(LED_PIN_R, r > 0);
if (LED_PIN_G < 255) digitalWrite(LED_PIN_G, g > 0);
if (LED_PIN_B < 255) digitalWrite(LED_PIN_B, b > 0);
} else {
if (LED_PIN_R < 255) digitalWrite(LED_PIN_R, r == 0);
if (LED_PIN_G < 255) digitalWrite(LED_PIN_G, g == 0);
if (LED_PIN_B < 255) digitalWrite(LED_PIN_B, b == 0);
}
#elif defined(LED_RGB)
if (r > 0) r = r * LED_MODE_BRIGHTNESS / 100;
if (g > 0) g = g * LED_MODE_BRIGHTNESS / 100;
if (b > 0) b = b * LED_MODE_BRIGHTNESS / 100;
if (LED_ANODE) {
r = 255 - r;
g = 255 - g;
b = 255 - b;
}
analogWrite(LED_PIN_R, r);
analogWrite(LED_PIN_G, g);
analogWrite(LED_PIN_B, b);
#elif defined(NEOPIXEL_LED)
for (size_t i = 0; i < LED_NEOPIXEL_NUM; i++) {
strip.setPixelColor(i, r, g, b);
}
strip.show();
#elif defined(LED_MY9291)
myled.setChannel(LED_MY92_CH_R, r);
myled.setChannel(LED_MY92_CH_G, g);
myled.setChannel(LED_MY92_CH_B, b);
myled.setChannel(LED_MY92_CH_BRIGHTNESS, LED_MODE_BRIGHTNESS);
myled.setState(true);
myled.update();
#endif // if defined(LED_DIGITAL)
}
void LED::setup() {
analogWriteRange(0xff);
#if defined(LED_DIGITAL) || defined(LED_RGB)
if (LED_PIN_R < 255) pinMode(LED_PIN_R, OUTPUT);
if (LED_PIN_G < 255) pinMode(LED_PIN_G, OUTPUT);
if (LED_PIN_B < 255) pinMode(LED_PIN_B, OUTPUT);
#elif defined(NEOPIXEL_LED)
strip.begin();
strip.setBrightness(LED_MODE_BRIGHTNESS);
strip.show();
#elif defined(LED_MY9291)
myled.setChannel(LED_MY92_CH_R, 0);
myled.setChannel(LED_MY92_CH_G, 0);
myled.setChannel(LED_MY92_CH_B, 0);
myled.setChannel(LED_MY92_CH_BRIGHTNESS, LED_MODE_BRIGHTNESS);
myled.setState(true);
myled.update();
#endif // if defined(LED_DIGITAL) || defined(LED_RGB)
}
void LED::update() {
if (!settings.getLedEnabled()) {
setMode(OFF);
} else if (scan.isScanning() && (scan.deauths < settings.getMinDeauths())) {
setMode(SCAN); setMode(SCAN);
} else if (attack.isRunning() || (scan.deauths >= settings.getMinDeauths())) { } else if (attack.isRunning() || (scan.deauths >= settings.getMinDeauths())) {
setMode(ATTACK); setMode(ATTACK);
@@ -28,51 +94,26 @@ void LED::update() {
} }
} }
void LED::printColor(uint8_t r, uint8_t g, uint8_t b) {
char s[30];
sprintf_P(s, L_OUTPUT, r, g, b);
prnt(String(s));
}
void LED::setMode(LED_MODE mode, bool force) { void LED::setMode(LED_MODE mode, bool force) {
if ((mode != this->mode) || force) { if ((mode != this->mode) || force) {
this->mode = mode; this->mode = mode;
switch (mode) { switch (mode) {
case OFF: case OFF:
setColor(0, 0, 0); setColor(LED_MODE_OFF);
break; break;
case SCAN: case SCAN:
setColor(0, 0, 255); setColor(LED_MODE_SCAN);
break; break;
case ATTACK: case ATTACK:
setColor(255, 0, 0); setColor(LED_MODE_ATTACK);
break; break;
case IDLE: case IDLE:
setColor(0, 255, 0); setColor(LED_MODE_IDLE);
break; break;
} }
} }
}
void LED::setBrightness(uint8_t brightness) {
this->brightness = brightness % 100;
}
void LED::tempEnable() {
tempEnabled = true;
prntln(L_ENABLED);
}
void LED::tempDisable() {
tempEnabled = false;
prntln(L_DISABLED);
}
bool LED::getTempEnabled() {
return tempEnabled;
} }

View File

@@ -1,8 +1,12 @@
#ifndef LED_h #ifndef LED_h
#define LED_h #define LED_h
// ===== [Includes] ===== //
#include "Arduino.h" // digitalWrite, analogWrite, pinMode #include "Arduino.h" // digitalWrite, analogWrite, pinMode
#include "A_config.h" // Config for LEDs #include "A_config.h" // Config for LEDs
#include "language.h" // Strings used in printColor and tempDisable
// ===== [Defines] ===== //
// Inlcude libraries for Neopixel or LED_MY92xx if used // Inlcude libraries for Neopixel or LED_MY92xx if used
#if defined(NEOPIXEL_LED) #if defined(NEOPIXEL_LED)
@@ -11,6 +15,10 @@
#include <my92xx.h> #include <my92xx.h>
#endif // if defined(NEOPIXEL_LED) #endif // if defined(NEOPIXEL_LED)
// ===== [Strings] ===== //
const char L_OUTPUT[] PROGMEM = "LED = (%u,%u,%u)";
// ===== [LED Mode Enum] ===== //
enum LED_MODE { enum LED_MODE {
OFF, OFF,
SCAN, SCAN,
@@ -18,12 +26,10 @@ enum LED_MODE {
IDLE IDLE
}; };
// ===== [LED Class] ===== //
class LED { class LED {
private: private:
bool tempEnabled = true; LED_MODE mode = OFF;
LED_MODE mode = OFF;
uint8_t brightness = 100;
#if defined(LED_NEOPIXEL_RGB) #if defined(LED_NEOPIXEL_RGB)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_NEOPIXEL_NUM, LED_NEOPIXEL_PIN, NEO_RGB + NEO_KHZ400); Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_NEOPIXEL_NUM, LED_NEOPIXEL_PIN, NEO_RGB + NEO_KHZ400);
@@ -33,89 +39,12 @@ class LED {
my92xx myled = my92xx(LED_MY92_MODEL, LED_MY92_NUM, LED_MY92_DATA, LED_MY92_CLK, MY92XX_COMMAND_DEFAULT); my92xx myled = my92xx(LED_MY92_MODEL, LED_MY92_NUM, LED_MY92_DATA, LED_MY92_CLK, MY92XX_COMMAND_DEFAULT);
#endif // if defined(NEOPIXEL_LED) #endif // if defined(NEOPIXEL_LED)
void setColor(uint8_t r, uint8_t g, uint8_t b, bool output = false);
public: public:
void setup() { void setup();
analogWriteRange(0xff);
brightness = LED_MODE_BRIGHTNESS;
#if defined(LED_DIGITAL) || defined(LED_RGB)
if (LED_PIN_R < 255) pinMode(LED_PIN_R, OUTPUT);
if (LED_PIN_G < 255) pinMode(LED_PIN_G, OUTPUT);
if (LED_PIN_B < 255) pinMode(LED_PIN_B, OUTPUT);
#elif defined(NEOPIXEL_LED)
strip.begin();
strip.setBrightness(brightness);
strip.show();
#elif defined(LED_MY9291)
myled.setChannel(LED_MY92_CH_R, 0);
myled.setChannel(LED_MY92_CH_G, 0);
myled.setChannel(LED_MY92_CH_B, 0);
myled.setChannel(LED_MY92_CH_BRIGHTNESS, brightness);
myled.setState(true);
myled.update();
#endif // if defined(LED_DIGITAL) || defined(LED_RGB)
}
void update(); void update();
void printColor(uint8_t r, uint8_t g, uint8_t b);
void setMode(LED_MODE mode, bool force = false); void setMode(LED_MODE mode, bool force = false);
void setBrightness(uint8_t brightness);
void setColor(uint8_t r, uint8_t g, uint8_t b, uint8_t brightness, bool output = false) {
setBrightness(brightness);
setColor(r, g, b, output);
}
void setColor(uint8_t r, uint8_t g, uint8_t b, bool output = false) {
if (output) printColor(r, g, b);
#if defined(LED_DIGITAL)
if (LED_ANODE) {
if (LED_PIN_R < 255) digitalWrite(LED_PIN_R, r > 0);
if (LED_PIN_G < 255) digitalWrite(LED_PIN_G, g > 0);
if (LED_PIN_B < 255) digitalWrite(LED_PIN_B, b > 0);
} else {
if (LED_PIN_R < 255) digitalWrite(LED_PIN_R, r == 0);
if (LED_PIN_G < 255) digitalWrite(LED_PIN_G, g == 0);
if (LED_PIN_B < 255) digitalWrite(LED_PIN_B, b == 0);
}
#elif defined(LED_RGB)
if ((r > 0) && (brightness > 0)) r = r * brightness / 100;
if ((g > 0) && (brightness > 0)) g = g * brightness / 100;
if ((b > 0) && (brightness > 0)) b = b * brightness / 100;
if (LED_ANODE) {
r = 255 - r;
g = 255 - g;
b = 255 - b;
}
analogWrite(LED_PIN_R, r);
analogWrite(LED_PIN_G, g);
analogWrite(LED_PIN_B, b);
#elif defined(NEOPIXEL_LED)
for (size_t i = 0; i < LED_NEOPIXEL_NUM; i++) {
strip.setPixelColor(i, r, g, b);
}
strip.show();
#elif defined(LED_MY9291)
myled.setChannel(LED_MY92_CH_R, r);
myled.setChannel(LED_MY92_CH_G, g);
myled.setChannel(LED_MY92_CH_B, b);
myled.setChannel(LED_MY92_CH_BRIGHTNESS, brightness);
myled.setState(true);
myled.update();
#endif // if defined(LED_DIGITAL)
}
void tempEnable();
void tempDisable();
bool getTempEnabled();
}; };
#endif // ifndef LED_h #endif // ifndef LED_h

File diff suppressed because it is too large Load Diff