Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
5 changes: 2 additions & 3 deletions max6675/max6675.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package max6675

import (
"errors"
"machine"

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

type Device struct {
bus drivers.SPI
cs machine.Pin
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 drivers.SPI, cs machine.Pin) *Device {
func NewDevice(bus drivers.SPI, cs drivers.Pin) *Device {
return &Device{
bus: bus,
cs: cs,
Expand Down
48 changes: 48 additions & 0 deletions max6675/max6675_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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)
cs := tester.NewPin(c)

var expectedCelsius float32 = 20.25

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

cs.ExpectSet(false)
spi.Expect(
[]byte{0, 0},
[]byte{byte(temp >> 8), byte(temp)},
)
cs.ExpectSet(true)

dev := max6675.NewDevice(spi, cs)

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))
}
8 changes: 8 additions & 0 deletions pin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package drivers

type Pin interface {
Get() bool
High()
Low()
Set(high bool)
}
73 changes: 73 additions & 0 deletions tester/pin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package tester

import "tinygo.org/x/drivers"

var _ drivers.Pin = (*Pin)(nil)
var _ drivers.Pin = (*NoopPin)(nil)

type pinExpectation struct {
get bool
value bool
}

type Pin struct {
c Failer
expectations []pinExpectation
}

func (p *Pin) ExpectGet(high bool) {
p.expectations = append(p.expectations, pinExpectation{get: true, value: high})
}

func (p *Pin) ExpectSet(high bool) {
p.expectations = append(p.expectations, pinExpectation{get: false, value: high})
}

func (p *Pin) Get() bool {
if len(p.expectations) == 0 {
p.c.Fatalf("unexpected pin read")
}
ex := p.expectations[0]
if !ex.get {
p.c.Fatalf("unexpected pin read")
}
p.expectations = p.expectations[1:]
return ex.value
}

func (p *Pin) Set(high bool) {
if len(p.expectations) == 0 {
p.c.Fatalf("unexpected pin write")
}
ex := p.expectations[0]
if ex.get {
p.c.Fatalf("unexpected pin write")
}
if ex.value != high {
p.c.Fatalf("unexpected pin write: got %v, expecting %v", high, ex.value)
}
p.expectations = p.expectations[1:]
}

func (p *Pin) High() {
p.Set(true)
}

func (p *Pin) Low() {
p.Set(false)
}

func NewPin(c Failer) *Pin {
return &Pin{c, []pinExpectation{}}
}

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) {}
48 changes: 48 additions & 0 deletions tester/spi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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})
}