From 648f44a621cebbb9483d31a8993da45f1d244967 Mon Sep 17 00:00:00 2001 From: Michael Hanselmann Date: Thu, 23 Dec 2021 21:13:26 +0100 Subject: [PATCH] waveshare213v2: Allow changing update mode without re-initialization It's perfectly acceptable to update a display in full mode before switching to partial mode and vice-versa. There's no need for a full soft-reset in-between. This change breaks the API: the "Init" function no longer receives a parameter. The default is to always start in the full update mode. Signed-off-by: Michael Hanselmann --- waveshare2in13v2/example_test.go | 4 ++-- waveshare2in13v2/waveshare213v2.go | 32 ++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/waveshare2in13v2/example_test.go b/waveshare2in13v2/example_test.go index 1ac1d8b..56a713f 100644 --- a/waveshare2in13v2/example_test.go +++ b/waveshare2in13v2/example_test.go @@ -37,7 +37,7 @@ func Example() { log.Fatalf("Failed to initialize driver: %v", err) } - err = dev.Init(false) + err = dev.Init() if err != nil { log.Fatalf("Failed to initialize display: %v", err) } @@ -77,7 +77,7 @@ func Example_other() { log.Fatalf("Failed to initialize driver: %v", err) } - err = dev.Init(false) + err = dev.Init() if err != nil { log.Fatalf("Failed to initialize display: %v", err) } diff --git a/waveshare2in13v2/waveshare213v2.go b/waveshare2in13v2/waveshare213v2.go index ce72891..40591ac 100644 --- a/waveshare2in13v2/waveshare213v2.go +++ b/waveshare2in13v2/waveshare213v2.go @@ -64,6 +64,7 @@ type Dev struct { busy gpio.PinIn buffer *image1bit.VerticalLSB + mode PartialUpdate opts *Opts } @@ -145,6 +146,7 @@ func New(p spi.Port, dc, cs, rst gpio.PinOut, busy gpio.PinIn, opts *Opts) (*Dev buffer: image1bit.NewVerticalLSB(image.Rectangle{ Max: image.Pt((opts.Width+7)/8*8, opts.Height), }), + mode: Full, opts: opts, } @@ -163,20 +165,20 @@ func NewHat(p spi.Port, opts *Opts) (*Dev, error) { return New(p, dc, cs, rst, busy, opts) } -func (d *Dev) configMode(ctrl controller, mode PartialUpdate) { +func (d *Dev) configMode(ctrl controller) { var lut LUT - if mode == Full { + if d.mode == Full { lut = d.opts.FullUpdate } else { lut = d.opts.PartialUpdate } - configDisplayMode(ctrl, mode, lut) + configDisplayMode(ctrl, d.mode, lut) } -// Init will initialize the display with the partial-update or full-update mode. -func (d *Dev) Init(partialUpdate PartialUpdate) error { +// Init configures the display for usage through the other functions. +func (d *Dev) Init() error { // Hardware Reset if err := d.reset(); err != nil { return err @@ -187,12 +189,30 @@ func (d *Dev) Init(partialUpdate PartialUpdate) error { initDisplay(&eh, d.opts) if eh.err == nil { - d.configMode(&eh, partialUpdate) + d.configMode(&eh) } return eh.err } +// SetUpdateMode changes the way updates to the displayed image are applied. In +// Full mode (the default) a full refresh is done with all pixels cleared and +// re-applied. In Partial mode only the changed pixels are updated (aligned to +// multiples of 8 on the horizontal axis), potentially leaving behind small +// optical artifacts due to the way e-paper displays work. +// +// The vendor datasheet recommends a full update at least once every 24 hours. +// When using partial updates the Clear function can be used for the purpose, +// followed by re-drawing. +func (d *Dev) SetUpdateMode(mode PartialUpdate) error { + d.mode = mode + + eh := errorHandler{d: *d} + d.configMode(&eh) + + return eh.err +} + // Clear clears the display. func (d *Dev) Clear(color color.Color) error { eh := errorHandler{d: *d}