Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

waveshare2in13v2: Add function for deep sleep, update both buffers on clear #42

Merged
merged 5 commits into from
Dec 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions waveshare2in13v2/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,14 @@ func configDisplayMode(ctrl controller, mode PartialUpdate, lut LUT) {
ctrl.sendCommand(writeLutRegister)
ctrl.sendData(lut[:70])

if mode == Partial {
// Undocumented command used in vendor example code.
ctrl.sendCommand(0x37)
ctrl.sendData([]byte{0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00})

ctrl.sendCommand(displayUpdateControl2)
ctrl.sendData([]byte{
displayUpdateEnableClock |
displayUpdateEnableAnalog,
})
ctrl.sendCommand(writeDisplayOptionRegister)
ctrl.sendData([]byte{0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00})

ctrl.sendCommand(masterActivation)
}
// Start up the parts likely used by a draw operation soon.
ctrl.sendCommand(displayUpdateControl2)
ctrl.sendData([]byte{displayUpdateEnableClock | displayUpdateEnableAnalog})

ctrl.sendCommand(masterActivation)
ctrl.waitUntilIdle()
}

Expand Down
3 changes: 3 additions & 0 deletions waveshare2in13v2/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ func TestConfigDisplayMode(t *testing.T) {
{cmd: writeVcomRegister, data: []byte{0x55}},
{cmd: borderWaveformControl, data: []byte{0x03}},
{cmd: writeLutRegister, data: bytes.Repeat([]byte{'F'}, 70)},
{cmd: 0x37, data: []byte{0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00}},
{cmd: displayUpdateControl2, data: []byte{0xc0}},
{cmd: masterActivation},
},
},
{
Expand Down
62 changes: 26 additions & 36 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 @@ -52,12 +51,12 @@ 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
commands []byte
devSize image.Point
buffer *image1bit.VerticalLSB
dstRect image.Rectangle
src image.Image
srcPts image.Point
}

