diff --git a/waveshare2in13v2/controller.go b/waveshare2in13v2/controller.go index fa4a7f3..f7827e7 100644 --- a/waveshare2in13v2/controller.go +++ b/waveshare2in13v2/controller.go @@ -76,3 +76,14 @@ func configDisplayMode(ctrl controller, mode PartialUpdate, lut LUT) { ctrl.waitUntilIdle() } + +func updateDisplay(ctrl controller, mode PartialUpdate) { + ctrl.sendCommand(displayUpdateControl1) + ctrl.sendData([]byte{0}) + + ctrl.sendCommand(displayUpdateControl2) + ctrl.sendData([]byte{0xC7}) + + ctrl.sendCommand(masterActivation) + ctrl.waitUntilIdle() +} diff --git a/waveshare2in13v2/controller_test.go b/waveshare2in13v2/controller_test.go index 4814d2f..45d8ede 100644 --- a/waveshare2in13v2/controller_test.go +++ b/waveshare2in13v2/controller_test.go @@ -118,3 +118,40 @@ func TestConfigDisplayMode(t *testing.T) { }) } } + +func TestUpdateDisplay(t *testing.T) { + for _, tc := range []struct { + name string + mode PartialUpdate + want []record + }{ + { + name: "full", + mode: Full, + want: []record{ + {cmd: displayUpdateControl1, data: []byte{0}}, + {cmd: displayUpdateControl2, data: []byte{0xc7}}, + {cmd: masterActivation}, + }, + }, + { + name: "partial", + mode: Partial, + want: []record{ + {cmd: displayUpdateControl1, data: []byte{0}}, + {cmd: displayUpdateControl2, data: []byte{0xc7}}, + {cmd: masterActivation}, + }, + }, + } { + t.Run(tc.name, func(t *testing.T) { + var got fakeController + + updateDisplay(&got, tc.mode) + + if diff := cmp.Diff([]record(got), tc.want, cmpopts.EquateEmpty(), cmp.AllowUnexported(record{})); diff != "" { + t.Errorf("updateDisplay() difference (-got +want):\n%s", diff) + } + }) + } +} diff --git a/waveshare2in13v2/waveshare213v2.go b/waveshare2in13v2/waveshare213v2.go index 40591ac..d119960 100644 --- a/waveshare2in13v2/waveshare213v2.go +++ b/waveshare2in13v2/waveshare213v2.go @@ -223,7 +223,7 @@ func (d *Dev) Clear(color color.Color) error { clearDisplay(&eh, image.Pt(d.opts.Width, d.opts.Height), c) if eh.err == nil { - eh.err = d.turnOnDisplay() + updateDisplay(&eh, Full) } return eh.err @@ -255,7 +255,7 @@ func (d *Dev) Draw(dstRect image.Rectangle, src image.Image, srcPts image.Point) drawImage(&eh, &opts) if eh.err == nil { - eh.err = d.turnOnDisplay() + updateDisplay(&eh, d.mode) } return eh.err @@ -284,7 +284,7 @@ func (d *Dev) DrawPartial(dstRect image.Rectangle, src image.Image, srcPts image } if eh.err == nil { - eh.err = d.turnOnDisplay() + updateDisplay(&eh, d.mode) } return eh.err @@ -300,18 +300,6 @@ func (d *Dev) String() string { return fmt.Sprintf("epd.Dev{%s, %s, Height: %d, Width: %d}", d.c, d.dc, d.opts.Height, d.opts.Width) } -func (d *Dev) turnOnDisplay() error { - eh := errorHandler{d: *d} - - eh.sendCommand(displayUpdateControl2) - eh.sendData([]byte{0xC7}) - eh.sendCommand(masterActivation) - - eh.waitUntilIdle() - - return eh.err -} - // Reset the hardware func (d *Dev) reset() error { eh := errorHandler{d: *d}