hybridgroup.gobot/drivers/i2c/hmc6352_driver.go

56 lines
1.2 KiB
Go
Raw Permalink Normal View History

2014-04-28 09:54:41 +08:00
package i2c
const hmc6352DefaultAddress = 0x21
// HMC6352Driver is a Driver for a HMC6352 digital compass
2014-04-28 09:54:41 +08:00
type HMC6352Driver struct {
*Driver
2014-04-28 09:54:41 +08:00
}
// NewHMC6352Driver creates a new driver with specified i2c interface
// Params:
//
// c Connector - the Adaptor to use with this Driver
//
// Optional params:
//
// i2c.WithBus(int): bus to use with this driver
// i2c.WithAddress(int): address to use with this driver
func NewHMC6352Driver(c Connector, options ...func(Config)) *HMC6352Driver {
h := &HMC6352Driver{
Driver: NewDriver(c, "HMC6352", hmc6352DefaultAddress),
2014-04-28 09:54:41 +08:00
}
h.afterStart = h.initialize
for _, option := range options {
option(h)
}
return h
2014-04-28 09:54:41 +08:00
}
2014-11-20 08:56:48 +08:00
// Heading returns the current heading
func (h *HMC6352Driver) Heading() (uint16, error) {
if _, err := h.connection.Write([]byte("A")); err != nil {
return 0, err
2014-11-20 08:56:48 +08:00
}
buf := []byte{0, 0}
bytesRead, err := h.connection.Read(buf)
2014-11-20 08:56:48 +08:00
if err != nil {
return 0, err
2014-11-20 08:56:48 +08:00
}
if bytesRead == 2 {
heading := (uint16(buf[1]) + uint16(buf[0])*256) / 10
return heading, nil
2014-11-20 08:56:48 +08:00
}
return 0, ErrNotEnoughBytes
2014-04-28 09:54:41 +08:00
}
func (h *HMC6352Driver) initialize() error {
if _, err := h.connection.Write([]byte("A")); err != nil {
return err
}
return nil
}