Files
smart-rgb-esp32/lib/light/light.h

98 lines
3.2 KiB
C
Raw Permalink Normal View History

2025-08-03 00:34:05 +02:00
#pragma once
#include <cstdint>
#include "pin.h"
struct LightInfo {
2025-08-30 21:38:24 +02:00
LightInfo() = default;
LightInfo(const std::string& id) {
uniqueId = id;
updateTopics();
}
void setUniqueId(const std::string& id) {
uniqueId = id;
updateTopics();
}
void updateTopics() {
discoveryTopic = discoveryTopicBase + uniqueId + "/config";
availabilityTopic = topicBase + uniqueId + "/availability";
stateTopic = topicBase + uniqueId + "/state";
jsonAttributesTopic = topicBase + uniqueId + "/attributes";
commandTopic = topicBase + uniqueId + "/state/set";
}
2025-08-03 00:34:05 +02:00
std::string uniqueId;
const std::string name = "Smart RGB Light";
2025-08-30 21:38:24 +02:00
const std::string discoveryTopicBase = "homeassistant/light/";
std::string discoveryTopic = discoveryTopicBase + uniqueId + "/config";
const std::string topicBase = "studiotj/";
std::string availabilityTopic = topicBase + uniqueId + "/availability";
std::string stateTopic = topicBase + uniqueId + "/state";
std::string jsonAttributesTopic = topicBase + uniqueId + "/attributes";
std::string stateValueTemplate = "{{ value_json.state }}";
std::string commandTopic = topicBase + uniqueId + "/state/set";
2025-08-03 00:34:05 +02:00
const std::string supportedColorModesValue = "['rgb', 'brightness']";
const std::string availabilityTemplate = "{{ value_json.availability }}";
};
struct DeviceInfo {
std::string name = "Smart RGB Light";
std::string model = "smart_rgb_light";
std::string identifier = "smart_rgb_light_";
std::string swVersion = "1.0"; // TODO: version will be generated.
std::string manufacturer = "Studio TJ";
};
enum LightType {
onOff,
brightness,
colorTemperature,
rgb,
rgbw,
rgbww,
};
2025-08-27 17:30:39 +02:00
enum ActiveMode {
modeRgb,
modeCct
};
2025-08-03 00:34:05 +02:00
class Light {
public:
2025-08-29 10:22:36 +02:00
Light(Pin* pinR, Pin* pinG, Pin* pinB, std::string uniqueId);
Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, std::string uniqueId);
Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, Pin* pinWW, std::string uniqueId);
2025-08-29 19:17:09 +02:00
void notifyOnline();
void notifyOffline();
2025-08-03 00:34:05 +02:00
void publishInitialState();
void publishCurrentState();
void setHsl(uint8_t h, uint8_t s, uint8_t l);
void setColorTemperature(uint16_t temperature);
void setBrightness(uint8_t brightness);
void turnOff();
private:
void handleCommand(const std::string& command);
void operatePin();
2025-08-27 10:40:39 +02:00
uint32_t correctGamma(uint32_t originalPwm);
2025-08-27 17:30:39 +02:00
void applyKelvin(uint32_t kelvin);
uint8_t r = 0; // Default to white
uint8_t g = 0; // Default to white
uint8_t b = 0; // Default to white
2025-08-03 00:34:05 +02:00
uint8_t cw = 255; // Default to white
uint8_t ww = 255; // Default to white
2025-08-27 17:30:39 +02:00
const uint32_t cwTempKelvin = 6000;
const uint32_t wwTempKelvin = 3000;
2025-08-03 00:34:05 +02:00
uint16_t colorTemperature;
uint8_t brightness;
2025-08-27 17:30:39 +02:00
uint32_t maxPwm;
2025-08-03 00:34:05 +02:00
bool isOn = false;
Pin* pinR;
Pin* pinG;
Pin* pinB;
Pin* pinCW;
Pin* pinWW;
LightInfo lightInfo;
DeviceInfo deviceInfo;
LightType lightType = onOff; // Default light type
2025-08-27 17:30:39 +02:00
ActiveMode activeMode = modeRgb;
2025-08-03 00:34:05 +02:00
};