]> oss.titaniummirror.com Git - rgblamp.git/blobdiff - rgb.c
Perceived intensities via the CIE Lighting formula
[rgblamp.git] / rgb.c
diff --git a/rgb.c b/rgb.c
index d2d6a805b629a054f73808a497054bf7480e0112..176d4d7ce303809259ba8df90b1d651d9f235fed 100644 (file)
--- a/rgb.c
+++ b/rgb.c
@@ -5,10 +5,65 @@
  */
 
 #include <htc.h>
+#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