mirror of https://github.com/periph/devices
waveshare213v2: Move initialization to testable functions
parent
347b4e237c
commit
cbda71a0c8
@ -0,0 +1,75 @@
|
||||
// Copyright 2021 The Periph Authors. All rights reserved.
|
||||
// Use of this source code is governed under the Apache License, Version 2.0
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
package waveshare2in13v2
|
||||
|
||||
type controller interface {
|
||||
sendCommand(byte)
|
||||
sendData([]byte)
|
||||
waitUntilIdle()
|
||||
}
|
||||
|
||||
func initDisplayFull(ctrl controller, opts *Opts) {
|
||||
ctrl.waitUntilIdle()
|
||||
ctrl.sendCommand(swReset)
|
||||
ctrl.waitUntilIdle()
|
||||
|
||||
ctrl.sendCommand(setAnalogBlockControl)
|
||||
ctrl.sendData([]byte{0x54})
|
||||
|
||||
ctrl.sendCommand(setDigitalBlockControl)
|
||||
ctrl.sendData([]byte{0x3B})
|
||||
|
||||
ctrl.sendCommand(driverOutputControl)
|
||||
ctrl.sendData([]byte{
|
||||
byte((opts.Height - 1) % 0xFF),
|
||||
byte((opts.Height - 1) / 0xFF),
|
||||
0x00,
|
||||
})
|
||||
|
||||
ctrl.sendCommand(borderWaveformControl)
|
||||
ctrl.sendData([]byte{0x03})
|
||||
|
||||
ctrl.sendCommand(writeVcomRegister)
|
||||
ctrl.sendData([]byte{0x55})
|
||||
|
||||
ctrl.sendCommand(gateDrivingVoltageControl)
|
||||
ctrl.sendData([]byte{gateDrivingVoltage19V})
|
||||
|
||||
ctrl.sendCommand(sourceDrivingVoltageControl)
|
||||
ctrl.sendData([]byte{sourceDrivingVoltageVSH1_15V, sourceDrivingVoltageVSH2_5V, sourceDrivingVoltageVSL_neg15V})
|
||||
|
||||
ctrl.sendCommand(setDummyLinePeriod)
|
||||
ctrl.sendData([]byte{0x30})
|
||||
|
||||
ctrl.sendCommand(setGateTime)
|
||||
ctrl.sendData([]byte{0x0A})
|
||||
|
||||
ctrl.sendCommand(writeLutRegister)
|
||||
ctrl.sendData(opts.FullUpdate[:70])
|
||||
}
|
||||
|
||||
func initDisplayPartial(ctrl controller, opts *Opts) {
|
||||
ctrl.sendCommand(writeVcomRegister)
|
||||
ctrl.sendData([]byte{0x26})
|
||||
|
||||
ctrl.waitUntilIdle()
|
||||
|
||||
ctrl.sendCommand(writeLutRegister)
|
||||
ctrl.sendData(opts.PartialUpdate[:70])
|
||||
|
||||
// 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{0xC0})
|
||||
|
||||
ctrl.sendCommand(masterActivation)
|
||||
|
||||
ctrl.waitUntilIdle()
|
||||
|
||||
ctrl.sendCommand(borderWaveformControl)
|
||||
ctrl.sendData([]byte{0x01})
|
||||
}
|
||||
@ -0,0 +1,110 @@
|
||||
// Copyright 2021 The Periph Authors. All rights reserved.
|
||||
// Use of this source code is governed under the Apache License, Version 2.0
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
package waveshare2in13v2
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
)
|
||||
|
||||
type record struct {
|
||||
cmd byte
|
||||
data []byte
|
||||
}
|
||||
|
||||
type fakeController []record
|
||||
|
||||
func (r *fakeController) sendCommand(cmd byte) {
|
||||
*r = append(*r, record{
|
||||
cmd: cmd,
|
||||
})
|
||||
}
|
||||
|
||||
func (r *fakeController) sendData(data []byte) {
|
||||
cur := &(*r)[len(*r)-1]
|
||||
cur.data = append(cur.data, data...)
|
||||
}
|
||||
|
||||
func (*fakeController) waitUntilIdle() {
|
||||
}
|
||||
|
||||
func TestInitDisplayFull(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
opts Opts
|
||||
want []record
|
||||
}{
|
||||
{
|
||||
name: "epd2in13v2",
|
||||
opts: EPD2in13v2,
|
||||
want: []record{
|
||||
{cmd: swReset},
|
||||
{cmd: setAnalogBlockControl, data: []byte{0x54}},
|
||||
{cmd: setDigitalBlockControl, data: []byte{0x3b}},
|
||||
{
|
||||
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,
|
||||
data: []byte{
|
||||
sourceDrivingVoltageVSH1_15V,
|
||||
sourceDrivingVoltageVSH2_5V,
|
||||
sourceDrivingVoltageVSL_neg15V,
|
||||
},
|
||||
},
|
||||
{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)
|
||||
|
||||
if diff := cmp.Diff([]record(got), tc.want, cmpopts.EquateEmpty(), cmp.AllowUnexported(record{})); diff != "" {
|
||||
t.Errorf("initDisplayFull() difference (-got +want):\n%s", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitDisplayPartial(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
opts Opts
|
||||
want []record
|
||||
}{
|
||||
{
|
||||
name: "epd2in13v2",
|
||||
opts: EPD2in13v2,
|
||||
want: []record{
|
||||
{cmd: writeVcomRegister, data: []byte{0x26}},
|
||||
{cmd: writeLutRegister, data: EPD2in13v2.PartialUpdate},
|
||||
{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)
|
||||
|
||||
if diff := cmp.Diff([]record(got), tc.want, cmpopts.EquateEmpty(), cmp.AllowUnexported(record{})); diff != "" {
|
||||
t.Errorf("initDisplayPartial() difference (-got +want):\n%s", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue