From 7533650d96a565dc0c90190d4a72e0bfee48212a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Wed, 16 Aug 2017 22:40:40 +0200 Subject: [PATCH] Prevent division by zero error in color temperature calculation --- code/espurna/light.ino | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/code/espurna/light.ino b/code/espurna/light.ino index d93a5a11..e2dfd0bf 100644 --- a/code/espurna/light.ino +++ b/code/espurna/light.ino @@ -128,7 +128,8 @@ void _color_array2rgb(unsigned int * array, float brightness, char * rgb) { void _color_temperature2array(unsigned long mireds, unsigned int * array) { // Force boundaries and conversion - kelvin = constrain(1000000UL / mireds, 1000, 40000) / 100; + if (mireds == 0) mireds = 1; + unsigned long kelvin = constrain(1000000UL / mireds, 1000, 40000) / 100; // Calculate colors unsigned int red = (kelvin <= 66) @@ -174,7 +175,6 @@ unsigned int _intensity2pwm(unsigned int intensity) { return _intensity2pwm(intensity, LIGHT_MAX_VALUE); } - // ----------------------------------------------------------------------------- // PROVIDER // ----------------------------------------------------------------------------- @@ -193,7 +193,11 @@ void _lightProviderSet(bool state, unsigned int red, unsigned int green, unsigne #if LIGHT_PROVIDER == LIGHT_PROVIDER_MY9192 _my9291->setState(state); - _my9291->setColor((my9291_color_t) { red * brightness, green * brightness, blue * brightness, white * brightness }); + red *= brightness; + green *= brightness; + blue *= brightness; + white *= brightness; + _my9291->setColor((my9291_color_t) { red, green, blue, white }); #endif #if (LIGHT_PROVIDER == LIGHT_PROVIDER_RGB) || (LIGHT_PROVIDER == LIGHT_PROVIDER_RGBW) || (LIGHT_PROVIDER == LIGHT_PROVIDER_RGB2W) @@ -209,7 +213,7 @@ void _lightProviderSet(bool state, unsigned int red, unsigned int green, unsigne #endif #if (LIGHT_PROVIDER == LIGHT_PROVIDER_RGB2W) analogWrite(RGBW_WHITE_PIN, _intensity2pwm(white, brightness)); - analogWrite(RGBW_WHITE2_PIN, _intensity2pwm(white,brightness)); + analogWrite(RGBW_WHITE2_PIN, _intensity2pwm(white, brightness)); #endif #endif