From bf68e8b94608641e69c1abd1a0ad33fd04379f5e Mon Sep 17 00:00:00 2001 From: Chris Ward Date: Mon, 17 Apr 2017 23:08:48 +0100 Subject: [PATCH 1/4] 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); From b078c8117fd3ec3c78689341f536174efaa6a677 Mon Sep 17 00:00:00 2001 From: Chris Ward Date: Tue, 18 Apr 2017 00:02:41 +0100 Subject: [PATCH 2/4] Added optional gamma correction for light --- code/espurna/light.ino | 48 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/code/espurna/light.ino b/code/espurna/light.ino index 9be59fe7..380ad68e 100644 --- a/code/espurna/light.ino +++ b/code/espurna/light.ino @@ -18,12 +18,37 @@ unsigned int _lightColor[3] = {0}; my9291 * _my9291; #endif +#if ENABLE_GAMMA_CORRECTION + #define GAMMA_TABLE_SIZE (256) + #undef LIGHT_PWM_RANGE + #define LIGHT_PWM_RANGE (4095) + + // Gamma Correction lookup table for gamma=2.8 and 12 bit (4095) full scale + const unsigned short gamma_table[GAMMA_TABLE_SIZE] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, + 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 8, 9, 10, 11, + 12, 13, 15, 16, 17, 18, 20, 21, 23, 25, 26, 28, 30, 32, 34, 36, + 38, 40, 43, 45, 48, 50, 53, 56, 59, 62, 65, 68, 71, 75, 78, 82, + 85, 89, 93, 97, 101, 105, 110, 114, 119, 123, 128, 133, 138, 143, 149, 154, + 159, 165, 171, 177, 183, 189, 195, 202, 208, 215, 222, 229, 236, 243, 250, 258, + 266, 273, 281, 290, 298, 306, 315, 324, 332, 341, 351, 360, 369, 379, 389, 399, + 409, 419, 430, 440, 451, 462, 473, 485, 496, 508, 520, 532, 544, 556, 569, 582, + 594, 608, 621, 634, 648, 662, 676, 690, 704, 719, 734, 749, 764, 779, 795, 811, + 827, 843, 859, 876, 893, 910, 927, 944, 962, 980, 998,1016,1034,1053,1072,1091, + 1110,1130,1150,1170,1190,1210,1231,1252,1273,1294,1316,1338,1360,1382,1404,1427, + 1450,1473,1497,1520,1544,1568,1593,1617,1642,1667,1693,1718,1744,1770,1797,1823, + 1850,1877,1905,1932,1960,1988,2017,2045,2074,2103,2133,2162,2192,2223,2253,2284, + 2315,2346,2378,2410,2442,2474,2507,2540,2573,2606,2640,2674,2708,2743,2778,2813, + 2849,2884,2920,2957,2993,3030,3067,3105,3143,3181,3219,3258,3297,3336,3376,3416, + 3456,3496,3537,3578,3619,3661,3703,3745,3788,3831,3874,3918,3962,4006,4050,4095 }; +#endif + #ifndef LIGHT_PWM_FREQUENCY - #define LIGHT_PWM_FREQUENCY (1000) + #define LIGHT_PWM_FREQUENCY (1000) #endif #ifndef LIGHT_PWM_RANGE - #define LIGHT_PWM_RANGE (255) + #define LIGHT_PWM_RANGE (255) #endif // ----------------------------------------------------------------------------- @@ -116,12 +141,17 @@ void color_temperature2array(unsigned int temperature, unsigned int * array) { 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 ENABLE_GAMMA_CORRECTION + pwm = (intensity < GAMMA_TABLE_SIZE) ? gamma_table[intensity] : PWM_RANGE; + #else + // Support integer multiples of 256 (-1) for the PWM_RANGE + // The divide should happen at compile time + pwm = intensity * ( (LIGHT_PWM_RANGE+1) / (LIGHT_MAX_VALUE+1) ); + #endif #if RGBW_INVERSE_LOGIC != 1 - pwm = LIGHT_PWM_RANGE - pwm; + pwm = LIGHT_PWM_RANGE - pwm; #endif return pwm; @@ -156,9 +186,9 @@ void _lightProviderSet(bool state, unsigned int red, unsigned int green, unsigne 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, intensity2pwm(white)); - #endif + #if (LIGHT_PROVIDER == LIGHT_PROVIDER_RGBW) + analogWrite(RGBW_WHITE_PIN, intensity2pwm(white)); + #endif #endif } From 4fb49d8ebcda80cf5c370271c4c426b7c06f2a9a Mon Sep 17 00:00:00 2001 From: Chris Ward Date: Mon, 24 Apr 2017 22:44:03 +0100 Subject: [PATCH 3/4] Coding style changes --- code/espurna/light.ino | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/code/espurna/light.ino b/code/espurna/light.ino index 380ad68e..473efe95 100644 --- a/code/espurna/light.ino +++ b/code/espurna/light.ino @@ -138,8 +138,7 @@ void color_temperature2array(unsigned int temperature, unsigned int * array) { // 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 _intensity2pwm(unsigned int intensity) { unsigned int pwm; #if ENABLE_GAMMA_CORRECTION @@ -183,11 +182,11 @@ void _lightProviderSet(bool state, unsigned int red, unsigned int green, unsigne // Check state if (!state) red = green = blue = white = 0; - analogWrite(RGBW_RED_PIN, intensity2pwm(red)); - analogWrite(RGBW_GREEN_PIN, intensity2pwm(green)); - analogWrite(RGBW_BLUE_PIN, intensity2pwm(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, intensity2pwm(white)); + analogWrite(RGBW_WHITE_PIN, _intensity2pwm(white)); #endif #endif From 0a46e20de44f354f72dac465b35c8382787b81ac Mon Sep 17 00:00:00 2001 From: Chris Ward Date: Thu, 27 Apr 2017 21:33:59 +0100 Subject: [PATCH 4/4] Chnaged remaining PWM_RANGE to LIGHT_PWM_RANGE --- code/espurna/light.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/espurna/light.ino b/code/espurna/light.ino index 473efe95..8f3f7038 100644 --- a/code/espurna/light.ino +++ b/code/espurna/light.ino @@ -142,9 +142,9 @@ unsigned int _intensity2pwm(unsigned int intensity) { unsigned int pwm; #if ENABLE_GAMMA_CORRECTION - pwm = (intensity < GAMMA_TABLE_SIZE) ? gamma_table[intensity] : PWM_RANGE; + pwm = (intensity < GAMMA_TABLE_SIZE) ? gamma_table[intensity] : LIGHT_PWM_RANGE; #else - // Support integer multiples of 256 (-1) for the PWM_RANGE + // 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) ); #endif