mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-16 21:03:23 +00:00
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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user