Remarks

AnalogLightSensor
Status Status badge: working
Source code GitHub
NuGet package NuGet Gallery for Meadow.Foundation

Code Example

AnalogLightSensor analogLightSensor;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing...");

    // configure our AnalogLightSensor sensor
    analogLightSensor = new AnalogLightSensor(
        analogPin: Device.Pins.A03);

    //==== IObservable Pattern with an optional notification filter.
    var consumer = AnalogLightSensor.CreateObserver(
        handler: result => Resolver.Log.Info($"Observer filter satisfied: {result.New.Lux:N2} lux, old: {result.Old.Value.Lux:N2} lux"),

        // only notify if the change is greater than 0.5
        filter: result => {
            if (result.Old is { } old) 
            {   //c# 8 pattern match syntax. checks for !null and assigns var.
                return (result.New - old).Abs().Lux > 0.5; // returns true if > 0.5  change.
            }
            return false;
        }
        // if you want to always get notified, pass null for the filter:
        //filter: null
    );
    analogLightSensor.Subscribe(consumer);

    // classical .NET events can also be used:
    analogLightSensor.LuminosityUpdated += (sender, result) => 
        Resolver.Log.Info($"Lux changed: {result.New.Lux:N2} lux, old: {result.Old?.Lux:N2} lux");

    //==== One-off reading use case/pattern
    ReadIlluminance().Wait();

    // Spin up the sampling thread so that events are raised and IObservable notifications are sent.
    analogLightSensor.StartUpdating(TimeSpan.FromMilliseconds(1000));

    return Task.CompletedTask;
}

protected async Task ReadIlluminance()
{
    var illuminance = await analogLightSensor.Read();
    Resolver.Log.Info($"Initial lux: {illuminance.Lux:N2} lux");
}

Sample project(s) available on GitHub

Characteristic Locus
Inheritance System.Object ObservableBase<Illuminance> SamplingSensorBase<Illuminance> > AnalogLightSensor
Implements ISamplingSensor<Illuminance> ILightSensor
Inherited Members SamplingSensorBase<Illuminance>.samplingLock SamplingSensorBase<Illuminance>.Updated SamplingSensorBase<Illuminance>.SamplingTokenSource SamplingSensorBase<Illuminance>.Conditions SamplingSensorBase<Illuminance>.IsSampling SamplingSensorBase<Illuminance>.UpdateInterval SamplingSensorBase<Illuminance>.Read() ObservableBase<Illuminance>.observers ObservableBase<Illuminance>.NotifyObservers(IChangeResult<Illuminance>) ObservableBase<Illuminance>.Subscribe(IObserver<IChangeResult<Illuminance>>) ObservableBase<Illuminance>.CreateObserver(Action<IChangeResult<Illuminance>>, Nullable<Predicate<IChangeResult<Illuminance>>>)
Namespace Meadow.Foundation.Sensors.Light
Assembly Meadow.Foundation.dll

Syntax

public class AnalogLightSensor : SamplingSensorBase<Illuminance>, ISamplingSensor<Illuminance>, ILightSensor

Constructors

AnalogLightSensor(IAnalogInputPort, AnalogLightSensor.Calibration)

New instance of the AnalogLightSensor class.

Declaration
public AnalogLightSensor(IAnalogInputPort analogInputPort, AnalogLightSensor.Calibration calibration = null)

Parameters

Type Name Description
IAnalogInputPort analogInputPort

Analog port the sensor is connected to.

AnalogLightSensor.Calibration calibration

Calibration for the analog sensor.

AnalogLightSensor(IPin, AnalogLightSensor.Calibration, Int32, Nullable<TimeSpan>)

New instance of the AnalogLightSensor class.

Declaration
public AnalogLightSensor(IPin analogPin, AnalogLightSensor.Calibration calibration = null, int sampleCount = 5, TimeSpan? sampleInterval = null)

Parameters

Type Name Description
IPin analogPin

Analog pin the sensor is connected to.

AnalogLightSensor.Calibration calibration

Calibration for the analog sensor.

System.Int32 sampleCount

How many samples to take during a given reading. These are automatically averaged to reduce noise.

System.Nullable<TimeSpan> sampleInterval

The time, in milliseconds, to wait in between samples during a reading.

Properties

AnalogInputPort

Analog port connected to sensor

Declaration
protected IAnalogInputPort AnalogInputPort { get; }

Property Value

Type Description
IAnalogInputPort

Illuminance

Current illuminance value read by sensor

Declaration
public Illuminance? Illuminance { get; }

Property Value

Type Description
System.Nullable<Illuminance>

LuminanceCalibration

Illuminance sensor callibration

Declaration
public AnalogLightSensor.Calibration LuminanceCalibration { get; protected set; }

Property Value

Type Description
AnalogLightSensor.Calibration

Methods

RaiseEventsAndNotify(IChangeResult<Illuminance>)

Notify subscibers of LuminosityUpdated event hander

Declaration
protected override void RaiseEventsAndNotify(IChangeResult<Illuminance> changeResult)

Parameters

Type Name Description
IChangeResult<Illuminance> changeResult

Change result with old and new Illuminance

Overrides

Meadow.Foundation.SamplingSensorBase<Illuminance>.RaiseEventsAndNotify(IChangeResult<Illuminance>)

ReadSensor()

Convenience method to get the current luminance. For frequent reads, use StartSampling() and StopSampling() in conjunction with the SampleBuffer.

Declaration
protected override Task<Illuminance> ReadSensor()

Returns

Type Description
Task<Illuminance>

Overrides

Meadow.Foundation.SamplingSensorBase<Illuminance>.ReadSensor()

StartUpdating(Nullable<TimeSpan>)

Starts continuously sampling the sensor.

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 override void StartUpdating(TimeSpan? updateInterval)

Parameters

Type Name Description
System.Nullable<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. The default is 5 seconds.

Overrides

Meadow.Foundation.SamplingSensorBase<Illuminance>.StartUpdating(System.Nullable<TimeSpan>)

StopUpdating()

Stops sampling the temperature.

Declaration
public override void StopUpdating()

Overrides

Meadow.Foundation.SamplingSensorBase<Illuminance>.StopUpdating()

VoltageToLuminance(Voltage)

Converts a voltage value to a level in centimeters, based on the current calibration values.

Declaration
protected Illuminance VoltageToLuminance(Voltage voltage)

Parameters

Type Name Description
Voltage voltage

Returns

Type Description
Illuminance

Events

LuminosityUpdated

Raised when the value of the reading changes.

Declaration
public event EventHandler<IChangeResult<Illuminance>> LuminosityUpdated

Event Type

Type Description
EventHandler<IChangeResult<Illuminance>>