From ec259767172d2e7217f11e1002bafc28d8d45b3c Mon Sep 17 00:00:00 2001 From: Max Prokhorov Date: Fri, 30 Aug 2019 07:17:55 +0300 Subject: [PATCH] domoticz: handle cmode=2 resolve #1763 --- code/espurna/config/prototypes.h | 18 ++++++++++++ code/espurna/domoticz.ino | 47 ++++++++++++++++++++------------ 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h index 8259d3d5..50661a36 100644 --- a/code/espurna/config/prototypes.h +++ b/code/espurna/config/prototypes.h @@ -161,6 +161,24 @@ int16_t i2c_read_int16(uint8_t address, uint8_t reg); int16_t i2c_read_int16_le(uint8_t address, uint8_t reg); void i2c_read_buffer(uint8_t address, uint8_t * buffer, size_t len); +// ----------------------------------------------------------------------------- +// Lights +// ----------------------------------------------------------------------------- + +unsigned char lightChannels(); + +void lightState(unsigned char i, bool state); +bool lightState(unsigned char i); + +void lightState(bool state); +bool lightState(); + +void lightBrightness(unsigned int brightness); +unsigned int lightBrightness(); + +unsigned int lightChannel(unsigned char id); +void lightChannel(unsigned char id, unsigned char value); + // ----------------------------------------------------------------------------- // MQTT // ----------------------------------------------------------------------------- diff --git a/code/espurna/domoticz.ino b/code/espurna/domoticz.ino index b4893732..02b6588c 100644 --- a/code/espurna/domoticz.ino +++ b/code/espurna/domoticz.ino @@ -57,10 +57,34 @@ void _domoticzLight(unsigned int idx, const JsonObject& root) { JsonObject& color = root["Color"]; if (!color.success()) return; + // for ColorMode... see: + // https://github.com/domoticz/domoticz/blob/development/hardware/ColorSwitch.h + // https://www.domoticz.com/wiki/Domoticz_API/JSON_URL's#Set_a_light_to_a_certain_color_or_color_temperature + + DEBUG_MSG_P(PSTR("[DOMOTICZ] Received rgb:%u,%u,%u ww:%u,cw:%u t:%u brightness:%u for IDX %u\n"), + color["r"].as(), + color["g"].as(), + color["b"].as(), + color["ww"].as(), + color["cw"].as(), + color["t"].as(), + color["Level"].as(), + idx + ); + // m field contains information about color mode (enum ColorMode from domoticz ColorSwitch.h): unsigned int cmode = color["m"]; - if (cmode == 3 || cmode == 4) { // ColorModeRGB or ColorModeCustom - see domoticz ColorSwitch.h + if (cmode == 2) { // ColorModeWhite - WW,CW,temperature (t unused for now) + + if (lightChannels() < 2) return; + + lightChannel(0, color["ww"]); + lightChannel(1, color["cw"]); + + } else if (cmode == 3 || cmode == 4) { // ColorModeRGB or ColorModeCustom + + if (lightChannels() < 3) return; lightChannel(0, color["r"]); lightChannel(1, color["g"]); @@ -71,29 +95,16 @@ void _domoticzLight(unsigned int idx, const JsonObject& root) { if (lightChannels() > 3) { lightChannel(3, color["ww"]); } - if (lightChannels() > 4) { lightChannel(4, color["cw"]); } - // domoticz uses 100 as maximum value while we're using Light::BRIGHTNESS_MAX (unsigned char) - unsigned char brightness = (root["Level"].as() / 100.0) * Light::BRIGHTNESS_MAX; - lightBrightness(brightness); - - DEBUG_MSG_P(PSTR("[DOMOTICZ] Received rgb:%u,%u,%u ww:%u,cw:%u brightness:%u for IDX %u\n"), - color["r"].as(), - color["g"].as(), - color["b"].as(), - color["ww"].as(), - color["cw"].as(), - brightness, - idx - ); - - lightUpdate(true, mqttForward()); - } + // domoticz uses 100 as maximum value while we're using Light::BRIGHTNESS_MAX (unsigned char) + lightBrightness((root["Level"].as() / 100.0) * Light::BRIGHTNESS_MAX); + lightUpdate(true, mqttForward()); + } #endif