From fe006cfef796b2fd6a391eeb6e71f3caa333fe1e Mon Sep 17 00:00:00 2001
From: Micah Elizabeth Scott <micah@scanlime.org>
Date: Thu, 18 Jul 2013 21:54:57 -0700
Subject: [PATCH] Tweaks to dithering algorithm

Damping to reduce oscillation, more realistic (larger-scale) test.
---
 src/hcolor.h | 8 +++++---
 src/main.cpp | 6 +++---
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/src/hcolor.h b/src/hcolor.h
index 45f7c7d..adbe490 100644
--- a/src/hcolor.h
+++ b/src/hcolor.h
@@ -94,9 +94,9 @@ struct HPixel {
     uint32_t dither() {
 
         // Incorporate the residual from last frame
-        int r16 = color.r + residual[0];
-        int g16 = color.g + residual[1];
-        int b16 = color.b + residual[2];
+        int r16 = color.r + (residual[0] >> 1);
+        int g16 = color.g + (residual[1] >> 1);
+        int b16 = color.b + (residual[2] >> 1);
 
         // Round to the nearest 8-bit value
         int r8 = std::min<int>(0xff, std::max<int>(0, (r16 + 0x80) >> 8));
@@ -104,9 +104,11 @@ struct HPixel {
         int b8 = std::min<int>(0xff, std::max<int>(0, (b16 + 0x80) >> 8));
 
         // Compute the error, after expanding the 8-bit value back to 16-bit.
+        #if 1
         residual[0] = r16 - (r8 * 257);
         residual[1] = g16 - (g8 * 257);
         residual[2] = b16 - (b8 * 257);
+        #endif
 
         return (r8 << 16) | (g8 << 8) | b8;
     }
diff --git a/src/main.cpp b/src/main.cpp
index 5875c65..92e7dcc 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -9,7 +9,7 @@
 #include <math.h>
 #include "hcolor.h"
 
-static const int ledsPerStrip = 16;
+static const int ledsPerStrip = 64;
 static const int ledsTotal = ledsPerStrip * 8;
 
 DMAMEM int displayMemory[ledsPerStrip * 6];
@@ -30,13 +30,13 @@ void setup()
 void loop()
 {
 	// XXX: Proof of concept
-	
+
 	for (int i = 0; i < 16; i++) {
 		unsigned c = pow(sin(i * 0.2 + millis() * 0.0005) * 0.5 + 0.5, 2.2) * 0x1000;
 	    pixbuf.pixels[i].color = HColor16(c>>2, c, c>>1);
    	}
 	
-	for (int i = 0; i < 1000; i++) {
+	for (int i = 0; i < 100; i++) {
 		pixbuf.show(leds);
 	}
 }
-- 
GitLab