diff --git a/src/core/config.cpp b/src/core/config.cpp index c741f2337..686c5a0ca 100644 --- a/src/core/config.cpp +++ b/src/core/config.cpp @@ -17,6 +17,9 @@ JsonDocument BruceConfig::toJson() const { setting["soundEnabled"] = soundEnabled; setting["wifiAtStartup"] = wifiAtStartup; + setting["ledBright"] = ledBright; + setting["ledColor"] = String(ledColor, HEX); + JsonObject _webUI = setting.createNestedObject("webUI"); _webUI["user"] = webUI.user; _webUI["pwd"] = webUI.pwd; @@ -99,6 +102,9 @@ void BruceConfig::fromFile() { if(!setting["soundEnabled"].isNull()) { soundEnabled = setting["soundEnabled"].as(); } else { count++; log_e("Fail"); } if(!setting["wifiAtStartup"].isNull()) { wifiAtStartup = setting["wifiAtStartup"].as(); } else { count++; log_e("Fail"); } + if(!setting["ledBright"].isNull()) { ledBright = setting["ledBright"].as(); } else { count++; log_e("Fail"); } + if(!setting["ledColor"].isNull()) { ledColor = strtoul(setting["ledColor"], nullptr, 16); } else { count++; log_e("Fail"); } + if(!setting["webUI"].isNull()) { JsonObject webUIObj = setting["webUI"].as(); webUI.user = webUIObj["user"].as(); @@ -182,7 +188,7 @@ void BruceConfig::saveFile() { file.close(); - if (setupSdCard()) copyToFs(LittleFS, SD, filepath,false); + if (setupSdCard()) copyToFs(LittleFS, SD, filepath, false); } @@ -194,6 +200,8 @@ void BruceConfig::validateConfig() { validateTmzValue(); validateSoundEnabledValue(); validateWifiAtStartupValue(); + validateLedBrightValue(); + validateLedColorValue(); validateRfScanRangeValue(); validateRfModuleValue(); validateRfidModuleValue(); @@ -291,6 +299,30 @@ void BruceConfig::validateWifiAtStartupValue() { } +void BruceConfig::setLedBright(int value) { + ledBright = value; + validateLedBrightValue(); + saveFile(); +} + + +void BruceConfig::validateLedBrightValue() { + ledBright = max(0, min(100, ledBright)); +} + + +void BruceConfig::setLedColor(uint32_t value) { + ledColor = value; + validateLedColorValue(); + saveFile(); +} + + +void BruceConfig::validateLedColorValue() { + ledColor = max((uint32_t)0, min(0xFFFFFFFF, ledColor)); +} + + void BruceConfig::setWebUICreds(const String& usr, const String& pwd) { webUI.user = usr; webUI.pwd = pwd; diff --git a/src/core/config.h b/src/core/config.h index 203a38fa1..e7e3b3599 100644 --- a/src/core/config.h +++ b/src/core/config.h @@ -43,6 +43,7 @@ class BruceConfig { uint16_t secColor = DEFAULT_PRICOLOR-0x2000; uint16_t bgColor = 0x0000; + // Settings int rotation = ROTATION > 1 ? 3 : 1; int dimmerSet = 10; int bright = 100; @@ -50,13 +51,20 @@ class BruceConfig { int soundEnabled = 1; int wifiAtStartup = 0; + // Led + int ledBright = 75; + uint32_t ledColor = 0; + + // Wifi Credential webUI = {"admin", "bruce"}; WiFiCredential wifiAp = {"BruceNet", "brucenet"}; std::map wifi = {}; + // IR int irTx = LED; int irRx = GROVE_SCL; + // RF int rfTx = GROVE_SDA; int rfRx = GROVE_SCL; int rfModule = M5_RF_MODULE; @@ -64,10 +72,13 @@ class BruceConfig { int rfFxdFreq = 1; int rfScanRange = 3; + // RFID int rfidModule = M5_RFID2_MODULE; + // GPS int gpsBaudrate = 9600; + // Misc String startupApp = ""; String wigleBasicToken = ""; int devMode = 0; @@ -75,10 +86,10 @@ class BruceConfig { std::vector disabledMenus = {}; std::vector qrCodes = { - {"Bruce AP", "WIFI:T:WPA;S:BruceNet;P:brucenet;;"}, - {"Bruce Wiki", "https://github.com/pr3y/Bruce/wiki"}, - {"Bruce Site", "https://bruce.computer"}, - {"Rickroll", "https://youtu.be/dQw4w9WgXcQ"} + {"Bruce AP", "WIFI:T:WPA;S:BruceNet;P:brucenet;;"}, + {"Bruce Wiki", "https://github.com/pr3y/Bruce/wiki"}, + {"Bruce Site", "https://bruce.computer"}, + {"Rickroll", "https://youtu.be/dQw4w9WgXcQ"} }; ///////////////////////////////////////////////////////////////////////////////////// @@ -95,9 +106,11 @@ class BruceConfig { void validateConfig(); JsonDocument toJson() const; + // Theme void setTheme(uint16_t primary, uint16_t secondary = NULL, uint16_t background = NULL); void validateTheme(); + // Settings void setRotation(int value); void validateRotationValue(); void setDimmer(int value); @@ -111,6 +124,13 @@ class BruceConfig { void setWifiAtStartup(int value); void validateWifiAtStartupValue(); + // Led + void setLedBright(int value); + void validateLedBrightValue(); + void setLedColor(uint32_t value); + void validateLedColorValue(); + + // Wifi void setWebUICreds(const String& usr, const String& pwd); void setWifiApCreds(const String& ssid, const String& pwd); void addWifiCredential(const String& ssid, const String& pwd); @@ -118,9 +138,11 @@ class BruceConfig { void removeQrCodeEntry(const String& menuName); String getWifiPassword(const String& ssid) const; + // IR void setIrTxPin(int value); void setIrRxPin(int value); + // RF void setRfTxPin(int value); void setRfRxPin(int value); void setRfModule(RFModules value); @@ -130,12 +152,15 @@ class BruceConfig { void setRfScanRange(int value, int fxdFreq = 0); void validateRfScanRangeValue(); + // RFID void setRfidModule(RFIDModules value); void validateRfidModuleValue(); + // GPS void setGpsBaudrate(int value); void validateGpsBaudrateValue(); + // Misc void setStartupApp(String value); void setWigleBasicToken(String value); void setDevMode(int value); diff --git a/src/core/menu_items/ConfigMenu.cpp b/src/core/menu_items/ConfigMenu.cpp index 1596573d1..6388ee138 100644 --- a/src/core/menu_items/ConfigMenu.cpp +++ b/src/core/menu_items/ConfigMenu.cpp @@ -4,6 +4,9 @@ #include "core/settings.h" #include "core/i2c_finder.h" #include "core/wifi_common.h" +#ifdef HAS_RGB_LED +#include "modules/others/led_control.h" +#endif void ConfigMenu::optionsMenu() { options = { @@ -11,6 +14,10 @@ void ConfigMenu::optionsMenu() { {"Dim Time", [=]() { setDimmerTimeMenu(); }}, {"Orientation", [=]() { gsetRotation(true); }}, {"UI Color", [=]() { setUIColor(); }}, + #ifdef HAS_RGB_LED + {"LED Color", [=]() { setLedColorConfig(); }}, + {"LED Brightness",[=]() { setLedBrightnessConfig(); }}, + #endif {"Sound On/Off", [=]() { setSoundConfig(); }}, {"Startup WiFi", [=]() { setWifiStartupConfig(); }}, {"Startup App", [=]() { setStartupApp(); }}, @@ -19,12 +26,12 @@ void ConfigMenu::optionsMenu() { {"Restart", [=]() { ESP.restart(); }}, }; - #if defined(T_EMBED_1101) - options.push_back({"Turn-off", [=]() { digitalWrite(PIN_POWER_ON,LOW); esp_sleep_enable_ext0_wakeup(GPIO_NUM_6,LOW); esp_deep_sleep_start(); }}); - #endif - if (bruceConfig.devMode) options.push_back({"Dev Mode", [=]() { devMenu(); }}); +#if defined(T_EMBED_1101) + options.emplace_back("Turn-off", [=]() { digitalWrite(PIN_POWER_ON,LOW); esp_sleep_enable_ext0_wakeup(GPIO_NUM_6,LOW); esp_deep_sleep_start(); }); +#endif + if (bruceConfig.devMode) options.emplace_back("Dev Mode", [=]() { devMenu(); }); - options.push_back({"Main Menu", [=]() { backToMenu(); }}); + options.emplace_back("Main Menu", [=]() { backToMenu(); }); loopOptions(options,false,true,"Config"); } diff --git a/src/core/menu_items/OthersMenu.cpp b/src/core/menu_items/OthersMenu.cpp index 3839dd53c..6022a494b 100644 --- a/src/core/menu_items/OthersMenu.cpp +++ b/src/core/menu_items/OthersMenu.cpp @@ -1,26 +1,17 @@ #include "OthersMenu.h" #include "core/display.h" -#include "core/sd_functions.h" +#include "core/utils.h" #include "modules/others/openhaystack.h" #include "modules/others/tururururu.h" -#include "modules/others/webInterface.h" #include "modules/others/qrcode_menu.h" #include "modules/others/mic.h" #include "modules/bjs_interpreter/interpreter.h" #include "modules/others/timer.h" -#include "core/utils.h" - #include "modules/others/clicker.h" #include "modules/others/bad_usb.h" -#ifdef HAS_RGB_LED -#include "modules/others/led_control.h" -#endif void OthersMenu::optionsMenu() { options = { - {"SD Card", [=]() { loopSD(SD); }}, - {"LittleFS", [=]() { loopSD(LittleFS); }}, - {"WebUI", [=]() { loopOptionsWebUi(); }}, {"QRCodes", [=]() { qrcode_menu(); }}, {"Megalodon", [=]() { shark_setup(); }}, #ifdef MIC_SPM1423 @@ -30,10 +21,6 @@ void OthersMenu::optionsMenu() { #ifdef HAS_KEYBOARD_HID {"USB Keyboard", [=]() { usb_keyboard(); }}, #endif - #ifdef HAS_RGB_LED - {"LED Control", [=]() { ledColorConfig(); }}, - {"LED Brightness", [=]() { ledBrightnessConfig(); }}, - #endif #ifdef USB_as_HID {"Clicker", [=]() { clicker_setup(); }}, #endif diff --git a/src/main.cpp b/src/main.cpp index 34d0f6d9d..0365cec06 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,7 +34,7 @@ keyStroke KeyStroke; TaskHandle_t xHandle; void __attribute__((weak)) taskInputHandler(void *parameter) { - while (true) { + while (true) { checkPowerSaveTime(); NextPress=false; PrevPress=false; @@ -88,7 +88,7 @@ uint8_t buff[1024] = {0}; TFT_eSprite sprite = TFT_eSprite(&tft); TFT_eSprite draw = TFT_eSprite(&tft); volatile int tftWidth = TFT_HEIGHT; - #ifdef HAS_TOUCH + #ifdef HAS_TOUCH volatile int tftHeight = TFT_WIDTH-20; // 20px to draw the TouchFooter(), were the btns are being read in touch devices. #else volatile int tftHeight = TFT_WIDTH; @@ -109,6 +109,7 @@ uint8_t buff[1024] = {0}; #include "core/settings.h" #include "core/serialcmds.h" #include "core/wifi_common.h" +#include "modules/others/led_control.h" #include "modules/others/audio.h" // for playAudioFile #include "modules/rf/rf.h" // for initCC1101once #include "modules/bjs_interpreter/interpreter.h" // for JavaScript interpreter @@ -167,7 +168,7 @@ void begin_tft(){ tft.setRotation(bruceConfig.rotation); //sometimes it misses the first command tft.setRotation(bruceConfig.rotation); tftWidth = tft.width(); - #ifdef HAS_TOUCH + #ifdef HAS_TOUCH tftHeight = tft.height() - 20; #else tftHeight = tft.height(); @@ -221,7 +222,7 @@ void boot_screen_anim() { } drawn=true; } - #if !defined(LITE_VERSION) + #if !defined(LITE_VERSION) if(!boot_img && (millis()-i>2200) && (millis()-i)<2700) tft.drawRect(2*tftWidth/3,tftHeight/2,2,2,bruceConfig.priColor); if(!boot_img && (millis()-i>2700) && (millis()-i)<2900) tft.fillRect(0,45,tftWidth,tftHeight-45,bruceConfig.bgColor); if(!boot_img && (millis()-i>2900) && (millis()-i)<3400) tft.drawXBitmap(2*tftWidth/3 - 30 ,5+tftHeight/2,bruce_small_bits, bruce_small_width, bruce_small_height,bruceConfig.bgColor,bruceConfig.priColor); @@ -258,6 +259,17 @@ void init_clock() { } +/********************************************************************* +** Function: init_led +** Led initialisation +*********************************************************************/ +void init_led() { +#ifdef HAS_RGB_LED + beginLed(); +#endif +} + + /********************************************************************* ** Function: startup_sound ** Play sound or tone depending on device hardware @@ -318,11 +330,12 @@ void setup() { bruceConfig.fromFile(); begin_tft(); init_clock(); + init_led(); // Some GPIO Settings (such as CYD's brightness control must be set after tft and sdcard) _post_setup_gpio(); // end of post gpio begin - + // This task keeps running all the time, will never stop xTaskCreate( taskInputHandler, // Task function @@ -331,7 +344,7 @@ void setup() { NULL, // Task parameters 2, // Task priority (0 to 3), loopTask has priority 2. &xHandle // Task handle (not used) - ); + ); boot_screen_anim(); startup_sound(); diff --git a/src/modules/others/led.cpp b/src/modules/others/led.cpp index 9605600b8..32cfc93bc 100644 --- a/src/modules/others/led.cpp +++ b/src/modules/others/led.cpp @@ -2,28 +2,24 @@ #ifdef HAS_RGB_LED #include #include "core/display.h" +#include "core/utils.h" #include -#define LED_BRIGHT_DEFAULT 245 - -int brightness = 75; CRGB leds[LED_COUNT]; -CRGB color = CRGB::Red; -CRGB hsvToRgb(uint16_t h, uint8_t s, uint8_t v) -{ +CRGB hsvToRgb(uint16_t h, uint8_t s, uint8_t v) { uint8_t f = (h % 60) * 255 / 60; uint8_t p = (255 - s) * (uint16_t)v / 255; uint8_t q = (255 - f * (uint16_t)s / 255) * (uint16_t)v / 255; uint8_t t = (255 - (255 - f) * (uint16_t)s / 255) * (uint16_t)v / 255; uint8_t r = 0, g = 0, b = 0; switch ((h / 60) % 6) { - case 0: r = v; g = t; b = p; break; - case 1: r = q; g = v; b = p; break; - case 2: r = p; g = v; b = t; break; - case 3: r = p; g = q; b = v; break; - case 4: r = t; g = p; b = v; break; - case 5: r = v; g = p; b = q; break; + case 0: r = v; g = t; b = p; break; + case 1: r = q; g = v; b = p; break; + case 2: r = p; g = v; b = t; break; + case 3: r = p; g = q; b = v; break; + case 4: r = t; g = p; b = v; break; + case 5: r = v; g = p; b = q; break; } CRGB c; @@ -33,67 +29,77 @@ CRGB hsvToRgb(uint16_t h, uint8_t s, uint8_t v) return c; } -void setColor(CRGB c) -{ - color = c; - for(int i = 0; i < LED_COUNT; i++){ - leds[i] = color; - } + +void beginLed() { +#ifdef RGB_LED_CLK + FastLED.addLeds(leds, LED_COUNT); +#else + FastLED.addLeds(leds, LED_COUNT); // Initialize the LED Object. Only 1 LED. +#endif + setLedColor(bruceConfig.ledColor); + setLedBrightness(bruceConfig.ledBright); +} + + +void setLedColor(CRGB color) { + for (int i = 0; i < LED_COUNT; i++) leds[i] = color; FastLED.show(); } -void setBrightness(int b) -{ - brightness = b; - FastLED.setBrightness(brightness); - FastLED.show(); + +void setLedBrightness(int value) { + value = max(0, min(255, value)); + int bright = 255 * value/100; + FastLED.setBrightness(bright); + FastLED.show(); } -void ledColorConfig() -{ - #ifdef RGB_LED_CLK - FastLED.addLeds(leds, LED_COUNT); - #else - FastLED.addLeds(leds, LED_COUNT); // Initialize the LED Object. Only 1 LED. - #endif - setBrightness(brightness); // Set LED Brightness +void setLedColorConfig() { + int idx; + if (bruceConfig.ledColor==CRGB::Black) idx=0; + else if (bruceConfig.ledColor==CRGB::Purple) idx=1; + else if (bruceConfig.ledColor==CRGB::White) idx=2; + else if (bruceConfig.ledColor==CRGB::Red) idx=3; + else if (bruceConfig.ledColor==CRGB::Green) idx=4; + else if (bruceConfig.ledColor==CRGB::Blue) idx=5; + else idx=6; // custom color options = { - {"OFF", [=]() - { setBrightness(0); }}, - {"PURPLE", [=]() - { setColor(CRGB::Purple); }}, - {"WHITE", [=]() - { setColor(CRGB::White); }}, - {"RED", [=]() - { setColor(CRGB::Red); }}, - {"GREEN", [=]() - { setColor(CRGB::Green); }}, - {"BLUE", [=]() - { setColor(CRGB::Blue); }}, + {"OFF", [=]() { bruceConfig.setLedColor(CRGB::Black); }, bruceConfig.ledColor == CRGB::Black }, + {"Purple", [=]() { bruceConfig.setLedColor(CRGB::Purple); }, bruceConfig.ledColor == CRGB::Purple}, + {"White", [=]() { bruceConfig.setLedColor(CRGB::White); }, bruceConfig.ledColor == CRGB::White}, + {"Red", [=]() { bruceConfig.setLedColor(CRGB::Red); }, bruceConfig.ledColor == CRGB::Red}, + {"Green", [=]() { bruceConfig.setLedColor(CRGB::Green); }, bruceConfig.ledColor == CRGB::Green}, + {"Blue", [=]() { bruceConfig.setLedColor(CRGB::Blue); }, bruceConfig.ledColor == CRGB::Blue}, }; + if (idx == 6) options.emplace_back("Custom Color", [=]() { backToMenu(); }, true); + options.emplace_back("Main Menu", [=]() { backToMenu(); }); + loopOptions(options); + setLedColor(bruceConfig.ledColor); } -void ledBrightnessConfig() -{ + +void setLedBrightnessConfig() { + int idx; + if (bruceConfig.ledBright==10) idx=0; + else if (bruceConfig.ledBright==25) idx=1; + else if (bruceConfig.ledBright==50) idx=2; + else if (bruceConfig.ledBright==75) idx=3; + else if (bruceConfig.ledBright==100) idx=4; options = { - {"10", [=]() - { setBrightness(10); }}, - {"25", [=]() - { setBrightness(20); }}, - {"50", [=]() - { setBrightness(50); }}, - {"75", [=]() - { setBrightness(75); }}, - {"100", [=]() - { setBrightness(100); }}, + {"10 %", [=]() { bruceConfig.setLedBright(10); }, bruceConfig.ledBright == 10 }, + {"25 %", [=]() { bruceConfig.setLedBright(25); }, bruceConfig.ledBright == 25 }, + {"50 %", [=]() { bruceConfig.setLedBright(50); }, bruceConfig.ledBright == 50 }, + {"75 %", [=]() { bruceConfig.setLedBright(75); }, bruceConfig.ledBright == 75 }, + {"100%", [=]() { bruceConfig.setLedBright(100); }, bruceConfig.ledBright == 100 }, + {"Main Menu", [=]() { backToMenu(); }}, }; - loopOptions(options); - + loopOptions(options, idx); + setLedBrightness(bruceConfig.ledBright); } #endif \ No newline at end of file diff --git a/src/modules/others/led_control.h b/src/modules/others/led_control.h index 22752ab6c..e41be81e8 100644 --- a/src/modules/others/led_control.h +++ b/src/modules/others/led_control.h @@ -6,10 +6,13 @@ #include CRGB hsvToRgb(uint16_t h, uint8_t s, uint8_t v); -void setColor(CRGB c); -void setBrightness(int b); -void ledColorConfig(); -void ledBrightnessConfig(); + +void beginLed(); +void setLedColor(CRGB color); +void setLedBrightness(int value); + +void setLedColorConfig(); +void setLedBrightnessConfig(); #endif #endif \ No newline at end of file