Skip to content

Commit

Permalink
bmxx80: add support for filters without using SenseContinuous (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
BGodding authored Sep 4, 2024
1 parent 67ec1da commit 2cf8fa1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion bmxx80/bmx280.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const (
// measurements are done.
type standby uint8

// Possible standby values, these determines the refresh rate.
// Possible standby values, these determine the refresh rate.
const (
s500us standby = 0
s10msBME standby = 6
Expand Down
4 changes: 2 additions & 2 deletions bmxx80/bmx280_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package bmxx80
import (
"errors"
"flag"
"io/ioutil"
"io"
"log"
"os"
"testing"
Expand Down Expand Up @@ -1028,7 +1028,7 @@ func (s *spiFail) Connect(f physic.Frequency, mode spi.Mode, bits int) (spi.Conn
func TestMain(m *testing.M) {
flag.Parse()
if !testing.Verbose() {
log.SetOutput(ioutil.Discard)
log.SetOutput(io.Discard)
}
os.Exit(m.Run())
}
46 changes: 30 additions & 16 deletions bmxx80/bmxx80.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,12 @@ type Opts struct {
// Humidity sensing is only supported on BME280. The value is ignored on other
// devices.
Humidity Oversampling
// Filter is only used while using SenseContinuous() and is only supported on
// BMx280.
// Filter is only used while using SenseContinuous() or with a pre-set standby duration
// It is only supported on BMx280.
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 {
Expand Down Expand Up @@ -236,18 +239,21 @@ func (d *Dev) Sense(e *physic.Env) error {
}

if d.is280 {
err := d.writeCommands([]byte{
// ctrl_meas
0xF4, byte(d.opts.Temperature)<<5 | byte(d.opts.Pressure)<<2 | byte(forced),
})
if err != nil {
return d.wrap(err)
}
doSleep(d.measDelay)
for idle := false; !idle; {
if idle, err = d.isIdle280(); err != nil {
// Skip setting mode to forced if we are already in normal mode
if d.opts.Filter == NoFilter || d.opts.Standby == 0 {
err := d.writeCommands([]byte{
// ctrl_meas
0xF4, byte(d.opts.Temperature)<<5 | byte(d.opts.Pressure)<<2 | byte(forced),
})
if err != nil {
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)
}
Expand Down Expand Up @@ -388,6 +394,14 @@ func (d *Dev) makeDev(opts *Opts) error {
}
}
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
if d.isBME {
b = []byte{
Expand All @@ -398,9 +412,9 @@ func (d *Dev) makeDev(opts *Opts) error {
// ctrl_hum
0xF2, byte(d.opts.Humidity),
// 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.
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 {
// BMP280 doesn't have humidity to control.
Expand All @@ -410,9 +424,9 @@ func (d *Dev) makeDev(opts *Opts) error {
// into normal but was not Halt'ed.
0xF4, byte(d.opts.Temperature)<<5 | byte(d.opts.Pressure)<<2 | byte(sleep),
// 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.
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)
Expand Down
2 changes: 1 addition & 1 deletion bmxx80/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
//
// 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
// 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.
package bmxx80

0 comments on commit 2cf8fa1

Please sign in to comment.