Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
bmxx80: fix humidity sensing
Browse files Browse the repository at this point in the history
It regressed in 6423b54. :( This is due to
integer overflow. Tested against the executable compiled from periph v2.

Refactor the tests to make the error messages clearer.

Fixes #256
  • Loading branch information
maruel committed Jul 30, 2018
1 parent a84f1d7 commit f34a1b4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
2 changes: 1 addition & 1 deletion devices/bmxx80/bmx280.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (d *Dev) sense280(e *physic.Env) error {
hRaw := int32(buf[6])<<8 | int32(buf[7])
h := physic.RelativeHumidity(d.cal280.compensateHumidityInt(hRaw, tFine))
// Convert base 1024 to base 1000.
e.Humidity = h * physic.MicroRH * 1000 / 1024
e.Humidity = h * 10000 / 1024 * physic.MicroRH
}
return nil
}
Expand Down
62 changes: 31 additions & 31 deletions devices/bmxx80/bmx280_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ func TestSPISenseBME280_success(t *testing.T) {
// TODO(maruel): The values do not make sense but I think I burned my SPI
// BME280 by misconnecting it in reverse for a few minutes. It still "work"
// but fail to read data. It could also be a bug in the driver. :(
if e.Temperature != 62680*physic.MilliCelsius+physic.ZeroCelsius {
t.Fatalf("temp %d", e.Temperature)
if expected := 62680*physic.MilliCelsius + physic.ZeroCelsius; e.Temperature != expected {
t.Fatalf("temperature %s(%d) != %s(%d)", expected, expected, e.Temperature, e.Temperature)
}
if e.Pressure != 99575933593750*physic.NanoPascal {
t.Fatalf("pressure %d", e.Pressure)
if expected := 99575933593750 * physic.NanoPascal; e.Pressure != expected {
t.Fatalf("pressure %s(%d) != %s(%d)", expected, expected, e.Pressure, e.Pressure)
}
if e.Humidity != 99501*physic.TenthMicroRH {
t.Fatalf("humidity %d", e.Humidity)
if expected := 995010 * physic.TenthMicroRH; e.Humidity != expected {
t.Fatalf("humidity %s(%d) != %s(%d)", expected, expected, e.Humidity, e.Humidity)
}
if err := s.Close(); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -331,14 +331,14 @@ func TestI2CSenseBMP280_success(t *testing.T) {
if err := dev.Sense(&e); err != nil {
t.Fatal(err)
}
if e.Temperature != 23720*physic.MilliCelsius+physic.ZeroCelsius {
t.Fatalf("temp %d", e.Temperature)
if expected := 23720*physic.MilliCelsius + physic.ZeroCelsius; e.Temperature != expected {
t.Fatalf("temperature %s(%d) != %s(%d)", expected, expected, e.Temperature, e.Temperature)
}
if e.Pressure != 100942695312500*physic.NanoPascal {
t.Fatalf("pressure %d", e.Pressure)
if expected := 100942695312500 * physic.NanoPascal; e.Pressure != expected {
t.Fatalf("pressure %s(%d) != %s(%d)", expected, expected, e.Pressure, e.Pressure)
}
if e.Humidity != 0 {
t.Fatalf("humidity %d", e.Humidity)
if expected := 0 * physic.MilliRH; e.Humidity != expected {
t.Fatalf("humidity %s(%d) != %s(%d)", expected, expected, e.Humidity, e.Humidity)
}
if err := dev.Halt(); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -384,14 +384,14 @@ func TestI2CSenseBME280_success(t *testing.T) {
if err := dev.Sense(&e); err != nil {
t.Fatal(err)
}
if e.Temperature != 23720*physic.MilliCelsius+physic.ZeroCelsius {
t.Fatalf("temp %d", e.Temperature)
if expected := 23720*physic.MilliCelsius + physic.ZeroCelsius; e.Temperature != expected {
t.Fatalf("temperature %s(%d) != %s(%d)", expected, expected, e.Temperature, e.Temperature)
}
if e.Pressure != 100942695312500*physic.NanoPascal {
t.Fatalf("pressure %d", e.Pressure)
if expected := 100942695312500 * physic.NanoPascal; e.Pressure != expected {
t.Fatalf("pressure %s(%d) != %s(%d)", expected, expected, e.Pressure, e.Pressure)
}
if e.Humidity != 653056*physic.TenthMicroRH {
t.Fatalf("humidity %d", e.Humidity)
if expected := 6530560 * physic.TenthMicroRH; e.Humidity != expected {
t.Fatalf("humidity %s(%d) != %s(%d)", expected, expected, e.Humidity, e.Humidity)
}
if err := dev.Halt(); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -516,14 +516,14 @@ func TestI2CSenseContinuous280_success(t *testing.T) {
case <-time.After(2 * time.Second):
t.Fatal("failed")
}
if e.Temperature != 23720*physic.MilliCelsius+physic.ZeroCelsius {
t.Fatalf("temp %d", e.Temperature)
if expected := 23720*physic.MilliCelsius + physic.ZeroCelsius; e.Temperature != expected {
t.Fatalf("temperature %s(%d) != %s(%d)", expected, expected, e.Temperature, e.Temperature)
}
if e.Pressure != 100942695312500*physic.NanoPascal {
t.Fatalf("pressure %d", e.Pressure)
if expected := 100942695312500 * physic.NanoPascal; e.Pressure != expected {
t.Fatalf("pressure %s(%d) != %s(%d)", expected, expected, e.Pressure, e.Pressure)
}
if e.Humidity != 653056*physic.TenthMicroRH {
t.Fatalf("humidity %d", e.Humidity)
if expected := 6530560 * physic.TenthMicroRH; e.Humidity != expected {
t.Fatalf("humidity %s(%d) != %s(%d)", expected, expected, e.Humidity, e.Humidity)
}

// This cancels the previous channel and resets the interval.
Expand All @@ -545,14 +545,14 @@ func TestI2CSenseContinuous280_success(t *testing.T) {
case <-time.After(2 * time.Second):
t.Fatal("failed")
}
if e.Temperature != 23720*physic.MilliCelsius+physic.ZeroCelsius {
t.Fatalf("temp %d", e.Temperature)
if expected := 23720*physic.MilliCelsius + physic.ZeroCelsius; e.Temperature != expected {
t.Fatalf("temperature %s(%d) != %s(%d)", expected, expected, e.Temperature, e.Temperature)
}
if e.Pressure != 100942695312500*physic.NanoPascal {
t.Fatalf("pressure %d", e.Pressure)
if expected := 100942695312500 * physic.NanoPascal; e.Pressure != expected {
t.Fatalf("pressure %s(%d) != %s(%d)", expected, expected, e.Pressure, e.Pressure)
}
if e.Humidity != 653056*physic.TenthMicroRH {
t.Fatalf("humidity %d", e.Humidity)
if expected := 6530560 * physic.TenthMicroRH; e.Humidity != expected {
t.Fatalf("humidity %s(%d) != %s(%d)", expected, expected, e.Humidity, e.Humidity)
}

if dev.Sense(&e) == nil {
Expand Down Expand Up @@ -763,7 +763,7 @@ func TestBme280Precision(t *testing.T) {
if e.Pressure != 15625*physic.MicroPascal/4 {
t.Fatal(e.Pressure)
}
if e.Humidity != physic.MicroRH*1000/1024 {
if e.Humidity != 90*physic.TenthMicroRH {
t.Fatal(int(e.Humidity))
}
if err := bus.Close(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion devices/bmxx80/bmxx80.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func (d *Dev) Precision(e *physic.Env) {
}

if d.isBME {
e.Humidity = physic.MicroRH * 1000 / 1024
e.Humidity = 10000 / 1024 * physic.MicroRH
}
}

Expand Down

0 comments on commit f34a1b4

Please sign in to comment.