forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hmc6352_driver.go
48 lines (40 loc) · 1.12 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
package i2c
import (
"github.com/hybridgroup/gobot"
)
type HMC6352Driver struct {
gobot.Driver
Heading uint16
}
// NewHMC6352Driver creates a new driver with specified name and i2c interface
func NewHMC6352Driver(a I2cInterface, name string) *HMC6352Driver {
return &HMC6352Driver{
Driver: *gobot.NewDriver(
name,
"HMC6352Driver",
a.(gobot.AdaptorInterface),
),
}
}
// adaptor returns HMC6352 adaptor
func (h *HMC6352Driver) adaptor() I2cInterface {
return h.Adaptor().(I2cInterface)
}
// Start writes initialization bytes and reads from adaptor
// using specified interval to update Heading
func (h *HMC6352Driver) Start() bool {
h.adaptor().I2cStart(0x21)
h.adaptor().I2cWrite([]byte("A"))
gobot.Every(h.Interval(), func() {
h.adaptor().I2cWrite([]byte("A"))
ret := h.adaptor().I2cRead(2)
if len(ret) == 2 {
h.Heading = (uint16(ret[1]) + uint16(ret[0])*256) / 10
}
})
return true
}
// Init returns true if device is initialized correctly
func (h *HMC6352Driver) Init() bool { return true }
// Halt returns true if devices is halted successfully
func (h *HMC6352Driver) Halt() bool { return true }