hybridgroup.gobot/platforms/i2c/hmc6352_driver.go

54 lines
1.2 KiB
Go
Raw Normal View History

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
var _ gobot.Driver = (*HMC6352Driver)(nil)
2014-04-28 09:54:41 +08:00
type HMC6352Driver struct {
name string
2014-11-30 04:10:23 +08:00
connection I2c
2014-04-28 09:54:41 +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{
name: name,
2014-11-30 04:10:23 +08:00
connection: a,
2014-04-28 09:54:41 +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) }
// Start initialized the hmc6352
func (h *HMC6352Driver) Start() (errs []error) {
2014-11-30 04:10:23 +08:00
if err := h.connection.I2cStart(0x21); err != nil {
return []error{err}
}
2014-11-30 04:10:23 +08:00
if err := h.connection.I2cWrite([]byte("A")); err != nil {
return []error{err}
2014-11-20 08:56:48 +08:00
}
return
2014-11-20 08:56:48 +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
}