2014-04-28 09:54:41 +08:00
|
|
|
package i2c
|
|
|
|
|
2014-11-30 04:10:23 +08:00
|
|
|
import "github.com/hybridgroup/gobot"
|
2014-04-28 09:54:41 +08:00
|
|
|
|
2014-11-23 11:38:39 +08:00
|
|
|
var _ gobot.Driver = (*HMC6352Driver)(nil)
|
2014-11-17 04:25:48 +08:00
|
|
|
|
2014-04-28 09:54:41 +08:00
|
|
|
type HMC6352Driver struct {
|
2014-11-23 11:38:39 +08:00
|
|
|
name string
|
2014-11-30 04:10:23 +08:00
|
|
|
connection I2c
|
2014-04-28 09:54:41 +08:00
|
|
|
}
|
|
|
|
|
2014-10-21 01:42:01 +08:00
|
|
|
// NewHMC6352Driver creates a new driver with specified name and i2c interface
|
2014-11-30 04:10:23 +08:00
|
|
|
func NewHMC6352Driver(a I2c, name string) *HMC6352Driver {
|
2014-04-28 09:54:41 +08:00
|
|
|
return &HMC6352Driver{
|
2014-11-23 11:38:39 +08:00
|
|
|
name: name,
|
2014-11-30 04:10:23 +08:00
|
|
|
connection: a,
|
2014-04-28 09:54:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-23 11:38:39 +08:00
|
|
|
func (h *HMC6352Driver) Name() string { return h.name }
|
2014-11-30 04:10:23 +08:00
|
|
|
func (h *HMC6352Driver) Connection() gobot.Connection { return h.connection.(gobot.Connection) }
|
2014-06-16 08:22:50 +08:00
|
|
|
|
2014-11-29 10:37:03 +08:00
|
|
|
// Start initialized the hmc6352
|
2014-11-20 15:21:19 +08:00
|
|
|
func (h *HMC6352Driver) Start() (errs []error) {
|
2014-11-30 04:10:23 +08:00
|
|
|
if err := h.connection.I2cStart(0x21); err != nil {
|
2014-11-20 15:21:19 +08:00
|
|
|
return []error{err}
|
|
|
|
}
|
2014-11-30 04:10:23 +08:00
|
|
|
if err := h.connection.I2cWrite([]byte("A")); err != nil {
|
2014-11-20 15:21:19 +08:00
|
|
|
return []error{err}
|
2014-11-20 08:56:48 +08:00
|
|
|
}
|
2014-11-20 15:21:19 +08:00
|
|
|
return
|
2014-11-20 08:56:48 +08:00
|
|
|
}
|
|
|
|
|
2014-11-20 15:21:19 +08:00
|
|
|
// Halt returns true if devices is halted successfully
|
|
|
|
func (h *HMC6352Driver) Halt() (errs []error) { return }
|
|
|
|
|
2014-11-20 08:56:48 +08:00
|
|
|
// Heading returns the current heading
|
|
|
|
func (h *HMC6352Driver) Heading() (heading uint16, err error) {
|
2014-11-30 04:10:23 +08:00
|
|
|
if err = h.connection.I2cWrite([]byte("A")); err != nil {
|
2014-11-20 08:56:48 +08:00
|
|
|
return
|
|
|
|
}
|
2014-11-30 04:10:23 +08:00
|
|
|
ret, err := h.connection.I2cRead(2)
|
2014-11-20 08:56:48 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(ret) == 2 {
|
|
|
|
heading = (uint16(ret[1]) + uint16(ret[0])*256) / 10
|
|
|
|
return
|
|
|
|
} else {
|
2014-11-30 04:10:23 +08:00
|
|
|
err = ErrNotEnoughBytes
|
2014-11-20 08:56:48 +08:00
|
|
|
}
|
|
|
|
return
|
2014-04-28 09:54:41 +08:00
|
|
|
}
|