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

57 lines
1.4 KiB
Go
Raw Normal View History

2014-04-27 18:54:41 -07:00
package i2c
2014-11-29 12:10:23 -08:00
import "github.com/hybridgroup/gobot"
2014-04-27 18:54:41 -07:00
var _ gobot.Driver = (*HMC6352Driver)(nil)
const hmc6352Address = 0x21
2014-04-27 18:54:41 -07:00
type HMC6352Driver struct {
name string
2014-11-29 12:10:23 -08:00
connection I2c
2014-04-27 18:54:41 -07:00
}
// NewHMC6352Driver creates a new driver with specified i2c interface
func NewHMC6352Driver(a I2c) *HMC6352Driver {
2014-04-27 18:54:41 -07:00
return &HMC6352Driver{
name: "HMC6352",
2014-11-29 12:10:23 -08:00
connection: a,
2014-04-27 18:54:41 -07:00
}
}
func (h *HMC6352Driver) Name() string { return h.name }
func (h *HMC6352Driver) SetName(n string) { h.name = n }
2014-11-29 12:10:23 -08:00
func (h *HMC6352Driver) Connection() gobot.Connection { return h.connection.(gobot.Connection) }
// Start initialized the hmc6352
func (h *HMC6352Driver) Start() (err error) {
if err := h.connection.I2cStart(hmc6352Address); err != nil {
return err
}
if err := h.connection.I2cWrite(hmc6352Address, []byte("A")); err != nil {
return err
2014-11-19 16:56:48 -08:00
}
return
2014-11-19 16:56:48 -08:00
}
// Halt returns true if devices is halted successfully
func (h *HMC6352Driver) Halt() (err error) { return }
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.I2cWrite(hmc6352Address, []byte("A")); err != nil {
2014-11-19 16:56:48 -08:00
return
}
ret, err := h.connection.I2cRead(hmc6352Address, 2)
2014-11-19 16: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-29 12:10:23 -08:00
err = ErrNotEnoughBytes
2014-11-19 16:56:48 -08:00
}
return
2014-04-27 18:54:41 -07:00
}