Lis3mdl sensor;
public override Task Initialize()
{
Resolver.Log.Info("Initialize hardware...");
sensor = new Lis3mdl(Device.CreateI2cBus());
sensor.Updated += HandleResult;
var consumer = Lis3mdl.CreateObserver(handler: result => HandleResult(this, result),
filter: result => FilterResult(result));
sensor.Subscribe(consumer);
sensor.StartUpdating(TimeSpan.FromSeconds(2));
return base.Initialize();
}
bool FilterResult(IChangeResult<MagneticField3D> result)
{
return result.New.Z > new MagneticField(0.1, MagneticField.UnitType.Gauss);
}
void HandleResult(object sender, IChangeResult<MagneticField3D> result)
{
var mag = result.New;
Resolver.Log.Info($"Magnetometer (gauss): X = {mag.X.Gauss:0.##}, Y = {mag.Y.Gauss:0.##}, Z = {mag.Z.Gauss:0.##}");
}