waveshare213v2: Use color for clearing

This is an API-breaking change, albeit a simple one: Previously the
Dev.Clear function received a byte named "color". It was sent directly
to the display, thus acting more like a pattern to fill the display
with. In practice only 0 and 255 are likely to be used.

With this change the function is changed to receive a value of type
color.Color which is then converted to image1bit.Bit before filling the
display. The latter is also given a unittest.

Signed-off-by: Michael Hanselmann <public@hansmi.ch>
pull/40/head
Michael Hanselmann 4 years ago committed by M-A
parent ee4a6e4f2e
commit 5179e40adf

@ -5,6 +5,7 @@
package waveshare2in13v2 package waveshare2in13v2
import ( import (
"bytes"
"image" "image"
"image/draw" "image/draw"
@ -128,3 +129,30 @@ func drawImage(ctrl controller, opts *drawOpts) {
ctrl.sendData(rowData) 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)
}
}

@ -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)
}
})
}
}

@ -5,7 +5,6 @@
package waveshare2in13v2 package waveshare2in13v2
import ( import (
"bytes"
"fmt" "fmt"
"image" "image"
"image/color" "image/color"
@ -249,23 +248,11 @@ func (d *Dev) Init(partialUpdate PartialUpdate) error {
} }
// Clear clears the display. // Clear clears the display.
func (d *Dev) Clear(color byte) error { func (d *Dev) Clear(color color.Color) error {
spec := (&drawOpts{
devSize: image.Pt(d.opts.Width, d.opts.Height),
dstRect: d.Bounds(),
}).spec()
eh := errorHandler{d: *d} eh := errorHandler{d: *d}
setMemoryArea(&eh, spec.MemRect) clearDisplay(&eh, image.Pt(d.opts.Width, d.opts.Height),
image1bit.BitModel.Convert(color).(image1bit.Bit))
eh.sendCommand(writeRAMBW)
data := bytes.Repeat([]byte{color}, spec.MemRect.Dx())
for y := 0; y < spec.MemRect.Max.Y; y++ {
eh.sendData(data)
}
if eh.err == nil { if eh.err == nil {
eh.err = d.turnOnDisplay() eh.err = d.turnOnDisplay()
@ -335,7 +322,7 @@ func (d *Dev) DrawPartial(dstRect image.Rectangle, src image.Image, srcPts image
// Halt clears the display. // Halt clears the display.
func (d *Dev) Halt() error { func (d *Dev) Halt() error {
return d.Clear(0xFF) return d.Clear(image1bit.On)
} }
// String returns a string containing configuration information. // String returns a string containing configuration information.

Loading…
Cancel
Save