Skip to content

Commit

Permalink
waveshare2in13v2: Use drawing functionality for clearing
Browse files Browse the repository at this point in the history
With the introduction of the shadow buffer in commit 4f99575 there's
always a sufficiently large memory buffer to keep the bytes to send for
clearing and the dedicated code using a smaller buffer is no longer
needed. This also has the advantage of automatically updating both
on-device buffers.
  • Loading branch information
hansmi authored and maruel committed Dec 27, 2021
1 parent 8a5bc2a commit 1b1970b
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 95 deletions.
28 changes: 0 additions & 28 deletions waveshare2in13v2/drawing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package waveshare2in13v2

import (
"bytes"
"image"
"image/draw"

Expand Down Expand Up @@ -130,30 +129,3 @@ func drawImage(ctrl controller, opts *drawOpts) {
sendImage(ctrl, cmd, s.MemRect, opts.buffer)
}
}

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)
}
}
55 changes: 0 additions & 55 deletions waveshare2in13v2/drawing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,58 +207,3 @@ 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)
}
})
}
}
15 changes: 3 additions & 12 deletions waveshare2in13v2/waveshare213v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,9 @@ func (d *Dev) SetUpdateMode(mode PartialUpdate) error {

// Clear clears the display.
func (d *Dev) Clear(color color.Color) error {
eh := errorHandler{d: *d}

c := image1bit.BitModel.Convert(color).(image1bit.Bit)
draw.Src.Draw(d.buffer, d.buffer.Bounds(), &image.Uniform{c}, image.Point{})

clearDisplay(&eh, image.Pt(d.opts.Width, d.opts.Height), c)

if eh.err == nil {
updateDisplay(&eh, Full)
}

return eh.err
return d.Draw(d.buffer.Bounds(), &image.Uniform{
C: image1bit.BitModel.Convert(color).(image1bit.Bit),
}, image.Point{})
}

// ColorModel returns a 1Bit color model.
Expand Down

0 comments on commit 1b1970b

Please sign in to comment.