Skip to content

Commit

Permalink
i2c: correct reads to use simple file reads for ads1015 adc, and incr…
Browse files Browse the repository at this point in the history
…ease test coverage

Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Apr 24, 2017
1 parent 2942098 commit 823d478
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions drivers/i2c/ads1015_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
36 changes: 36 additions & 0 deletions drivers/i2c/ads1015_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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"))
}

0 comments on commit 823d478

Please sign in to comment.