From 058b4ac664e67663f98c7b48f4df9956c203d00a Mon Sep 17 00:00:00 2001 From: bezineb5 Date: Sun, 11 Nov 2018 21:49:32 -0500 Subject: [PATCH] ads1x15: Fixes #334 - Removed extra delay (#336) * [ADS1x15] Removed extra delay * Update ads1x15_test.go Do not panic in case of unread packet. --- experimental/devices/ads1x15/ads1x15.go | 5 ++-- experimental/devices/ads1x15/ads1x15_test.go | 27 +++++++++++++------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/experimental/devices/ads1x15/ads1x15.go b/experimental/devices/ads1x15/ads1x15.go index d3c2f76..def5d8f 100644 --- a/experimental/devices/ads1x15/ads1x15.go +++ b/experimental/devices/ads1x15/ads1x15.go @@ -226,9 +226,8 @@ func (d *Dev) PinForChannel(c Channel, maxVoltage physic.ElectricPotential, f ph configBytes := [2]byte{} binary.BigEndian.PutUint16(configBytes[:], config) - // The wait for the ADC sample to finish is based on the sample rate plus a - // small offset to be sure (0.1 millisecond). - waitTime := time.Second/time.Duration(dataRate) + 100*time.Microsecond + // The wait for the ADC sample to finish is based on the sample rate. + waitTime := time.Second / time.Duration(dataRate) return &analogPin{ adc: d, diff --git a/experimental/devices/ads1x15/ads1x15_test.go b/experimental/devices/ads1x15/ads1x15_test.go index 268fa21..8ab584a 100644 --- a/experimental/devices/ads1x15/ads1x15_test.go +++ b/experimental/devices/ads1x15/ads1x15_test.go @@ -76,7 +76,7 @@ func TestPinADC_Read(t *testing.T) { Ops: []i2ctest.IO{ { Addr: 0x48, - W: []byte{0x1, 0x91, 0xc3}, + W: []byte{0x1, 0x91, 0x3}, R: []byte{}, }, { @@ -92,7 +92,7 @@ func TestPinADC_Read(t *testing.T) { t.Fatal(err) } // Obtain an analog pin from the ADC - pin, err := d.PinForChannel(Channel0Minus3, 5*physic.Volt, 1*physic.Hertz, SaveEnergy) + pin, err := d.PinForChannel(Channel0Minus3, 5*physic.Volt, 1*physic.Hertz, BestQuality) if err != nil { t.Fatal(err) } @@ -120,13 +120,12 @@ func TestPinADC_Read(t *testing.T) { } } -/* https://github.com/google/periph/issues/334 func TestPinADC_ReadContinous(t *testing.T) { b := i2ctest.Playback{ Ops: []i2ctest.IO{ { Addr: 0x48, - W: []byte{0x1, 0x91, 0x3}, + W: []byte{0x1, 0x91, 0xc3}, R: []byte{}, }, { @@ -136,7 +135,7 @@ func TestPinADC_ReadContinous(t *testing.T) { }, { Addr: 0x48, - W: []byte{0x1, 0x91, 0x3}, + W: []byte{0x1, 0x91, 0xc3}, R: []byte{}, }, { @@ -144,10 +143,20 @@ func TestPinADC_ReadContinous(t *testing.T) { W: []byte{0x0}, R: []byte{0x52, 0xc0}, }, - // We add one extra exchange, as halting the polling is not instant + // We add 2 extra exchanges, as halting the polling is not instant { Addr: 0x48, - W: []byte{0x1, 0x91, 0x3}, + W: []byte{0x1, 0x91, 0xc3}, + R: []byte{}, + }, + { + Addr: 0x48, + W: []byte{0x0}, + R: []byte{0x52, 0xc0}, + }, + { + Addr: 0x48, + W: []byte{0x1, 0x91, 0xc3}, R: []byte{}, }, { @@ -156,6 +165,7 @@ func TestPinADC_ReadContinous(t *testing.T) { R: []byte{0x52, 0xc0}, }, }, + DontPanic: true, } rawValues := []int32{21200, 21184} @@ -166,7 +176,7 @@ func TestPinADC_ReadContinous(t *testing.T) { t.Fatal(err) } // Obtain an analog pin from the ADC - pin, err := d.PinForChannel(Channel0Minus3, 5*physic.Volt, 100*physic.Hertz, BestQuality) + pin, err := d.PinForChannel(Channel0Minus3, 5*physic.Volt, 100*physic.Hertz, SaveEnergy) if err != nil { t.Fatal(err) } @@ -198,4 +208,3 @@ func TestPinADC_ReadContinous(t *testing.T) { t.Fatal(err) } } -*/