Skip to main content

Class AnalogInputPortBase

Provides a base implementation for much of the common tasks of implementing IAnalogInputPort

Assembly: Meadow.Contracts.dll
View Source
Declaration
public abstract class AnalogInputPortBase : AnalogPortBase, IAnalogInputPort, IAnalogPort, IPort<IAnalogChannelInfo>, IDisposable, IObservable<IChangeResult<Voltage>>

Inheritance: System.Object -> Meadow.Hardware.PortBase<C>

Derived:
Meadow.Hardware.AnalogInputPort

Implements:
Meadow.Hardware.IAnalogInputPort, System.IDisposable, System.IObservable<Meadow.IChangeResult<Meadow.Units.Voltage>>

Properties

BufferSyncRoot

Gets the internal synchronization object for the analog buffer

View Source
Declaration
protected object BufferSyncRoot { get; }

VoltageSampleBuffer

Gets the sample buffer. Make sure to call StartUpdating() before use.

View Source
Declaration
public Voltage[] VoltageSampleBuffer { get; }

UpdateInterval

A TimeSpan that specifies how long to wait between readings. This value influences how often *Updated events are raised and IObservable consumers are notified.

View Source
Declaration
public TimeSpan UpdateInterval { get; protected set; }

SampleCount

Number of samples to take per reading. If > 1 then the port will take multiple readings and These are automatically averaged to reduce noise, a process known as oversampling.

View Source
Declaration
public int SampleCount { get; protected set; }

SampleInterval

Duration in between samples when oversampling.

View Source
Declaration
public TimeSpan SampleInterval { get; protected set; }

ReferenceVoltage

The reference voltage being used for the ADC comparison

View Source
Declaration
public Voltage ReferenceVoltage { get; protected set; }

Voltage

Gets the average value of the values in the buffer. Use in conjunction with StartUpdating() for long-running analog sampling. For occasional sampling, use Read().

View Source
Declaration
public virtual Voltage Voltage { get; }

Observers

A collection of state observers

View Source
Declaration
protected List<IObserver<IChangeResult<Voltage>>> Observers { get; }

Methods

ConvertReadingToVoltage(int)

Converts a raw 32-bit reading to a scaled value in volts

View Source
Declaration
protected double ConvertReadingToVoltage(int rawReading)
Returns

System.Double

Parameters
TypeNameDescription
System.Int32rawReadingThe raw reading to convert

Read()

Convenience method to get the current voltage. For frequent reads, use StartUpdating() and StopUpdating() in conjunction with the SampleBuffer.

View Source
Declaration
public abstract Task<Voltage> Read()
Returns

System.Threading.Tasks.Task<Meadow.Units.Voltage>

StartUpdating(TimeSpan?)

Starts continuously sampling the analog port.

This method also starts raising Changed events and IObservable subscribers getting notified. Use the readIntervalDuration parameter to specify how often events and notifications are raised/sent.

View Source
Declaration
public abstract void StartUpdating(TimeSpan? updateInterval)
Parameters
TypeName
System.Nullable<System.TimeSpan>updateInterval

StopUpdating()

Stops sampling the analog port.

View Source
Declaration
public abstract void StopUpdating()

RaiseChangedAndNotify(IChangeResult<Voltage>)

Raises the Changed event and notifies the observers with the specified change result.

View Source
Declaration
protected void RaiseChangedAndNotify(IChangeResult<Voltage> changeResult)
Parameters
TypeNameDescription
Meadow.IChangeResult<UNIT>changeResultThe change result to be passed to the event and observers.

Subscribe(IObserver<IChangeResult<Voltage>>)

Subscribes an observer to receive notifications of voltage change.

View Source
Declaration
public IDisposable Subscribe(IObserver<IChangeResult<Voltage>> observer)
Returns

System.IDisposable: An IDisposable object that can be used to unsubscribe from the observer.

Parameters
TypeNameDescription
System.IObserver<Meadow.IChangeResult<Meadow.Units.Voltage>>observerThe observer to subscribe.

Events

Updated

Raised when the value of the reading changes.

View Source
Declaration
public event EventHandler<IChangeResult<Voltage>> Updated
Event Type

System.EventHandler<Meadow.IChangeResult<Meadow.Units.Voltage>>

Implements