Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expectation driven SPI test helper (RFC) #742

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions max6675/max6675.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ package max6675

import (
"errors"
"machine"

"tinygo.org/x/drivers"
)

// ErrThermocoupleOpen is returned when the thermocouple input is open.
// i.e. not attached or faulty
var ErrThermocoupleOpen = errors.New("thermocouple input open")

type Device struct {
bus machine.SPI
cs machine.Pin
bus drivers.SPI
cs drivers.Pin
}

// Create a new Device to read from a MAX6675 thermocouple.
// Pins must be configured before use. Frequency for SPI
// should be 4.3MHz maximum.
func NewDevice(bus machine.SPI, cs machine.Pin) *Device {
func NewDevice(bus drivers.SPI, cs drivers.Pin) *Device {
return &Device{
bus: bus,
cs: cs,
Expand Down
45 changes: 45 additions & 0 deletions max6675/max6675_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package max6675_test

import (
"testing"

qt "github.com/frankban/quicktest"
"tinygo.org/x/drivers/max6675"
"tinygo.org/x/drivers/tester"
)

func Test_MAX6675_Read(t *testing.T) {
c := qt.New(t)
spi := tester.NewSPIBus(c)

var expectedCelsius float32 = 20.25

temp := uint16(expectedCelsius/0.25) << 3

spi.Expect(
[]byte{0, 0},
[]byte{byte(temp >> 8), byte(temp)},
)

dev := max6675.NewDevice(spi, tester.NewNoopPin())

actual, err := dev.Read()
c.Assert(err, qt.Equals, nil)
c.Assert(actual, qt.Equals, expectedCelsius)
}

func Test_MAX6675_Read_ErrThermocoupleOpen(t *testing.T) {
c := qt.New(t)
spi := tester.NewSPIBus(c)

spi.Expect(
[]byte{0, 0},
[]byte{0, 0x04},
)

dev := max6675.NewDevice(spi, tester.NewNoopPin())

actual, err := dev.Read()
c.Assert(err, qt.Equals, max6675.ErrThermocoupleOpen)
c.Assert(actual, qt.Equals, float32(0))
}
12 changes: 12 additions & 0 deletions pin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package drivers

type Pin interface {
// Configure(config PinConfig)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented lines?

Get() bool
High()
Low()
//PortMaskClear() (*uint32, uint32)
// PortMaskSet() (*uint32, uint32)
Set(high bool)
// SetInterrupt(change PinChange, callback func(Pin)) error
}
12 changes: 12 additions & 0 deletions tester/pin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package tester

type NoopPin struct{}

func NewNoopPin() *NoopPin {
return &NoopPin{}
}

func (n *NoopPin) Get() bool { return true }
func (n *NoopPin) High() {}
func (n *NoopPin) Low() {}
func (n *NoopPin) Set(high bool) {}
56 changes: 56 additions & 0 deletions tester/spi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package tester

import (
"bytes"
)

type spiExpectation struct {
write []byte
read []byte
}

type SPIBus struct {
c Failer
expectations []spiExpectation
}

func NewSPIBus(c Failer) *SPIBus {
return &SPIBus{
c: c,
expectations: []spiExpectation{},
}
}

func (s *SPIBus) Tx(w, r []byte) error {
if len(s.expectations) == 0 {
s.c.Fatalf("unexpected SPI exchange")
}
ex := s.expectations[0]
if !bytes.Equal(ex.write, w) {
s.c.Fatalf("unexpected SPI write: got %#v, expecting %#v", w, ex.write)
}
copy(r, ex.read)
s.expectations = s.expectations[1:]
return nil
}

func (s *SPIBus) Transfer(b byte) (byte, error) {
buf := make([]byte, 1)
err := s.Tx([]byte{b}, buf)
return buf[0], err
}

func (s *SPIBus) Expect(in []byte, out []byte) {
if len(in) != len(out) {
s.c.Fatalf("Expect: input and output slices must be the same length")
}
s.expectations = append(s.expectations, spiExpectation{in, out})
}

type Pin struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be in tester/pin.go?

c Failer
}

func NewPin(c Failer) *Pin {
return &Pin{c}
}