still wip

This commit is contained in:
2025-08-27 10:40:39 +02:00
parent f5fff82fad
commit d8943c00ab
4 changed files with 58 additions and 8 deletions

View File

@@ -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<uint32_t>(r * brightnessFactor);
rSetpoint = correctGamma(rSetpoint);
// if (rSetpoint < minPwmValue && rSetpoint > 0) {
// rSetpoint = minPwmValue;
// }
gSetpoint = static_cast<uint32_t>(g * brightnessFactor);
gSetpoint = correctGamma(gSetpoint);
// if (gSetpoint < minPwmValue && gSetpoint > 0) {
// gSetpoint = minPwmValue;
// }
bSetpoint = static_cast<uint32_t>(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<uint32_t>(pow(pwmPercentage, 1 / gammaCorrection) * 255);
}