Skip to content

Commit

Permalink
Add error to i2c interface
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Nov 20, 2014
1 parent 15852db commit 8250083
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 101 deletions.
71 changes: 42 additions & 29 deletions platforms/i2c/blinkm_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ func NewBlinkMDriver(a I2cInterface, name string) *BlinkMDriver {
red := byte(params["red"].(float64))
green := byte(params["green"].(float64))
blue := byte(params["blue"].(float64))
b.Rgb(red, green, blue)
return nil
return b.Rgb(red, green, blue)
})
b.AddCommand("Fade", func(params map[string]interface{}) interface{} {
red := byte(params["red"].(float64))
green := byte(params["green"].(float64))
blue := byte(params["blue"].(float64))
b.Fade(red, green, blue)
return nil
return b.Fade(red, green, blue)
})
b.AddCommand("FirmwareVersion", func(params map[string]interface{}) interface{} {
return b.FirmwareVersion()
version, err := b.FirmwareVersion()
return map[string]interface{}{"version": version, "err": err}
})
b.AddCommand("Color", func(params map[string]interface{}) interface{} {
return b.Color()
color, err := b.Color()
return map[string]interface{}{"color": color, "err": err}
})

return b
Expand All @@ -57,45 +57,58 @@ func (b *BlinkMDriver) adaptor() I2cInterface {
return b.Adaptor().(I2cInterface)
}

