From 56a67a66263c393d941fc4d642e79fc6509640db Mon Sep 17 00:00:00 2001 From: deadprogram Date: Tue, 28 Mar 2017 15:03:08 +0200 Subject: [PATCH] i2c: ensure bmp180 returns Temperature() error, if any Signed-off-by: deadprogram --- drivers/i2c/bmp180_driver.go | 3 ++- examples/firmata_bmp180.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 examples/firmata_bmp180.go diff --git a/drivers/i2c/bmp180_driver.go b/drivers/i2c/bmp180_driver.go index 03b90fbd..78e579e7 100644 --- a/drivers/i2c/bmp180_driver.go +++ b/drivers/i2c/bmp180_driver.go @@ -127,6 +127,7 @@ func (d *BMP180Driver) initialization() (err error) { binary.Read(buf, binary.BigEndian, &d.calibrationCoefficients.mb) binary.Read(buf, binary.BigEndian, &d.calibrationCoefficients.mc) binary.Read(buf, binary.BigEndian, &d.calibrationCoefficients.md) + return nil } @@ -139,7 +140,7 @@ func (d *BMP180Driver) Halt() (err error) { func (d *BMP180Driver) Temperature() (temp float32, err error) { var rawTemp int16 if rawTemp, err = d.rawTemp(); err != nil { - return 0, nil + return 0, err } return d.calculateTemp(rawTemp), nil } diff --git a/examples/firmata_bmp180.go b/examples/firmata_bmp180.go new file mode 100644 index 00000000..f0118112 --- /dev/null +++ b/examples/firmata_bmp180.go @@ -0,0 +1,36 @@ +// +build example +// +// Do not build by default. + +package main + +import ( + "fmt" + "os" + "time" + + "gobot.io/x/gobot" + "gobot.io/x/gobot/drivers/i2c" + "gobot.io/x/gobot/platforms/firmata" +) + +func main() { + firmataAdaptor := firmata.NewAdaptor(os.Args[1]) + bmp180 := i2c.NewBMP180Driver(firmataAdaptor) + + work := func() { + gobot.Every(1*time.Second, func() { + //fmt.Println("Pressure", mpl115a2.Pressure()) + t, _ := bmp180.Temperature() + fmt.Println("Temperature", t) + }) + } + + robot := gobot.NewRobot("bmp180bot", + []gobot.Connection{firmataAdaptor}, + []gobot.Device{bmp180}, + work, + ) + + robot.Start() +}