mcp23017: use new tester package

Also change the `tester` package slightly to use an exposed `Registers`
array rather adding yet another accessor method to retrieve a register
value. This seems to me more transparent and "obvious" - we aren't trying
to hide the fact that there's just a simple memory store there.
Also unexport the `assertRegisterRange` method which was never intended
to be part of the public API.
This commit is contained in:
Roger Peppe
2021-01-18 12:56:19 +00:00
committed by Ron Evans
parent ce5e443084
commit 8cb226938b
7 changed files with 78 additions and 169 deletions
+9 -31
View File
@@ -10,7 +10,7 @@ type I2CDevice struct {
addr uint8
// Registers holds the device registers. It can be inspected
// or changed as desired for testing.
registers [MaxRegisters]uint8
Registers [MaxRegisters]uint8
// If Err is non-nil, it will be returned as the error from the
// I2C methods.
Err error
@@ -29,35 +29,13 @@ func (d *I2CDevice) Addr() uint8 {
return d.addr
}
// SetupRegisters sets all of the Device registers.
// It is intended to be used when setting up a fake device
// for testing expected vs. actual values.
func (d *I2CDevice) SetupRegisters(regs []uint8) {
if len(regs) > MaxRegisters {
panic("exceeded maximum number of registers for fake device")
}
for k, v := range regs {
d.registers[k] = v
}
}
// SetupRegister sets one of the Device registers.
// It is intended to be used when setting up a fake device
// for testing expected vs. actual values.
func (d *I2CDevice) SetupRegister(r, v uint8) {
if r > MaxRegisters {
panic("exceeded maximum number of registers for fake device")
}
d.registers[r] = v
}
// ReadRegister implements I2C.ReadRegister.
func (d *I2CDevice) ReadRegister(r uint8, buf []byte) error {
if d.Err != nil {
return d.Err
}
d.AssertRegisterRange(r, buf)
copy(buf, d.registers[r:])
d.assertRegisterRange(r, buf)
copy(buf, d.Registers[r:])
return nil
}
@@ -66,18 +44,18 @@ func (d *I2CDevice) WriteRegister(r uint8, buf []byte) error {
if d.Err != nil {
return d.Err
}
d.AssertRegisterRange(r, buf)
copy(d.registers[r:], buf)
d.assertRegisterRange(r, buf)
copy(d.Registers[r:], buf)
return nil
}
// AssertRegisterRange asserts that reading or writing the given
// assertRegisterRange asserts that reading or writing the given
// register and subsequent registers is in range of the available registers.
func (d *I2CDevice) AssertRegisterRange(r uint8, buf []byte) {
if int(r) >= len(d.registers) {
func (d *I2CDevice) assertRegisterRange(r uint8, buf []byte) {
if int(r) >= len(d.Registers) {
d.c.Fatalf("register read/write [%#x, %#x] start out of range", r, int(r)+len(buf))
}
if int(r)+len(buf) > len(d.registers) {
if int(r)+len(buf) > len(d.Registers) {
d.c.Fatalf("register read/write [%#x, %#x] end out of range", r, int(r)+len(buf))
}
}
+16
View File
@@ -1,5 +1,7 @@
package tester
import "fmt"
// I2CBus implements the I2C interface in memory for testing.
type I2CBus struct {
c Failer
@@ -16,10 +18,24 @@ func NewI2CBus(c Failer) *I2CBus {
}
// AddDevice adds a new mock device to the mock I2C bus.
// It panics if a device with the same address is added more than once.
func (bus *I2CBus) AddDevice(d *I2CDevice) {
for _, dev := range bus.devices {
if dev.Addr() == d.addr {
panic(fmt.Errorf("device already added at address %#x", d))
}
}
bus.devices = append(bus.devices, d)
}
// NewDevice creates a new device with the given address
// and adds it to the mock I2C bus.
func (bus *I2CBus) NewDevice(addr uint8) *I2CDevice {
dev := NewI2CDevice(bus.c, addr)
bus.AddDevice(dev)
return dev
}
// ReadRegister implements I2C.ReadRegister.
func (bus *I2CBus) ReadRegister(addr uint8, r uint8, buf []byte) error {
return bus.FindDevice(addr).ReadRegister(r, buf)