forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hmc6352_driver.go
55 lines (46 loc) · 1.33 KB
/
hmc6352_driver.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
package i2c
import "github.com/hybridgroup/gobot"
var _ gobot.Driver = (*HMC6352Driver)(nil)
const hmc6352Address = 0x21
type HMC6352Driver struct {
name string
connection I2c
}
// NewHMC6352Driver creates a new driver with specified name and i2c interface
func NewHMC6352Driver(a I2c, name string) *HMC6352Driver {
return &HMC6352Driver{
name: name,
connection: a,
}
}
func (h *HMC6352Driver) Name() string { return h.name }
func (h *HMC6352Driver) Connection() gobot.Connection { return h.connection.(gobot.Connection) }
// Start initialized the hmc6352
func (h *HMC6352Driver) Start() (errs []error) {
if err := h.connection.I2cStart(hmc6352Address); err != nil {
return []error{err}
}
if err := h.connection.I2cWrite(hmc6352Address, []byte("A")); err != nil {
return []error{err}
}
return
}
// Halt returns true if devices is halted successfully
func (h *HMC6352Driver) Halt() (errs []error) { return }
// Heading returns the current heading
func (h *HMC6352Driver) Heading() (heading uint16, err error) {
if err = h.connection.I2cWrite(hmc6352Address, []byte("A")); err != nil {
return
}
ret, err := h.connection.I2cRead(hmc6352Address, 2)
if err != nil {
return
}
if len(ret) == 2 {
heading = (uint16(ret[1]) + uint16(ret[0])*256) / 10
return
} else {
err = ErrNotEnoughBytes
}
return
}