Skip to content
Snippets Groups Projects
Commit a079c823 authored by Micah Elizabeth Scott's avatar Micah Elizabeth Scott
Browse files

Test harness for tweaking the dither algorithm

parent ee6d9979
No related branches found
No related tags found
No related merge requests found
......@@ -52,7 +52,7 @@ OBJS := $(C_FILES:.c=.o) $(CPP_FILES:.cpp=.o)
all: $(TARGET).hex
$(TARGET).elf: $(OBJS) $(LDSCRIPT)
$(CC) $(LDFLAGS) -o $@ $(OBJS)
$(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
%.hex: %.elf
$(SIZE) $<
......
......@@ -92,15 +92,22 @@ struct HPixel {
/// Temporal dithering algorithm. Returns a 24-bit RGB color.
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];
// Round to the nearest 8-bit value
int r8 = std::min<int>(0xff, std::max<int>(0, (r16 + 0x80) >> 8));
int g8 = std::min<int>(0xff, std::max<int>(0, (g16 + 0x80) >> 8));
int b8 = std::min<int>(0xff, std::max<int>(0, (b16 + 0x80) >> 8));
residual[0] = r16 - (r8 | (r8 << 8));
residual[1] = g16 - (g8 | (g8 << 8));
residual[2] = b16 - (b8 | (b8 << 8));
// Compute the error, after expanding the 8-bit value back to 16-bit.
residual[0] = r16 - (r8 * 257);
residual[1] = g16 - (g8 * 257);
residual[2] = b16 - (b8 * 257);
return (r8 << 16) | (g8 << 8) | b8;
}
};
......
......@@ -6,9 +6,10 @@
*/
#include <OctoWS2811.h>
#include <math.h>
#include "hcolor.h"
static const int ledsPerStrip = 64;
static const int ledsPerStrip = 16;
static const int ledsTotal = ledsPerStrip * 8;
DMAMEM int displayMemory[ledsPerStrip * 6];
......@@ -19,15 +20,23 @@ HPixelBuffer<ledsTotal> pixbuf;
void setup()
{
leds.begin();
Serial.begin(115200);
for (unsigned i = 0; i < ledsTotal; ++i) {
pixbuf.pixels[i].color = HColor16(0x0080, 0x0080, 0x0080);
pixbuf.pixels[i].color = HColor16(0,0,0);
}
}
void loop()
{
pixbuf.pixels[0].color.b = millis() & 0xffff;
pixbuf.show(leds);
// 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++) {
pixbuf.show(leds);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment