seesaw: remove delay from Read(...)

This commit is contained in:
Thomas Richner
2023-11-01 21:31:30 +01:00
committed by BCG
parent f7b80ef95a
commit 68da68c8c8
3 changed files with 20 additions and 15 deletions
+5 -1
View File
@@ -4,6 +4,7 @@ import (
"machine" "machine"
"strconv" "strconv"
"time" "time"
"tinygo.org/x/drivers/seesaw" "tinygo.org/x/drivers/seesaw"
) )
@@ -18,8 +19,11 @@ func main() {
dev.Address = 0x36 dev.Address = 0x36
// the soil sensor is especially slow, let's give it some more time
dev.ReadDelay = readDelay
var buf [2]byte var buf [2]byte
err := dev.Read(seesaw.ModuleTouchBase, seesaw.FunctionTouchChannelOffset, buf[:], readDelay) err := dev.Read(seesaw.ModuleTouchBase, seesaw.FunctionTouchChannelOffset, buf[:])
if err != nil { if err != nil {
panic(err) panic(err)
} }
+13 -12
View File
@@ -18,8 +18,9 @@ import (
// built on top of it come with their own respective default addresses. // built on top of it come with their own respective default addresses.
const DefaultAddress = 0x49 const DefaultAddress = 0x49
// empirically determined standardDelay, the one from the official library seems to be too short (250us) // DefaultReadDelay is an empirically determined delay used when reading from the device,
const defaultDelay = 100 * time.Millisecond // the one from the official library seems to be too short (250us)
const DefaultReadDelay = 100 * time.Millisecond
const ( const (
seesawHwIdCodeSAMD09 = 0x55 // HW ID code for SAMD09 seesawHwIdCodeSAMD09 = 0x55 // HW ID code for SAMD09
@@ -27,16 +28,16 @@ const (
) )
type Device struct { type Device struct {
bus drivers.I2C bus drivers.I2C
Address uint16 Address uint16
standardDelay time.Duration ReadDelay time.Duration
} }
func New(bus drivers.I2C) *Device { func New(bus drivers.I2C) *Device {
return &Device{ return &Device{
bus: bus, bus: bus,
Address: DefaultAddress, Address: DefaultAddress,
standardDelay: defaultDelay, ReadDelay: DefaultReadDelay,
} }
} }
@@ -92,16 +93,16 @@ func (d *Device) WriteRegister(module ModuleBaseAddress, function FunctionAddres
// ReadRegister reads a single register from seesaw // ReadRegister reads a single register from seesaw
func (d *Device) ReadRegister(module ModuleBaseAddress, function FunctionAddress) (byte, error) { func (d *Device) ReadRegister(module ModuleBaseAddress, function FunctionAddress) (byte, error) {
var buf [1]byte var buf [1]byte
err := d.Read(module, function, buf[:], d.standardDelay) err := d.Read(module, function, buf[:])
if err != nil { if err != nil {
return 0, err return 0, err
} }
return buf[0], nil 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 // 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 var cmd [2]byte
cmd[0] = byte(module) cmd[0] = byte(module)
cmd[1] = byte(function) 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. // 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. // See seesaw datasheet for timings for specific modules.
time.Sleep(delay) time.Sleep(d.ReadDelay)
return d.bus.Tx(d.Address, nil, buf) return d.bus.Tx(d.Address, nil, buf)
} }
+2 -2
View File
@@ -2,7 +2,6 @@ package seesaw
import ( import (
"testing" "testing"
"time"
"github.com/frankban/quicktest" "github.com/frankban/quicktest"
) )
@@ -57,9 +56,10 @@ func TestDevice_Read(t *testing.T) {
) )
sut := New(mocked) sut := New(mocked)
sut.ReadDelay = 0
var buf [5]byte var buf [5]byte
err := sut.Read(ModuleTouchBase, FunctionTouchChannelOffset, buf[:], time.Nanosecond) err := sut.Read(ModuleTouchBase, FunctionTouchChannelOffset, buf[:])
qt := quicktest.New(t) qt := quicktest.New(t)
qt.Assert(err, quicktest.IsNil) qt.Assert(err, quicktest.IsNil)