waveshare213v2: Unify initialization, separate mode config

Merge the common logic in the "initDisplayFull" and "initDisplayPartial"
functions and separate out the parts related to the display update mode.
In a forthcoming change the update mode will no longer be set when
initializing, but rather via a separate setter function, requiring the
ability to reconfigure the display controller without a full
re-initialization.

Signed-off-by: Michael Hanselmann <public@hansmi.ch>
pull/41/head
Michael Hanselmann 4 years ago
parent bb13c4c789
commit 41c01d54b6

@ -10,7 +10,7 @@ type controller interface {
waitUntilIdle()
}
func initDisplayFull(ctrl controller, opts *Opts) {
func initDisplay(ctrl controller, opts *Opts) {
ctrl.waitUntilIdle()
ctrl.sendCommand(swReset)
ctrl.waitUntilIdle()
@ -28,12 +28,6 @@ func initDisplayFull(ctrl controller, opts *Opts) {
0x00,
})
ctrl.sendCommand(borderWaveformControl)
ctrl.sendData([]byte{0x03})
ctrl.sendCommand(writeVcomRegister)
ctrl.sendData([]byte{0x55})
ctrl.sendCommand(gateDrivingVoltageControl)
ctrl.sendData([]byte{gateDrivingVoltage19V})
@ -45,20 +39,31 @@ func initDisplayFull(ctrl controller, opts *Opts) {
ctrl.sendCommand(setGateTime)
ctrl.sendData([]byte{0x0A})
}
ctrl.sendCommand(writeLutRegister)
ctrl.sendData(opts.FullUpdate[:70])
func configDisplayMode(ctrl controller, mode PartialUpdate, lut LUT) {
var vcom byte
var borderWaveformControlValue byte
switch mode {
case Full:
vcom = 0x55
borderWaveformControlValue = 0x03
case Partial:
vcom = 0x24
borderWaveformControlValue = 0x01
}
func initDisplayPartial(ctrl controller, opts *Opts) {
ctrl.sendCommand(writeVcomRegister)
ctrl.sendData([]byte{0x26})
ctrl.sendData([]byte{vcom})
ctrl.waitUntilIdle()
ctrl.sendCommand(borderWaveformControl)
ctrl.sendData([]byte{borderWaveformControlValue})
ctrl.sendCommand(writeLutRegister)
ctrl.sendData(opts.PartialUpdate[:70])
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})
@ -67,9 +72,7 @@ func initDisplayPartial(ctrl controller, opts *Opts) {
ctrl.sendData([]byte{0xC0})
ctrl.sendCommand(masterActivation)
}
ctrl.waitUntilIdle()
ctrl.sendCommand(borderWaveformControl)
ctrl.sendData([]byte{0x01})
}

@ -5,6 +5,7 @@
package waveshare2in13v2
import (
"bytes"
"testing"
"github.com/google/go-cmp/cmp"
@ -32,7 +33,7 @@ func (r *fakeController) sendData(data []byte) {
func (*fakeController) waitUntilIdle() {
}
func TestInitDisplayFull(t *testing.T) {
func TestInitDisplay(t *testing.T) {
for _, tc := range []struct {
name string
opts Opts
@ -49,8 +50,6 @@ func TestInitDisplayFull(t *testing.T) {
cmd: driverOutputControl,
data: []byte{250 - 1, 0, 0},
},
{cmd: borderWaveformControl, data: []byte{0x03}},
{cmd: writeVcomRegister, data: []byte{0x55}},
{cmd: gateDrivingVoltageControl, data: []byte{gateDrivingVoltage19V}},
{
cmd: sourceDrivingVoltageControl,
@ -62,48 +61,59 @@ func TestInitDisplayFull(t *testing.T) {
},
{cmd: setDummyLinePeriod, data: []byte{0x30}},
{cmd: setGateTime, data: []byte{0x0a}},
{cmd: writeLutRegister, data: EPD2in13v2.FullUpdate},
},
},
} {
t.Run(tc.name, func(t *testing.T) {
var got fakeController
initDisplayFull(&got, &tc.opts)
initDisplay(&got, &tc.opts)
if diff := cmp.Diff([]record(got), tc.want, cmpopts.EquateEmpty(), cmp.AllowUnexported(record{})); diff != "" {
t.Errorf("initDisplayFull() difference (-got +want):\n%s", diff)
t.Errorf("initDisplay() difference (-got +want):\n%s", diff)
}
})
}
}
func TestInitDisplayPartial(t *testing.T) {
func TestConfigDisplayMode(t *testing.T) {
for _, tc := range []struct {
name string
opts Opts
mode PartialUpdate
lut LUT
want []record
}{
{
name: "epd2in13v2",
opts: EPD2in13v2,
name: "full",
mode: Full,
lut: bytes.Repeat([]byte{'F'}, 100),
want: []record{
{cmd: writeVcomRegister, data: []byte{0x26}},
{cmd: writeLutRegister, data: EPD2in13v2.PartialUpdate},
{cmd: writeVcomRegister, data: []byte{0x55}},
{cmd: borderWaveformControl, data: []byte{0x03}},
{cmd: writeLutRegister, data: bytes.Repeat([]byte{'F'}, 70)},
},
},
{
name: "partial",
mode: Partial,
lut: bytes.Repeat([]byte{'P'}, 70),
want: []record{
{cmd: writeVcomRegister, data: []byte{0x24}},
{cmd: borderWaveformControl, data: []byte{0x01}},
{cmd: writeLutRegister, data: bytes.Repeat([]byte{'P'}, 70)},
{cmd: 0x37, data: []byte{0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00}},
{cmd: displayUpdateControl2, data: []byte{0xc0}},
{cmd: masterActivation},
{cmd: borderWaveformControl, data: []byte{0x01}},
},
},
} {
t.Run(tc.name, func(t *testing.T) {
var got fakeController
initDisplayPartial(&got, &tc.opts)
configDisplayMode(&got, tc.mode, tc.lut)
if diff := cmp.Diff([]record(got), tc.want, cmpopts.EquateEmpty(), cmp.AllowUnexported(record{})); diff != "" {
t.Errorf("initDisplayPartial() difference (-got +want):\n%s", diff)
t.Errorf("configDisplayMode() difference (-got +want):\n%s", diff)
}
})
}

@ -163,6 +163,18 @@ func NewHat(p spi.Port, opts *Opts) (*Dev, error) {
return New(p, dc, cs, rst, busy, opts)
}
func (d *Dev) configMode(ctrl controller, mode PartialUpdate) {
var lut LUT
if mode == Full {
lut = d.opts.FullUpdate
} else {
lut = d.opts.PartialUpdate
}
configDisplayMode(ctrl, mode, lut)
}
// Init will initialize the display with the partial-update or full-update mode.
func (d *Dev) Init(partialUpdate PartialUpdate) error {
// Hardware Reset
@ -172,10 +184,10 @@ func (d *Dev) Init(partialUpdate PartialUpdate) error {
eh := errorHandler{d: *d}
if partialUpdate {
initDisplayPartial(&eh, d.opts)
} else {
initDisplayFull(&eh, d.opts)
initDisplay(&eh, d.opts)
if eh.err == nil {
d.configMode(&eh, partialUpdate)
}
return eh.err

Loading…
Cancel
Save