private Tsl2591 sensor;
public override Task Initialize()
{
Resolver.Log.Info("Initialize...");
var i2c = Device.CreateI2cBus();
sensor = new Tsl2591(i2c);
var consumer = Tsl2591.CreateObserver(
handler: result => Resolver.Log.Info($"Observer: filter satisfied: {result.New.VisibleLight?.Lux:N2}Lux, old: {result.Old?.VisibleLight?.Lux:N2}Lux"),
filter: result =>
{
if (result.Old is { } old)
{
return ((result.New.VisibleLight.Value - old.VisibleLight.Value).Abs().Lux > 100);
}
return false;
});
sensor.Subscribe(consumer);
sensor.Updated += (sender, result) =>
{
Resolver.Log.Info($" Integrated Light: {result.New.Lux:N2}Lux");
};
sensor.InfraredUpdated += (sender, result) =>
{
Resolver.Log.Info($" Infrared Light: {result.New.Lux:N2}Lux");
};
sensor.VisibleLightUpdated += (sender, result) =>
{
Resolver.Log.Info($" Visible Light: {result.New.Lux:N2}Lux");
};
sensor.FullSpectrumUpdated += (sender, result) =>
{
Resolver.Log.Info($" Full Spectrum Light: {result.New.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));
}