diff --git a/waveshare2in13v2/drawing.go b/waveshare2in13v2/drawing.go index ce7d2f0..4fe200d 100644 --- a/waveshare2in13v2/drawing.go +++ b/waveshare2in13v2/drawing.go @@ -5,6 +5,7 @@ package waveshare2in13v2 import ( + "bytes" "image" "image/draw" @@ -128,3 +129,30 @@ func drawImage(ctrl controller, opts *drawOpts) { ctrl.sendData(rowData) } } + +func clearDisplay(ctrl controller, size image.Point, color image1bit.Bit) { + var colorValue byte + + if color == image1bit.On { + colorValue = 0xff + } + + spec := (&drawOpts{ + devSize: size, + dstRect: image.Rectangle{Max: size}, + }).spec() + + if spec.MemRect.Empty() { + return + } + + setMemoryArea(ctrl, spec.MemRect) + + ctrl.sendCommand(writeRAMBW) + + data := bytes.Repeat([]byte{colorValue}, spec.MemRect.Dx()) + + for y := 0; y < spec.MemRect.Max.Y; y++ { + ctrl.sendData(data) + } +} diff --git a/waveshare2in13v2/drawing_test.go b/waveshare2in13v2/drawing_test.go index 7c5d62e..087bd43 100644 --- a/waveshare2in13v2/drawing_test.go +++ b/waveshare2in13v2/drawing_test.go @@ -144,3 +144,58 @@ func TestDrawImage(t *testing.T) { }) } } + +func TestClearDisplay(t *testing.T) { + for _, tc := range []struct { + name string + size image.Point + color image1bit.Bit + want []record + }{ + { + name: "empty", + }, + { + name: "off", + size: image.Pt(100, 10), + color: image1bit.Off, + want: []record{ + {cmd: dataEntryModeSetting, data: []byte{0x3}}, + {cmd: setRAMXAddressStartEndPosition, data: []byte{0, (100+7)/8 - 1}}, + {cmd: setRAMYAddressStartEndPosition, data: []byte{0, 0, 10 - 1, 0}}, + {cmd: setRAMXAddressCounter, data: []byte{0}}, + {cmd: setRAMYAddressCounter, data: []byte{0, 0}}, + { + cmd: writeRAMBW, + data: bytes.Repeat([]byte{0}, 13*10), + }, + }, + }, + { + name: "on", + size: image.Pt(32, 20), + color: image1bit.On, + want: []record{ + {cmd: dataEntryModeSetting, data: []byte{0x3}}, + {cmd: setRAMXAddressStartEndPosition, data: []byte{0, 32/8 - 1}}, + {cmd: setRAMYAddressStartEndPosition, data: []byte{0, 0, 20 - 1, 0}}, + {cmd: setRAMXAddressCounter, data: []byte{0}}, + {cmd: setRAMYAddressCounter, data: []byte{0, 0}}, + { + cmd: writeRAMBW, + data: bytes.Repeat([]byte{0xff}, 4*20), + }, + }, + }, + } { + t.Run(tc.name, func(t *testing.T) { + var got fakeController + + clearDisplay(&got, tc.size, tc.color) + + if diff := cmp.Diff([]record(got), tc.want, cmpopts.EquateEmpty(), cmp.AllowUnexported(record{})); diff != "" { + t.Errorf("clearDisplay() difference (-got +want):\n%s", diff) + } + }) + } +} diff --git a/waveshare2in13v2/waveshare213v2.go b/waveshare2in13v2/waveshare213v2.go index 0fcde57..fafd78c 100644 --- a/waveshare2in13v2/waveshare213v2.go +++ b/waveshare2in13v2/waveshare213v2.go @@ -5,7 +5,6 @@ package waveshare2in13v2 import ( - "bytes" "fmt" "image" "image/color" @@ -249,23 +248,11 @@ func (d *Dev) Init(partialUpdate PartialUpdate) error { } // Clear clears the display. -func (d *Dev) Clear(color byte) error { - spec := (&drawOpts{ - devSize: image.Pt(d.opts.Width, d.opts.Height), - dstRect: d.Bounds(), - }).spec() - +func (d *Dev) Clear(color color.Color) error { eh := errorHandler{d: *d} - setMemoryArea(&eh, spec.MemRect) - - eh.sendCommand(writeRAMBW) - - data := bytes.Repeat([]byte{color}, spec.MemRect.Dx()) - - for y := 0; y < spec.MemRect.Max.Y; y++ { - eh.sendData(data) - } + clearDisplay(&eh, image.Pt(d.opts.Width, d.opts.Height), + image1bit.BitModel.Convert(color).(image1bit.Bit)) if eh.err == nil { eh.err = d.turnOnDisplay() @@ -335,7 +322,7 @@ func (d *Dev) DrawPartial(dstRect image.Rectangle, src image.Image, srcPts image // Halt clears the display. func (d *Dev) Halt() error { - return d.Clear(0xFF) + return d.Clear(image1bit.On) } // String returns a string containing configuration information.