From 8ec566564c4fec9db8fd4a84f2a6230cef8b80cb Mon Sep 17 00:00:00 2001 From: Michael Hanselmann Date: Thu, 23 Dec 2021 21:40:50 +0100 Subject: [PATCH] waveshare213v2: Implement flicker-free partial updates Update both image buffers (B/W and red) and configure the controller to only refresh the changed areas. With this change the "DrawPartial" function becomes equivalent to "Draw" and is thus marked as deprecated. It didn't really provide additional functionality before as partial updates weren't truly implemented. Signed-off-by: Michael Hanselmann --- waveshare2in13v2/controller.go | 9 +++++++- waveshare2in13v2/controller_test.go | 2 +- waveshare2in13v2/waveshare213v2.go | 34 ++++++++++------------------- 3 files changed, 20 insertions(+), 25 deletions(-) diff --git a/waveshare2in13v2/controller.go b/waveshare2in13v2/controller.go index f7827e7..2af1568 100644 --- a/waveshare2in13v2/controller.go +++ b/waveshare2in13v2/controller.go @@ -78,8 +78,15 @@ func configDisplayMode(ctrl controller, mode PartialUpdate, lut LUT) { } func updateDisplay(ctrl controller, mode PartialUpdate) { + var displayUpdateFlags byte + + if mode == Partial { + // Make use of red buffer + displayUpdateFlags = 0b1000_0000 + } + ctrl.sendCommand(displayUpdateControl1) - ctrl.sendData([]byte{0}) + ctrl.sendData([]byte{displayUpdateFlags}) ctrl.sendCommand(displayUpdateControl2) ctrl.sendData([]byte{0xC7}) diff --git a/waveshare2in13v2/controller_test.go b/waveshare2in13v2/controller_test.go index 45d8ede..c536ea7 100644 --- a/waveshare2in13v2/controller_test.go +++ b/waveshare2in13v2/controller_test.go @@ -138,7 +138,7 @@ func TestUpdateDisplay(t *testing.T) { name: "partial", mode: Partial, want: []record{ - {cmd: displayUpdateControl1, data: []byte{0}}, + {cmd: displayUpdateControl1, data: []byte{0x80}}, {cmd: displayUpdateControl2, data: []byte{0xc7}}, {cmd: masterActivation}, }, diff --git a/waveshare2in13v2/waveshare213v2.go b/waveshare2in13v2/waveshare213v2.go index d119960..c3d68a1 100644 --- a/waveshare2in13v2/waveshare213v2.go +++ b/waveshare2in13v2/waveshare213v2.go @@ -239,30 +239,10 @@ func (d *Dev) Bounds() image.Rectangle { return image.Rect(0, 0, d.opts.Width, d.opts.Height) } -// Draw draws the given image to the display. +// Draw draws the given image to the display. Only the destination area is +// uploaded. Depending on the update mode the whole display or the destination +// area is refreshed. func (d *Dev) Draw(dstRect image.Rectangle, src image.Image, srcPts image.Point) error { - opts := drawOpts{ - cmd: writeRAMBW, - devSize: image.Pt(d.opts.Width, d.opts.Height), - buffer: d.buffer, - dstRect: dstRect, - src: src, - srcPts: srcPts, - } - - eh := errorHandler{d: *d} - - drawImage(&eh, &opts) - - if eh.err == nil { - updateDisplay(&eh, d.mode) - } - - return eh.err -} - -// DrawPartial draws the given image to the display. Display will update only changed pixel. -func (d *Dev) DrawPartial(dstRect image.Rectangle, src image.Image, srcPts image.Point) error { opts := drawOpts{ devSize: image.Pt(d.opts.Width, d.opts.Height), buffer: d.buffer, @@ -273,6 +253,7 @@ func (d *Dev) DrawPartial(dstRect image.Rectangle, src image.Image, srcPts image eh := errorHandler{d: *d} + // Keep the two buffers in sync. for _, cmd := range []byte{writeRAMBW, writeRAMRed} { opts.cmd = cmd @@ -290,6 +271,13 @@ func (d *Dev) DrawPartial(dstRect image.Rectangle, src image.Image, srcPts image return eh.err } +// DrawPartial draws the given image to the display. +// +// Deprecated: Use Draw instead. DrawPartial merely forwards all calls. +func (d *Dev) DrawPartial(dstRect image.Rectangle, src image.Image, srcPts image.Point) error { + return d.Draw(dstRect, src, srcPts) +} + // Halt clears the display. func (d *Dev) Halt() error { return d.Clear(image1bit.On)