From 42624e257c474339c6e74a0b470761885a6555fd Mon Sep 17 00:00:00 2001 From: quinte17 Date: Sun, 16 Oct 2016 21:30:29 +0200 Subject: [PATCH] make the i2c address of bme280 configurable (#5) make the i2c address of bme280 configurable The address can either be 0x76 or 0x77. It depends on hardware configuration. If you connect SDO to GND its 0x76 If you connect SDO to V(DDIO) its 0x77. * Add me to the authors. * the bme280 cmd uses the new address option * initialize default addr on definition --- AUTHORS | 1 + CONTRIBUTORS | 1 + devices/bme280/bme280.go | 18 +++++++++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 420dc67..4ce072d 100644 --- a/AUTHORS +++ b/AUTHORS @@ -4,4 +4,5 @@ # some cases, their employer may be the copyright holder. To see the full list # of contributors, see the revision history in source control. Google Inc. +Stephan Sperber Thorsten von Eicken diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 405ee39..5611806 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -27,4 +27,5 @@ # Please keep the list sorted. Marc-Antoine Ruel +Stephan Sperber Thorsten von Eicken diff --git a/devices/bme280/bme280.go b/devices/bme280/bme280.go index 3d17de3..1f88c43 100644 --- a/devices/bme280/bme280.go +++ b/devices/bme280/bme280.go @@ -117,6 +117,10 @@ func (d *Dev) Stop() error { // and FOff for filter if planing to call frequently, else use S500ms to get a // bit more than one reading per second. // +// Address is only used on creation of an I²C-device. Its default value is 0x76. +// It can be set to 0x77. Both values depend on HW configuration of the sensor's +// SDO pin. This has no effect with NewSPI() +// // BUG(maruel): Remove the Standby flag and replace with a // WaitForNextSample(time.Duration). Then use the closest value automatically. type Opts struct { @@ -125,6 +129,7 @@ type Opts struct { Humidity Oversampling Standby Standby Filter Filter + Address uint16 } // NewI2C returns an object that communicates over I²C to BME280 environmental @@ -133,7 +138,18 @@ type Opts struct { // It is recommended to call Stop() when done with the device so it stops // sampling. func NewI2C(i i2c.Conn, opts *Opts) (*Dev, error) { - d := &Dev{d: &i2c.Dev{Conn: i, Addr: 0x76}, isSPI: false} + addr := uint16(0x76) + if opts != nil { + switch opts.Address { + case 0x76, 0x77: + addr = opts.Address + case 0x00: + // do not do anything + default: + return nil, errors.New("given address not supported by device") + } + } + d := &Dev{d: &i2c.Dev{Conn: i, Addr: addr}, isSPI: false} if err := d.makeDev(opts); err != nil { return nil, err }