-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcp9808.go
101 lines (81 loc) · 2.26 KB
/
mcp9808.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Package mcp9808 provides an interface to the MCP9808 temperature sensor.
// Datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/25095A.pdf
//
// Deprecated: Use https://periph.io instead. As of 2019-04-02, MCP9808 support
// is in package "periph.io/x/periph/experimental/devices/mcp9808".
package mcp9808
import (
"encoding/binary"
"fmt"
"golang.org/x/exp/io/i2c"
)
const (
// Default I2C address for device
defaultI2CAddr = 0x18
// Register addresses. See page 16 of the datasheet.
regConfig = 0x01
regUpperTempTrip = 0x02
regLowerTempTrip = 0x03
regCriticalTempTrip = 0x04
regTemp = 0x05
regManufacturerID = 0x06
regDeviceID = 0x07
regResolution = 0x08
devPath = "/dev/i2c-1"
// Expected values for manufacturer and device ID
manufacturerID = 0x54
deviceID = 0x400
)
type MCP9808 struct {
device *i2c.Device
}
func New() (*MCP9808, error) {
d, err := i2c.Open(&i2c.Devfs{Dev: devPath}, defaultI2CAddr)
if err != nil {
return nil, fmt.Errorf("mcp9808: %v", err)
}
return &MCP9808{
device: d,
}, nil
}
func (m *MCP9808) Close() error {
return m.device.Close()
}
func (m *MCP9808) Check() error {
mID, err := m.ReadUint16(regManufacturerID)
if err != nil {
return fmt.Errorf("mcp9808: error reading manufacturer ID: %v", err)
}
dID, err := m.ReadUint16(regDeviceID)
if err != nil {
return fmt.Errorf("mcp9808: error reading device ID: %v", err)
}
if mID != manufacturerID || dID != deviceID {
return fmt.Errorf(
"mcp9808: incorrect manufacturer or device ID. Got 0x%x, 0x%x. Expected 0x%x, 0x%x.",
mID, dID, manufacturerID, deviceID)
}
return nil
}
func (m *MCP9808) ReadUint16(reg byte) (uint16, error) {
b := make([]byte, 2)
if err := m.device.ReadReg(reg, b); err != nil {
return 0, err
}
return binary.BigEndian.Uint16(b), nil
}
func tempRegisterToCelsius(regVal uint16) float32 {
// The temp register uses two's complement. See pages 24-25 of the datasheet.
temp := float32(regVal&0x0fff) / 16.0
if regVal&0x1000 > 0 {
temp -= 256.0
}
return temp
}
func (m *MCP9808) ReadTemp() (float32, error) {
regVal, err := m.ReadUint16(regTemp)
if err != nil {
return 0, fmt.Errorf("mcp9808: error reading temp: %v", err)
}
return tempRegisterToCelsius(regVal), nil
}