- Code: Select all | TOGGLE FULL SIZE
diff --git a/Arduino/LEDstream_LPD8806/LEDstream_LPD8806.pde b/Arduino/LEDstream_LPD8806/LEDstream_LPD8806.pde
index 1fc79f0..9f995c6 100644
--- a/Arduino/LEDstream_LPD8806/LEDstream_LPD8806.pde
+++ b/Arduino/LEDstream_LPD8806/LEDstream_LPD8806.pde
@@ -178,7 +178,7 @@ void loop() {
buffer[byteNum++] = c; // Store in data buffer
if(byteNum == 3) { // Have a full LED's worth?
while(byteNum > 0) { // Issue data in LPD8806 order...
- i = 0x80 | (buffer[byteOrder[--byteNum]] >> 1);
+ i = 0x80 | gamma(buffer[byteOrder[--byteNum]]);
while(!(SPSR & _BV(SPIF))); // Wait for prior byte out
SPDR = i; // Issue new byte
}
@@ -230,3 +230,43 @@ static boolean timeout(
return false; // No timeout
}
+// Gamma correction compensates for our eyes' nonlinear perception of
+// intensity. It's the LAST step before a pixel value is stored, and
+// allows intermediate rendering/processing to occur in linear space.
+// The table contains 256 elements (8 bit input), though the outputs are
+// only 7 bits (0 to 127). This is normal and intentional by design: it
+// allows all the rendering code to operate in the more familiar unsigned
+// 8-bit colorspace (used in a lot of existing graphics code), and better
+// preserves accuracy where repeated color blending operations occur.
+// Only the final end product is converted to 7 bits, the native format
+// for the LPD8806 LED driver. Gamma correction and 7-bit decimation
+// thus occur in a single operation.
+PROGMEM prog_uchar gammaTable[] = {
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4,
+ 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7,
+ 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11,
+ 11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15, 16, 16,
+ 16, 17, 17, 17, 18, 18, 18, 19, 19, 20, 20, 21, 21, 21, 22, 22,
+ 23, 23, 24, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30,
+ 30, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 37, 37, 38, 38, 39,
+ 40, 40, 41, 41, 42, 43, 43, 44, 45, 45, 46, 47, 47, 48, 49, 50,
+ 50, 51, 52, 52, 53, 54, 55, 55, 56, 57, 58, 58, 59, 60, 61, 62,
+ 62, 63, 64, 65, 66, 67, 67, 68, 69, 70, 71, 72, 73, 74, 74, 75,
+ 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
+ 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,104,105,106,107,108,
+ 109,110,111,113,114,115,116,117,118,120,121,122,123,125,126,127
+};
+
+// This function (which actually gets 'inlined' anywhere it's called)
+// exists so that gammaTable can reside out of the way down here in the
+// utility code...didn't want that huge table distracting or intimidating
+// folks before even getting into the real substance of the program, and
+// the compiler permits forward references to functions but not data.
+inline byte gamma(byte x) {
+ return pgm_read_byte(&gammaTable[x]);
+}
+
+