mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-12 19:03:42 +00:00
add correct Tx implementation for mock I2C interfaces
This commit is contained in:
+12
-2
@@ -75,6 +75,16 @@ func (d *I2CDevice16) WriteRegister(r uint8, buf []byte) error {
|
||||
|
||||
// Tx implements I2C.Tx.
|
||||
func (bus *I2CDevice16) Tx(w, r []byte) error {
|
||||
// TODO: implement this
|
||||
return nil
|
||||
switch len(w) {
|
||||
case 0:
|
||||
bus.c.Fatalf("i2c mock: need a write byte")
|
||||
return nil
|
||||
case 1:
|
||||
return bus.ReadRegister(w[0], r)
|
||||
default:
|
||||
if len(r) > 0 || len(w) == 1 {
|
||||
bus.c.Fatalf("i2c mock: unsupported lengths in Tx(%d, %d)", len(w), len(r))
|
||||
}
|
||||
return bus.WriteRegister(w[0], w[1:])
|
||||
}
|
||||
}
|
||||
|
||||
+15
-2
@@ -38,6 +38,9 @@ func (d *I2CDevice8) ReadRegister(r uint8, buf []byte) error {
|
||||
if d.Err != nil {
|
||||
return d.Err
|
||||
}
|
||||
if len(buf) == 0 {
|
||||
d.c.Fatalf("no register buffer to read into")
|
||||
}
|
||||
d.assertRegisterRange(r, buf)
|
||||
copy(buf, d.Registers[r:])
|
||||
return nil
|
||||
@@ -55,8 +58,18 @@ func (d *I2CDevice8) WriteRegister(r uint8, buf []byte) error {
|
||||
|
||||
// Tx implements I2C.Tx.
|
||||
func (bus *I2CDevice8) Tx(w, r []byte) error {
|
||||
// TODO: implement this
|
||||
return nil
|
||||
switch len(w) {
|
||||
case 0:
|
||||
bus.c.Fatalf("i2c mock: need a write byte")
|
||||
return nil
|
||||
case 1:
|
||||
return bus.ReadRegister(w[0], r)
|
||||
default:
|
||||
if len(r) > 0 || len(w) == 1 {
|
||||
bus.c.Fatalf("i2c mock: unsupported lengths in Tx(%d, %d)", len(w), len(r))
|
||||
}
|
||||
return bus.WriteRegister(w[0], w[1:])
|
||||
}
|
||||
}
|
||||
|
||||
// assertRegisterRange asserts that reading or writing the given
|
||||
|
||||
Reference in New Issue
Block a user