Skip to content

Commit

Permalink
analog: rename Reading to Sample
Browse files Browse the repository at this point in the history
This makes more sense.
  • Loading branch information
maruel committed Nov 12, 2018
1 parent 1e2770e commit 0437744
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
27 changes: 16 additions & 11 deletions experimental/conn/analog/analog.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ import (
"periph.io/x/periph/conn/pin"
)

// Reading is the result of PinADC.Read().
type Reading struct {
// V is the interpreted electrical level.
// Sample is one analog sample.
//
// Raw must be set, but V may or may not be set, depending if the device knows
// the electrical tension this measurement represents.
type Sample struct {
// V is the interpreted electrical tension.
V physic.ElectricPotential
// Raw is the raw measurement.
Raw int32
Expand All @@ -25,18 +28,20 @@ type Reading struct {
type PinADC interface {
pin.Pin
// Range returns the maximum supported range [min, max] of the values.
Range() (Reading, Reading)
//
// It is possible for a DAC that the Sample.V value is not set.
Range() (Sample, Sample)
// Read returns the current pin level.
Read() (Reading, error)
Read() (Sample, error)
}

// PinDAC is an digital-to-analog-conversion output.
type PinDAC interface {
pin.Pin
// Range returns the maximum supported range [min, max] of the values.
//
// It is possible for a DAC that the Reading.V value is not set.
Range() (Reading, Reading)
// It is possible for a DAC that the Sample.V value is not set.
Range() (Sample, Sample)
// Out sets an analog output value.
Out(v int32) error
}
Expand Down Expand Up @@ -73,12 +78,12 @@ func (invalidPin) Halt() error {
return errInvalidPin
}

func (invalidPin) Range() (Reading, Reading) {
return Reading{}, Reading{}
func (invalidPin) Range() (Sample, Sample) {
return Sample{}, Sample{}
}

func (invalidPin) Read() (Reading, error) {
return Reading{}, errInvalidPin
func (invalidPin) Read() (Sample, error) {
return Sample{}, errInvalidPin
}

func (invalidPin) Out(v int32) error {
Expand Down
22 changes: 11 additions & 11 deletions experimental/devices/ads1x15/ads1x15.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ type PinADC interface {
analog.PinADC
// ReadContinuous opens a channel and reads continuously at the frequency the
// pin was configured for.
ReadContinuous() <-chan analog.Reading
ReadContinuous() <-chan analog.Sample
}

// Dev is an handle to an ADS1015/ADS1115 ADC.
Expand Down Expand Up @@ -239,15 +239,15 @@ func (d *Dev) PinForChannel(c Channel, maxVoltage physic.ElectricPotential, f ph
}, nil
}

func (d *Dev) executePreparedQuery(query []byte, waitTime time.Duration, voltageMultiplier physic.ElectricPotential) (analog.Reading, error) {
func (d *Dev) executePreparedQuery(query []byte, waitTime time.Duration, voltageMultiplier physic.ElectricPotential) (analog.Sample, error) {
// Lock the ADC converter to avoid multiple simultaneous readings.
d.mu.Lock()
defer d.mu.Unlock()

// Send the config value to start the ADC conversion.
// Explicitly break the 16-bit value down to a big endian pair of bytes.
if err := d.c.Tx(query, nil); err != nil {
return analog.Reading{}, err
return analog.Sample{}, err
}

// Wait for the ADC sample to finish.
Expand All @@ -256,12 +256,12 @@ func (d *Dev) executePreparedQuery(query []byte, waitTime time.Duration, voltage
// Retrieve the result.
data := []byte{0, 0}
if err := d.c.Tx([]byte{ads1x15PointerConversion}, data); err != nil {
return analog.Reading{}, err
return analog.Sample{}, err
}

// Convert the raw data into physical value.
raw := int16(binary.BigEndian.Uint16(data))
return analog.Reading{
return analog.Sample{
Raw: int32(raw),
V: physic.ElectricPotential(raw) * voltageMultiplier / physic.ElectricPotential(1<<15),
}, nil
Expand Down Expand Up @@ -400,18 +400,18 @@ type analogPin struct {
}

// Range returns the maximum supported range [min, max] of the values.
func (p *analogPin) Range() (analog.Reading, analog.Reading) {
max := analog.Reading{Raw: math.MaxInt16, V: p.voltageMultiplier}
min := analog.Reading{Raw: -math.MaxInt16, V: -p.voltageMultiplier}
func (p *analogPin) Range() (analog.Sample, analog.Sample) {
max := analog.Sample{Raw: math.MaxInt16, V: p.voltageMultiplier}
min := analog.Sample{Raw: -math.MaxInt16, V: -p.voltageMultiplier}
return min, max
}

// Read returns the current pin level.
func (p *analogPin) Read() (analog.Reading, error) {
func (p *analogPin) Read() (analog.Sample, error) {
return p.adc.executePreparedQuery(p.query[:], p.waitTime, p.voltageMultiplier)
}

func (p *analogPin) ReadContinuous() <-chan analog.Reading {
func (p *analogPin) ReadContinuous() <-chan analog.Sample {
// We need to lock if there are multiple Halt or ReadContinuous
// calls simultaneously.
p.mu.Lock()
Expand All @@ -422,7 +422,7 @@ func (p *analogPin) ReadContinuous() <-chan analog.Reading {
p.stop <- struct{}{}
p.stop = nil
}
reading := make(chan analog.Reading, 16)
reading := make(chan analog.Sample, 16)
p.stop = make(chan struct{})
t := time.NewTicker(p.requestedFrequency.Duration())

Expand Down

0 comments on commit 0437744

Please sign in to comment.