From 823d478a51f9aa6fbe2d0dbf21e5107993e18591 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Mon, 24 Apr 2017 18:15:49 +0200 Subject: [PATCH] i2c: correct reads to use simple file reads for ads1015 adc, and increase test coverage Signed-off-by: deadprogram --- drivers/i2c/ads1015_driver.go | 4 ++-- drivers/i2c/ads1015_driver_test.go | 36 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/drivers/i2c/ads1015_driver.go b/drivers/i2c/ads1015_driver.go index beb40c476..481004ce1 100644 --- a/drivers/i2c/ads1015_driver.go +++ b/drivers/i2c/ads1015_driver.go @@ -141,7 +141,7 @@ func (d *ADS1015Driver) ReadADCDifference01() (val int16, err error) { cfg |= ADS1015RegConfigMuxDiff01 - if err = d.connection.WriteWordData(ADS1015RegPointerConfig, cfg); err != nil { + if _, err = d.connection.Write([]byte{ADS1015RegPointerConfig, byte(cfg >> 8), byte(cfg & 0xff)}); err != nil { return } @@ -159,7 +159,7 @@ func (d *ADS1015Driver) ReadADCDifference23() (val int16, err error) { cfg |= ADS1015RegConfigMuxDiff23 - if err = d.connection.WriteWordData(ADS1015RegPointerConfig, cfg); err != nil { + if _, err = d.connection.Write([]byte{ADS1015RegPointerConfig, byte(cfg >> 8), byte(cfg & 0xff)}); err != nil { return } diff --git a/drivers/i2c/ads1015_driver_test.go b/drivers/i2c/ads1015_driver_test.go index b5cc78f29..bff12178e 100644 --- a/drivers/i2c/ads1015_driver_test.go +++ b/drivers/i2c/ads1015_driver_test.go @@ -48,6 +48,12 @@ func TestADS1015DriverOptions(t *testing.T) { gobottest.Assert(t, d.gain, uint16(ADS1015RegConfigPga2048V)) } +func TestADS1015StartAndHalt(t *testing.T) { + d, _ := initTestADS1015DriverWithStubbedAdaptor() + gobottest.Assert(t, d.Start(), nil) + gobottest.Assert(t, d.Halt(), nil) +} + func TestADS1015StartConnectError(t *testing.T) { d, adaptor := initTestADS1015DriverWithStubbedAdaptor() adaptor.Testi2cConnectErr(true) @@ -69,6 +75,18 @@ func TestADS1015DriverAnalogRead(t *testing.T) { gobottest.Assert(t, val, 1584) gobottest.Assert(t, err, nil) + val, err = d.AnalogRead("1") + gobottest.Assert(t, val, 1584) + gobottest.Assert(t, err, nil) + + val, err = d.AnalogRead("2") + gobottest.Assert(t, val, 1584) + gobottest.Assert(t, err, nil) + + val, err = d.AnalogRead("3") + gobottest.Assert(t, val, 1584) + gobottest.Assert(t, err, nil) + val, err = d.AnalogRead("0-1") gobottest.Assert(t, val, 1584) gobottest.Assert(t, err, nil) @@ -96,3 +114,21 @@ func TestADS1015DriverAnalogReadInvalidPin(t *testing.T) { _, err := d.AnalogRead("99") gobottest.Assert(t, err, errors.New("Invalid channel.")) } + +func TestADS1015DriverAnalogReadWriteError(t *testing.T) { + d, a := initTestADS1015DriverWithStubbedAdaptor() + d.Start() + + a.i2cWriteImpl = func([]byte) (int, error) { + return 0, errors.New("write error") + } + + _, err := d.AnalogRead("0") + gobottest.Assert(t, err, errors.New("write error")) + + _, err = d.AnalogRead("0-1") + gobottest.Assert(t, err, errors.New("write error")) + + _, err = d.AnalogRead("2-3") + gobottest.Assert(t, err, errors.New("write error")) +}