From 7c15fb5d1a71a026c37e848138a769aa674c5cd1 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Ruel Date: Mon, 5 Aug 2019 08:46:42 -0400 Subject: [PATCH] ads1x15: implement pin.PinFunc The interface pin.PinFunc will be merged into pin.Pin in v4. Increase coverage to 85%. --- experimental/devices/ads1x15/ads1x15.go | 25 ++++++++- experimental/devices/ads1x15/ads1x15_test.go | 56 +++++++++++++++++--- 2 files changed, 74 insertions(+), 7 deletions(-) diff --git a/experimental/devices/ads1x15/ads1x15.go b/experimental/devices/ads1x15/ads1x15.go index 7efc98a..64c4ad9 100644 --- a/experimental/devices/ads1x15/ads1x15.go +++ b/experimental/devices/ads1x15/ads1x15.go @@ -14,6 +14,7 @@ import ( "periph.io/x/periph/conn/i2c" "periph.io/x/periph/conn/physic" + "periph.io/x/periph/conn/pin" "periph.io/x/periph/experimental/conn/analog" ) @@ -457,7 +458,25 @@ func (p *analogPin) Number() int { } func (p *analogPin) Function() string { - return "ADC" + return string(p.Func()) +} + +// Func implements pin.PinFunc. +func (p *analogPin) Func() pin.Func { + return analog.ADC +} + +// SupportedFuncs implements pin.PinFunc. +func (p *analogPin) SupportedFuncs() []pin.Func { + return []pin.Func{analog.ADC} +} + +// SetFunc implements pin.PinFunc. +func (p *analogPin) SetFunc(f pin.Func) error { + if f == analog.ADC { + return nil + } + return errors.New("pin function cannot be changed") } func (p *analogPin) Halt() error { @@ -476,3 +495,7 @@ func (p *analogPin) Halt() error { func (p *analogPin) String() string { return p.Name() } + +var _ analog.PinADC = &analogPin{} +var _ pin.Pin = &analogPin{} +var _ pin.PinFunc = &analogPin{} diff --git a/experimental/devices/ads1x15/ads1x15_test.go b/experimental/devices/ads1x15/ads1x15_test.go index 8ab584a..d989b74 100644 --- a/experimental/devices/ads1x15/ads1x15_test.go +++ b/experimental/devices/ads1x15/ads1x15_test.go @@ -5,10 +5,13 @@ package ads1x15 import ( + "reflect" "testing" "periph.io/x/periph/conn/i2c/i2ctest" "periph.io/x/periph/conn/physic" + "periph.io/x/periph/conn/pin" + "periph.io/x/periph/experimental/conn/analog" ) func TestChannel_String(t *testing.T) { @@ -59,6 +62,7 @@ func TestChannel_number(t *testing.T) { func TestDev_String(t *testing.T) { b := i2ctest.Playback{} + defer b.Close() d, err := NewADS1115(&b, &DefaultOpts) if err != nil { t.Fatal(err) @@ -71,6 +75,44 @@ func TestDev_String(t *testing.T) { } } +func TestPinADC(t *testing.T) { + b := i2ctest.Playback{} + defer b.Close() + d, err := NewADS1015(&b, &DefaultOpts) + if err != nil { + t.Fatal(err) + } + // Obtain an analog pin from the ADC + p, err := d.PinForChannel(Channel0Minus3, 5*physic.Volt, 1*physic.Hertz, BestQuality) + if err != nil { + t.Fatal(err) + } + if v := p.String(); v != "ADS1015(0-3)" { + t.Fatal(v) + } + if v := p.Name(); v != "ADS1015(0-3)" { + t.Fatal(v) + } + if v := p.Number(); v != 5 { + t.Fatal(v) + } + if v := p.Function(); v != "ADC" { + t.Fatal(v) + } + if v := p.(pin.PinFunc).Func(); v != analog.ADC { + t.Fatal(v) + } + if v := p.(pin.PinFunc).SupportedFuncs(); !reflect.DeepEqual(v, []pin.Func{analog.ADC}) { + t.Fatal(v) + } + if err := p.(pin.PinFunc).SetFunc(analog.ADC); err != nil { + t.Fatal(err) + } + if err := p.(pin.PinFunc).SetFunc(pin.FuncNone); err == nil { + t.Fatal("expected failure") + } +} + func TestPinADC_Read(t *testing.T) { b := i2ctest.Playback{ Ops: []i2ctest.IO{ @@ -86,19 +128,20 @@ func TestPinADC_Read(t *testing.T) { }, }, } + defer b.Close() d, err := NewADS1015(&b, &DefaultOpts) if err != nil { t.Fatal(err) } // Obtain an analog pin from the ADC - pin, err := d.PinForChannel(Channel0Minus3, 5*physic.Volt, 1*physic.Hertz, BestQuality) + p, err := d.PinForChannel(Channel0Minus3, 5*physic.Volt, 1*physic.Hertz, BestQuality) if err != nil { t.Fatal(err) } // Read values from ADC. - reading, err := pin.Read() + reading, err := p.Read() if err != nil { t.Fatal(err) } @@ -111,7 +154,7 @@ func TestPinADC_Read(t *testing.T) { t.Fatalf("Found %s, expected %s", reading.V, -33*physic.MilliVolt) } - if err := pin.Halt(); err != nil { + if err := p.Halt(); err != nil { t.Fatal(err) } @@ -167,6 +210,7 @@ func TestPinADC_ReadContinous(t *testing.T) { }, DontPanic: true, } + defer b.Close() rawValues := []int32{21200, 21184} voltValues := []physic.ElectricPotential{3975 * physic.MilliVolt, 3972 * physic.MilliVolt} @@ -176,13 +220,13 @@ 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, SaveEnergy) + p, err := d.PinForChannel(Channel0Minus3, 5*physic.Volt, 100*physic.Hertz, SaveEnergy) if err != nil { t.Fatal(err) } // Read values from ADC. - c := pin.ReadContinuous() + c := p.ReadContinuous() var i = 0 for reading := range c { @@ -200,7 +244,7 @@ func TestPinADC_ReadContinous(t *testing.T) { } } - if err := pin.Halt(); err != nil { + if err := p.Halt(); err != nil { t.Fatal(err) }