From d8943c00ab2ac5f96d6702a232961a02e629438d Mon Sep 17 00:00:00 2001 From: Tianyu Liu Date: Wed, 27 Aug 2025 10:40:39 +0200 Subject: [PATCH] still wip --- .vscode/settings.json | 3 ++- lib/light/light.cpp | 47 ++++++++++++++++++++++++++++++++++++++++--- lib/light/light.h | 1 + src/main.cpp | 15 ++++++++++---- 4 files changed, 58 insertions(+), 8 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 025eb3b..854982c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -55,5 +55,6 @@ "thread": "cpp", "cinttypes": "cpp", "typeinfo": "cpp" - } + }, + "idf.portWin": "COM4" } \ No newline at end of file diff --git a/lib/light/light.cpp b/lib/light/light.cpp index ab6f19f..78434ec 100644 --- a/lib/light/light.cpp +++ b/lib/light/light.cpp @@ -2,8 +2,11 @@ #include "light.h" #include "mqtt.h" -constexpr uint16_t CONFIG_MSG_SIZE = 1024; -constexpr uint16_t STATUS_MSG_SIZE = 128; +constexpr uint16_t configMsgSize = 1024; +constexpr uint16_t statusMsgSize = 128; +constexpr uint8_t minPwmValue = 0; +constexpr float gammaCorrection = 2.2f; +constexpr uint8_t maxPwm = 255; const struct { String available = "online"; @@ -19,6 +22,14 @@ Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, Mqtt* mqttClient, std::string uniq subscribeToMqttTopics(); } +Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, Pin* pinWW, Mqtt* mqttClient, std::string uniqueId) + : pinR(pinR), pinG(pinG), pinB(pinB), pinCW(pinCW), pinWW(pinWW), mqttClient(mqttClient) { + lightInfo.uniqueId = uniqueId; + lightType = LightType::rgbww; + publishInitialState(); + subscribeToMqttTopics(); +} + void Light::publishInitialState() { // Publish the initial state of the light JsonDocument configInfo; @@ -89,12 +100,30 @@ void Light::operatePin() { } float brightnessFactor = brightness / 255.0f; rSetpoint = static_cast(r * brightnessFactor); + rSetpoint = correctGamma(rSetpoint); + // if (rSetpoint < minPwmValue && rSetpoint > 0) { + // rSetpoint = minPwmValue; + // } gSetpoint = static_cast(g * brightnessFactor); + gSetpoint = correctGamma(gSetpoint); + // if (gSetpoint < minPwmValue && gSetpoint > 0) { + // gSetpoint = minPwmValue; + // } bSetpoint = static_cast(b * brightnessFactor); + bSetpoint = correctGamma(bSetpoint); + // if (bSetpoint < minPwmValue && bSetpoint > 0) { + // bSetpoint = minPwmValue; + // } Serial.printf("Setting RGB: R=%d, G=%d, B=%d with brightness factor: %.2f\n", rSetpoint, gSetpoint, bSetpoint, brightnessFactor); pinR->setLedLevel(rSetpoint); pinG->setLedLevel(gSetpoint); pinB->setLedLevel(bSetpoint); + if (pinCW != nullptr) { + pinCW->setLedLevel(cw); + } + if (pinWW != nullptr) { + pinWW->setLedLevel(ww); + } Serial.printf("Set RGB: R=%d, G=%d, B=%d\n", r, g, b); } @@ -129,8 +158,14 @@ void Light::handleCommand(const std::string& command) { r = color["r"] | 255; // Default to 255 if not provided g = color["g"] | 255; // Default to 255 if not provided b = color["b"] | 255; // Default to 255 if not provided + if (lightType == LightType::rgbw || lightType == LightType::rgbww) { + ww = color["w"] | 255; // Default to 255 if not provided + } + if (lightType == LightType::rgbww) { + cw = color["cw"] | 255; // Default to 255 if not provided + } } - if (lightType == LightType::rgb) + if (lightType == LightType::rgb || lightType == LightType::rgbw || lightType == LightType::rgbww) { operatePin(); } @@ -183,4 +218,10 @@ void Light::publishCurrentState() { serializeJson(stateInfo, stateJson); Serial.println("Publishing current state: " + String(stateJson.c_str())); mqttClient->publish(lightInfo.stateTopic, stateJson); +} + +uint32_t Light::correctGamma(uint32_t originalPwm) { + // Apply gamma correction to the PWM value + float pwmPercentage = originalPwm / 255.0f; + return static_cast(pow(pwmPercentage, 1 / gammaCorrection) * 255); } \ No newline at end of file diff --git a/lib/light/light.h b/lib/light/light.h index b830d5b..b6885f5 100644 --- a/lib/light/light.h +++ b/lib/light/light.h @@ -74,6 +74,7 @@ public: private: void handleCommand(const std::string& command); void operatePin(); + uint32_t correctGamma(uint32_t originalPwm); uint8_t r = 255; // Default to white uint8_t g = 255; // Default to white uint8_t b = 255; // Default to white diff --git a/src/main.cpp b/src/main.cpp index b24419d..d5bc921 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,9 +16,11 @@ Task *updateTask = nullptr; Task *mqttTickTask = nullptr; Task *mqttCheckConnectionTask = nullptr; -Pin *pinR = new Pin(14, true, true, 5000, 0); // Example pin numbers, adjust as needed -Pin *pinG = new Pin(15, true, true, 5000, 1); -Pin *pinB = new Pin(16, true, true, 5000, 2); +Pin *pinR = new Pin(16, true, true, 5000, 0); // Example pin numbers, adjust as needed +Pin *pinG = new Pin(17, true, true, 5000, 1); +Pin *pinB = new Pin(18, true, true, 5000, 2); +Pin *pinCW = new Pin(19, true, true, 5000, 3); +Pin *pinWW = new Pin(21, true, true, 5000, 4); Scheduler *scheduler; @@ -28,12 +30,17 @@ void setup() { // put your setup code here, to run once: Serial.begin(115200); Serial.println("Starting Smart RGB ESP32..."); + pinR->setLedLevel(0); + pinG->setLedLevel(0); + pinB->setLedLevel(0); + pinCW->setLedLevel(0); + pinWW->setLedLevel(0); network = new Network("smart-rgb"); otaHandler = new OTAHandler("smart-rgb-ota"); network->registerMDNS(); Mqtt::connect("10.238.75.81", 1883, "smart_rgb_client", "mqtt", "mqtt"); delay(1000); // Wait for MQTT connection to stabilize - light = new Light(pinR, pinG, pinB, mqttClient, "smart_rgb_light"); + light = new Light(pinR, pinG, pinB, pinCW, pinWW, mqttClient, "smart_rgb_light"); initializeScheduler(); }