File tree Expand file tree Collapse file tree 5 files changed +130
-4
lines changed Expand file tree Collapse file tree 5 files changed +130
-4
lines changed Original file line number Diff line number Diff line change @@ -3,22 +3,23 @@ package max6675
3
3
4
4
import (
5
5
"errors"
6
- "machine"
6
+
7
+ "tinygo.org/x/drivers"
7
8
)
8
9
9
10
// ErrThermocoupleOpen is returned when the thermocouple input is open.
10
11
// i.e. not attached or faulty
11
12
var ErrThermocoupleOpen = errors .New ("thermocouple input open" )
12
13
13
14
type Device struct {
14
- bus machine .SPI
15
- cs machine .Pin
15
+ bus drivers .SPI
16
+ cs drivers .Pin
16
17
}
17
18
18
19
// Create a new Device to read from a MAX6675 thermocouple.
19
20
// Pins must be configured before use. Frequency for SPI
20
21
// should be 4.3MHz maximum.
21
- func NewDevice (bus machine .SPI , cs machine .Pin ) * Device {
22
+ func NewDevice (bus drivers .SPI , cs drivers .Pin ) * Device {
22
23
return & Device {
23
24
bus : bus ,
24
25
cs : cs ,
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ package drivers
2
+
3
+ type Pin interface {
4
+ // Configure(config PinConfig)
5
+ Get () bool
6
+ High ()
7
+ Low ()
8
+ //PortMaskClear() (*uint32, uint32)
9
+ // PortMaskSet() (*uint32, uint32)
10
+ Set (high bool )
11
+ // SetInterrupt(change PinChange, callback func(Pin)) error
12
+ }
Original file line number Diff line number Diff line change
1
+ package tester
2
+
3
+ type NoopPin struct {}
4
+
5
+ func NewNoopPin () * NoopPin {
6
+ return & NoopPin {}
7
+ }
8
+
9
+ func (n * NoopPin ) Get () bool { return true }
10
+ func (n * NoopPin ) High () {}
11
+ func (n * NoopPin ) Low () {}
12
+ func (n * NoopPin ) Set (high bool ) {}
Original file line number Diff line number Diff line change
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
+ }
49
+
50
+ type Pin struct {
51
+ c Failer
52
+ }
53
+
54
+ func NewPin (c Failer ) * Pin {
55
+ return & Pin {c }
56
+ }
You can’t perform that action at this time.
0 commit comments