From: R. Steve McKown Date: Fri, 16 Dec 2011 03:03:41 +0000 (-0700) Subject: Perceived intensities via the CIE Lighting formula X-Git-Tag: 1.0~9 X-Git-Url: https://oss.titaniummirror.com/gitweb?p=rgblamp.git;a=commitdiff_plain;h=b3ddf1998eea5cda9f775b84d14fba96090b8c2f Perceived intensities via the CIE Lighting formula --- diff --git a/rgb.c b/rgb.c index d2d6a80..176d4d7 100644 --- a/rgb.c +++ b/rgb.c @@ -5,10 +5,65 @@ */ #include +#include "rgb.h" #define PINS_PORTB 0b00101001 #define PINS_PORTC 0b00000100 +/* To get a perceived linear output, the PWM value must be adjusted according + * to CIE Lightness. The human eye perceives changes at lower intensities + * more readily that at higher intensities. + * + * L* = 116(Y/Yn)^1/3 - 16 , Y/Yn > 0.008856 + * L* = 903.3(Y/Yn), Y/Yn <= 0.008856 +*/ + +#ifdef CEIL256 +const unsigned char pwm_table[] = { + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 2, 2, 2, 2, 2, + 2, 2, 2, 3, 3, 3, 3, 3, + 3, 3, 4, 4, 4, 4, 4, 5, + 5, 5, 5, 5, 6, 6, 6, 6, + 6, 7, 7, 7, 7, 8, 8, 8, + 8, 9, 9, 9, 10, 10, 10, 11, + 11, 11, 12, 12, 12, 13, 13, 13, + 14, 14, 14, 15, 15, 16, 16, 17, + 17, 17, 18, 18, 19, 19, 20, 20, + 21, 21, 22, 22, 23, 23, 24, 24, + 25, 25, 26, 27, 27, 28, 28, 29, + 30, 30, 31, 31, 32, 33, 33, 34, + 35, 35, 36, 37, 38, 38, 39, 40, + 41, 41, 42, 43, 44, 45, 45, 46, + 47, 48, 49, 50, 51, 51, 52, 53, + 54, 55, 56, 57, 58, 59, 60, 61, + 62, 63, 64, 65, 66, 67, 68, 69, + 70, 71, 73, 74, 75, 76, 77, 78, + 80, 81, 82, 83, 84, 86, 87, 88, + 90, 91, 92, 93, 95, 96, 98, 99, + 100, 102, 103, 105, 106, 107, 109, 110, + 112, 113, 115, 116, 118, 120, 121, 123, + 124, 126, 128, 129, 131, 133, 134, 136, + 138, 139, 141, 143, 145, 146, 148, 150, + 152, 154, 156, 157, 159, 161, 163, 165, + 167, 169, 171, 173, 175, 177, 179, 181, + 183, 185, 187, 189, 192, 194, 196, 198, + 200, 203, 205, 207, 209, 212, 214, 216, + 218, 221, 223, 226, 228, 230, 233, 235, + 238, 240, 243, 245, 248, 250, 253, 255 +}; +#else +#ifdef CEIL32 +const unsigned char pwm_table[] = { + 0, 1, 2, 3, 4, 5, 7, 9, + 12, 15, 18, 22, 27, 32, 38, 44, + 51, 58, 67, 76, 86, 96, 108, 120, + 134, 148, 163, 180, 197, 216, 235, 255 +}; +#endif +#endif + void rgb_init() { /* Initialize rgb diff --git a/rgb.h b/rgb.h index 4456c80..b2149ed 100644 --- a/rgb.h +++ b/rgb.h @@ -10,6 +10,10 @@ #include "isr.h" +/* Optional brightness compensation. Pick zero or one */ +#define CEIL256 +#undef CEIL32 + /* Initialize the RGB LED assembly. Outputs are zero, PWM is off. */ void rgb_init(); @@ -20,6 +24,28 @@ void rgb_init(); void rgb_off(); /* Set a PWM value for each color LED. 0=off, 255=full on. */ +#ifdef CEIL256 +extern const unsigned char pwm_table[]; +#define rgb_set(red, grn, blu, wht) do { \ + ndi(); /* FIXME: doesn't seem to fix flicker */ \ + CCPR3L = pwm_table[(red)]; \ + CCPR2L = pwm_table[(grn)]; \ + CCPR4L = pwm_table[(blu)]; \ + CCPR1L = pwm_table[(wht)]; \ + nei(); \ +} while (0) +#else +#ifdef CEIL32 +extern const unsigned char pwm_table[]; +#define rgb_set(red, grn, blu, wht) do { \ + ndi(); /* FIXME: doesn't seem to fix flicker */ \ + CCPR3L = pwm_table[(red) >> 3]; \ + CCPR2L = pwm_table[(grn) >> 3]; \ + CCPR4L = pwm_table[(blu) >> 3]; \ + CCPR1L = pwm_table[(wht) >> 3]; \ + nei(); \ +} while (0) +#else #define rgb_set(red, grn, blu, wht) do { \ ndi(); /* FIXME: doesn't seem to fix flicker */ \ CCPR3L = (red); \ @@ -28,5 +54,7 @@ void rgb_off(); CCPR1L = (wht); \ nei(); \ } while (0) +#endif +#endif #endif