From: Dobrica Pavlinusic Date: Fri, 1 Jan 2016 20:36:26 +0000 (+0100) Subject: added double buffering to prevent tearning X-Git-Url: http://git.rot13.org/?p=Arduino;a=commitdiff_plain;h=6bc2030a3b0a0a4c9dc7203a325fdfacdae55be6 added double buffering to prevent tearning --- diff --git a/Hub08_LedMatrix/Hub08_LedMatrix.ino b/Hub08_LedMatrix/Hub08_LedMatrix.ino index 1a207f5..ce96623 100644 --- a/Hub08_LedMatrix/Hub08_LedMatrix.ino +++ b/Hub08_LedMatrix/Hub08_LedMatrix.ino @@ -33,7 +33,7 @@ LEDMatrix matrix(4, 5, 6, 7, 9, 11, 10, 13); // Display Buffer 128 = 64 * 16 / 8 -uint8_t displaybuf[(WIDTH/8) * HEIGHT]; +uint8_t displaybuf[(WIDTH/8) * HEIGHT*2]; uint8_t displaybuf_w[((WIDTH/8)+1) * HEIGHT]; @@ -146,7 +146,7 @@ void loop() for (int o=0; o<8; o++) { uint8_t *src = displaybuf_w + pos; - uint8_t *dest = displaybuf; + uint8_t *dest = matrix.offscreen_buffer(); int i = 0; @@ -168,6 +168,7 @@ void loop() i++; } } + matrix.swap(); } } diff --git a/Hub08_LedMatrix/LEDMatrix.cpp b/Hub08_LedMatrix/LEDMatrix.cpp index 133cfb2..8cae64e 100644 --- a/Hub08_LedMatrix/LEDMatrix.cpp +++ b/Hub08_LedMatrix/LEDMatrix.cpp @@ -47,6 +47,7 @@ LEDMatrix::LEDMatrix(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t oe, uin #if USE_SPI SPI.begin(); #endif + buffer = 0; } void LEDMatrix::begin(uint8_t *displaybuf, uint16_t width, uint16_t height) @@ -135,6 +136,9 @@ void LEDMatrix::scan() } uint8_t *head = displaybuf + row * (width / 8); + + if ( buffer ) head += (width/8) * height; + for (uint8_t line = 0; line < (height / 16); line++) { uint8_t *ptr = head; head += width * 2; // width * 16 / 8 @@ -173,6 +177,18 @@ void LEDMatrix::scan() row = (row + 1) & 0x0F; } +uint8_t *LEDMatrix::offscreen_buffer() +{ + uint8_t *buff = displaybuf; + if ( ! buffer ) buff += (width/8) * height; + return buff; +} + +void LEDMatrix::swap() +{ + buffer = ! buffer; +} + void LEDMatrix::on() { state = 1; diff --git a/Hub08_LedMatrix/LEDMatrix.h b/Hub08_LedMatrix/LEDMatrix.h index 34cf7da..c8feb72 100644 --- a/Hub08_LedMatrix/LEDMatrix.h +++ b/Hub08_LedMatrix/LEDMatrix.h @@ -25,7 +25,7 @@ #include // use hardware SPI -#define USE_SPI 1 +#define USE_SPI 0 class LEDMatrix { public: @@ -72,6 +72,10 @@ public: */ void scan(); + void swap(); + + uint8_t *offscreen_buffer(); + void reverse(); uint8_t isReversed(); @@ -87,6 +91,7 @@ private: uint16_t height; uint8_t mask; uint8_t state; + uint8_t buffer; }; #endif