Skip to content

Commit 68da68c

Browse files
thomasrichner-ovivabgould
authored andcommitted
seesaw: remove delay from Read(...)
1 parent f7b80ef commit 68da68c

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

examples/seesaw/main.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"machine"
55
"strconv"
66
"time"
7+
78
"tinygo.org/x/drivers/seesaw"
89
)
910

@@ -18,8 +19,11 @@ func main() {
1819

1920
dev.Address = 0x36
2021

22+
// the soil sensor is especially slow, let's give it some more time
23+
dev.ReadDelay = readDelay
24+
2125
var buf [2]byte
22-
err := dev.Read(seesaw.ModuleTouchBase, seesaw.FunctionTouchChannelOffset, buf[:], readDelay)
26+
err := dev.Read(seesaw.ModuleTouchBase, seesaw.FunctionTouchChannelOffset, buf[:])
2327
if err != nil {
2428
panic(err)
2529
}

seesaw/seesaw.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,26 @@ import (
1818
// built on top of it come with their own respective default addresses.
1919
const DefaultAddress = 0x49
2020

21-
// empirically determined standardDelay, the one from the official library seems to be too short (250us)
22-
const defaultDelay = 100 * time.Millisecond
21+
// DefaultReadDelay is an empirically determined delay used when reading from the device,
22+
// the one from the official library seems to be too short (250us)
23+
const DefaultReadDelay = 100 * time.Millisecond
2324

2425
const (
2526
seesawHwIdCodeSAMD09 = 0x55 // HW ID code for SAMD09
2627
seesawHwIdCodeTINY8x7 = 0x87 // HW ID code for ATtiny817
2728
)
2829

2930
type Device struct {
30-
bus drivers.I2C
31-
Address uint16
32-
standardDelay time.Duration
31+
bus drivers.I2C
32+
Address uint16
33+
ReadDelay time.Duration
3334
}
3435

3536
func New(bus drivers.I2C) *Device {
3637
return &Device{
37-
bus: bus,
38-
Address: DefaultAddress,
39-
standardDelay: defaultDelay,
38+
bus: bus,
39+
Address: DefaultAddress,
40+
ReadDelay: DefaultReadDelay,
4041
}
4142
}
4243

@@ -92,16 +93,16 @@ func (d *Device) WriteRegister(module ModuleBaseAddress, function FunctionAddres
9293
// ReadRegister reads a single register from seesaw
9394
func (d *Device) ReadRegister(module ModuleBaseAddress, function FunctionAddress) (byte, error) {
9495
var buf [1]byte
95-
err := d.Read(module, function, buf[:], d.standardDelay)
96+
err := d.Read(module, function, buf[:])
9697
if err != nil {
9798
return 0, err
9899
}
99100
return buf[0], nil
100101
}
101102

102-
// Read reads a number of bytes from the device after sending the read command and waiting 'standardDelay'. The delays depend
103+
// Read reads a number of bytes from the device after sending the read command and waiting 'ReadDelay'. The delays depend
103104
// on the module and function and are documented in the seesaw datasheet
104-
func (d *Device) Read(module ModuleBaseAddress, function FunctionAddress, buf []byte, delay time.Duration) error {
105+
func (d *Device) Read(module ModuleBaseAddress, function FunctionAddress, buf []byte) error {
105106
var cmd [2]byte
106107
cmd[0] = byte(module)
107108
cmd[1] = byte(function)
@@ -113,7 +114,7 @@ func (d *Device) Read(module ModuleBaseAddress, function FunctionAddress, buf []
113114

114115
// This is needed for the client seesaw device to flush its RX buffer and process the command.
115116
// See seesaw datasheet for timings for specific modules.
116-
time.Sleep(delay)
117+
time.Sleep(d.ReadDelay)
117118

118119
return d.bus.Tx(d.Address, nil, buf)
119120
}

seesaw/seesaw_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package seesaw
22

33
import (
44
"testing"
5-
"time"
65

76
"github.com/frankban/quicktest"
87
)
@@ -57,9 +56,10 @@ func TestDevice_Read(t *testing.T) {
5756
)
5857

5958
sut := New(mocked)
59+
sut.ReadDelay = 0
6060

6161
var buf [5]byte
62-
err := sut.Read(ModuleTouchBase, FunctionTouchChannelOffset, buf[:], time.Nanosecond)
62+
err := sut.Read(ModuleTouchBase, FunctionTouchChannelOffset, buf[:])
6363

6464
qt := quicktest.New(t)
6565
qt.Assert(err, quicktest.IsNil)

0 commit comments

Comments
 (0)