From f34a1b4e75c5935f6269723ae45eacd0aa1d1a2a Mon Sep 17 00:00:00 2001 From: Marc-Antoine Ruel Date: Mon, 30 Jul 2018 14:50:07 -0400 Subject: [PATCH] bmxx80: fix humidity sensing It regressed in 6423b54aadda465317e7dcf236fac6fb6f0e8202. :( 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 --- devices/bmxx80/bmx280.go | 2 +- devices/bmxx80/bmx280_test.go | 62 +++++++++++++++++------------------ devices/bmxx80/bmxx80.go | 2 +- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/devices/bmxx80/bmx280.go b/devices/bmxx80/bmx280.go index 133e84a3b..8b84cfe21 100644 --- a/devices/bmxx80/bmx280.go +++ b/devices/bmxx80/bmx280.go @@ -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 } diff --git a/devices/bmxx80/bmx280_test.go b/devices/bmxx80/bmx280_test.go index bc1738216..4007bb16b 100644 --- a/devices/bmxx80/bmx280_test.go +++ b/devices/bmxx80/bmx280_test.go @@ -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) @@ -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) @@ -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) @@ -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. @@ -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 { @@ -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 { diff --git a/devices/bmxx80/bmxx80.go b/devices/bmxx80/bmxx80.go index 552156358..2fe873c58 100644 --- a/devices/bmxx80/bmxx80.go +++ b/devices/bmxx80/bmxx80.go @@ -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 } }