/
mag3110.go
57 lines (49 loc) · 1.7 KB
/
mag3110.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
// Package mag3110 implements a driver for the MAG3110 3-axis magnetometer by
// Freescale/NXP.
//
// Datasheet: https://www.nxp.com/docs/en/data-sheet/MAG3110.pdf
package mag3110
import (
"machine"
)
// Device wraps an I2C connection to a MAG3110 device.
type Device struct {
bus machine.I2C
Address uint16
}
// New creates a new MAG3110 connection. The I2C bus must already be
// configured.
//
// This function only creates the Device object, it does not touch the device.
func New(bus machine.I2C) Device {
return Device{bus, Address}
}
// Connected returns whether a MAG3110 has been found.
// It does a "who am I" request and checks the response.
func (d Device) Connected() bool {
data := []byte{0}
d.bus.ReadRegister(uint8(d.Address), WHO_AM_I, data)
return data[0] == 0xC4
}
// Configure sets up the device for communication.
func (d Device) Configure() {
d.bus.WriteRegister(uint8(d.Address), CTRL_REG2, []uint8{0x80}) // Power down when not used
}
// ReadMagnetic reads the vectors of the magnetic field of the device and
// returns it.
func (d Device) ReadMagnetic() (x int16, y int16, z int16) {
d.bus.WriteRegister(uint8(d.Address), CTRL_REG1, []uint8{0x1a}) // Request a measurement
data := make([]byte, 6)
d.bus.ReadRegister(uint8(d.Address), OUT_X_MSB, data)
x = int16((uint16(data[0]) << 8) | uint16(data[1]))
y = int16((uint16(data[2]) << 8) | uint16(data[3]))
z = int16((uint16(data[4]) << 8) | uint16(data[5]))
return
}
// ReadTemperature reads and returns the current die temperature in
// celsius milli degrees (ºC/1000).
func (d Device) ReadTemperature() (int32, error) {
data := make([]byte, 1)
d.bus.ReadRegister(uint8(d.Address), DIE_TEMP, data)
return int32(data[0]) * 1000, nil
}