type drawSpec struct {
Expand All @@ -82,28 +81,25 @@ func (o *drawOpts) spec() drawSpec {
return s
}

// drawImage sends an image to the controller after setting up the registers.
func drawImage(ctrl controller, opts *drawOpts) {
s := opts.spec()

if s.MemRect.Empty() {
// sendImage sends an image to the controller after setting up the registers.
// The area is in bytes on the horizontal axis.
func sendImage(ctrl controller, cmd byte, area image.Rectangle, img *image1bit.VerticalLSB) {
if area.Empty() {
return
}

draw.Src.Draw(opts.buffer, s.DstRect, opts.src, opts.srcPts)

setMemoryArea(ctrl, s.MemRect)
setMemoryArea(ctrl, area)

ctrl.sendCommand(opts.cmd)
ctrl.sendCommand(cmd)

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

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

for bit := 0; bit < 8; bit++ {
if opts.buffer.BitAt(((s.MemRect.Min.X+x)*8)+bit, y) {
if img.BitAt(((area.Min.X+x)*8)+bit, y) {
rowData[x] |= 0x80 >> bit
}
}
Expand All @@ -113,29 +109,23 @@ func drawImage(ctrl controller, opts *drawOpts) {
}
}

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()
func drawImage(ctrl controller, opts *drawOpts) {
s := opts.spec()

if spec.MemRect.Empty() {
if s.MemRect.Empty() {
return
}

setMemoryArea(ctrl, spec.MemRect)
draw.Src.Draw(opts.buffer, s.DstRect, opts.src, opts.srcPts)

ctrl.sendCommand(writeRAMBW)
commands := opts.commands

data := bytes.Repeat([]byte{colorValue}, spec.MemRect.Dx())
if len(commands) == 0 {
commands = []byte{writeRAMBW, writeRAMRed}
}

for y := 0; y < spec.MemRect.Max.Y; y++ {
ctrl.sendData(data)
// Keep the two buffers in sync.
for _, cmd := range commands {
sendImage(ctrl, cmd, s.MemRect, opts.buffer)
}
}
116 changes: 71 additions & 45 deletions waveshare2in13v2/drawing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package waveshare2in13v2
import (
"bytes"
"image"
"image/draw"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -58,50 +59,64 @@ func TestDrawSpec(t *testing.T) {
}
}

func TestDrawImage(t *testing.T) {
func TestSendImage(t *testing.T) {
for _, tc := range []struct {
name string
opts drawOpts
cmd byte
area image.Rectangle
img *image1bit.VerticalLSB
want []record
}{
{
name: "empty",
opts: drawOpts{
src: &image.Uniform{image1bit.On},
},
},
{
name: "partial",
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),
cmd: writeRAMBW,
area: image.Rect(2, 20, 4, 40),
img: image1bit.NewVerticalLSB(image.Rect(0, 0, 64, 64)),
want: []record{
{cmd: dataEntryModeSetting, data: []byte{0x3}},
{cmd: setRAMXAddressStartEndPosition, data: []byte{2, 4 - 1}},
{cmd: setRAMYAddressStartEndPosition, data: []byte{20, 0, 40 - 1, 0}},
{cmd: setRAMXAddressCounter, data: []byte{2}},
{cmd: setRAMYAddressCounter, data: []byte{20, 0}},
{
cmd: writeRAMBW,
data: bytes.Repeat([]byte{0}, 2*(30-10)),
},
},
},
{
name: "partial non-aligned",
cmd: writeRAMRed,
area: image.Rect(2, 4, 6, 8),
img: func() *image1bit.VerticalLSB {
img := image1bit.NewVerticalLSB(image.Rect(0, 0, 64, 64))
draw.Src.Draw(img, image.Rect(17, 4, 41, 8), &image.Uniform{image1bit.On}, image.Point{})
return img
}(),
want: []record{
{cmd: dataEntryModeSetting, data: []byte{0x3}},
{cmd: setRAMXAddressStartEndPosition, data: []byte{2, 6 - 1}},
{cmd: setRAMYAddressStartEndPosition, data: []byte{4, 0, 8 - 1, 0}},
{cmd: setRAMXAddressCounter, data: []byte{2}},
{cmd: setRAMYAddressCounter, data: []byte{4, 0}},
{
cmd: writeRAMBW,
cmd: writeRAMRed,
data: bytes.Repeat([]byte{0x7f, 0xff, 0xff, 0x80}, 4),
},
},
},
{
name: "full",
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),
},
cmd: writeRAMBW,
area: image.Rect(0, 0, 10, 120),
img: func() *image1bit.VerticalLSB {
img := image1bit.NewVerticalLSB(image.Rect(0, 0, 80, 120))
draw.Src.Draw(img, image.Rect(0, 0, 80, 120), &image.Uniform{image1bit.On}, image.Point{})
return img
}(),
want: []record{
{cmd: dataEntryModeSetting, data: []byte{0x3}},
{cmd: setRAMXAddressStartEndPosition, data: []byte{0, 10 - 1}},
Expand All @@ -118,65 +133,76 @@ func TestDrawImage(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
var got fakeController

drawImage(&got, &tc.opts)
sendImage(&got, tc.cmd, tc.area, tc.img)

if diff := cmp.Diff([]record(got), tc.want, cmpopts.EquateEmpty(), cmp.AllowUnexported(record{})); diff != "" {
t.Errorf("drawImage() difference (-got +want):\n%s", diff)
t.Errorf("sendImage() difference (-got +want):\n%s", diff)
}
})
}
}

func TestClearDisplay(t *testing.T) {
func TestDrawImage(t *testing.T) {
for _, tc := range []struct {
name string
size image.Point
color image1bit.Bit
want []record
name string
opts drawOpts
want []record
}{
{
name: "empty",
},
{
name: "off",
size: image.Pt(100, 10),
color: image1bit.Off,
name: "partial",
opts: drawOpts{
commands: []byte{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),
},
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: setRAMXAddressStartEndPosition, data: []byte{2, 6 - 1}},
{cmd: setRAMYAddressStartEndPosition, data: []byte{4, 0, 8 - 1, 0}},
{cmd: setRAMXAddressCounter, data: []byte{2}},
{cmd: setRAMYAddressCounter, data: []byte{4, 0}},
{
cmd: writeRAMBW,
data: bytes.Repeat([]byte{0}, 13*10),
data: bytes.Repeat([]byte{0x7f, 0xff, 0xff, 0x80}, 4),
},
},
},
{
name: "on",
size: image.Pt(32, 20),
color: image1bit.On,
name: "full",
opts: drawOpts{
commands: []byte{writeRAMRed},
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),
},
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: setRAMXAddressStartEndPosition, data: []byte{0, 10 - 1}},
{cmd: setRAMYAddressStartEndPosition, data: []byte{0, 0, 120 - 1, 0}},
{cmd: setRAMXAddressCounter, data: []byte{0}},
{cmd: setRAMYAddressCounter, data: []byte{0, 0}},
{
cmd: writeRAMBW,
data: bytes.Repeat([]byte{0xff}, 4*20),
cmd: writeRAMRed,
data: bytes.Repeat([]byte{0xff}, 80/8*120),
},
},
},
} {
t.Run(tc.name, func(t *testing.T) {
var got fakeController

clearDisplay(&got, tc.size, tc.color)
drawImage(&got, &tc.opts)

if diff := cmp.Diff([]record(got), tc.want, cmpopts.EquateEmpty(), cmp.AllowUnexported(record{})); diff != "" {
t.Errorf("clearDisplay() difference (-got +want):\n%s", diff)
t.Errorf("drawImage() difference (-got +want):\n%s", diff)
}
})
}
Expand Down
Loading