still wip
This commit is contained in:
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -55,5 +55,6 @@
|
||||
"thread": "cpp",
|
||||
"cinttypes": "cpp",
|
||||
"typeinfo": "cpp"
|
||||
}
|
||||
},
|
||||
"idf.portWin": "COM4"
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -184,3 +219,9 @@ void Light::publishCurrentState() {
|
||||
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);
|
||||
}
|
||||
@@ -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
|
||||
|
||||
15
src/main.cpp
15
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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user