2014-04-27 18:54:41 -07:00
|
|
|
package i2c
|
|
|
|
|
2016-12-08 13:24:03 +01:00
|
|
|
import "gobot.io/x/gobot"
|
2014-04-27 18:54:41 -07:00
|
|
|
|
2015-07-03 18:57:29 -07:00
|
|
|
const hmc6352Address = 0x21
|
|
|
|
|
2016-12-20 18:59:26 +01:00
|
|
|
// HMC6352Driver is a Driver for a HMC6352 digital compass
|
2014-04-27 18:54:41 -07:00
|
|
|
type HMC6352Driver struct {
|
2014-11-22 19:38:39 -08:00
|
|
|
name string
|
2017-02-06 00:19:42 +01:00
|
|
|
connector I2cConnector
|
|
|
|
connection I2cConnection
|
2017-02-09 14:35:48 +01:00
|
|
|
I2cConfig
|
2014-04-27 18:54:41 -07:00
|
|
|
}
|
|
|
|
|
2016-09-25 14:08:18 +02:00
|
|
|
// NewHMC6352Driver creates a new driver with specified i2c interface
|
2017-02-09 14:35:48 +01:00
|
|
|
func NewHMC6352Driver(a I2cConnector, options ...func(I2cConfig)) *HMC6352Driver {
|
2017-02-09 11:23:36 +01:00
|
|
|
hmc := &HMC6352Driver{
|
2017-02-06 00:19:42 +01:00
|
|
|
name: gobot.DefaultName("HMC6352"),
|
|
|
|
connector: a,
|
2017-02-09 14:35:48 +01:00
|
|
|
I2cConfig: NewI2cConfig(),
|
2014-04-27 18:54:41 -07:00
|
|
|
}
|
2017-02-09 11:23:36 +01:00
|
|
|
|
|
|
|
for _, option := range options {
|
|
|
|
option(hmc)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hmc
|
2014-04-27 18:54:41 -07:00
|
|
|
}
|
|
|
|
|
2016-12-20 18:59:26 +01:00
|
|
|
// Name returns the name for this Driver
|
2017-02-02 15:46:00 +01:00
|
|
|
func (h *HMC6352Driver) Name() string { return h.name }
|
2016-12-20 18:59:26 +01:00
|
|
|
|
|
|
|
// SetName sets the name for this Driver
|
2017-02-02 15:46:00 +01:00
|
|
|
func (h *HMC6352Driver) SetName(n string) { h.name = n }
|
2016-12-20 18:59:26 +01:00
|
|
|
|
|
|
|
// Connection returns the connection for this Driver
|
2017-02-06 00:19:42 +01:00
|
|
|
func (h *HMC6352Driver) Connection() gobot.Connection { return h.connector.(gobot.Connection) }
|
2014-06-15 17:22:50 -07:00
|
|
|
|
2016-12-20 18:59:26 +01:00
|
|
|
// Start initializes the hmc6352
|
2016-11-07 14:55:21 +01:00
|
|
|
func (h *HMC6352Driver) Start() (err error) {
|
2017-02-09 14:35:48 +01:00
|
|
|
bus := h.GetBus(h.connector.I2cGetDefaultBus())
|
|
|
|
address := h.GetAddress(hmc6352Address)
|
2017-02-09 11:23:36 +01:00
|
|
|
|
2017-02-09 14:35:48 +01:00
|
|
|
h.connection, err = h.connector.I2cGetConnection(address, bus)
|
2017-02-06 00:19:42 +01:00
|
|
|
if err != nil {
|
2016-11-07 14:55:21 +01:00
|
|
|
return err
|
2014-11-19 23:21:19 -08:00
|
|
|
}
|
2017-02-06 00:19:42 +01:00
|
|
|
|
|
|
|
if _, err := h.connection.Write([]byte("A")); err != nil {
|
2016-11-07 14:55:21 +01:00
|
|
|
return err
|
2014-11-19 16:56:48 -08:00
|
|
|
}
|
2014-11-19 23:21:19 -08:00
|
|
|
return
|
2014-11-19 16:56:48 -08:00
|
|
|
}
|
|
|
|
|
2014-11-19 23:21:19 -08:00
|
|
|
// Halt returns true if devices is halted successfully
|
2016-11-07 14:55:21 +01:00
|
|
|
func (h *HMC6352Driver) Halt() (err error) { return }
|
2014-11-19 23:21:19 -08:00
|
|
|
|
2014-11-19 16:56:48 -08:00
|
|
|
// Heading returns the current heading
|
|
|
|
func (h *HMC6352Driver) Heading() (heading uint16, err error) {
|
2017-02-06 00:19:42 +01:00
|
|
|
if _, err = h.connection.Write([]byte("A")); err != nil {
|
2014-11-19 16:56:48 -08:00
|
|
|
return
|
|
|
|
}
|
2017-02-06 00:19:42 +01:00
|
|
|
buf := []byte{0, 0}
|
|
|
|
bytesRead, err := h.connection.Read(buf)
|
2014-11-19 16:56:48 -08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-02-06 00:19:42 +01:00
|
|
|
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
|
|
|
}
|