diff --git a/code/espurna/light.ino b/code/espurna/light.ino index 2449a923..8f3f7038 100644 --- a/code/espurna/light.ino +++ b/code/espurna/light.ino @@ -18,6 +18,39 @@ 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) +#endif + +#ifndef LIGHT_PWM_RANGE + #define LIGHT_PWM_RANGE (255) +#endif + // ----------------------------------------------------------------------------- // UTILS // ----------------------------------------------------------------------------- @@ -103,6 +136,26 @@ 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; + + #if ENABLE_GAMMA_CORRECTION + pwm = (intensity < GAMMA_TABLE_SIZE) ? gamma_table[intensity] : LIGHT_PWM_RANGE; + #else + // 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 + + #if RGBW_INVERSE_LOGIC != 1 + pwm = LIGHT_PWM_RANGE - pwm; + #endif + + return pwm; +} + // ----------------------------------------------------------------------------- // PROVIDER // ----------------------------------------------------------------------------- @@ -129,21 +182,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); - #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_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 #endif } @@ -236,8 +280,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);