#pragma once #include #include "pin.h" struct LightInfo { 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"; } std::string uniqueId; const std::string name = "Smart RGB Light"; 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"; 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, }; enum ActiveMode { modeRgb, modeCct }; class Light { public: 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); void notifyOnline(); void notifyOffline(); 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(); uint32_t correctGamma(uint32_t originalPwm); 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 uint8_t cw = 255; // Default to white uint8_t ww = 255; // Default to white const uint32_t cwTempKelvin = 6000; const uint32_t wwTempKelvin = 3000; uint16_t colorTemperature; uint8_t brightness; uint32_t maxPwm; bool isOn = false; Pin* pinR; Pin* pinG; Pin* pinB; Pin* pinCW; Pin* pinWW; LightInfo lightInfo; DeviceInfo deviceInfo; LightType lightType = onOff; // Default light type ActiveMode activeMode = modeRgb; };