Remarks
Tsl2591 | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
Code Example
Tsl2591 sensor;
public override Task Initialize()
{
Resolver.Log.Info("Initialize...");
// 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 => Resolver.Log.Info($"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) => {
Resolver.Log.Info($" Full Spectrum Light: {result.New.FullSpectrum?.Lux:N2}Lux");
Resolver.Log.Info($" Infrared Light: {result.New.Infrared?.Lux:N2}Lux");
Resolver.Log.Info($" Visible Light: {result.New.VisibleLight?.Lux:N2}Lux");
Resolver.Log.Info($" Integrated Light: {result.New.Integrated?.Lux:N2}Lux");
};
return Task.CompletedTask;
}
public override async Task Run()
{
var result = await sensor.Read();
Resolver.Log.Info("Initial Readings:");
Resolver.Log.Info($" Full Spectrum Light: {result.FullSpectrum?.Lux:N2}Lux");
Resolver.Log.Info($" Infrared Light: {result.Infrared?.Lux:N2}Lux");
Resolver.Log.Info($" Visible Light: {result.VisibleLight?.Lux:N2}Lux");
Resolver.Log.Info($" Integrated Light: {result.Integrated?.Lux:N2}Lux");
sensor.StartUpdating(TimeSpan.FromSeconds(1));
}
Sample project(s) available on GitHub
Syntax
public class Tsl2591 : ByteCommsSensorBase<(Illuminance? FullSpectrum, Illuminance? Infrared, Illuminance? VisibleLight, Illuminance? Integrated)>, ISamplingSensor<(Illuminance? FullSpectrum, Illuminance? Infrared, Illuminance? VisibleLight, Illuminance? Integrated)>, ISamplingSensor<(Illuminance? FullSpectrum, Illuminance? Infrared, Illuminance? VisibleLight, Illuminance? Integrated)>, IDisposable, ILightSensor, IDisposable
Constructors
Tsl2591(II2cBus, Byte)
Create a new Tsl2591 object
Declaration
public Tsl2591(II2cBus i2cBus, byte address = null)
Parameters
Type | Name | Description |
---|---|---|
II2cBus | i2cBus | The I2C bus |
System.Byte | address | The I2C address |
Properties
DeviceID
Sensor device ID
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
Sensor package ID
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()
Power the sensor off
Declaration
public void PowerOff()
PowerOn()
Power the sensor on
Declaration
public void PowerOn()
RaiseEventsAndNotify(IChangeResult<(Nullable<Illuminance> FullSpectrum, Nullable<Illuminance> Infrared, Nullable<Illuminance> VisibleLight, Nullable<Illuminance> Integrated)>)
Raise events for subcribers and notify of value changes
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 | The updated sensor data |
ReadSensor()
Reads data from the sensor
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>>> | The latest sensor reading |
Overrides
Meadow.Foundation.SamplingSensorBase<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Illuminance>, System.Nullable<Illuminance>, System.Nullable<Illuminance>>>.ReadSensor()
Events
FullSpectrumUpdated
Raised when Full Spectrum Illuminance value changes
Declaration
public event EventHandler<IChangeResult<Illuminance>> FullSpectrumUpdated
Event Type
Type | Description |
---|---|
EventHandler<IChangeResult<Illuminance>> |
InfraredUpdated
Raised when Infrared Illuminance value changes
Declaration
public event EventHandler<IChangeResult<Illuminance>> InfraredUpdated
Event Type
Type | Description |
---|---|
EventHandler<IChangeResult<Illuminance>> |
LuminosityUpdated
Raised when Luminosity value changes
Declaration
public event EventHandler<IChangeResult<Illuminance>> LuminosityUpdated
Event Type
Type | Description |
---|---|
EventHandler<IChangeResult<Illuminance>> |
VisibleLightUpdated
Raised when Visible Light value changes
Declaration
public event EventHandler<IChangeResult<Illuminance>> VisibleLightUpdated
Event Type
Type | Description |
---|---|
EventHandler<IChangeResult<Illuminance>> |