Skip to main content

Meadow.Foundation.Sensors.Light.AnalogLightSensor

AnalogLightSensor
StatusStatus badge: working
Source codeGitHub
NuGet packageNuGet 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)
{
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.IlluminanceUpdated += (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

Class AnalogLightSensor

Represents an analog light sensor

Assembly: Meadow.Foundation.dll
View Source
Declaration
public class AnalogLightSensor : SamplingSensorBase<Illuminance>, IObservable<IChangeResult<Illuminance>>, ILightSensor, ISamplingSensor<Illuminance>, ISensor<Illuminance>, ISensor, ISamplingSensor, IDisposable

Inheritance: System.Object -> Meadow.Foundation.ObservableBase<UNIT>

Implements:
System.IObservable<Meadow.IChangeResult<Meadow.Units.Illuminance>>, Meadow.Peripherals.Sensors.Light.ILightSensor, Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Illuminance>, Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Illuminance>, Meadow.Peripherals.Sensors.ISensor, Meadow.Peripherals.Sensors.ISamplingSensor, System.IDisposable

Properties

AnalogInputPort

Analog port connected to sensor

View Source
Declaration
protected IAnalogInputPort AnalogInputPort { get; }

LuminanceCalibration

Illuminance sensor calibration

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

Illuminance

Current illuminance value read by sensor

View Source
Declaration
public Illuminance? Illuminance { get; }

IsDisposed

Is the object disposed

View Source
Declaration
public bool IsDisposed { get; }

Methods

ReadSensor()

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

View Source
Declaration
protected override Task<Illuminance> ReadSensor()
Returns

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

StartUpdating(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.

View Source
Declaration
public override void StartUpdating(TimeSpan? updateInterval)
Parameters
TypeNameDescription
System.Nullable<System.TimeSpan>updateIntervalA 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. |

StopUpdating()

Stops sampling the temperature.

View Source
Declaration
public override void StopUpdating()

VoltageToLuminance(Voltage)

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

View Source
Declaration
protected Illuminance VoltageToLuminance(Voltage voltage)
Returns

Meadow.Units.Illuminance

Parameters
TypeName
Meadow.Units.Voltagevoltage

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

View Source
Declaration
public void Dispose()

Dispose(bool)

Dispose of the object

View Source
Declaration
protected virtual void Dispose(bool disposing)
Parameters
TypeNameDescription
System.BooleandisposingIs disposing

Implements

  • System.IObservable<Meadow.IChangeResult<Meadow.Units.Illuminance>>
  • Meadow.Peripherals.Sensors.Light.ILightSensor
  • Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Illuminance>
  • Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Illuminance>
  • Meadow.Peripherals.Sensors.ISensor
  • Meadow.Peripherals.Sensors.ISamplingSensor
  • System.IDisposable