AnalogLightSensor analogLightSensor;
public override Task Initialize()
{
Resolver.Log.Info("Initializing...");
analogLightSensor = new AnalogLightSensor(
analogPin: Device.Pins.A03);
var consumer = AnalogLightSensor.CreateObserver(
handler: result => Resolver.Log.Info($"Observer filter satisfied: {result.New.Lux:N2} lux, old: {result.Old.Value.Lux:N2} lux"),
filter: result =>
{
if (result.Old is { } old)
{
return (result.New - old).Abs().Lux > 0.5;
}
return false;
}
);
analogLightSensor.Subscribe(consumer);
analogLightSensor.IlluminanceUpdated += (sender, result) =>
Resolver.Log.Info($"Lux changed: {result.New.Lux:N2} lux, old: {result.Old?.Lux:N2} lux");
ReadIlluminance().Wait();
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");
}