1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-04 22:17:39 +08:00
hybridgroup.gobot/drivers/i2c/hmc6352_driver.go

56 lines
1.2 KiB
Go
Raw Normal View History

2014-04-27 18:54:41 -07:00
package i2c
const hmc6352DefaultAddress = 0x21
// HMC6352Driver is a Driver for a HMC6352 digital compass
2014-04-27 18:54:41 -07:00
type HMC6352Driver struct {
*Driver
2014-04-27 18:54:41 -07: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-27 18:54:41 -07:00
}
h.afterStart = h.initialize
for _, option := range options {
option(h)
}
return h
2014-04-27 18:54:41 -07:00
}
2014-11-19 16:56:48 -08:00
// Heading returns the current heading
func (h *HMC6352Driver) Heading() (heading uint16, err error) {
if _, err = h.connection.Write([]byte("A")); err != nil {
2014-11-19 16:56:48 -08:00
return
}
buf := []byte{0, 0}
bytesRead, err := h.connection.Read(buf)
2014-11-19 16:56:48 -08:00
if err != nil {
return
}
if bytesRead == 2 {
heading = (uint16(buf[1]) + uint16(buf[0])*256) / 10
2014-11-19 16:56:48 -08:00
return
}
err = ErrNotEnoughBytes
2014-11-19 16:56:48 -08:00
return
2014-04-27 18:54:41 -07:00
}
func (h *HMC6352Driver) initialize() error {
if _, err := h.connection.Write([]byte("A")); err != nil {
return err
}
return nil
}