Tcs3472x sensor;
RgbPwmLed rgbLed;
public override Task Initialize()
{
Console.WriteLine("Initialize...");
sensor = new Tcs3472x(Device.CreateI2cBus());
rgbLed = new RgbPwmLed(
Device,
Device.Pins.OnboardLedRed,
Device.Pins.OnboardLedGreen,
Device.Pins.OnboardLedBlue,
commonType: CommonType.CommonAnode);
var consumer = Tcs3472x.CreateObserver(
handler: result => Console.WriteLine($"Observer: filter satisifed: {result.New.AmbientLight?.Lux:N2}Lux, old: {result.Old?.AmbientLight?.Lux:N2}Lux"),
filter: result => {
if (result.Old is { } old) {
return ((result.New.AmbientLight.Value - old.AmbientLight.Value).Abs().Lux > 100);
}
return false;
});
sensor.Subscribe(consumer);
sensor.Updated += (sender, result) => {
Console.WriteLine($" Ambient Light: {result.New.AmbientLight?.Lux:N2}Lux");
Console.WriteLine($" Color: {result.New.Color}");
if (result.New.Color is { } color) { rgbLed.SetColor(color); }
};
return Task.CompletedTask;
}
public override async Task Run()
{
var (AmbientLight, Color, Valid) = await sensor.Read();
Console.WriteLine("Initial Readings:");
Console.WriteLine($" Visible Light: {AmbientLight?.Lux:N2}Lux");
Console.WriteLine($" Color: {Color}");
if (Color is { } color) { rgbLed.SetColor(color); }
sensor.StartUpdating(TimeSpan.FromSeconds(1));
}