Remarks
Tsl2591 | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
Code Example
Tsl2591 sensor;
public MeadowApp()
{
Console.WriteLine("Initializing...");
// configure our sensor on the I2C Bus
var i2c = Device.CreateI2cBus();
sensor = new Tsl2591(i2c);
// Example that uses an IObservable subscription to only be notified when the filter is satisfied
var consumer = Tsl2591.CreateObserver(
handler: result => Console.WriteLine($"Observer: filter satisifed: {result.New.VisibleLight?.Lux:N2}Lux, old: {result.Old?.VisibleLight?.Lux:N2}Lux"),
// only notify if the visible light changes by 100 lux (put your hand over the sensor to trigger)
filter: result => {
if (result.Old is { } old) { //c# 8 pattern match syntax. checks for !null and assigns var.
// returns true if > 100lux change
return ( (result.New.VisibleLight.Value - old.VisibleLight.Value).Abs().Lux > 100 );
}
return false;
});
sensor.Subscribe(consumer);
// classical .NET events can also be used:
sensor.Updated += (sender, result) => {
Console.WriteLine($" Full Spectrum Light: {result.New.FullSpectrum?.Lux:N2}Lux");
Console.WriteLine($" Infrared Light: {result.New.Infrared?.Lux:N2}Lux");
Console.WriteLine($" Visible Light: {result.New.VisibleLight?.Lux:N2}Lux");
Console.WriteLine($" Integrated Light: {result.New.Integrated?.Lux:N2}Lux");
};
//==== one-off read
ReadConditions().Wait();
// start updating continuously
sensor.StartUpdating(TimeSpan.FromSeconds(1));
}
protected async Task ReadConditions()
{
var result = await sensor.Read();
Console.WriteLine("Initial Readings:");
Console.WriteLine($" Full Spectrum Light: {result.FullSpectrum?.Lux:N2}Lux");
Console.WriteLine($" Infrared Light: {result.Infrared?.Lux:N2}Lux");
Console.WriteLine($" Visible Light: {result.VisibleLight?.Lux:N2}Lux");
Console.WriteLine($" Integrated Light: {result.Integrated?.Lux:N2}Lux");
}
Sample project(s) available on GitHub
Syntax
public class Tsl2591 : ByteCommsSensorBase<(Illuminance? FullSpectrum, Illuminance? Infrared, Illuminance? VisibleLight, Illuminance? Integrated)>, IDisposable, ILightSensor, IDisposable
Constructors
Tsl2591(II2cBus, Byte, Int32)
Declaration
public Tsl2591(II2cBus bus, byte address = null, int updateIntervalMs = 1000)
Parameters
Type | Name | Description |
---|---|---|
II2cBus | bus | |
System.Byte | address | |
System.Int32 | updateIntervalMs |
Properties
DeviceID
Declaration
public int DeviceID { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
FullSpectrumLuminosity
Full spectrum luminosity (visible and infrared light combined).
Declaration
public Illuminance? FullSpectrumLuminosity { get; }
Property Value
Type | Description |
---|---|
System.Nullable<Illuminance> |
Gain
Gain of the sensor.
Declaration
public Tsl2591.GainFactor Gain { get; set; }
Property Value
Type | Description |
---|---|
Tsl2591.GainFactor |
Illuminance
Visible lux.
Declaration
public Illuminance? Illuminance { get; }
Property Value
Type | Description |
---|---|
System.Nullable<Illuminance> |
InfraredLuminosity
Infrared light luminosity.
Declaration
public Illuminance? InfraredLuminosity { get; }
Property Value
Type | Description |
---|---|
System.Nullable<Illuminance> |
IntegrationTime
Integration time for the sensor.
Declaration
public Tsl2591.IntegrationTimes IntegrationTime { get; set; }
Property Value
Type | Description |
---|---|
Tsl2591.IntegrationTimes |
PackageID
Declaration
public int PackageID { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
VisibleLightLuminosity
Visible light luminosity.
Declaration
public Illuminance? VisibleLightLuminosity { get; }
Property Value
Type | Description |
---|---|
System.Nullable<Illuminance> |
Methods
PowerOff()
Declaration
public void PowerOff()
PowerOn()
Declaration
public void PowerOn()
RaiseEventsAndNotify(IChangeResult<(Nullable<Illuminance> FullSpectrum, Nullable<Illuminance> Infrared, Nullable<Illuminance> VisibleLight, Nullable<Illuminance> Integrated)>)
Declaration
protected override void RaiseEventsAndNotify(IChangeResult<(Illuminance? FullSpectrum, Illuminance? Infrared, Illuminance? VisibleLight, Illuminance? Integrated)> changeResult)
Parameters
Type | Name | Description |
---|---|---|
IChangeResult<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Illuminance>, System.Nullable<Illuminance>, System.Nullable<Illuminance>>> | changeResult |
ReadSensor()
Declaration
protected override Task<(Illuminance? FullSpectrum, Illuminance? Infrared, Illuminance? VisibleLight, Illuminance? Integrated)> ReadSensor()
Returns
Type | Description |
---|---|
Task<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Illuminance>, System.Nullable<Illuminance>, System.Nullable<Illuminance>>> |
Overrides
Meadow.Foundation.SensorBase<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Illuminance>, System.Nullable<Illuminance>, System.Nullable<Illuminance>>>.ReadSensor()
Events
FullSpectrumUpdated
Declaration
public event EventHandler<IChangeResult<Illuminance>> FullSpectrumUpdated
Event Type
Type | Description |
---|---|
EventHandler<IChangeResult<Illuminance>> |
InfraredUpdated
Declaration
public event EventHandler<IChangeResult<Illuminance>> InfraredUpdated
Event Type
Type | Description |
---|---|
EventHandler<IChangeResult<Illuminance>> |
LuminosityUpdated
Declaration
public event EventHandler<IChangeResult<Illuminance>> LuminosityUpdated
Event Type
Type | Description |
---|---|
EventHandler<IChangeResult<Illuminance>> |
VisibleLightUpdated
Declaration
public event EventHandler<IChangeResult<Illuminance>> VisibleLightUpdated
Event Type
Type | Description |
---|---|
EventHandler<IChangeResult<Illuminance>> |