Explorar el Código

color and brightness commands

master
gituser hace 1 mes
padre
commit
5b8c26fe0f
Se han modificado 5 ficheros con 84 adiciones y 31 borrados
  1. 20
    2
      HtmlPages.h
  2. 14
    1
      LEDLamp.ino
  3. 43
    25
      LEDStripManager.cpp
  4. 6
    2
      LEDStripManager.h
  5. 1
    1
      sloeber.ino.cpp

+ 20
- 2
HtmlPages.h Ver fichero

@@ -273,7 +273,7 @@ const String getSTAControlPage(String macAddress) {
input {
width: 100%;
padding: 12px;
margin: 8px 0;
margin-top: 10px;
border-radius: 10px;
border: none;
outline: none;
@@ -308,6 +308,10 @@ const String getSTAControlPage(String macAddress) {
align-items: center;
margin-top: 10px;
}

.rowTop {
margin-top: 20px;
}
.mac {
font-size: 11px;
@@ -367,11 +371,17 @@ const String getSTAControlPage(String macAddress) {
+ R"rawliteral(</div>
<div class="btn-row">
<button type="button" onclick="ledOn()">LED EIN</button>
<div id="led_status">⚫</div>
<button type="button" onclick="ledOn()">LED EIN</button>
<button type="button" onclick="ledOff()">LED AUS</button>
</div>
<input id="id_cmd" class="rowTop" placeholder="Commad...">

<div class="btn-row">
<button type="button" onclick="sendCommand()">Command senden</button>
</div>
<div class="btn-row rowTop">
<button type="button" onclick="resetWifiCfg()">Wifi-Konfiguration löschen</button>
</div>
<div id="reset_status" class="info">-</div>
@@ -428,6 +438,14 @@ const String getSTAControlPage(String macAddress) {
ws.send("{\"cmd\":\"RESET_WIFI_CFG\",\"val\":\"\"}");
}
}

function sendCommand() {
if (ws && ws.readyState === 1) {
cmd = document.getElementById('id_cmd').value
ws.send(cmd);
}
}

</script>


+ 14
- 1
LEDLamp.ino Ver fichero

@@ -121,7 +121,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length)
Serial.print("WS empfangen: ");
Serial.println(msg);

JsonDocument doc;
JsonDocument doc; // e.g: {"cmd":"SWITCH_LED","val":"on"}
deserializeJson(doc, msg);
const char *cmd = doc["cmd"];

@@ -149,6 +149,19 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length)
LEDModes mode = parseMode(value);
ledMngr.onModeChange(mode);

} else if (strToCommand(cmd) == CMD_CHG_COLOR) {
const char *value = doc["val"];
unsigned int r, g, b, w;
if (sscanf(value, "%u,%u,%u,%u", &r, &g, &b, &w) == 4) {
ledMngr.onColorChange(r, g, b, w);
}
} else if (strToCommand(cmd) == CMD_CHG_BRIGHTNESS) {
const char *value = doc["val"];
unsigned int br;
if (sscanf(value, "%u", &br) == 1) {
ledMngr.onBrightnessChange(br);
}

} else if (strToCommand(cmd) == CMD_RESET_WIFI_CFG) {

resetWifiConfiguration(num);

+ 43
- 25
LEDStripManager.cpp Ver fichero

@@ -33,21 +33,28 @@ void LEDStripManager::begin() {
if (cfg.version != CONFIG_VERSION) {
// Initalize cfg with defaults
cfg = configManager.setToDefaultConfig();
configManager.loadConfig();
cfg = configManager.getConfig();
}

strip.begin(); // Initialisiert den NeoPixel-Streifen
strip.clear();
strip.show(); // Setzt alle LEDs auf "aus"
strip.setBrightness(cfg.brightness); // Helligkeit (0 bis 255)

// Setzt die erste LED (Index 0) auf rotes Licht (Rot, Grün, Blau, Weiss)
strip.setPixelColor(0, strip.Color(255, 0, 0, 0));
// Setzt die zweite LED auf grün
strip.setPixelColor(1, strip.Color(0, 255, 0, 0));
strip.setPixelColor(2, strip.Color(0, 0, 255, 0));
strip.setPixelColor(3, strip.Color(0, 0, 0, 255));
// Schickt die Farbinformationen zum LED-Streifen
show();

if (cfg.mode == LM_SINGLE_COLOR) {
strip.setBrightness(cfg.brightness); // Helligkeit (0 bis 255)
setAll(cfg.color.r, cfg.color.g, cfg.color.b, cfg.color.w);
} else {
// Setzt die erste LED (Index 0) auf rotes Licht (Rot, Grün, Blau, Weiss)
strip.setPixelColor(0, strip.Color(255, 0, 0, 0));
// Setzt die zweite LED auf grün
strip.setPixelColor(1, strip.Color(0, 255, 0, 0));
strip.setPixelColor(2, strip.Color(0, 0, 255, 0));
strip.setPixelColor(3, strip.Color(0, 0, 0, 255));
// Schickt die Farbinformationen zum LED-Streifen
show();
}

}

void LEDStripManager::clear() {
@@ -65,14 +72,8 @@ void LEDStripManager::on() {
delay(50);
digitalWrite(LED_STRIP_DATA_PIN, HIGH);
delay(50);
strip.begin();
strip.show();
// TODO: Load the state from the eeprom
strip.setPixelColor(0, strip.Color(255, 0, 0, 0));
strip.setPixelColor(1, strip.Color(0, 255, 0, 0));
strip.setPixelColor(2, strip.Color(0, 0, 255, 0));
strip.setPixelColor(3, strip.Color(0, 0, 0, 255));
strip.show();

begin();
}

void LEDStripManager::off() {
@@ -86,21 +87,21 @@ void LEDStripManager::off() {

void LEDStripManager::setBrightness(uint8_t brightness) {
strip.setBrightness(brightness);
strip.show();
}

void LEDStripManager::setPixel(uint16_t index, uint8_t red, uint8_t green, uint8_t blue) {
void LEDStripManager::setPixel(uint16_t index, uint8_t red, uint8_t green, uint8_t blue, uint8_t white) {
if (index >= strip.numPixels()) {
return;
}
strip.setPixelColor(index, strip.Color(red, green, blue));
strip.setPixelColor(index, strip.Color(red, green, blue, white));
strip.show();
}

void LEDStripManager::setAll(uint8_t red, uint8_t green, uint8_t blue) {
void LEDStripManager::setAll(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(red, green, blue));
strip.setPixelColor(i, strip.Color(red, green, blue, white));
}

strip.show();
}

@@ -110,7 +111,25 @@ void LEDStripManager::onModeChange(LEDModes mode) {
configManager.saveConfig(cfg);

//TODO: react on mode change on strip
}

void LEDStripManager::onColorChange(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) {
Config cfg = configManager.getConfig();
cfg.color.r = red;
cfg.color.g = green;
cfg.color.b = blue;
cfg.color.w = white;
configManager.saveConfig(cfg);

this->setAll(red, green, blue, white);
}

void LEDStripManager::onBrightnessChange(uint8_t brightness) {
Config cfg = configManager.getConfig();
cfg.brightness = brightness;
configManager.saveConfig(cfg);

this->setBrightness(brightness);
}

uint32_t
@@ -142,4 +161,3 @@ void LEDStripManager::rainbow(uint8_t wait) {
delay(wait);
}
}


+ 6
- 2
LEDStripManager.h Ver fichero

@@ -28,14 +28,18 @@ class LEDStripManager {

void setBrightness(uint8_t brightness);

void setPixel(uint16_t index, uint8_t red, uint8_t green, uint8_t blue);
void setPixel(uint16_t index, uint8_t red, uint8_t green, uint8_t blue, uint8_t white);

void setAll(uint8_t red, uint8_t green, uint8_t blue);
void setAll(uint8_t red, uint8_t green, uint8_t blue, uint8_t white);

void rainbow(uint8_t wait);

void onModeChange(LEDModes mode);

void onColorChange(uint8_t red, uint8_t green, uint8_t blue, uint8_t white);

void onBrightnessChange(uint8_t brightness);

private:
Adafruit_NeoPixel strip;


+ 1
- 1
sloeber.ino.cpp Ver fichero

@@ -2,7 +2,7 @@
//This is a automatic generated file
//Please do not modify this file
//If you touch this file your change will be overwritten during the next build
//This file has been generated on 2026-06-23 22:04:18
//This file has been generated on 2026-06-27 09:18:25

#include "Arduino.h"
#include "Arduino.h"

Cargando…
Cancelar
Guardar