From bf68e8b94608641e69c1abd1a0ad33fd04379f5e Mon Sep 17 00:00:00 2001 From: Chris Ward Date: Mon, 17 Apr 2017 23:08:48 +0100 Subject: [PATCH] Added support for setting light PWM frequency and range --- code/espurna/light.ino | 47 ++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/code/espurna/light.ino b/code/espurna/light.ino index 2449a923..9be59fe7 100644 --- a/code/espurna/light.ino +++ b/code/espurna/light.ino @@ -18,6 +18,14 @@ unsigned int _lightColor[3] = {0}; my9291 * _my9291; #endif +#ifndef LIGHT_PWM_FREQUENCY + #define LIGHT_PWM_FREQUENCY (1000) +#endif + +#ifndef LIGHT_PWM_RANGE + #define LIGHT_PWM_RANGE (255) +#endif + // ----------------------------------------------------------------------------- // UTILS // ----------------------------------------------------------------------------- @@ -103,6 +111,22 @@ void color_temperature2array(unsigned int temperature, unsigned int * array) { array[2] = constrain(blue, 0, LIGHT_MAX_VALUE); } +// Converts a color intensity value (0..255) to a pwm value +// This takes care of positive or negative logic +unsigned int intensity2pwm(unsigned int intensity) +{ + unsigned int pwm; + // Support integer multiples of 256 (-1) for the LIGHT_PWM_RANGE + // The divide should happen at compile time + pwm = intensity * ((LIGHT_PWM_RANGE+1) / (LIGHT_MAX_VALUE+1)); + + #if RGBW_INVERSE_LOGIC != 1 + pwm = LIGHT_PWM_RANGE - pwm; + #endif + + return pwm; +} + // ----------------------------------------------------------------------------- // PROVIDER // ----------------------------------------------------------------------------- @@ -129,21 +153,12 @@ void _lightProviderSet(bool state, unsigned int red, unsigned int green, unsigne // Check state if (!state) red = green = blue = white = 0; - if (RGBW_INVERSE_LOGIC) { - analogWrite(RGBW_RED_PIN, red); - analogWrite(RGBW_GREEN_PIN, green); - analogWrite(RGBW_BLUE_PIN, blue); + analogWrite(RGBW_RED_PIN, intensity2pwm(red)); + analogWrite(RGBW_GREEN_PIN, intensity2pwm(green)); + analogWrite(RGBW_BLUE_PIN, intensity2pwm(blue)); #if (LIGHT_PROVIDER == LIGHT_PROVIDER_RGBW) - analogWrite(RGBW_WHITE_PIN, white); - #endif - } else { - analogWrite(RGBW_RED_PIN, LIGHT_MAX_VALUE - red); - analogWrite(RGBW_GREEN_PIN, LIGHT_MAX_VALUE - green); - analogWrite(RGBW_BLUE_PIN, LIGHT_MAX_VALUE - blue); - #if (LIGHT_PROVIDER == LIGHT_PROVIDER_RGBW) - analogWrite(RGBW_WHITE_PIN, LIGHT_MAX_VALUE - white); - #endif - } + analogWrite(RGBW_WHITE_PIN, intensity2pwm(white)); + #endif #endif } @@ -236,8 +251,8 @@ void lightSetup() { #endif #if (LIGHT_PROVIDER == LIGHT_PROVIDER_RGB) || (LIGHT_PROVIDER == LIGHT_PROVIDER_RGBW) - analogWriteRange(LIGHT_MAX_VALUE); - analogWriteFreq(1000); + analogWriteRange(LIGHT_PWM_RANGE); + analogWriteFreq(LIGHT_PWM_FREQUENCY); pinMode(RGBW_RED_PIN, OUTPUT); pinMode(RGBW_GREEN_PIN, OUTPUT); pinMode(RGBW_BLUE_PIN, OUTPUT);