1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-01 13:48:57 +08:00
hybridgroup.gobot/drivers/i2c/hmc6352_driver.go

76 lines
1.7 KiB
Go
Raw Normal View History

2014-04-27 18:54:41 -07:00
package i2c
import "gobot.io/x/gobot"
2014-04-27 18:54:41 -07:00
const hmc6352Address = 0x21
// HMC6352Driver is a Driver for a HMC6352 digital compass
2014-04-27 18:54:41 -07:00
type HMC6352Driver struct {
name string
connector I2cConnector
connection I2cConnection
I2cConfig
2014-04-27 18:54:41 -07:00
}
// NewHMC6352Driver creates a new driver with specified i2c interface
func NewHMC6352Driver(a I2cConnector, options ...func(I2cConfig)) *HMC6352Driver {
hmc := &HMC6352Driver{
name: gobot.DefaultName("HMC6352"),
connector: a,
I2cConfig: NewI2cConfig(),
2014-04-27 18:54:41 -07:00
}
for _, option := range options {
option(hmc)
}
return hmc
2014-04-27 18:54:41 -07:00
}
// Name returns the name for this Driver
func (h *HMC6352Driver) Name() string { return h.name }
// SetName sets the name for this Driver
func (h *HMC6352Driver) SetName(n string) { h.name = n }
// Connection returns the connection for this Driver
func (h *HMC6352Driver) Connection() gobot.Connection { return h.connector.(gobot.Connection) }
// Start initializes the hmc6352
func (h *HMC6352Driver) Start() (err error) {
bus := h.GetBus(h.connector.I2cGetDefaultBus())
address := h.GetAddress(hmc6352Address)
h.connection, err = h.connector.I2cGetConnection(address, bus)
if err != nil {
return err
}
if _, err := h.connection.Write([]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.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
} 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
}