/
max31855.go
83 lines (62 loc) · 1.77 KB
/
max31855.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package max31855
import (
"errors"
"fmt"
"periph.io/x/periph/conn/physic"
"periph.io/x/periph/conn/spi"
)
// ErrOpenCircuit - Thermocouple is not connected
var ErrOpenCircuit error = errors.New("Thermocouple is not connected")
// ErrShortToGround - Short Circuit to Ground
var ErrShortToGround error = errors.New("Short Circuit to Ground")
// ErrShortToVcc - Short Circuit to Power
var ErrShortToVcc error = errors.New("Short Circuit to Power")
// ErrReadingValue - Error Reading Value
var ErrReadingValue error = errors.New("Error Reading Value")
// Dev - A handle to contain the SPI connection
type Dev struct {
c spi.Conn
}
// New - Connects to the MAX31855
func New(p spi.Port) (*Dev, error) {
c, err := p.Connect(5*physic.MegaHertz, spi.Mode0, 8)
if err != nil {
return nil, err
}
d := &Dev{
c: c,
}
return d, nil
}
// Temp - contains the temperature at both ends of the thermcouple
type Temp struct {
Thermocouple physic.Temperature
Internal physic.Temperature
}
// GetTemp - Gets the current temperature in Celcius
func (d *Dev) GetTemp() (Temp, error) {
raw := make([]byte, 4)
if err := d.c.Tx(nil, raw); err != nil {
return Temp{}, err
}
if raw[3]&0x01 != 0 {
return Temp{}, ErrOpenCircuit
}
if raw[3]&0x02 != 0 {
return Temp{}, ErrShortToGround
}
if raw[3]&0x04 != 0 {
return Temp{}, ErrShortToVcc
}
var internal physic.Temperature
var thermocouple physic.Temperature
thermocoupleWord := ((uint16(raw[0]) << 8) | uint16(raw[1])) >> 2
thermocouple.Set(fmt.Sprintf("%fC", float64(int16(thermocoupleWord))*0.25))
internalWord := ((uint16(raw[2]) << 8) | uint16(raw[3])) >> 4
internal.Set(fmt.Sprintf("%fC", float64(int16(internalWord))*0.0625))
temp := Temp{
Internal: internal,
Thermocouple: thermocouple,
}
return temp, nil
}