From bcd2bf807fce3bbef989166fa6adaec8ce97ffbd Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Tue, 15 Oct 2019 18:12:56 +0300 Subject: [PATCH] light: configure colortemp limits from settings --- code/espurna/config/general.h | 8 -------- code/espurna/light.h | 8 -------- code/espurna/light.ino | 26 +++++++++++++++++++++----- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 70e79c20..f76a7bf8 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -1220,14 +1220,6 @@ #define LIGHT_WARMWHITE_MIRED 500 // Warmwhite Strip, Value must be __ABOVE__ W1!! (Default: 2000 Kelvin/500 MiRed) #endif -#ifndef LIGHT_COLDWHITE_KELVIN -#define LIGHT_COLDWHITE_KELVIN 6536 -#endif - -#ifndef LIGHT_WARMWHITE_KELVIN -#define LIGHT_WARMWHITE_KELVIN 2000 -#endif - #ifndef LIGHT_STEP #define LIGHT_STEP 32 // Step size #endif diff --git a/code/espurna/light.h b/code/espurna/light.h index 68f5bb4a..e59233c8 100644 --- a/code/espurna/light.h +++ b/code/espurna/light.h @@ -11,14 +11,6 @@ namespace Light { constexpr const long BRIGHTNESS_MIN = LIGHT_MIN_BRIGHTNESS; constexpr const long BRIGHTNESS_MAX = LIGHT_MAX_BRIGHTNESS; - // Default to the Philips Hue value that HA also use. - // https://developers.meethue.com/documentation/core-concepts - constexpr const long MIREDS_COLDWHITE = LIGHT_COLDWHITE_MIRED; - constexpr const long MIREDS_WARMWHITE = LIGHT_WARMWHITE_MIRED; - - constexpr const long KELVIN_WARMWHITE = LIGHT_WARMWHITE_KELVIN; - constexpr const long KELVIN_COLDWHITE = LIGHT_COLDWHITE_KELVIN; - constexpr const long PWM_MIN = LIGHT_MIN_PWM; constexpr const long PWM_MAX = LIGHT_MAX_PWM; constexpr const long PWM_LIMIT = LIGHT_LIMIT_PWM; diff --git a/code/espurna/light.ino b/code/espurna/light.ino index f3d7b73c..6e0a5dc4 100644 --- a/code/espurna/light.ino +++ b/code/espurna/light.ino @@ -56,7 +56,16 @@ unsigned int _light_transition_time = LIGHT_TRANSITION_TIME; bool _light_dirty = false; bool _light_state = false; unsigned char _light_brightness = Light::BRIGHTNESS_MAX; -unsigned int _light_mireds = lround((Light::MIREDS_COLDWHITE + Light::MIREDS_WARMWHITE) / 2); + +// Default to the Philips Hue value that HA also use. +// https://developers.meethue.com/documentation/core-concepts +long _light_mireds_cold = LIGHT_COLDWHITE_MIRED; +long _light_mireds_warm = LIGHT_WARMWHITE_MIRED; + +long _light_kelvin_cold = (1000000L / _light_mireds_cold); +long _light_kelvin_warm = (1000000L / _light_mireds_warm); + +long _light_mireds = lround((_light_mireds_cold + _light_mireds_warm) / 2L); using light_brightness_func_t = void(); light_brightness_func_t* _light_brightness_func = nullptr; @@ -154,7 +163,7 @@ void _lightApplyBrightnessColor() { if (_light_use_cct) { // This change the range from 153-500 to 0-347 so we get a value between 0 and 1 in the end. - double miredFactor = ((double) _light_mireds - (double) Light::MIREDS_COLDWHITE)/((double) Light::MIREDS_WARMWHITE - (double) Light::MIREDS_COLDWHITE); + double miredFactor = ((double) _light_mireds - (double) _light_mireds_cold)/((double) _light_mireds_warm - (double) _light_mireds_cold); // set cold white _light_channel[3].inputValue = 0; @@ -320,11 +329,11 @@ void _fromHSV(const char * hsv) { // https://github.com/stelgenhof/AiLight // Color temperature is measured in mireds (kelvin = 1e6/mired) long _toKelvin(const long mireds) { - return constrain(static_cast(1000000L / mireds), Light::KELVIN_WARMWHITE, Light::KELVIN_COLDWHITE); + return constrain(static_cast(1000000L / mireds), _light_kelvin_warm, _light_kelvin_cold); } long _toMireds(const long kelvin) { - return constrain(static_cast(lround(1000000L / kelvin)), Light::MIREDS_COLDWHITE, Light::MIREDS_WARMWHITE); + return constrain(static_cast(lround(1000000L / kelvin)), _light_mireds_cold, _light_mireds_warm); } void _lightMireds(const long kelvin) { @@ -335,7 +344,7 @@ void _lightMiredsCCT(const long kelvin) { _lightMireds(kelvin); // This change the range from 153-500 to 0-347 so we get a value between 0 and 1 in the end. - const double factor = ((double) _light_mireds - (double) Light::MIREDS_COLDWHITE)/((double) Light::MIREDS_WARMWHITE - (double) Light::MIREDS_COLDWHITE); + const double factor = ((double) _light_mireds - (double) _light_mireds_cold)/((double) _light_mireds_warm - (double) _light_mireds_cold); _setCCTInputValue( lround(factor * Light::VALUE_MAX), lround(((double) 1.0 - factor) * Light::VALUE_MAX) @@ -1270,6 +1279,13 @@ void _lightConfigure() { setSetting("useCCT", _light_use_cct); } + if (_light_use_cct) { + _light_mireds_cold = getSetting("lightColdMired", LIGHT_COLDWHITE_MIRED).toInt(); + _light_mireds_warm = getSetting("lightWarmMired", LIGHT_WARMWHITE_MIRED).toInt(); + _light_kelvin_cold = (1000000L / _light_mireds_cold); + _light_kelvin_warm = (1000000L / _light_mireds_warm); + } + _light_use_gamma = getSetting("useGamma", LIGHT_USE_GAMMA).toInt() == 1; _light_use_transitions = getSetting("useTransitions", LIGHT_USE_TRANSITIONS).toInt() == 1; _light_transition_time = getSetting("lightTime", LIGHT_TRANSITION_TIME).toInt();