feature/add_mqtt_and_more #2
@@ -5,8 +5,8 @@
|
|||||||
constexpr uint16_t configMsgSize = 1024;
|
constexpr uint16_t configMsgSize = 1024;
|
||||||
constexpr uint16_t statusMsgSize = 128;
|
constexpr uint16_t statusMsgSize = 128;
|
||||||
constexpr uint8_t minPwmValue = 0;
|
constexpr uint8_t minPwmValue = 0;
|
||||||
constexpr float gammaCorrection = 2.2f;
|
constexpr double gammaCorrection = 2.4;
|
||||||
constexpr uint8_t maxPwm = 255;
|
constexpr uint32_t maxPwmUi = 255;
|
||||||
|
|
||||||
const struct {
|
const struct {
|
||||||
String available = "online";
|
String available = "online";
|
||||||
@@ -18,6 +18,8 @@ Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, Mqtt* mqttClient, std::string uniq
|
|||||||
: pinR(pinR), pinG(pinG), pinB(pinB), pinCW(nullptr), pinWW(nullptr), mqttClient(mqttClient) {
|
: pinR(pinR), pinG(pinG), pinB(pinB), pinCW(nullptr), pinWW(nullptr), mqttClient(mqttClient) {
|
||||||
lightInfo.uniqueId = uniqueId;
|
lightInfo.uniqueId = uniqueId;
|
||||||
lightType = LightType::rgb;
|
lightType = LightType::rgb;
|
||||||
|
uint8_t bits = pinR->getLedResolutionBits();
|
||||||
|
maxPwm = (bits >= 1 && bits <= 31) ? ((1u << bits) - 1u) : 255u;
|
||||||
publishInitialState();
|
publishInitialState();
|
||||||
subscribeToMqttTopics();
|
subscribeToMqttTopics();
|
||||||
}
|
}
|
||||||
@@ -26,6 +28,8 @@ Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, Pin* pinWW, Mqtt* mqtt
|
|||||||
: pinR(pinR), pinG(pinG), pinB(pinB), pinCW(pinCW), pinWW(pinWW), mqttClient(mqttClient) {
|
: pinR(pinR), pinG(pinG), pinB(pinB), pinCW(pinCW), pinWW(pinWW), mqttClient(mqttClient) {
|
||||||
lightInfo.uniqueId = uniqueId;
|
lightInfo.uniqueId = uniqueId;
|
||||||
lightType = LightType::rgbww;
|
lightType = LightType::rgbww;
|
||||||
|
uint8_t bits = pinR->getLedResolutionBits();
|
||||||
|
maxPwm = (bits >= 1 && bits <= 31) ? ((1u << bits) - 1u) : 255u;
|
||||||
publishInitialState();
|
publishInitialState();
|
||||||
subscribeToMqttTopics();
|
subscribeToMqttTopics();
|
||||||
}
|
}
|
||||||
@@ -44,8 +48,11 @@ void Light::publishInitialState() {
|
|||||||
configInfo["unique_id"] = this->lightInfo.uniqueId;
|
configInfo["unique_id"] = this->lightInfo.uniqueId;
|
||||||
configInfo["name"] = this->lightInfo.name;
|
configInfo["name"] = this->lightInfo.name;
|
||||||
configInfo["schema"] = "json";
|
configInfo["schema"] = "json";
|
||||||
// configInfo["json_attributes_topic"] = this->lightInfo.statusTopic;
|
configInfo["json_attributes_topic"] = this->lightInfo.jsonAttributesTopic;
|
||||||
configInfo["command_topic"] = this->lightInfo.commandTopic;
|
configInfo["command_topic"] = this->lightInfo.commandTopic;
|
||||||
|
configInfo["color_temp_kelvin"] = true;
|
||||||
|
configInfo["max_kelvin"] = cwTempKelvin;
|
||||||
|
configInfo["min_kelvin"] = wwTempKelvin;
|
||||||
JsonArray availabilityInfo = configInfo["availability"].to<JsonArray>();
|
JsonArray availabilityInfo = configInfo["availability"].to<JsonArray>();
|
||||||
JsonObject availabilityItem = availabilityInfo.add<JsonObject>();
|
JsonObject availabilityItem = availabilityInfo.add<JsonObject>();
|
||||||
availabilityItem["topic"] = this->lightInfo.availabilityTopic;
|
availabilityItem["topic"] = this->lightInfo.availabilityTopic;
|
||||||
@@ -56,7 +63,7 @@ void Light::publishInitialState() {
|
|||||||
} else if (lightType == LightType::rgbw) {
|
} else if (lightType == LightType::rgbw) {
|
||||||
supportedColorModes.add("rgbw");
|
supportedColorModes.add("rgbw");
|
||||||
} else if (lightType == LightType::rgbww) {
|
} else if (lightType == LightType::rgbww) {
|
||||||
supportedColorModes.add("rgbww");
|
supportedColorModes.add("rgb");
|
||||||
supportedColorModes.add("color_temp");
|
supportedColorModes.add("color_temp");
|
||||||
} else if (lightType == LightType::colorTemperature) {
|
} else if (lightType == LightType::colorTemperature) {
|
||||||
supportedColorModes.add("color_temp");
|
supportedColorModes.add("color_temp");
|
||||||
@@ -66,7 +73,6 @@ void Light::publishInitialState() {
|
|||||||
supportedColorModes.add("onoff");
|
supportedColorModes.add("onoff");
|
||||||
}
|
}
|
||||||
configInfo["state_topic"] = this->lightInfo.stateTopic;
|
configInfo["state_topic"] = this->lightInfo.stateTopic;
|
||||||
// configInfo["state_value_template"] = this->lightInfo.stateValueTemplate;
|
|
||||||
|
|
||||||
std::string configJson;
|
std::string configJson;
|
||||||
serializeJson(configInfo, configJson);
|
serializeJson(configInfo, configJson);
|
||||||
@@ -74,57 +80,82 @@ void Light::publishInitialState() {
|
|||||||
|
|
||||||
std::string stateJson;
|
std::string stateJson;
|
||||||
JsonDocument stateInfo;
|
JsonDocument stateInfo;
|
||||||
stateInfo["state"] = "OFF"; // Initial state is OFF
|
stateInfo["state"] = "OFF";
|
||||||
// stateInfo["availability"] = Availability.available; // Initial availability
|
stateInfo["brightness"] = maxPwmUi;
|
||||||
stateInfo["brightness"] = 0; // Initial brightness
|
brightness = maxPwmUi;
|
||||||
JsonObject rgbValue = stateInfo["rgb_value"].to<JsonObject>();
|
JsonObject color = stateInfo["color"].to<JsonObject>();
|
||||||
rgbValue["r"] = 255;
|
color["r"] = 0;
|
||||||
rgbValue["g"] = 255;
|
r = 0;
|
||||||
rgbValue["b"] = 255;
|
color["g"] = 0;
|
||||||
|
g = 0;
|
||||||
|
color["b"] = 0;
|
||||||
|
b = 0;
|
||||||
serializeJson(stateInfo, stateJson);
|
serializeJson(stateInfo, stateJson);
|
||||||
std::string availabilityJson;
|
std::string availabilityJson;
|
||||||
JsonDocument availabilityInfoDoc;
|
JsonDocument availabilityInfoDoc;
|
||||||
availabilityInfoDoc["availability"] = Availability.available; // Initial availability
|
availabilityInfoDoc["availability"] = Availability.available;
|
||||||
serializeJson(availabilityInfoDoc, availabilityJson);
|
serializeJson(availabilityInfoDoc, availabilityJson);
|
||||||
mqttClient->publish(lightInfo.stateTopic, stateJson);
|
mqttClient->publish(lightInfo.stateTopic, stateJson);
|
||||||
mqttClient->publish(lightInfo.availabilityTopic, availabilityJson);
|
mqttClient->publish(lightInfo.availabilityTopic, availabilityJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Light::operatePin() {
|
void Light::operatePin() {
|
||||||
uint32_t rSetpoint = r;
|
auto clamp8 = [](int v)->uint8_t { return v < 0 ? 0 : (v > 255 ? 255 : v); };
|
||||||
uint32_t gSetpoint = g;
|
|
||||||
uint32_t bSetpoint = b;
|
|
||||||
if (!isOn) {
|
if (!isOn) {
|
||||||
turnOff();
|
turnOff();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
float brightnessFactor = brightness / 255.0f;
|
uint8_t r8 = clamp8(r);
|
||||||
rSetpoint = static_cast<uint32_t>(r * brightnessFactor);
|
uint8_t g8 = clamp8(g);
|
||||||
rSetpoint = correctGamma(rSetpoint);
|
uint8_t b8 = clamp8(b);
|
||||||
// if (rSetpoint < minPwmValue && rSetpoint > 0) {
|
uint8_t cw8 = clamp8(cw);
|
||||||
// rSetpoint = minPwmValue;
|
uint8_t ww8 = clamp8(ww);
|
||||||
// }
|
uint8_t br8 = clamp8(brightness);
|
||||||
gSetpoint = static_cast<uint32_t>(g * brightnessFactor);
|
|
||||||
gSetpoint = correctGamma(gSetpoint);
|
uint32_t rGamma = correctGamma(r8);
|
||||||
// if (gSetpoint < minPwmValue && gSetpoint > 0) {
|
uint32_t gGamma = correctGamma(g8);
|
||||||
// gSetpoint = minPwmValue;
|
uint32_t bGamma = correctGamma(b8);
|
||||||
// }
|
uint32_t cwGamma = correctGamma(cw8);
|
||||||
bSetpoint = static_cast<uint32_t>(b * brightnessFactor);
|
uint32_t wwGamma = correctGamma(ww8);
|
||||||
bSetpoint = correctGamma(bSetpoint);
|
uint32_t brGamma = correctGamma(br8);
|
||||||
// if (bSetpoint < minPwmValue && bSetpoint > 0) {
|
|
||||||
// bSetpoint = minPwmValue;
|
auto mixHw = [this](uint32_t cG, uint32_t bG) -> uint32_t {
|
||||||
// }
|
return static_cast<uint32_t>((static_cast<uint64_t>(cG) * bG + (maxPwm / 2)) / maxPwm);
|
||||||
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);
|
uint32_t rSetpoint = mixHw(rGamma, brGamma);
|
||||||
pinB->setLedLevel(bSetpoint);
|
uint32_t gSetpoint = mixHw(gGamma, brGamma);
|
||||||
if (pinCW != nullptr) {
|
uint32_t bSetpoint = mixHw(bGamma, brGamma);
|
||||||
pinCW->setLedLevel(cw);
|
uint32_t cwSetpoint = 0;
|
||||||
|
uint32_t wwSetpoint = 0;
|
||||||
|
|
||||||
|
if (activeMode == ActiveMode::modeCct) {
|
||||||
|
uint32_t peak = cwGamma > wwGamma ? cwGamma : wwGamma;
|
||||||
|
if (peak > 0) {
|
||||||
|
uint32_t cwGammaUp = static_cast<uint32_t>(
|
||||||
|
(static_cast<uint64_t>(cwGamma) * maxPwm + (peak / 2)) / peak
|
||||||
|
);
|
||||||
|
uint32_t wwGammaUp = static_cast<uint32_t>(
|
||||||
|
(static_cast<uint64_t>(wwGamma) * maxPwm + (peak / 2)) / peak
|
||||||
|
);
|
||||||
|
if (cwGammaUp > maxPwm) cwGammaUp = maxPwm;
|
||||||
|
if (wwGammaUp > maxPwm) wwGammaUp = maxPwm;
|
||||||
|
|
||||||
|
cwSetpoint = mixHw(cwGammaUp, brGamma);
|
||||||
|
wwSetpoint = mixHw(wwGammaUp, brGamma);
|
||||||
|
} else {
|
||||||
|
cwSetpoint = wwSetpoint = 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cwSetpoint = mixHw(cwGamma, brGamma);
|
||||||
|
wwSetpoint = mixHw(wwGamma, brGamma);
|
||||||
}
|
}
|
||||||
if (pinWW != nullptr) {
|
|
||||||
pinWW->setLedLevel(ww);
|
if (pinR) pinR->setLedLevel(rSetpoint);
|
||||||
}
|
if (pinG) pinG->setLedLevel(gSetpoint);
|
||||||
Serial.printf("Set RGB: R=%d, G=%d, B=%d\n", r, g, b);
|
if (pinB) pinB->setLedLevel(bSetpoint);
|
||||||
|
if (pinCW) pinCW->setLedLevel(cwSetpoint);
|
||||||
|
if (pinWW) pinWW->setLedLevel(wwSetpoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Light::subscribeToMqttTopics() {
|
void Light::subscribeToMqttTopics() {
|
||||||
@@ -155,30 +186,22 @@ void Light::handleCommand(const std::string& command) {
|
|||||||
}
|
}
|
||||||
if (commandJson["color"].is<JsonObject>()) {
|
if (commandJson["color"].is<JsonObject>()) {
|
||||||
JsonObject color = commandJson["color"];
|
JsonObject color = commandJson["color"];
|
||||||
r = color["r"] | 255; // Default to 255 if not provided
|
r = color["r"] | maxPwmUi;
|
||||||
g = color["g"] | 255; // Default to 255 if not provided
|
g = color["g"] | maxPwmUi;
|
||||||
b = color["b"] | 255; // Default to 255 if not provided
|
b = color["b"] | maxPwmUi;
|
||||||
if (lightType == LightType::rgbw || lightType == LightType::rgbww) {
|
cw = 0;
|
||||||
ww = color["w"] | 255; // Default to 255 if not provided
|
ww = 0;
|
||||||
}
|
activeMode = ActiveMode::modeRgb;
|
||||||
if (lightType == LightType::rgbww) {
|
}
|
||||||
cw = color["cw"] | 255; // Default to 255 if not provided
|
if (commandJson["color_temp"].is<int>()) {
|
||||||
}
|
colorTemperature = commandJson["color_temp"].as<int>();
|
||||||
|
applyKelvin(colorTemperature);
|
||||||
}
|
}
|
||||||
if (lightType == LightType::rgb || lightType == LightType::rgbw || lightType == LightType::rgbww)
|
if (lightType == LightType::rgb || lightType == LightType::rgbw || lightType == LightType::rgbww)
|
||||||
{
|
{
|
||||||
operatePin();
|
operatePin();
|
||||||
|
publishCurrentState();
|
||||||
}
|
}
|
||||||
publishCurrentState();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Light::turnOn() {
|
|
||||||
isOn = true;
|
|
||||||
if (pinR != nullptr) pinR->setLedLevel(r);
|
|
||||||
if (pinG != nullptr) pinG->setLedLevel(g);
|
|
||||||
if (pinB != nullptr) pinB->setLedLevel(b);
|
|
||||||
if (pinCW != nullptr) pinCW->setLedLevel(cw);
|
|
||||||
if (pinWW != nullptr) pinWW->setLedLevel(ww);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Light::turnOff() {
|
void Light::turnOff() {
|
||||||
@@ -193,35 +216,55 @@ void Light::turnOff() {
|
|||||||
void Light::publishCurrentState() {
|
void Light::publishCurrentState() {
|
||||||
// Publish the current state of the light
|
// Publish the current state of the light
|
||||||
JsonDocument stateInfo;
|
JsonDocument stateInfo;
|
||||||
|
JsonDocument attributeInfo;
|
||||||
stateInfo["state"] = isOn ? "ON" : "OFF";
|
stateInfo["state"] = isOn ? "ON" : "OFF";
|
||||||
stateInfo["availability"] = Availability.available; // Current availability
|
stateInfo["availability"] = Availability.available; // Current availability
|
||||||
stateInfo["brightness"] = brightness;
|
stateInfo["brightness"] = brightness;
|
||||||
if (lightType == LightType::rgb || lightType == LightType::rgbw || lightType == LightType::rgbww) {
|
if (activeMode == ActiveMode::modeRgb) {
|
||||||
JsonObject rgbValue = stateInfo["color"].to<JsonObject>();
|
JsonObject color = stateInfo["color"].to<JsonObject>();
|
||||||
rgbValue["r"] = r;
|
color["r"] = r;
|
||||||
rgbValue["g"] = g;
|
color["g"] = g;
|
||||||
rgbValue["b"] = b;
|
color["b"] = b;
|
||||||
if (lightType == LightType::rgb) {
|
stateInfo["color_mode"] = "rgb";
|
||||||
stateInfo["color_mode"] = "rgb";
|
} else if (activeMode == ActiveMode::modeCct) {
|
||||||
}
|
stateInfo["color_temp"] = colorTemperature;
|
||||||
if (lightType == LightType::rgbw) {
|
stateInfo["color_mode"] = "color_temp";
|
||||||
stateInfo["color_mode"] = "rgbw";
|
|
||||||
rgbValue["w"] = ww;
|
|
||||||
} else if (lightType == LightType::rgbww) {
|
|
||||||
stateInfo["color_mode"] = "rgbww";
|
|
||||||
rgbValue["cw"] = cw;
|
|
||||||
rgbValue["ww"] = ww;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
attributeInfo["pwmR"] = pinR->getLedLevel();
|
||||||
|
attributeInfo["pwmG"] = pinG->getLedLevel();
|
||||||
|
attributeInfo["pwmB"] = pinB->getLedLevel();
|
||||||
|
if (pinCW != nullptr)
|
||||||
|
attributeInfo["pwmCW"] = pinCW->getLedLevel();
|
||||||
|
if (pinWW != nullptr)
|
||||||
|
attributeInfo["pwmWW"] = pinWW->getLedLevel();
|
||||||
|
|
||||||
std::string stateJson;
|
std::string stateJson;
|
||||||
serializeJson(stateInfo, stateJson);
|
serializeJson(stateInfo, stateJson);
|
||||||
Serial.println("Publishing current state: " + String(stateJson.c_str()));
|
Serial.println("Publishing current state: " + String(stateJson.c_str()));
|
||||||
mqttClient->publish(lightInfo.stateTopic, stateJson);
|
mqttClient->publish(lightInfo.stateTopic, stateJson);
|
||||||
|
std::string attributeJson;
|
||||||
|
serializeJson(attributeInfo, attributeJson);
|
||||||
|
Serial.println("Publishing current attributes: " + String(attributeJson.c_str()));
|
||||||
|
mqttClient->publish(lightInfo.jsonAttributesTopic, attributeJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t Light::correctGamma(uint32_t originalPwm) {
|
uint32_t Light::correctGamma(uint32_t originalPwm) {
|
||||||
// Apply gamma correction to the PWM value
|
double normalized = static_cast<double>(originalPwm) / maxPwmUi;
|
||||||
float pwmPercentage = originalPwm / 255.0f;
|
if (normalized <= 0.04045) {
|
||||||
return static_cast<uint32_t>(pow(pwmPercentage, 1 / gammaCorrection) * 255);
|
return static_cast<uint32_t>((normalized / 12.92) * maxPwm);
|
||||||
|
} else {
|
||||||
|
return static_cast<uint32_t>(pow((normalized + 0.055) / 1.055, gammaCorrection) * maxPwm);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Light::applyKelvin(uint32_t kelvin) {
|
||||||
|
if (kelvin > cwTempKelvin) kelvin = cwTempKelvin;
|
||||||
|
if (kelvin < wwTempKelvin) kelvin = wwTempKelvin;
|
||||||
|
double tLin = static_cast<double>(kelvin - wwTempKelvin) / static_cast<double>(cwTempKelvin - wwTempKelvin);
|
||||||
|
r = 0;
|
||||||
|
g = 0;
|
||||||
|
b = 0;
|
||||||
|
cw = static_cast<uint8_t>(tLin * maxPwmUi);
|
||||||
|
ww = static_cast<uint8_t>((1.0 - tLin) * maxPwmUi);
|
||||||
|
activeMode = ActiveMode::modeCct;
|
||||||
}
|
}
|
||||||
@@ -11,6 +11,7 @@ struct LightInfo {
|
|||||||
const std::string baseTopic = "studiotj/smart-rgb/light";
|
const std::string baseTopic = "studiotj/smart-rgb/light";
|
||||||
const std::string availabilityTopic = "studiotj/smart-rgb/light/status";
|
const std::string availabilityTopic = "studiotj/smart-rgb/light/status";
|
||||||
const std::string stateTopic = "studiotj/smart-rgb/light/state";
|
const std::string stateTopic = "studiotj/smart-rgb/light/state";
|
||||||
|
const std::string jsonAttributesTopic = "studiotj/smart-rgb/light/attributes";
|
||||||
const std::string stateValueTemplate = "{{ value_json.state }}";
|
const std::string stateValueTemplate = "{{ value_json.state }}";
|
||||||
const std::string commandTopic = "studiotj/smart-rgb/light/state/set";
|
const std::string commandTopic = "studiotj/smart-rgb/light/state/set";
|
||||||
const std::string brightnessCommandTopic = "studiotj/smart-rgb/light/brightness/set";
|
const std::string brightnessCommandTopic = "studiotj/smart-rgb/light/brightness/set";
|
||||||
@@ -57,6 +58,11 @@ enum LightType {
|
|||||||
rgbww,
|
rgbww,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum ActiveMode {
|
||||||
|
modeRgb,
|
||||||
|
modeCct
|
||||||
|
};
|
||||||
|
|
||||||
class Light {
|
class Light {
|
||||||
public:
|
public:
|
||||||
Light(Pin* pinR, Pin* pinG, Pin* pinB, Mqtt* mqttClient, std::string uniqueId);
|
Light(Pin* pinR, Pin* pinG, Pin* pinB, Mqtt* mqttClient, std::string uniqueId);
|
||||||
@@ -68,20 +74,23 @@ public:
|
|||||||
void setHsl(uint8_t h, uint8_t s, uint8_t l);
|
void setHsl(uint8_t h, uint8_t s, uint8_t l);
|
||||||
void setColorTemperature(uint16_t temperature);
|
void setColorTemperature(uint16_t temperature);
|
||||||
void setBrightness(uint8_t brightness);
|
void setBrightness(uint8_t brightness);
|
||||||
void turnOn();
|
|
||||||
void turnOff();
|
void turnOff();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void handleCommand(const std::string& command);
|
void handleCommand(const std::string& command);
|
||||||
void operatePin();
|
void operatePin();
|
||||||
uint32_t correctGamma(uint32_t originalPwm);
|
uint32_t correctGamma(uint32_t originalPwm);
|
||||||
uint8_t r = 255; // Default to white
|
void applyKelvin(uint32_t kelvin);
|
||||||
uint8_t g = 255; // Default to white
|
uint8_t r = 0; // Default to white
|
||||||
uint8_t b = 255; // 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 cw = 255; // Default to white
|
||||||
uint8_t ww = 255; // Default to white
|
uint8_t ww = 255; // Default to white
|
||||||
|
const uint32_t cwTempKelvin = 6000;
|
||||||
|
const uint32_t wwTempKelvin = 3000;
|
||||||
uint16_t colorTemperature;
|
uint16_t colorTemperature;
|
||||||
uint8_t brightness;
|
uint8_t brightness;
|
||||||
|
uint32_t maxPwm;
|
||||||
bool isOn = false;
|
bool isOn = false;
|
||||||
Pin* pinR;
|
Pin* pinR;
|
||||||
Pin* pinG;
|
Pin* pinG;
|
||||||
@@ -92,4 +101,5 @@ private:
|
|||||||
LightInfo lightInfo;
|
LightInfo lightInfo;
|
||||||
DeviceInfo deviceInfo;
|
DeviceInfo deviceInfo;
|
||||||
LightType lightType = onOff; // Default light type
|
LightType lightType = onOff; // Default light type
|
||||||
|
ActiveMode activeMode = modeRgb;
|
||||||
};
|
};
|
||||||
@@ -5,11 +5,15 @@ Pin::Pin(int pinNumber, bool isOutput, bool isLed, uint32_t ledFrequency, uint8_
|
|||||||
: pinNumber(pinNumber), output(isOutput), isLed(isLed), ledChannel(ledChannel) {
|
: pinNumber(pinNumber), output(isOutput), isLed(isLed), ledChannel(ledChannel) {
|
||||||
pinMode(pinNumber, isOutput ? OUTPUT : INPUT);
|
pinMode(pinNumber, isOutput ? OUTPUT : INPUT);
|
||||||
if (isLed) {
|
if (isLed) {
|
||||||
ledcSetup(ledChannel, ledFrequency, 8); // Setup LEDC for PWM with 8-bit resolution
|
ledcSetup(ledChannel, ledFrequency, ledResolutionBits); // Setup LEDC for PWM with 8-bit resolution
|
||||||
ledcAttachPin(pinNumber, ledChannel); // Attach the pin to the LEDC channel
|
ledcAttachPin(pinNumber, ledChannel); // Attach the pin to the LEDC channel
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const uint8_t Pin::getLedResolutionBits() const {
|
||||||
|
return ledResolutionBits;
|
||||||
|
}
|
||||||
|
|
||||||
void Pin::setHigh() {
|
void Pin::setHigh() {
|
||||||
if (output) {
|
if (output) {
|
||||||
digitalWrite(pinNumber, HIGH);
|
digitalWrite(pinNumber, HIGH);
|
||||||
@@ -25,8 +29,12 @@ void Pin::setLow() {
|
|||||||
void Pin::setLedLevel(uint32_t level) {
|
void Pin::setLedLevel(uint32_t level) {
|
||||||
if (output && isLed) {
|
if (output && isLed) {
|
||||||
ledcWrite(ledChannel, level);
|
ledcWrite(ledChannel, level);
|
||||||
|
ledLevel = level;
|
||||||
}
|
}
|
||||||
// analogWrite(pinNumber, level); // Use analogWrite for PWM control
|
}
|
||||||
|
|
||||||
|
uint32_t Pin::getLedLevel() const {
|
||||||
|
return ledLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Pin::read() {
|
bool Pin::read() {
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ public:
|
|||||||
void setHigh();
|
void setHigh();
|
||||||
void setLow();
|
void setLow();
|
||||||
void setLedLevel(uint32_t level);
|
void setLedLevel(uint32_t level);
|
||||||
|
uint32_t getLedLevel() const;
|
||||||
|
const uint8_t getLedResolutionBits() const;
|
||||||
bool read();
|
bool read();
|
||||||
int getPinNumber() const;
|
int getPinNumber() const;
|
||||||
bool isOutput() const;
|
bool isOutput() const;
|
||||||
@@ -13,6 +15,8 @@ public:
|
|||||||
private:
|
private:
|
||||||
uint8_t ledChannel = 0; // LED channel for PWM
|
uint8_t ledChannel = 0; // LED channel for PWM
|
||||||
uint8_t pinNumber;
|
uint8_t pinNumber;
|
||||||
|
uint32_t ledLevel = 0;
|
||||||
|
const uint8_t ledResolutionBits = 12;
|
||||||
bool output;
|
bool output;
|
||||||
bool isLed = false; // Flag to indicate if this pin is used for LED control
|
bool isLed = false; // Flag to indicate if this pin is used for LED control
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user