Remarks
AnalogLightSensor | |
---|---|
Status | |
Source code | GitHub |
NuGet package |
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
Syntax
public class AnalogLightSensor : SamplingSensorBase<Illuminance>, IObservable<IChangeResult<Illuminance>>, ILightSensor, ISamplingSensor<Illuminance>, ISensor<Illuminance>
Constructors
AnalogLightSensor(IAnalogInputPort, 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. |
Remarks
AnalogLightSensor | |
---|---|
Status | |
Source code | GitHub |
NuGet package |
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");
}
AnalogLightSensor(IPin, Calibration?, int, 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. |
int | sampleCount | How many samples to take during a given reading. These are automatically averaged to reduce noise. |
TimeSpan? | sampleInterval | The time, in milliseconds, to wait in between samples during a reading. |
Remarks
AnalogLightSensor | |
---|---|
Status | |
Source code | GitHub |
NuGet package |
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");
}
Properties
AnalogInputPort
Analog port connected to sensor
Declaration
protected IAnalogInputPort AnalogInputPort { get; }
Property Value
Type | Description |
---|---|
IAnalogInputPort |
Remarks
AnalogLightSensor | |
---|---|
Status | |
Source code | GitHub |
NuGet package |
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");
}
Illuminance
Current illuminance value read by sensor
Declaration
public Illuminance? Illuminance { get; }
Property Value
Type | Description |
---|---|
Illuminance? |
Remarks
AnalogLightSensor | |
---|---|
Status | |
Source code | GitHub |
NuGet package |
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");
}
LuminanceCalibration
Illuminance sensor callibration
Declaration
public AnalogLightSensor.Calibration LuminanceCalibration { get; protected set; }
Property Value
Type | Description |
---|---|
AnalogLightSensor.Calibration |
Remarks
AnalogLightSensor | |
---|---|
Status | |
Source code | GitHub |
NuGet package |
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");
}
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
Remarks
AnalogLightSensor | |
---|---|
Status | |
Source code | GitHub |
NuGet package |
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");
}
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
Remarks
AnalogLightSensor | |
---|---|
Status | |
Source code | GitHub |
NuGet package |
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");
}
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.
Declaration
public override void StartUpdating(TimeSpan? updateInterval)
Parameters
Type | Name | Description |
---|---|---|
TimeSpan? | updateInterval | A |
Overrides
Remarks
AnalogLightSensor | |
---|---|
Status | |
Source code | GitHub |
NuGet package |
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");
}
StopUpdating()
Stops sampling the temperature.
Declaration
public override void StopUpdating()
Overrides
Remarks
AnalogLightSensor | |
---|---|
Status | |
Source code | GitHub |
NuGet package |
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");
}
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 |
Remarks
AnalogLightSensor | |
---|---|
Status | |
Source code | GitHub |
NuGet package |
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");
}
Events
LuminosityUpdated
Raised when the value of the reading changes.
Declaration
public event EventHandler<IChangeResult<Illuminance>> LuminosityUpdated
Event Type
Type | Description |
---|---|
EventHandler<IChangeResult<Illuminance>> |
Remarks
AnalogLightSensor | |
---|---|
Status | |
Source code | GitHub |
NuGet package |
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");
}