Skip to content

Commit

Permalink
added Strip Mode
Browse files Browse the repository at this point in the history
fixed Version
added set Brightness to Web Config Page
  • Loading branch information
vr6syncro committed Sep 18, 2023
1 parent 442fec5 commit 8237950
Show file tree
Hide file tree
Showing 9 changed files with 329 additions and 93 deletions.
35 changes: 18 additions & 17 deletions src/Bambulab_Esp32_LEDController.ino
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,17 @@ 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());
preferences.putString("user", user.c_str());
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();
}

Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -275,9 +272,11 @@ void setup() {
String password = doc["password"].as<String>();
String serial = doc["serial"].as<String>();
bool debugValue = doc["debug"].as<bool>();
int brightnessValue = doc["brightness"].as<int>();
int szenarioValue = doc["szenario"].as<int>();
int led_scenario_3Value = doc["led_scenario_3"].as<int>();

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");

Expand Down Expand Up @@ -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();
}

Expand All @@ -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();
}

Expand Down Expand Up @@ -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");
Expand All @@ -759,17 +761,16 @@ 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("===========================================");
delay(3000);
ESP.restart();
}


// config over serial function
String askForInput(const String& prompt, const String& currentValue) {
Serial.print(prompt);
Expand Down
7 changes: 6 additions & 1 deletion src/config.h
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
30 changes: 15 additions & 15 deletions src/effects.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@

#include <FastLED.h>

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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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();
}
}
Expand All @@ -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;
Expand All @@ -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();
}
Expand All @@ -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++;
}
}

Expand All @@ -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++;
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/ledhandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,6 +34,8 @@ void setWifiConnecting() {
FastLED.show();
} else if (szenario == 2) {
// Handle "logo" scenario
} else if (szenario == 3) {
// Handle "Strip" scenario
}
}

Expand All @@ -40,6 +46,8 @@ void setMqttConnected() {
FastLED.show();
} else if (szenario == 2) {
// Handle "logo" scenario
} else if (szenario == 3) {
// Handle "Strip" scenario
}
}

Expand All @@ -55,6 +63,8 @@ void setMqttDisconnected() {
FastLED.show();
} else if (szenario == 2) {
// Handle "logo" scenario
} else if (szenario == 3) {
// Handle "Strip" scenario
}
}

Expand Down
31 changes: 17 additions & 14 deletions src/ledhandle_icon_case.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@

#ifndef LEDHANDLE_ICON_CASE_H
#define LEDHANDLE_ICON_CASE_H

#include <FastLED.h>

// 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) {
Expand Down Expand Up @@ -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;
Expand Down
27 changes: 13 additions & 14 deletions src/ledhandle_logo_case.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,13 @@
#include <FastLED.h>


//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) {
Expand Down Expand Up @@ -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);
}

Expand Down
Loading

0 comments on commit 8237950

Please sign in to comment.