i2c: ensure bmp180 returns Temperature() error, if any

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-03-28 15:03:08 +02:00
parent a772e835d9
commit 56a67a6626
2 changed files with 38 additions and 1 deletions

View File

@ -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.mb)
binary.Read(buf, binary.BigEndian, &d.calibrationCoefficients.mc) binary.Read(buf, binary.BigEndian, &d.calibrationCoefficients.mc)
binary.Read(buf, binary.BigEndian, &d.calibrationCoefficients.md) binary.Read(buf, binary.BigEndian, &d.calibrationCoefficients.md)
return nil return nil
} }
@ -139,7 +140,7 @@ func (d *BMP180Driver) Halt() (err error) {
func (d *BMP180Driver) Temperature() (temp float32, err error) { func (d *BMP180Driver) Temperature() (temp float32, err error) {
var rawTemp int16 var rawTemp int16
if rawTemp, err = d.rawTemp(); err != nil { if rawTemp, err = d.rawTemp(); err != nil {
return 0, nil return 0, err
} }
return d.calculateTemp(rawTemp), nil return d.calculateTemp(rawTemp), nil
} }

View File

@ -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()
}