bmxx80: add support for filters without using SenseContinuous (#74)

pull/77/head
Benjamin Godding 2 years ago committed by GitHub
parent 67ec1da964
commit 2cf8fa10c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -75,7 +75,7 @@ const (
// measurements are done. // measurements are done.
type standby uint8 type standby uint8
// Possible standby values, these determines the refresh rate. // Possible standby values, these determine the refresh rate.
const ( const (
s500us standby = 0 s500us standby = 0
s10msBME standby = 6 s10msBME standby = 6

@ -7,7 +7,7 @@ package bmxx80
import ( import (
"errors" "errors"
"flag" "flag"
"io/ioutil" "io"
"log" "log"
"os" "os"
"testing" "testing"
@ -1028,7 +1028,7 @@ func (s *spiFail) Connect(f physic.Frequency, mode spi.Mode, bits int) (spi.Conn
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
flag.Parse() flag.Parse()
if !testing.Verbose() { if !testing.Verbose() {
log.SetOutput(ioutil.Discard) log.SetOutput(io.Discard)
} }
os.Exit(m.Run()) os.Exit(m.Run())
} }

@ -138,9 +138,12 @@ type Opts struct {
// Humidity sensing is only supported on BME280. The value is ignored on other // Humidity sensing is only supported on BME280. The value is ignored on other
// devices. // devices.
Humidity Oversampling Humidity Oversampling
// Filter is only used while using SenseContinuous() and is only supported on // Filter is only used while using SenseContinuous() or with a pre-set standby duration
// BMx280. // It is only supported on BMx280.
Filter Filter Filter Filter
// Standby Used with Filter to control the time between samples.
// If this is set we enable the filter on device creation and set the device mode to normal instead of sleep.
Standby time.Duration
} }
func (o *Opts) delayTypical280() time.Duration { func (o *Opts) delayTypical280() time.Duration {
@ -236,18 +239,21 @@ func (d *Dev) Sense(e *physic.Env) error {
} }
if d.is280 { if d.is280 {
err := d.writeCommands([]byte{ // Skip setting mode to forced if we are already in normal mode
// ctrl_meas if d.opts.Filter == NoFilter || d.opts.Standby == 0 {
0xF4, byte(d.opts.Temperature)<<5 | byte(d.opts.Pressure)<<2 | byte(forced), err := d.writeCommands([]byte{
}) // ctrl_meas
if err != nil { 0xF4, byte(d.opts.Temperature)<<5 | byte(d.opts.Pressure)<<2 | byte(forced),
return d.wrap(err) })
} if err != nil {
doSleep(d.measDelay)
for idle := false; !idle; {
if idle, err = d.isIdle280(); err != nil {
return d.wrap(err) return d.wrap(err)
} }
doSleep(d.measDelay)
for idle := false; !idle; {
if idle, err = d.isIdle280(); err != nil {
return d.wrap(err)
}
}
} }
return d.sense280(e) return d.sense280(e)
} }
@ -388,6 +394,14 @@ func (d *Dev) makeDev(opts *Opts) error {
} }
} }
d.cal280 = newCalibration(tph[:], h[:]) d.cal280 = newCalibration(tph[:], h[:])
standbyDuration := s1s
filter := NoFilter
startingMode := sleep
if d.opts.Filter != NoFilter && d.opts.Standby != 0 {
standbyDuration = chooseStandby(d.isBME, d.opts.Standby)
filter = d.opts.Filter
startingMode = normal
}
var b []byte var b []byte
if d.isBME { if d.isBME {
b = []byte{ b = []byte{
@ -398,9 +412,9 @@ func (d *Dev) makeDev(opts *Opts) error {
// ctrl_hum // ctrl_hum
0xF2, byte(d.opts.Humidity), 0xF2, byte(d.opts.Humidity),
// config // config
0xF5, byte(s1s)<<5 | byte(NoFilter)<<2, 0xF5, byte(standbyDuration)<<5 | byte(filter)<<2,
// As per page 25, ctrl_meas must be re-written last. // As per page 25, ctrl_meas must be re-written last.
0xF4, byte(d.opts.Temperature)<<5 | byte(d.opts.Pressure)<<2 | byte(sleep), 0xF4, byte(d.opts.Temperature)<<5 | byte(d.opts.Pressure)<<2 | byte(startingMode),
} }
} else { } else {
// BMP280 doesn't have humidity to control. // BMP280 doesn't have humidity to control.
@ -410,9 +424,9 @@ func (d *Dev) makeDev(opts *Opts) error {
// into normal but was not Halt'ed. // into normal but was not Halt'ed.
0xF4, byte(d.opts.Temperature)<<5 | byte(d.opts.Pressure)<<2 | byte(sleep), 0xF4, byte(d.opts.Temperature)<<5 | byte(d.opts.Pressure)<<2 | byte(sleep),
// config // config
0xF5, byte(s1s)<<5 | byte(NoFilter)<<2, 0xF5, byte(standbyDuration)<<5 | byte(filter)<<2,
// As per page 25, ctrl_meas must be re-written last. // As per page 25, ctrl_meas must be re-written last.
0xF4, byte(d.opts.Temperature)<<5 | byte(d.opts.Pressure)<<2 | byte(sleep), 0xF4, byte(d.opts.Temperature)<<5 | byte(d.opts.Pressure)<<2 | byte(startingMode),
} }
} }
return d.writeCommands(b) return d.writeCommands(b)

@ -32,6 +32,6 @@
// //
// The results of the calculations in the algorithm on page 15 are partly // The results of the calculations in the algorithm on page 15 are partly
// wrong. It looks like the original authors used non-integer calculations and // wrong. It looks like the original authors used non-integer calculations and
// some nubers were rounded. Take the results of the calculations with a grain // some numbers were rounded. Take the results of the calculations with a grain
// of salt. // of salt.
package bmxx80 package bmxx80

Loading…
Cancel
Save