diff --git a/examples/seesaw/main.go b/examples/seesaw/main.go index 14460b0..95472d2 100644 --- a/examples/seesaw/main.go +++ b/examples/seesaw/main.go @@ -4,6 +4,7 @@ import ( "machine" "strconv" "time" + "tinygo.org/x/drivers/seesaw" ) @@ -18,8 +19,11 @@ func main() { dev.Address = 0x36 + // the soil sensor is especially slow, let's give it some more time + dev.ReadDelay = readDelay + var buf [2]byte - err := dev.Read(seesaw.ModuleTouchBase, seesaw.FunctionTouchChannelOffset, buf[:], readDelay) + err := dev.Read(seesaw.ModuleTouchBase, seesaw.FunctionTouchChannelOffset, buf[:]) if err != nil { panic(err) } diff --git a/seesaw/seesaw.go b/seesaw/seesaw.go index 7d93021..3859a8f 100644 --- a/seesaw/seesaw.go +++ b/seesaw/seesaw.go @@ -18,8 +18,9 @@ import ( // built on top of it come with their own respective default addresses. const DefaultAddress = 0x49 -// empirically determined standardDelay, the one from the official library seems to be too short (250us) -const defaultDelay = 100 * time.Millisecond +// DefaultReadDelay is an empirically determined delay used when reading from the device, +// the one from the official library seems to be too short (250us) +const DefaultReadDelay = 100 * time.Millisecond const ( seesawHwIdCodeSAMD09 = 0x55 // HW ID code for SAMD09 @@ -27,16 +28,16 @@ const ( ) type Device struct { - bus drivers.I2C - Address uint16 - standardDelay time.Duration + bus drivers.I2C + Address uint16 + ReadDelay time.Duration } func New(bus drivers.I2C) *Device { return &Device{ - bus: bus, - Address: DefaultAddress, - standardDelay: defaultDelay, + bus: bus, + Address: DefaultAddress, + ReadDelay: DefaultReadDelay, } } @@ -92,16 +93,16 @@ func (d *Device) WriteRegister(module ModuleBaseAddress, function FunctionAddres // ReadRegister reads a single register from seesaw func (d *Device) ReadRegister(module ModuleBaseAddress, function FunctionAddress) (byte, error) { var buf [1]byte - err := d.Read(module, function, buf[:], d.standardDelay) + err := d.Read(module, function, buf[:]) if err != nil { return 0, err } return buf[0], nil } -// Read reads a number of bytes from the device after sending the read command and waiting 'standardDelay'. The delays depend +// Read reads a number of bytes from the device after sending the read command and waiting 'ReadDelay'. The delays depend // on the module and function and are documented in the seesaw datasheet -func (d *Device) Read(module ModuleBaseAddress, function FunctionAddress, buf []byte, delay time.Duration) error { +func (d *Device) Read(module ModuleBaseAddress, function FunctionAddress, buf []byte) error { var cmd [2]byte cmd[0] = byte(module) cmd[1] = byte(function) @@ -113,7 +114,7 @@ func (d *Device) Read(module ModuleBaseAddress, function FunctionAddress, buf [] // This is needed for the client seesaw device to flush its RX buffer and process the command. // See seesaw datasheet for timings for specific modules. - time.Sleep(delay) + time.Sleep(d.ReadDelay) return d.bus.Tx(d.Address, nil, buf) } diff --git a/seesaw/seesaw_test.go b/seesaw/seesaw_test.go index 51a05fa..478238f 100644 --- a/seesaw/seesaw_test.go +++ b/seesaw/seesaw_test.go @@ -2,7 +2,6 @@ package seesaw import ( "testing" - "time" "github.com/frankban/quicktest" ) @@ -57,9 +56,10 @@ func TestDevice_Read(t *testing.T) { ) sut := New(mocked) + sut.ReadDelay = 0 var buf [5]byte - err := sut.Read(ModuleTouchBase, FunctionTouchChannelOffset, buf[:], time.Nanosecond) + err := sut.Read(ModuleTouchBase, FunctionTouchChannelOffset, buf[:]) qt := quicktest.New(t) qt.Assert(err, quicktest.IsNil)