mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-16 04:43:25 +00:00
tester: improve API surface and implement one more test function in lis2mdl driver
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
+8
-12
@@ -1,27 +1,23 @@
|
||||
package tester
|
||||
|
||||
import (
|
||||
qt "github.com/frankban/quicktest"
|
||||
)
|
||||
|
||||
// I2CBus implements the I2C interface in memory for testing.
|
||||
type I2CBus struct {
|
||||
C *qt.C
|
||||
Devices []*I2CDevice
|
||||
c Failer
|
||||
devices []*I2CDevice
|
||||
}
|
||||
|
||||
// NewI2CBus returns an I2CBus mock I2C instance that uses c to flag errors
|
||||
// if they happen. After creating a I2C instance, add devices
|
||||
// to it with addDevice before using NewI2CBus interface.
|
||||
func NewI2CBus(c *qt.C) *I2CBus {
|
||||
func NewI2CBus(c Failer) *I2CBus {
|
||||
return &I2CBus{
|
||||
C: c,
|
||||
c: c,
|
||||
}
|
||||
}
|
||||
|
||||
// AddDevice adds a new mock device to the mock I2C bus.
|
||||
func (bus *I2CBus) AddDevice(d *I2CDevice) {
|
||||
bus.Devices = append(bus.Devices, d)
|
||||
bus.devices = append(bus.devices, d)
|
||||
}
|
||||
|
||||
// ReadRegister implements I2C.ReadRegister.
|
||||
@@ -42,11 +38,11 @@ func (bus *I2CBus) Tx(addr uint16, input, output []byte) error {
|
||||
|
||||
// FindDevice returns the device with the given address.
|
||||
func (bus *I2CBus) FindDevice(addr uint8) *I2CDevice {
|
||||
for _, dev := range bus.Devices {
|
||||
if dev.Addr == addr {
|
||||
for _, dev := range bus.devices {
|
||||
if dev.Addr() == addr {
|
||||
return dev
|
||||
}
|
||||
}
|
||||
bus.C.Fatalf("invalid device addr %#x passed to i2c bus", addr)
|
||||
bus.c.Fatalf("invalid device addr %#x passed to i2c bus", addr)
|
||||
panic("unreachable")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user