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
+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)