private Mmc5603 sensor;
public override Task Initialize()
{
Resolver.Log.Info("Initialize...");
sensor = new Mmc5603(Device.CreateI2cBus());
sensor.Updated += (sender, result) =>
{
Resolver.Log.Info($"Magnetic Field: [X:{result.New.X.MicroTesla:N2}," +
$"Y:{result.New.Y.MicroTesla:N2}," +
$"Z:{result.New.Z.MicroTesla:N2} (MicroTeslas)]");
};
var consumer = Mmc5603.CreateObserver(
handler: result => Resolver.Log.Info($"Observer: [x] changed by threshold; new [x]: X:{result.New.X.MicroTesla:N2}, old: X:{result.Old?.X.MicroTesla:N2}"),
filter: result =>
{
if (result.Old is { } old)
{
return (result.New - old).Y > new MagneticField(1, MU.MicroTesla);
}
return false;
});
sensor.Subscribe(consumer);
return Task.CompletedTask;
}
public override async Task Run()
{
var result = await sensor.Read();
Resolver.Log.Info("Initial Readings:");
Resolver.Log.Info(
$"Magnetic field: [X:{result.X.MicroTesla:N2}," +
$"Y:{result.Y.MicroTesla:N2}," +
$"Z:{result.Z.MicroTesla:N2} (MicroTeslas)]");
sensor.StartUpdating(TimeSpan.FromMilliseconds(1500));
}