1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-29 13:49:14 +08:00
hybridgroup.gobot/platforms/i2c/hmc6352_driver.go

64 lines
1.5 KiB
Go
Raw Normal View History

2014-04-27 18:54:41 -07:00
package i2c
import (
2014-11-19 16:56:48 -08:00
"errors"
2014-04-27 18:54:41 -07:00
"github.com/hybridgroup/gobot"
)
var _ gobot.Driver = (*HMC6352Driver)(nil)
2014-04-27 18:54:41 -07:00
type HMC6352Driver struct {
name string
connection gobot.Connection
2014-04-27 18:54:41 -07:00
}
// NewHMC6352Driver creates a new driver with specified name and i2c interface
2014-05-22 21:33:05 -07:00
func NewHMC6352Driver(a I2cInterface, name string) *HMC6352Driver {
2014-04-27 18:54:41 -07:00
return &HMC6352Driver{
name: name,
connection: a.(gobot.Connection),
2014-04-27 18:54:41 -07:00
}
}
func (h *HMC6352Driver) Name() string { return h.name }
func (h *HMC6352Driver) Connection() gobot.Connection { return h.connection }
// adaptor returns HMC6352 adaptor
func (h *HMC6352Driver) adaptor() I2cInterface {
return h.Connection().(I2cInterface)
}
// Start writes initialization bytes and reads from adaptor
// using specified interval to update Heading
func (h *HMC6352Driver) Start() (errs []error) {
if err := h.adaptor().I2cStart(0x21); err != nil {
return []error{err}
}
if err := h.adaptor().I2cWrite([]byte("A")); err != nil {
return []error{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() (errs []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.adaptor().I2cWrite([]byte("A")); err != nil {
return
}
ret, err := h.adaptor().I2cRead(2)
if err != nil {
return
}
if len(ret) == 2 {
heading = (uint16(ret[1]) + uint16(ret[0])*256) / 10
return
} else {
err = errors.New("Not enough bytes read")
}
return
2014-04-27 18:54:41 -07:00
}