From 82379505bfdcae15109506c7ee3085133aa8b91e Mon Sep 17 00:00:00 2001 From: vr6syncro Date: Mon, 18 Sep 2023 21:04:41 +0200 Subject: [PATCH] added Strip Mode fixed Version added set Brightness to Web Config Page --- src/Bambulab_Esp32_LEDController.ino | 35 ++++---- src/config.h | 7 +- src/effects.h | 30 +++---- src/ledhandle.h | 12 ++- src/ledhandle_icon_case.h | 31 ++++--- src/ledhandle_logo_case.h | 27 +++--- src/ledhandle_stripe_case.h | 121 +++++++++++++++++++++++++ src/startpage.h | 129 ++++++++++++++++++++++----- src/ws2812b.h | 30 +++++-- 9 files changed, 329 insertions(+), 93 deletions(-) create mode 100644 src/ledhandle_stripe_case.h diff --git a/src/Bambulab_Esp32_LEDController.ino b/src/Bambulab_Esp32_LEDController.ino index edef646..a39db32 100644 --- a/src/Bambulab_Esp32_LEDController.ino +++ b/src/Bambulab_Esp32_LEDController.ino @@ -128,7 +128,7 @@ void sendGcodeCommand(const char* command, const char* param); void sendLedControlCommand(const char* ledNode, const char* ledMode, int ledOnTime, int ledOffTime, int loopTimes, int intervalTime); //save config from web -void saveConfigFromWeb(const String& server, const String& port, const String& user, const String& password, const String& serial, bool debugValue, int szenarioValue) { +void saveConfigFromWeb(const String& server, const String& port, const String& user, const String& password, const String& serial, bool debugValue, int szenarioValue, int brightnessValue, int led_scenario_3Value) { preferences.begin("mqtt_config", false); preferences.putString("server", server.c_str()); preferences.putString("port", port.c_str()); @@ -136,7 +136,9 @@ void saveConfigFromWeb(const String& server, const String& port, const String& u preferences.putString("password", password.c_str()); preferences.putString("serial", serial.c_str()); preferences.putBool("debug", debugValue); + preferences.putString("brightness", String(brightnessValue).c_str()); preferences.putString("szenario", String(szenarioValue).c_str()); + preferences.putString("led_scenario_3", String(led_scenario_3Value).c_str()); preferences.end(); } @@ -161,15 +163,12 @@ void setup() { Serial.begin(115200); client.setBufferSize(20000); + // Preferences + loadPreferences(); initFastLED(); setFastLedAllBlack(); - //Serial.println("ESP-IDF version is: " + String(esp_get_idf_version())); - - // Preferences - loadPreferences(); - // WiFiManager WiFiManager wifiManager; setWifiConnecting(); @@ -213,13 +212,11 @@ void setup() { Serial.println("Initial setup complete."); Serial.println("Send 'config' to configure settings via serial console."); - setupOTA(); printWelcomeMessage(); delay(3000); checkForUpdates(); - server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) { String startpage_html_content = getStartPage(); request->send(200, "text/html", startpage_html_content); @@ -275,9 +272,11 @@ void setup() { String password = doc["password"].as(); String serial = doc["serial"].as(); bool debugValue = doc["debug"].as(); + int brightnessValue = doc["brightness"].as(); int szenarioValue = doc["szenario"].as(); + int led_scenario_3Value = doc["led_scenario_3"].as(); - saveConfigFromWeb(server, port, user, password, serial, debugValue, szenarioValue); + saveConfigFromWeb(server, port, user, password, serial, debugValue, szenarioValue, brightnessValue, led_scenario_3Value); ws.sendTXT(clientNum, "Config saved successfully"); @@ -453,9 +452,12 @@ void loadPreferences() { preferences.getString("password", mqtt_password, sizeof(mqtt_password)); preferences.getString("serial", serial_number, sizeof(serial_number)); debug = preferences.getBool("debug"); - String szenario_string = preferences.getString("szenario"); + String szenario_string = preferences.getString("szenario", "1"); szenario = szenario_string.toInt(); - brightness = preferences.getInt("brightness", 84); + String brightness_string = preferences.getString("brightness", "24"); + brightness = brightness_string.toInt(); + String led_scenario_3_string = preferences.getString("led_scenario_3" , "1"); + led_scenario_3 = led_scenario_3_string.toInt(); preferences.end(); } @@ -469,7 +471,6 @@ void savePreferences(WiFiManagerParameter& server, WiFiManagerParameter& port, W preferences.putString("serial", serial.getValue()); preferences.putBool("debug", debug); preferences.putString("szenario", szenario_param.getValue()); - preferences.putInt("brightness", brightness); preferences.end(); } @@ -743,12 +744,13 @@ void configureSerial() { Serial.println(); String szenarioString = askForInput("Szenario - Type 1 for Icon Cover and 2 for Logo Cover", preferences.getString("szenario", "")); Serial.println(); + String led_scenario_3String = askForInput("Number of LEDs for Scenario 3", preferences.getString("led_scenario_3", "")); + Serial.println(); Serial.println("-------------------------------------------"); Serial.println("Debugging Status (on/off)? Current: " + String(debug ? "on" : "off")); Serial.println(); - while (!Serial.available()) - ; + while (!Serial.available()); String debugStatus = Serial.readStringUntil('\n'); debugStatus.trim(); debug = (debugStatus == "on"); @@ -759,9 +761,9 @@ void configureSerial() { preferences.putString("password", password); preferences.putString("serial", serial); preferences.putString("szenario", szenarioString); + preferences.putString("led_scenario_3", led_scenario_3String); preferences.putBool("debug", debug); - preferences.end(); - + Serial.println("-------------------------------------------"); Serial.println("Configuration saved. Reboot in 3 seconds."); Serial.println("==========================================="); @@ -769,7 +771,6 @@ void configureSerial() { ESP.restart(); } - // config over serial function String askForInput(const String& prompt, const String& currentValue) { Serial.print(prompt); diff --git a/src/config.h b/src/config.h index 4efad5b..11b5adf 100644 --- a/src/config.h +++ b/src/config.h @@ -1,7 +1,7 @@ #ifndef CONFIG_H #define CONFIG_H -const char* firmwareVersion = "V2.1"; +const char* firmwareVersion = "V3.0"; int szenario = 1; bool updateAvailable = false; @@ -46,4 +46,9 @@ int hms_error_count = 0; // Variable to keep track of the number of H unsigned long lastAttemptTime = 0; const unsigned long RECONNECT_INTERVAL = 5000; // 5 seconds +// Print finished +unsigned long finishTime = 0; +const unsigned long finishDisplayDuration = 180000; //3 minutes +bool finishProcessed = false; + #endif \ No newline at end of file diff --git a/src/effects.h b/src/effects.h index c4f7bfd..7e6fb5b 100644 --- a/src/effects.h +++ b/src/effects.h @@ -3,16 +3,18 @@ #include +extern int NUM_LEDS; -unsigned long previousMillis_blink = 0; // Speichert den Zeitpunkt des letzten Blinkens -bool ledState = false; // Zustand der LED: true = eingeschaltet, false = ausgeschaltet + +unsigned long previousMillis_blink = 0; +bool ledState = false; unsigned long previousMillis_fade = 0; -uint8_t fadeBrightness = 0; // Aktuelle Helligkeit der LED für den Fade-Effekt -bool isFadingIn = true; // Gibt an, ob die LED gerade heller wird +uint8_t fadeBrightness = 0; +bool isFadingIn = true; unsigned long previousMillis_rainbow = 0; -uint8_t hueOffset = 0; // Dieser Wert wird mit der Zeit erhöht, um den Regenbogeneffekt zu verschieben +uint8_t hueOffset = 0; // sample: blinkLEDNonBlocking(4, 1000, CRGB::Red); void blinkLEDNonBlocking(int ledIndex, int blinkDelay = 500, CRGB color = CRGB::White) { @@ -40,7 +42,7 @@ void fadeInFadeOutNonBlocking(int ledIndex, CRGB color, int fadeDelay = 10) { previousMillis_fade = currentMillis; if (isFadingIn) { - if (fadeBrightness < brightness) { // Hier verwenden wir die globale brightness-Variable + if (fadeBrightness < brightness) { fadeBrightness++; } else { isFadingIn = false; @@ -54,7 +56,7 @@ void fadeInFadeOutNonBlocking(int ledIndex, CRGB color, int fadeDelay = 10) { } leds[ledIndex] = color; - leds[ledIndex].fadeToBlackBy(map(fadeBrightness, 0, brightness, 255, 0)); // Berücksichtigung der globalen brightness + leds[ledIndex].fadeToBlackBy(map(fadeBrightness, 0, brightness, 255, 0)); FastLED.show(); } } @@ -67,7 +69,7 @@ void fadeInFadeOutAllLedsNonBlocking(CRGB color, int fadeDelay = 10) { previousMillis_fade = currentMillis; if (isFadingIn) { - if (fadeBrightness < brightness) { // Hier verwenden wir die globale brightness-Variable + if (fadeBrightness < brightness) { fadeBrightness++; } else { isFadingIn = false; @@ -80,9 +82,9 @@ void fadeInFadeOutAllLedsNonBlocking(CRGB color, int fadeDelay = 10) { } } - for (int i = 0; i < NUM_LEDS; i++) { // Vorausgesetzt, Sie haben eine Konstante namens NUM_LEDS, die die Anzahl der LEDs angibt. + for (int i = 0; i < NUM_LEDS; i++) { leds[i] = color; - leds[i].fadeToBlackBy(map(fadeBrightness, 0, brightness, 255, 0)); // Berücksichtigung der globalen brightness + leds[i].fadeToBlackBy(map(fadeBrightness, 0, brightness, 255, 0)); } FastLED.show(); } @@ -97,12 +99,11 @@ void movingRainbowEffectNonBlocking(int rainbowDelay = 10) { previousMillis_rainbow = currentMillis; for (int i = 0; i < NUM_LEDS; i++) { - // Der Farbhue wird basierend auf der Position der LED und dem globalen Offset berechnet leds[i] = CHSV((i * 256 / NUM_LEDS) + hueOffset, 255, 255); } FastLED.show(); - hueOffset++; // Erhöht den Farbhue-Offset, um den Regenbogeneffekt zu verschieben + hueOffset++; } } @@ -112,13 +113,12 @@ void movingRainbowEffectNonBlockingForThreeLEDs(int rainbowDelay = 10) { if (currentMillis - previousMillis_rainbow >= rainbowDelay) { previousMillis_rainbow = currentMillis; - for (int i = 0; i < 3; i++) { // Nur für LEDs 0, 1 und 2 - // Der Farbhue wird basierend auf der Position der LED und dem globalen Offset berechnet + for (int i = 0; i < 3; i++) { leds[i] = CHSV((i * 256 / NUM_LEDS) + hueOffset, 255, 255); } FastLED.show(); - hueOffset++; // Erhöht den Farbhue-Offset, um den Regenbogeneffekt zu verschieben + hueOffset++; } } diff --git a/src/ledhandle.h b/src/ledhandle.h index 797469e..d9c1f09 100644 --- a/src/ledhandle.h +++ b/src/ledhandle.h @@ -6,15 +6,19 @@ //include ledhandler for different cover #include "ledhandle_icon_case.h" #include "ledhandle_logo_case.h" +#include "ledhandle_stripe_case.h" -// Function to control LEDs based on the selected scenario - 1 is Icon Cover and 2 is Logo Cover +// Function to control LEDs based on the selected scenario - 1 is Icon Cover and 2 is Logo Cover and 3 is Strip Mode void ledControlSwitch() { if (szenario == 1) { ledControlIconFastLED(); } else if (szenario == 2) { ledControlLogoFastLED(); + } else if (szenario == 3) { + ledControlStripeFastLED(); } + } //All LED off @@ -30,6 +34,8 @@ void setWifiConnecting() { FastLED.show(); } else if (szenario == 2) { // Handle "logo" scenario + } else if (szenario == 3) { + // Handle "Strip" scenario } } @@ -40,6 +46,8 @@ void setMqttConnected() { FastLED.show(); } else if (szenario == 2) { // Handle "logo" scenario + } else if (szenario == 3) { + // Handle "Strip" scenario } } @@ -55,6 +63,8 @@ void setMqttDisconnected() { FastLED.show(); } else if (szenario == 2) { // Handle "logo" scenario + } else if (szenario == 3) { + // Handle "Strip" scenario } } diff --git a/src/ledhandle_icon_case.h b/src/ledhandle_icon_case.h index 9318f1d..24305bc 100644 --- a/src/ledhandle_icon_case.h +++ b/src/ledhandle_icon_case.h @@ -1,21 +1,16 @@ - #ifndef LEDHANDLE_ICON_CASE_H #define LEDHANDLE_ICON_CASE_H #include -// Deklariere welche Variablen genutzt werden sollen + extern bool hmsErrorExists; extern int printer_stage; extern bool f_layerInspection; extern String gcodeState; +extern int NUM_LEDS; void ledControlIconFastLED() { - /* - if (debug) { - Serial.println("Entering Icon Led Handler"); - } - */ if (!f_layerInspection && hmsErrorExists) { leds[5] = CRGB::Red; } else if (f_layerInspection && hmsErrorExists) { @@ -179,20 +174,28 @@ void ledControlIconFastLED() { break; } } else if (gcodeState == "PAUSE") { - // Insert the LED control code for the PAUSE state here - // Example: leds[0] = CRGB::Yellow; leds[1] = CRGB::Yellow; leds[2] = CRGB::Yellow; leds[4] = CRGB::White; leds[5] = CRGB::White; } else if (gcodeState == "FINISH") { - // Insert the LED control code for the PAUSE state here - // Example: - movingRainbowEffectNonBlockingForThreeLEDs(10); + if (finishTime == 0) { + finishTime = millis(); + } + if (millis() - finishTime < finishDisplayDuration) { + movingRainbowEffectNonBlockingForThreeLEDs(10); + } else { + leds[0] = CRGB::White; + leds[1] = CRGB::White; + leds[2] = CRGB::White; + leds[4] = CRGB::White; + finishProcessed = true; + } + } else if (gcodeState != "FINISH") { + finishTime = 0; + finishProcessed = false; } else { - // Insert the LED control code for any other gcodeState values here - // Example: leds[0] = CRGB::Purple; leds[1] = CRGB::Purple; leds[2] = CRGB::Purple; diff --git a/src/ledhandle_logo_case.h b/src/ledhandle_logo_case.h index ea51526..158cd84 100644 --- a/src/ledhandle_logo_case.h +++ b/src/ledhandle_logo_case.h @@ -4,19 +4,13 @@ #include -//Deklariere welche Variablen genutzt werden sollen extern bool hmsErrorExists; extern int printer_stage; extern bool f_layerInspection; extern String gcodeState; - +extern int NUM_LEDS; void ledControlLogoFastLED() { - /* - if (debug) { - Serial.println("Entering Icon Led Handler"); - } - */ if (!f_layerInspection && hmsErrorExists) { fill_solid(leds, NUM_LEDS, CRGB::Red); } else if (f_layerInspection && hmsErrorExists) { @@ -101,16 +95,21 @@ void ledControlLogoFastLED() { break; } } else if (gcodeState == "PAUSE") { - // Insert the LED control code for the PAUSE state here - // Example: fill_solid(leds, NUM_LEDS, CRGB::Yellow); } else if (gcodeState == "FINISH") { - // Insert the LED control code for the PAUSE state here - // Example: - movingRainbowEffectNonBlocking(10); + if (finishTime == 0) { + finishTime = millis(); + } + if (millis() - finishTime < finishDisplayDuration) { + movingRainbowEffectNonBlockingForThreeLEDs(10); + } else { + fill_solid(leds, NUM_LEDS, CRGB::White); + finishProcessed = true; + } + } else if (gcodeState != "FINISH") { + finishTime = 0; + finishProcessed = false; } else { - // Insert the LED control code for any other gcodeState values here - // Example: fill_solid(leds, NUM_LEDS, CRGB::Purple); } diff --git a/src/ledhandle_stripe_case.h b/src/ledhandle_stripe_case.h new file mode 100644 index 0000000..548bfe2 --- /dev/null +++ b/src/ledhandle_stripe_case.h @@ -0,0 +1,121 @@ +#ifndef LEDHANDLE_STRIPE_CASE_H +#define LEDHANDLE_STRIPE_CASE_H + +#include + + +extern bool hmsErrorExists; +extern int printer_stage; +extern bool f_layerInspection; +extern String gcodeState; +extern int NUM_LEDS; + +void ledControlStripeFastLED() { + if (!f_layerInspection && hmsErrorExists) { + fill_solid(leds, NUM_LEDS, CRGB::Red); + } else if (f_layerInspection && hmsErrorExists) { + fill_solid(leds, NUM_LEDS, CRGB::Black); + } else { + + } + + if (gcodeState == "IDLE" || gcodeState == "RUNNING") { + switch (printer_stage) { + case -1: + fill_solid(leds, NUM_LEDS, CRGB::White); + break; + case 0: + fill_solid(leds, NUM_LEDS, CRGB::White); + break; + case 11: + fill_solid(leds, NUM_LEDS, CRGB::Black); + break; + case 1: + fill_solid(leds, NUM_LEDS, CRGB::Black); + break; + case 2: + fill_solid(leds, NUM_LEDS, CRGB::Green); + break; + case 4: + fill_solid(leds, NUM_LEDS, CRGB::Yellow); + break; + case 9: + fill_solid(leds, NUM_LEDS, CRGB::Black); + break; + case 13: + fill_solid(leds, NUM_LEDS, CRGB::Black); + break; + case 15: + fill_solid(leds, NUM_LEDS, CRGB::Black); + break; + case 7: + fill_solid(leds, NUM_LEDS, CRGB::Black); + break; + case 10: // Printer Leveling + fill_solid(leds, NUM_LEDS, CRGB::Black); + break; + case 5: // Printer M400 Pause + fill_solid(leds, NUM_LEDS, CRGB::Yellow); + break; + case 6: // Printer Filament Runout + fill_solid(leds, NUM_LEDS, CRGB::Orange); + break; + case 8: + fill_solid(leds, NUM_LEDS, CRGB::Black); + break; + case 18: + fill_solid(leds, NUM_LEDS, CRGB::Black); + break; + case 19: + fill_solid(leds, NUM_LEDS, CRGB::Black); + break; + case 12: + fill_solid(leds, NUM_LEDS, CRGB::Black); + break; + case 14: + fill_solid(leds, NUM_LEDS, CRGB::Black); + break; + case 3: // Printer Calibrating + fill_solid(leds, NUM_LEDS, CRGB::Black); + break; + case 16: // Printer Paused by User + fill_solid(leds, NUM_LEDS, CRGB::Yellow); + break; + case 17: + fill_solid(leds, NUM_LEDS, CRGB::Red); + break; + case 20: + fill_solid(leds, NUM_LEDS, CRGB::Red); + break; + case 21: + fill_solid(leds, NUM_LEDS, CRGB::Red); + break; + default: + fill_solid(leds, NUM_LEDS, CRGB::White); + break; + } + } else if (gcodeState == "PAUSE") { + // Insert the LED control code for the PAUSE state here + // Example: + fill_solid(leds, NUM_LEDS, CRGB::Yellow); + } else if (gcodeState == "FINISH") { + if (finishTime == 0) { + finishTime = millis(); + } + if (millis() - finishTime < finishDisplayDuration) { + fill_solid(leds, NUM_LEDS, CRGB::Green); + } else { + fill_solid(leds, NUM_LEDS, CRGB::White); + finishProcessed = true; + } + } else if (gcodeState != "FINISH") { + finishTime = 0; + finishProcessed = false; + } else { + fill_solid(leds, NUM_LEDS, CRGB::Purple); + } + + FastLED.show(); +} + +#endif \ No newline at end of file diff --git a/src/startpage.h b/src/startpage.h index 639b80c..1c2ec3e 100644 --- a/src/startpage.h +++ b/src/startpage.h @@ -9,6 +9,8 @@ extern char mqtt_password[30]; extern char serial_number[20]; extern bool debug; extern int szenario; +extern int brightness; +extern int led_scenario_3; extern bool updateAvailable; extern String latestVersion; @@ -193,20 +195,47 @@ String getStartPage() {
  • Update the Config
  • -
  • -
  • -
  • -
  • -
  • -
  • -

    Type 1 for Icon Cover and 2 for Logo Cover

    -
  • +
  • + + +
  • +
  • + +
  • +
  • + +
  • +
  • + + +
  • +
  • + + +
  • +
  • + + +
  • +
  • + + +
  • +