Characteristic | Locus |
---|---|
Inheritance | object PortBase<IAnalogChannelInfo> AnalogPortBase > AnalogInputPortBase > AnalogInputPort |
Implements | IAnalogInputPort IAnalogPort IPort<IAnalogChannelInfo> IDisposable IObservable<IChangeResult<Voltage>> |
Inherited Members | PortBase<IAnalogChannelInfo>.disposed PortBase<IAnalogChannelInfo>.Channel PortBase<IAnalogChannelInfo>.Pin PortBase<IAnalogChannelInfo>.Dispose(bool) PortBase<IAnalogChannelInfo>.Dispose() object.Equals(object) object.Equals(object, object) object.GetHashCode() object.GetType() object.MemberwiseClone() object.ReferenceEquals(object, object) object.ToString() |
Namespace | Meadow.Hardware |
Assembly | Meadow.Contracts.dll |
Syntax
public abstract class AnalogInputPortBase : AnalogPortBase, IAnalogInputPort, IAnalogPort, IPort<IAnalogChannelInfo>, IDisposable, IObservable<IChangeResult<Voltage>>
Constructors
AnalogInputPortBase(IPin, IAnalogChannelInfo, int, TimeSpan, Voltage)
Initializes a new instance of the AnalogInputPortBase class.
Declaration
protected AnalogInputPortBase(IPin pin, IAnalogChannelInfo channel, int sampleCount, TimeSpan sampleInterval, Voltage referenceVoltage)
Parameters
Type | Name | Description |
---|---|---|
IPin | pin | The pin associated with the analog input port. |
IAnalogChannelInfo | channel | The channel information for the analog input port. |
int | sampleCount | The number of samples to be taken during each reading. |
TimeSpan | sampleInterval | The time interval between samples during each reading. |
Voltage | referenceVoltage | The reference voltage used for analog-to-digital conversion. |
Properties
BufferSyncRoot
Gets the internal synchronization object for the analog buffer
Declaration
protected object BufferSyncRoot { get; }
Property Value
Type | Description |
---|---|
object |
Observers
A collection of state observers
Declaration
protected List<IObserver<IChangeResult<Voltage>>> Observers { get; }
Property Value
Type | Description |
---|---|
List<IObserver<IChangeResult<Voltage>>> |
ReferenceVoltage
The reference voltage being used for the ADC comparison
Declaration
public Voltage ReferenceVoltage { get; protected set; }
Property Value
Type | Description |
---|---|
Voltage |
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.
Declaration
public int SampleCount { get; protected set; }
Property Value
Type | Description |
---|---|
int |
SampleInterval
Duration in between samples when oversampling.
Declaration
public TimeSpan SampleInterval { get; protected set; }
Property Value
Type | Description |
---|---|
TimeSpan |
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.
Declaration
public TimeSpan UpdateInterval { get; protected set; }
Property Value
Type | Description |
---|---|
TimeSpan |
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().
Declaration
public virtual Voltage Voltage { get; }
Property Value
Type | Description |
---|---|
Voltage | The average buffer value. |
VoltageSampleBuffer
Gets the sample buffer. Make sure to call StartUpdating() before use.
Declaration
public Voltage[] VoltageSampleBuffer { get; }
Property Value
Type | Description |
---|---|
Voltage[] | The sample buffer. |
Methods
ConvertReadingToVoltage(int)
Converts a raw 32-bit reading to a scaled value in volts
Declaration
protected double ConvertReadingToVoltage(int rawReading)
Parameters
Type | Name | Description |
---|---|---|
int | rawReading | The raw reading to convert |
Returns
Type | Description |
---|---|
double |
RaiseChangedAndNotify(IChangeResult<Voltage>)
Raises the Changed event and notifies the observers with the specified change result.
Declaration
protected void RaiseChangedAndNotify(IChangeResult<Voltage> changeResult)
Parameters
Type | Name | Description |
---|---|---|
IChangeResult<Voltage> | changeResult | The change result to be passed to the event and observers. |
Read()
Convenience method to get the current voltage. For frequent reads, use StartUpdating() and StopUpdating() in conjunction with the SampleBuffer.
Declaration
public abstract Task<Voltage> Read()
Returns
Type | Description |
---|---|
Task<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.
Declaration
public abstract void StartUpdating(TimeSpan? updateInterval)
Parameters
Type | Name | Description |
---|---|---|
TimeSpan? | updateInterval |
StopUpdating()
Stops sampling the analog port.
Declaration
public abstract void StopUpdating()
Subscribe(IObserver<IChangeResult<Voltage>>)
Subscribes an observer to receive notifications of voltage change.
Declaration
public IDisposable Subscribe(IObserver<IChangeResult<Voltage>> observer)
Parameters
Type | Name | Description |
---|---|---|
IObserver<IChangeResult<Voltage>> | observer | The observer to subscribe. |
Returns
Type | Description |
---|---|
IDisposable | An IDisposable object that can be used to unsubscribe from the observer. |
Events
Updated
Raised when the value of the reading changes.
Declaration
public event EventHandler<IChangeResult<Voltage>> Updated
Event Type
Type | Description |
---|---|
EventHandler<IChangeResult<Voltage>> |