// Start writes start bytes and resets color
func (b *BlinkMDriver) Start() error {
b.adaptor().I2cStart(0x09)
b.adaptor().I2cWrite([]byte("o"))
b.Rgb(0, 0, 0)
// Start writes start bytes
func (b *BlinkMDriver) Start() (err error) {
if err = b.adaptor().I2cStart(0x09); err != nil {
return
}
if err = b.adaptor().I2cWrite([]byte("o")); err != nil {
return
}
return nil
}

// Halt returns true if device is halted successfully
func (b *BlinkMDriver) Halt() error { return nil }

// Rgb sets color using r,g,b params
func (b *BlinkMDriver) Rgb(red byte, green byte, blue byte) {
b.adaptor().I2cWrite([]byte("n"))
b.adaptor().I2cWrite([]byte{red, green, blue})
func (b *BlinkMDriver) Rgb(red byte, green byte, blue byte) (err error) {
if err = b.adaptor().I2cWrite([]byte("n")); err != nil {
return
}
err = b.adaptor().I2cWrite([]byte{red, green, blue})
return
}

// Fade removes color using r,g,b params
func (b *BlinkMDriver) Fade(red byte, green byte, blue byte) {
b.adaptor().I2cWrite([]byte("c"))
b.adaptor().I2cWrite([]byte{red, green, blue})
func (b *BlinkMDriver) Fade(red byte, green byte, blue byte) (err error) {
if err = b.adaptor().I2cWrite([]byte("c")); err != nil {
return
}
err = b.adaptor().I2cWrite([]byte{red, green, blue})
return
}

// FirmwareVersion returns version with MAYOR.minor format
func (b *BlinkMDriver) FirmwareVersion() string {
b.adaptor().I2cWrite([]byte("Z"))
data := b.adaptor().I2cRead(2)
if len(data) != 2 {
return ""
func (b *BlinkMDriver) FirmwareVersion() (version string, err error) {
if err = b.adaptor().I2cWrite([]byte("Z")); err != nil {
return
}
data, err := b.adaptor().I2cRead(2)
if len(data) != 2 || err != nil {
return
}
return fmt.Sprintf("%v.%v", data[0], data[1])
return fmt.Sprintf("%v.%v", data[0], data[1]), nil
}

// Color returns an array with current rgb color
func (b *BlinkMDriver) Color() []byte {
b.adaptor().I2cWrite([]byte("g"))
data := b.adaptor().I2cRead(3)
if len(data) != 3 {
return make([]byte, 0)
func (b *BlinkMDriver) Color() (color []byte, err error) {
if err = b.adaptor().I2cWrite([]byte("g")); err != nil {
return
}
data, err := b.adaptor().I2cRead(3)
if len(data) != 3 || err != nil {
return []byte{}, err
}
return []byte{data[0], data[1], data[2]}
return []byte{data[0], data[1], data[2]}, nil
}
21 changes: 14 additions & 7 deletions platforms/i2c/blinkm_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,17 @@ func TestNewBlinkMDriverCommands_FirmwareVersion(t *testing.T) {

result := blinkM.Driver.Command("FirmwareVersion")(param)

gobot.Assert(t, result, blinkM.FirmwareVersion())
version, _ := blinkM.FirmwareVersion()
gobot.Assert(t, result.(map[string]interface{})["version"].(string), version)

// When len(data) is not 2
adaptor.i2cReadImpl = func() []byte {
return []byte{99}
}
result = blinkM.Driver.Command("FirmwareVersion")(param)

gobot.Assert(t, result, blinkM.FirmwareVersion())
version, _ = blinkM.FirmwareVersion()
gobot.Assert(t, result.(map[string]interface{})["version"].(string), version)
}

func TestNewBlinkMDriverCommands_Color(t *testing.T) {
Expand All @@ -81,7 +83,8 @@ func TestNewBlinkMDriverCommands_Color(t *testing.T) {

result := blinkM.Driver.Command("Color")(param)

gobot.Assert(t, result, blinkM.Color())
color, _ := blinkM.Color()
gobot.Assert(t, result.(map[string]interface{})["color"].([]byte), color)
}

// Methods
Expand All @@ -104,14 +107,16 @@ func TestBlinkMDriverFirmwareVersion(t *testing.T) {
return []byte{99, 1}
}

gobot.Assert(t, blinkM.FirmwareVersion(), "99.1")
version, _ := blinkM.FirmwareVersion()
gobot.Assert(t, version, "99.1")

// when len(data) is not 2
adaptor.i2cReadImpl = func() []byte {
return []byte{99}
}

gobot.Assert(t, blinkM.FirmwareVersion(), "")
version, _ = blinkM.FirmwareVersion()
gobot.Assert(t, version, "")
}

func TestBlinkMDriverColor(t *testing.T) {
Expand All @@ -122,13 +127,15 @@ func TestBlinkMDriverColor(t *testing.T) {
return []byte{99, 1, 2}
}

gobot.Assert(t, blinkM.Color(), []byte{99, 1, 2})
color, _ := blinkM.Color()
gobot.Assert(t, color, []byte{99, 1, 2})

// when len(data) is not 3
adaptor.i2cReadImpl = func() []byte {
return []byte{99}
}

gobot.Assert(t, blinkM.Color(), []byte{})
color, _ = blinkM.Color()
gobot.Assert(t, color, []byte{})

}
38 changes: 25 additions & 13 deletions platforms/i2c/hmc6352_driver.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package i2c

import (
"errors"

"github.com/hybridgroup/gobot"
)

var _ gobot.DriverInterface = (*HMC6352Driver)(nil)

type HMC6352Driver struct {
gobot.Driver
Heading uint16
}

// NewHMC6352Driver creates a new driver with specified name and i2c interface
Expand All @@ -29,18 +30,29 @@ func (h *HMC6352Driver) adaptor() I2cInterface {

// Start writes initialization bytes and reads from adaptor
// using specified interval to update Heading
func (h *HMC6352Driver) Start() error {
h.adaptor().I2cStart(0x21)
h.adaptor().I2cWrite([]byte("A"))

gobot.Every(h.Interval(), func() {
h.adaptor().I2cWrite([]byte("A"))
ret := h.adaptor().I2cRead(2)
if len(ret) == 2 {
h.Heading = (uint16(ret[1]) + uint16(ret[0])*256) / 10
}
})
return nil
func (h *HMC6352Driver) Start() (err error) {
if err = h.adaptor().I2cStart(0x21); err != nil {
return
}
return h.adaptor().I2cWrite([]byte("A"))
}

// 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
}

// Halt returns true if devices is halted successfully
Expand Down
10 changes: 4 additions & 6 deletions platforms/i2c/hmc6352_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,13 @@ func TestHMC6352DriverStart(t *testing.T) {

numberOfCyclesForEvery := 3

// Make sure "Heading" is set to 0 so that we assert
// its new value after executing "Start()"
gobot.Assert(t, hmc.Heading, uint16(0))

hmc.SetInterval(1 * time.Millisecond)
gobot.Assert(t, hmc.Start(), nil)
go func() {
for {
<-time.After(time.Duration(numberOfCyclesForEvery) * time.Millisecond)
if hmc.Heading == uint16(2534) {
heading, _ := hmc.Heading()
if heading == uint16(2534) {
sem <- true
}
}
Expand All @@ -83,7 +80,8 @@ func TestHMC6352DriverStart(t *testing.T) {
go func() {
for {
<-time.After(time.Duration(numberOfCyclesForEvery) * time.Millisecond)
if hmc.Heading == uint16(0) {
heading, _ := hmc.Heading()
if heading == uint16(0) {
sem <- true
}
}
Expand Down
6 changes: 3 additions & 3 deletions platforms/i2c/i2c.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package i2c

type I2cInterface interface {
I2cStart(byte)
I2cRead(uint) []byte
I2cWrite([]byte)
I2cStart(address byte) (err error)
I2cRead(len uint) (data []byte, err error)
I2cWrite(buf []byte) (err error)
}
44 changes: 33 additions & 11 deletions platforms/i2c/mpl115a2_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ type MPL115A2Driver struct {

// NewMPL115A2Driver creates a new driver with specified name and i2c interface
func NewMPL115A2Driver(a I2cInterface, name string) *MPL115A2Driver {
return &MPL115A2Driver{
m := &MPL115A2Driver{
Driver: *gobot.NewDriver(
name,
"MPL115A2Driver",
a.(gobot.AdaptorInterface),
),
}
m.AddEvent("error")
return m
}

// adaptor returns MPL115A2 adaptor
Expand All @@ -52,19 +54,32 @@ func (h *MPL115A2Driver) adaptor() I2cInterface {

// Start writes initialization bytes and reads from adaptor
// using specified interval to accelerometer andtemperature data
func (h *MPL115A2Driver) Start() error {
func (h *MPL115A2Driver) Start() (err error) {
var temperature uint16
var pressure uint16
var pressureComp float32

h.initialization()
if err = h.initialization(); err != nil {
return
}

gobot.Every(h.Interval(), func() {
h.adaptor().I2cWrite([]byte{MPL115A2_REGISTER_STARTCONVERSION, 0})
if err := h.adaptor().I2cWrite([]byte{MPL115A2_REGISTER_STARTCONVERSION, 0}); err != nil {
gobot.Publish(h.Event("error"), err)
return
}
<-time.After(5 * time.Millisecond)

h.adaptor().I2cWrite([]byte{MPL115A2_REGISTER_PRESSURE_MSB})
ret := h.adaptor().I2cRead(4)
if err = h.adaptor().I2cWrite([]byte{MPL115A2_REGISTER_PRESSURE_MSB}); err != nil {
gobot.Publish(h.Event("error"), err)
return
}

ret, err := h.adaptor().I2cRead(4)
if err != nil {
gobot.Publish(h.Event("error"), err)
return
}
if len(ret) == 4 {
buf := bytes.NewBuffer(ret)
binary.Read(buf, binary.BigEndian, &pressure)
Expand All @@ -84,15 +99,22 @@ func (h *MPL115A2Driver) Start() error {
// Halt returns true if devices is halted successfully
func (h *MPL115A2Driver) Halt() error { return nil }

func (h *MPL115A2Driver) initialization() bool {
func (h *MPL115A2Driver) initialization() (err error) {
var coA0 int16
var coB1 int16
var coB2 int16
var coC12 int16

h.adaptor().I2cStart(0x60)
h.adaptor().I2cWrite([]byte{MPL115A2_REGISTER_A0_COEFF_MSB})
ret := h.adaptor().I2cRead(8)
if err = h.adaptor().I2cStart(0x60); err != nil {
return
}
if err = h.adaptor().I2cWrite([]byte{MPL115A2_REGISTER_A0_COEFF_MSB}); err != nil {
return
}
ret, err := h.adaptor().I2cRead(8)
if err != nil {
return
}
buf := bytes.NewBuffer(ret)

binary.Read(buf, binary.BigEndian, &coA0)
Expand All @@ -107,5 +129,5 @@ func (h *MPL115A2Driver) initialization() bool {
h.B2 = float32(coB2) / 16384.0
h.C12 = float32(coC12) / 4194304.0

return true
return
}
Loading

0 comments on commit 8250083

Please sign in to comment.