From 47145686288fbb1ba15e17a5196094ed2ea9ec3e Mon Sep 17 00:00:00 2001 From: deadprogram Date: Wed, 12 Apr 2017 16:44:15 +0200 Subject: [PATCH] i2c: small refactor and increase test coverage for tsl2561 Signed-off-by: deadprogram --- drivers/i2c/tsl2561_driver.go | 22 +++++++++++++--------- drivers/i2c/tsl2561_driver_test.go | 3 +++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/drivers/i2c/tsl2561_driver.go b/drivers/i2c/tsl2561_driver.go index 538d2f07..591cd7e7 100644 --- a/drivers/i2c/tsl2561_driver.go +++ b/drivers/i2c/tsl2561_driver.go @@ -418,15 +418,7 @@ func (d *TSL2561Driver) getData() (broadband uint16, ir uint16, err error) { return } - // Wait x ms for ADC to complete - switch d.integrationTime { - case TSL2561IntegrationTime13MS: - time.Sleep(15 * time.Millisecond) - case TSL2561IntegrationTime101MS: - time.Sleep(120 * time.Millisecond) - case TSL2561IntegrationTime402MS: - time.Sleep(450 * time.Millisecond) - } + d.waitForADC() // Reads a two byte value from channel 0 (visible + infrared) broadband, err = d.connection.ReadWordData(tsl2561CommandBit | tsl2561WordBit | tsl2561RegisterChan0Low) @@ -504,3 +496,15 @@ func (d *TSL2561Driver) getBM(ratio uint32) (b uint32, m uint32) { } return } + +func (d *TSL2561Driver) waitForADC() { + switch d.integrationTime { + case TSL2561IntegrationTime13MS: + time.Sleep(15 * time.Millisecond) + case TSL2561IntegrationTime101MS: + time.Sleep(120 * time.Millisecond) + case TSL2561IntegrationTime402MS: + time.Sleep(450 * time.Millisecond) + } + return +} diff --git a/drivers/i2c/tsl2561_driver_test.go b/drivers/i2c/tsl2561_driver_test.go index e5e01f64..8a9fd1ec 100644 --- a/drivers/i2c/tsl2561_driver_test.go +++ b/drivers/i2c/tsl2561_driver_test.go @@ -242,6 +242,7 @@ func TestTSL2561getClipScaling13MS(t *testing.T) { WithTSL2561AutoGain) c, s := d.getClipScaling() + d.waitForADC() gobottest.Assert(t, c, uint16(tsl2561Clipping13MS)) gobottest.Assert(t, s, uint32(tsl2561LuxCHScaleTInt0)) } @@ -253,6 +254,7 @@ func TestTSL2561getClipScaling101MS(t *testing.T) { WithTSL2561AutoGain) c, s := d.getClipScaling() + d.waitForADC() gobottest.Assert(t, c, uint16(tsl2561Clipping101MS)) gobottest.Assert(t, s, uint32(tsl2561LuxChScaleTInt1)) } @@ -264,6 +266,7 @@ func TestTSL2561getClipScaling402MS(t *testing.T) { WithTSL2561AutoGain) c, s := d.getClipScaling() + d.waitForADC() gobottest.Assert(t, c, uint16(tsl2561Clipping402MS)) gobottest.Assert(t, s, uint32(1<