Skip to content

Commit

Permalink
seesaw: remove delay from Read(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasrichner-oviva authored and bgould committed Dec 4, 2023
1 parent f7b80ef commit 68da68c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
6 changes: 5 additions & 1 deletion examples/seesaw/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"machine"
"strconv"
"time"

"tinygo.org/x/drivers/seesaw"
)

Expand All @@ -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)
}
Expand Down
25 changes: 13 additions & 12 deletions seesaw/seesaw.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,26 @@ 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
seesawHwIdCodeTINY8x7 = 0x87 // HW ID code for ATtiny817
)

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,
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions seesaw/seesaw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package seesaw

import (
"testing"
"time"

"github.com/frankban/quicktest"
)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 68da68c

Please sign in to comment.