Skip to content

Commit 5d61ae2

Browse files
committed
Initial expectation based SPI test helper
1 parent 156d6e7 commit 5d61ae2

File tree

5 files changed

+123
-3
lines changed

5 files changed

+123
-3
lines changed

max6675/max6675.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package max6675
33

44
import (
55
"errors"
6-
"machine"
76

87
"tinygo.org/x/drivers"
98
)
@@ -14,13 +13,13 @@ var ErrThermocoupleOpen = errors.New("thermocouple input open")
1413

1514
type Device struct {
1615
bus drivers.SPI
17-
cs machine.Pin
16+
cs drivers.Pin
1817
}
1918

2019
// Create a new Device to read from a MAX6675 thermocouple.
2120
// Pins must be configured before use. Frequency for SPI
2221
// should be 4.3MHz maximum.
23-
func NewDevice(bus drivers.SPI, cs machine.Pin) *Device {
22+
func NewDevice(bus drivers.SPI, cs drivers.Pin) *Device {
2423
return &Device{
2524
bus: bus,
2625
cs: cs,

max6675/max6675_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package max6675_test
2+
3+
import (
4+
"testing"
5+
6+
qt "github.com/frankban/quicktest"
7+
"tinygo.org/x/drivers/max6675"
8+
"tinygo.org/x/drivers/tester"
9+
)
10+
11+
func Test_MAX6675_Read(t *testing.T) {
12+
c := qt.New(t)
13+
spi := tester.NewSPIBus(c)
14+
15+
var expectedCelsius float32 = 20.25
16+
17+
temp := uint16(expectedCelsius/0.25) << 3
18+
19+
spi.Expect(
20+
[]byte{0, 0},
21+
[]byte{byte(temp >> 8), byte(temp)},
22+
)
23+
24+
dev := max6675.NewDevice(spi, tester.NewNoopPin())
25+
26+
actual, err := dev.Read()
27+
c.Assert(err, qt.Equals, nil)
28+
c.Assert(actual, qt.Equals, expectedCelsius)
29+
}
30+
31+
func Test_MAX6675_Read_ErrThermocoupleOpen(t *testing.T) {
32+
c := qt.New(t)
33+
spi := tester.NewSPIBus(c)
34+
35+
spi.Expect(
36+
[]byte{0, 0},
37+
[]byte{0, 0x04},
38+
)
39+
40+
dev := max6675.NewDevice(spi, tester.NewNoopPin())
41+
42+
actual, err := dev.Read()
43+
c.Assert(err, qt.Equals, max6675.ErrThermocoupleOpen)
44+
c.Assert(actual, qt.Equals, float32(0))
45+
}

pin.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package drivers
2+
3+
type Pin interface {
4+
Get() bool
5+
High()
6+
Low()
7+
Set(high bool)
8+
}

tester/pin.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package tester
2+
3+
type Pin struct {
4+
c Failer
5+
}
6+
7+
func NewPin(c Failer) *Pin {
8+
return &Pin{c}
9+
}
10+
11+
type NoopPin struct{}
12+
13+
func NewNoopPin() *NoopPin {
14+
return &NoopPin{}
15+
}
16+
17+
func (n *NoopPin) Get() bool { return true }
18+
func (n *NoopPin) High() {}
19+
func (n *NoopPin) Low() {}
20+
func (n *NoopPin) Set(high bool) {}

tester/spi.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package tester
2+
3+
import (
4+
"bytes"
5+
)
6+
7+
type spiExpectation struct {
8+
write []byte
9+
read []byte
10+
}
11+
12+
type SPIBus struct {
13+
c Failer
14+
expectations []spiExpectation
15+
}
16+
17+
func NewSPIBus(c Failer) *SPIBus {
18+
return &SPIBus{
19+
c: c,
20+
expectations: []spiExpectation{},
21+
}
22+
}
23+
24+
func (s *SPIBus) Tx(w, r []byte) error {
25+
if len(s.expectations) == 0 {
26+
s.c.Fatalf("unexpected SPI exchange")
27+
}
28+
ex := s.expectations[0]
29+
if !bytes.Equal(ex.write, w) {
30+
s.c.Fatalf("unexpected SPI write: got %#v, expecting %#v", w, ex.write)
31+
}
32+
copy(r, ex.read)
33+
s.expectations = s.expectations[1:]
34+
return nil
35+
}
36+
37+
func (s *SPIBus) Transfer(b byte) (byte, error) {
38+
buf := make([]byte, 1)
39+
err := s.Tx([]byte{b}, buf)
40+
return buf[0], err
41+
}
42+
43+
func (s *SPIBus) Expect(in []byte, out []byte) {
44+
if len(in) != len(out) {
45+
s.c.Fatalf("Expect: input and output slices must be the same length")
46+
}
47+
s.expectations = append(s.expectations, spiExpectation{in, out})
48+
}

0 commit comments

Comments
 (0)