Skip to content

Commit

Permalink
waveshare213v2: Keep display contents in shadow buffer
Browse files Browse the repository at this point in the history
This is necessary for partial updates of the display--not just
incremental updates of the full display, but updating part of the
display in general.

On the horizontal axis only whole bytes can be written, so updates need
to be aligned to 8 pixels. One option would be to require the caller of
Dev.Draw to only operate at multiples of 8. Another would be to read
back the contents of bytes to be updated partially and combine the data.
According to the datasheet the controller supports that (command 0x27).

The simplest option, however, is to maintain a shadow buffer in the
driver. Updates are applied to the buffer from which only the changed
destination rectangle is sent to the display. The permanent cost is
a bit more than 4kB of memory for a 122x250 display while saving on
memory allocations for a temporary image buffer on updates.

Signed-off-by: Michael Hanselmann <public@hansmi.ch>
  • Loading branch information
hansmi authored and maruel committed Dec 24, 2021
1 parent 6052af9 commit 4f99575
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
24 changes: 6 additions & 18 deletions waveshare2in13v2/drawing.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func setMemoryArea(ctrl controller, area image.Rectangle) {
type drawOpts struct {
cmd byte
devSize image.Point
buffer *image1bit.VerticalLSB
dstRect image.Rectangle
src image.Image
srcPts image.Point
Expand All @@ -63,15 +64,9 @@ type drawSpec struct {
// Destination on display in pixels, normalized to fit into actual size.
DstRect image.Rectangle

// Size of memory area to write; horizontally in bytes, vertically in
// pixels.
// Area to send to device; horizontally in bytes (thus aligned to
// 8 pixels), vertically in pixels.
MemRect image.Rectangle

// Size of image buffer, horizontally aligned to multiples of 8 pixels.
BufferSize image.Point

// Destination rectangle within image buffer.
BufferRect image.Rectangle
}

func (o *drawOpts) spec() drawSpec {
Expand All @@ -83,12 +78,6 @@ func (o *drawOpts) spec() drawSpec {
s.DstRect.Min.X/8, s.DstRect.Min.Y,
(s.DstRect.Max.X+7)/8, s.DstRect.Max.Y,
)
s.BufferSize = image.Pt(s.MemRect.Dx()*8, s.MemRect.Dy())
s.BufferRect = image.Rectangle{
Min: image.Point{X: s.DstRect.Min.X - (s.MemRect.Min.X * 8)},
Max: image.Point{Y: s.DstRect.Dy()},
}
s.BufferRect.Max.X = s.BufferRect.Min.X + s.DstRect.Dx()

return s
}
Expand All @@ -101,21 +90,20 @@ func drawImage(ctrl controller, opts *drawOpts) {
return
}

img := image1bit.NewVerticalLSB(image.Rectangle{Max: s.BufferSize})
draw.Src.Draw(img, s.BufferRect, opts.src, opts.srcPts)
draw.Src.Draw(opts.buffer, s.DstRect, opts.src, opts.srcPts)

setMemoryArea(ctrl, s.MemRect)

ctrl.sendCommand(opts.cmd)

rowData := make([]byte, s.MemRect.Dx())

for y := 0; y < img.Bounds().Dy(); y++ {
for y := s.MemRect.Min.Y; y < s.MemRect.Max.Y; y++ {
for x := 0; x < len(rowData); x++ {
rowData[x] = 0

for bit := 0; bit < 8; bit++ {
if img.BitAt((x*8)+bit, y) {
if opts.buffer.BitAt(((s.MemRect.Min.X+x)*8)+bit, y) {
rowData[x] |= 0x80 >> bit
}
}
Expand Down
16 changes: 8 additions & 8 deletions waveshare2in13v2/drawing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,24 @@ func TestDrawSpec(t *testing.T) {
name: "smaller than display",
opts: drawOpts{
devSize: image.Pt(100, 200),
buffer: image1bit.NewVerticalLSB(image.Rect(0, 0, 120, 210)),
dstRect: image.Rect(17, 4, 25, 8),
},
want: drawSpec{
DstRect: image.Rect(17, 4, 25, 8),
MemRect: image.Rect(2, 4, 4, 8),
BufferSize: image.Pt(16, 4),
BufferRect: image.Rect(1, 0, 9, 4),
DstRect: image.Rect(17, 4, 25, 8),
MemRect: image.Rect(2, 4, 4, 8),
},
},
{
name: "larger than display",
opts: drawOpts{
devSize: image.Pt(100, 200),
buffer: image1bit.NewVerticalLSB(image.Rect(0, 0, 100, 200)),
dstRect: image.Rect(-20, 50, 125, 300),
},
want: drawSpec{
DstRect: image.Rect(0, 50, 100, 200),
MemRect: image.Rect(0, 50, 13, 200),
BufferSize: image.Pt(13*8, 150),
BufferRect: image.Rect(0, 0, 100, 150),
DstRect: image.Rect(0, 50, 100, 200),
MemRect: image.Rect(0, 50, 13, 200),
},
},
} {
Expand Down Expand Up @@ -77,6 +75,7 @@ func TestDrawImage(t *testing.T) {
opts: drawOpts{
cmd: writeRAMBW,
devSize: image.Pt(64, 64),
buffer: image1bit.NewVerticalLSB(image.Rect(0, 0, 64, 64)),
dstRect: image.Rect(17, 4, 41, 8),
src: &image.Uniform{image1bit.On},
srcPts: image.Pt(0, 0),
Expand All @@ -98,6 +97,7 @@ func TestDrawImage(t *testing.T) {
opts: drawOpts{
cmd: writeRAMBW,
devSize: image.Pt(80, 120),
buffer: image1bit.NewVerticalLSB(image.Rect(0, 0, 80, 120)),
dstRect: image.Rect(0, 0, 80, 120),
src: &image.Uniform{image1bit.On},
srcPts: image.Pt(33, 44),
Expand Down
17 changes: 15 additions & 2 deletions waveshare2in13v2/waveshare213v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"image"
"image/color"
"image/draw"
"time"

"periph.io/x/conn/v3"
Expand Down Expand Up @@ -62,6 +63,8 @@ type Dev struct {
rst gpio.PinOut
busy gpio.PinIn

buffer *image1bit.VerticalLSB

opts *Opts
}

Expand Down Expand Up @@ -139,9 +142,15 @@ func New(p spi.Port, dc, cs, rst gpio.PinOut, busy gpio.PinIn, opts *Opts) (*Dev
cs: cs,
rst: rst,
busy: busy,
buffer: image1bit.NewVerticalLSB(image.Rectangle{
Max: image.Pt((opts.Width+7)/8*8, opts.Height),
}),
opts: opts,
}

// Default color
draw.Src.Draw(d.buffer, d.buffer.Bounds(), &image.Uniform{image1bit.On}, image.Point{})

return d, nil
}

Expand Down Expand Up @@ -176,8 +185,10 @@ func (d *Dev) Init(partialUpdate PartialUpdate) error {
func (d *Dev) Clear(color color.Color) error {
eh := errorHandler{d: *d}

clearDisplay(&eh, image.Pt(d.opts.Width, d.opts.Height),
image1bit.BitModel.Convert(color).(image1bit.Bit))
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 {
eh.err = d.turnOnDisplay()
Expand All @@ -201,6 +212,7 @@ func (d *Dev) Draw(dstRect image.Rectangle, src image.Image, srcPts image.Point)
opts := drawOpts{
cmd: writeRAMBW,
devSize: image.Pt(d.opts.Width, d.opts.Height),
buffer: d.buffer,
dstRect: dstRect,
src: src,
srcPts: srcPts,
Expand All @@ -221,6 +233,7 @@ func (d *Dev) Draw(dstRect image.Rectangle, src image.Image, srcPts image.Point)
func (d *Dev) DrawPartial(dstRect image.Rectangle, src image.Image, srcPts image.Point) error {
opts := drawOpts{
devSize: image.Pt(d.opts.Width, d.opts.Height),
buffer: d.buffer,
dstRect: dstRect,
src: src,
srcPts: srcPts,
Expand Down

0 comments on commit 4f99575

Please sign in to comment.