From 46ffccb6bcd08ff80469e7cbc4da5a4d733b38d6 Mon Sep 17 00:00:00 2001 From: Stefan Kremser Date: Fri, 10 May 2019 13:11:37 +0200 Subject: [PATCH] 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. --- esp8266_deauther/A_config.h | 35 +- esp8266_deauther/LED.cpp | 117 ++-- esp8266_deauther/LED.h | 97 +-- esp8266_deauther/language.h | 1176 +++++++++++++++++------------------ 4 files changed, 711 insertions(+), 714 deletions(-) diff --git a/esp8266_deauther/A_config.h b/esp8266_deauther/A_config.h index ef5881a..981ee54 100644 --- a/esp8266_deauther/A_config.h +++ b/esp8266_deauther/A_config.h @@ -179,6 +179,10 @@ // ===== LED ===== // #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_MY92_NUM 1 @@ -195,6 +199,10 @@ // ===== LED ===== // #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_MY92_NUM 1 @@ -248,6 +256,22 @@ #define LED_ANODE false #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 #define LED_MODE_BRIGHTNESS 10 #endif /* ifndef LED_MODE_BRIGHTNESS */ @@ -333,7 +357,11 @@ #define LED_NEOPIXEL_NUM 1 #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_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 */ \ No newline at end of file diff --git a/esp8266_deauther/LED.cpp b/esp8266_deauther/LED.cpp index f667f10..313814f 100644 --- a/esp8266_deauther/LED.cpp +++ b/esp8266_deauther/LED.cpp @@ -1,25 +1,91 @@ #include "LED.h" -// Strings used in printColor and tempDisable -#include "language.h" - -// For Update() +// ===== [Includes] ===== // +// used for update() #include "Settings.h" #include "Attack.h" #include "Scan.h" +// ===== [External] ===== // +// used for update() extern Settings settings; extern Attack attack; extern Scan scan; -void LED::update() { - if (!tempEnabled) return; +void LED::setColor(uint8_t r, uint8_t g, uint8_t b, bool output) { + if (output) { + char s[30]; - if (!settings.getLedEnabled() && tempEnabled) { - tempDisable(); + sprintf_P(s, L_OUTPUT, r, g, b); + 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); } else if (attack.isRunning() || (scan.deauths >= settings.getMinDeauths())) { 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) { if ((mode != this->mode) || force) { this->mode = mode; switch (mode) { case OFF: - setColor(0, 0, 0); + setColor(LED_MODE_OFF); break; case SCAN: - setColor(0, 0, 255); + setColor(LED_MODE_SCAN); break; case ATTACK: - setColor(255, 0, 0); + setColor(LED_MODE_ATTACK); break; case IDLE: - setColor(0, 255, 0); + setColor(LED_MODE_IDLE); 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; } \ No newline at end of file diff --git a/esp8266_deauther/LED.h b/esp8266_deauther/LED.h index 9802b46..0ab94ec 100644 --- a/esp8266_deauther/LED.h +++ b/esp8266_deauther/LED.h @@ -1,8 +1,12 @@ #ifndef LED_h #define LED_h +// ===== [Includes] ===== // #include "Arduino.h" // digitalWrite, analogWrite, pinMode #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 #if defined(NEOPIXEL_LED) @@ -11,6 +15,10 @@ #include #endif // if defined(NEOPIXEL_LED) +// ===== [Strings] ===== // +const char L_OUTPUT[] PROGMEM = "LED = (%u,%u,%u)"; + +// ===== [LED Mode Enum] ===== // enum LED_MODE { OFF, SCAN, @@ -18,12 +26,10 @@ enum LED_MODE { IDLE }; +// ===== [LED Class] ===== // class LED { private: - bool tempEnabled = true; - LED_MODE mode = OFF; - - uint8_t brightness = 100; + LED_MODE mode = OFF; #if defined(LED_NEOPIXEL_RGB) 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); #endif // if defined(NEOPIXEL_LED) + void setColor(uint8_t r, uint8_t g, uint8_t b, bool output = false); + public: - 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 setup(); void update(); - - void printColor(uint8_t r, uint8_t g, uint8_t b); - 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 \ No newline at end of file diff --git a/esp8266_deauther/language.h b/esp8266_deauther/language.h index dcb1560..e621152 100644 --- a/esp8266_deauther/language.h +++ b/esp8266_deauther/language.h @@ -1,591 +1,585 @@ -#ifndef language_h -#define language_h - -#include "Arduino.h" - -extern String str(const char* ptr); -extern String keyword(const char* keywordPtr); -extern bool eqls(const char* str, const char* keywordPtr); -extern bool eqls(String str, const char* keywordPtr); -extern String b2s(bool input); -extern String b2a(bool input); -extern bool s2b(String input); -extern void prnt(String s); -extern void prnt(bool b); -extern void prnt(char c); -extern void prnt(const char* ptr); -extern void prnt(int i); -extern void prntln(); -extern void prntln(String s); -extern void prntln(bool b); -extern void prntln(char c); -extern void prntln(const char* ptr); -extern void prntln(int i); - -/* - The following variables are the strings used for the serial interface, display interface and settings. - The keywords for the serial CLI have a simple structure to save a bit of memory and CPU time: - - every keyword has a unique string - - / is used for optional characters, i.e. 'enable/d' makes 'enable' and 'enabled' - - , is used for seperations, i.e. 'select/ed,-s' makes 'select', 'selected' and '-s' - - everything is in lowercase - */ - -// ===== GLOBAL STRINGS ===== // - -// Often used characters, therefor in the RAM -const char CURSOR = '|'; -const char SPACE = ' '; -const char DOUBLEPOINT = ':'; -const char EQUALS = '='; -const char HASHSIGN = '#'; -const char ASTERIX = '*'; -const char PERCENT = '%'; -const char DASH = '-'; -const char QUESTIONMARK = '?'; -const char ZERO = '0'; -const char S = 's'; -const char M = 'm'; -const char D = 'd'; -const char DOUBLEQUOTES = '\"'; -const char SLASH = '/'; -const char NEWLINE = '\n'; -const char CARRIAGERETURN = '\r'; -const char SEMICOLON = ';'; -const char BACKSLASH = '\\'; -const char POINT = '.'; -const char VERTICALBAR = '|'; -const char COMMA = ','; -const char ENDOFLINE = '\0'; -const char OPEN_BRACKET = '['; -const char CLOSE_BRACKET = ']'; -const char OPEN_CURLY_BRACKET = '{'; -const char CLOSE_CURLY_BRACKET = '}'; - -const char STR_TRUE[] PROGMEM = "true"; -const char STR_FALSE[] PROGMEM = "false"; -const char STR_MIN[] PROGMEM = "min"; - -// ===== SETUP ===== // -const char SETUP_OK[] PROGMEM = "OK"; -const char SETUP_ERROR[] PROGMEM = "ERROR"; -const char SETUP_MOUNT_SPIFFS[] PROGMEM = "Mounting SPIFFS..."; -const char SETUP_FORMAT_SPIFFS[] PROGMEM = "Formatting SPIFFS..."; -const char SETUP_SERIAL_WARNING[] PROGMEM = "Warning: Serial deactivated"; -const char SETUP_STARTED[] PROGMEM = "STARTED! \\o/"; -const char SETUP_COPYING[] PROGMEM = "Copying "; -const char SETUP_PROGMEM_TO_SPIFFS[] PROGMEM = " from PROGMEM to SPIFFS..."; - -// ===== SERIAL COMMAND LINE INTERFACE ===== // -const char CLI_SCAN[] PROGMEM = "scan"; // scan -const char CLI_REBOOT[] PROGMEM = "reboot"; // reboot -const char CLI_STATUS[] PROGMEM = "status"; // status -const char CLI_SHOW[] PROGMEM = "show"; // show -const char CLI_REMOVE[] PROGMEM = "remove"; // remove -const char CLI_SET[] PROGMEM = "set"; // set -const char CLI_STOP[] PROGMEM = "stop"; // stop -const char CLI_LOAD[] PROGMEM = "load"; // load -const char CLI_SAVE[] PROGMEM = "save"; // save -const char CLI_ADD[] PROGMEM = "add"; // add -const char CLI_DESELECT[] PROGMEM = "deselect"; // deselect -const char CLI_CLEAR[] PROGMEM = "clear"; // clear -const char CLI_SYSINFO[] PROGMEM = "sysinfo"; // sysinfo -const char CLI_RESET[] PROGMEM = "reset"; // reset -const char CLI_ON[] PROGMEM = "on"; // on -const char CLI_OFF[] PROGMEM = "off"; // off -const char CLI_RANDOM[] PROGMEM = "random"; // random -const char CLI_GET[] PROGMEM = "get"; // get -const char CLI_INFO[] PROGMEM = "info"; // info -const char CLI_HELP[] PROGMEM = "help"; // help -const char CLI_RICE[] PROGMEM = "rice"; // rice -const char CLI_FORMAT[] PROGMEM = "format"; // format -const char CLI_DELETE[] PROGMEM = "delete"; // delete -const char CLI_PRINT[] PROGMEM = "print"; // print -const char CLI_RUN[] PROGMEM = "run"; // run -const char CLI_WRITE[] PROGMEM = "write"; // write -const char CLI_LED[] PROGMEM = "led"; // led -const char CLI_SEND[] PROGMEM = "send"; // send -const char CLI_CUSTOM[] PROGMEM = "custom"; // custom -const char CLI_DELAY[] PROGMEM = "delay"; // delay -const char CLI_REPLACE[] PROGMEM = "replace"; // replace -const char CLI_DRAW[] PROGMEM = "draw"; // draw -const char CLI_SCRIPT[] PROGMEM = "script"; // script -const char CLI_STARTAP[] PROGMEM = "startap"; // startap -const char CLI_STOPAP[] PROGMEM = "stopap"; // stopap -const char CLI_RENAME[] PROGMEM = "rename"; // rename -const char CLI_COPY[] PROGMEM = "copy"; // copy -const char CLI_ENABLE[] PROGMEM = "enable/d"; // enable, enabled -const char CLI_DISABLE[] PROGMEM = "disable/d"; // disable, disabled -const char CLI_WPA2[] PROGMEM = "wpa/2,-wpa/2"; // wpa, wpa2, -wpa, -wpa2 -const char CLI_ATTACK[] PROGMEM = "attack/s"; // attack, attacks -const char CLI_CHICKEN[] PROGMEM = "chicken/s"; // chicken, chickens -const char CLI_SETTING[] PROGMEM = "setting/s"; // setting, settings -const char CLI_ID[] PROGMEM = "id,-i/d"; // id, -i, -id -const char CLI_ALL[] PROGMEM = "all,-a"; // all, -a -const char CLI_TIME[] PROGMEM = "time,-t"; // time, -t -const char CLI_CONTINUE[] PROGMEM = "continue,-c"; // continue, -c -const char CLI_CHANNEL[] PROGMEM = "channel,-ch"; // channel, -ch -const char CLI_MAC[] PROGMEM = "mac,-m"; // mac, -m -const char CLI_BSSID[] PROGMEM = "bssid,-b"; // bssid, -b -const char CLI_BEACON[] PROGMEM = "beacon,-b"; // bssid, -b -const char CLI_DEAUTH[] PROGMEM = "deauth,-d"; // deauth, -d -const char CLI_DEAUTHALL[] PROGMEM = "deauthall,-da"; // deauthall, -da -const char CLI_PROBE[] PROGMEM = "probe,-p"; // probe, -p -const char CLI_NOOUTPUT[] PROGMEM = "nooutput,-no"; // nooutput, -no -const char CLI_FORCE[] PROGMEM = "force,-f"; // force, -f -const char CLI_TIMEOUT[] PROGMEM = "timeout,-t"; // timeout, -t -const char CLI_WIFI[] PROGMEM = "wifi,-w"; // wifi, -w -const char CLI_CLONES[] PROGMEM = "clones,-cl"; // clones, -cl -const char CLI_PATH[] PROGMEM = "path,-p"; // path, -p -const char CLI_PASSWORD[] PROGMEM = "password,-ps/wd"; // password, -ps, -pswd -const char CLI_HIDDEN[] PROGMEM = "hidden,-h"; // hidden, -h -const char CLI_CAPTIVEPORTAL[] PROGMEM = "captiveportal,-cp"; // captiveportal, -cp -const char CLI_SELECT[] PROGMEM = "select/ed,-s"; // select, selected, -s -const char CLI_SSID[] PROGMEM = "ssid/s,-s/s"; // ssid, ssids, -s, -ss -const char CLI_AP[] PROGMEM = "ap/s,-ap/s"; // ap, aps, -ap, -aps -const char CLI_STATION[] PROGMEM = "station/s,-st"; // station, stations, -st -const char CLI_NAME[] PROGMEM = "name/s,-n"; // name, names, -n -const char CLI_LINE[] PROGMEM = "line/s,-l"; // line, lines, -l -const char CLI_COMMENT[] PROGMEM = "//"; // // -const char CLI_SCREEN[] PROGMEM = "screen"; // screen -const char CLI_MODE[] PROGMEM = "mode,-m"; // mode -const char CLI_MODE_BUTTONTEST[] PROGMEM = "buttontest"; // buttontest -const char CLI_MODE_PACKETMONITOR[] PROGMEM = "packetmonitor"; // packetmonitor -const char CLI_MODE_LOADINGSCREEN[] PROGMEM = "loadingscreen"; // loading -const char CLI_MODE_MENU[] PROGMEM = "menu"; // menu - -const char CLI_HELP_HELP[] PROGMEM = "help"; -const char CLI_HELP_SCAN[] PROGMEM = "scan [] [-t