Lsm303agr sensor;
public override Task Initialize()
{
Resolver.Log.Info("Initialize hardware...");
sensor = new Lsm303agr(Device.CreateI2cBus());
sensor.Updated += HandleResult;
var consumer = Lsm303agr.CreateObserver(handler: result => HandleResult(this, result),
filter: result => FilterResult(result));
sensor.Subscribe(consumer);
sensor.StartUpdating(TimeSpan.FromMilliseconds(2000));
return base.Initialize();
}
bool FilterResult(IChangeResult<(Acceleration3D? Acceleration3D, MagneticField3D? MagneticField3D)> result)
{
return result.New.Acceleration3D.Value.Z > new Acceleration(0.1, Acceleration.UnitType.Gravity);
}
void HandleResult(object sender,
IChangeResult<(Acceleration3D? Acceleration3D,
MagneticField3D? MagneticField3D)> result)
{
var accel = result.New.Acceleration3D.Value;
var mag = result.New.MagneticField3D.Value;
Resolver.Log.Info($"AccelX={accel.X.Gravity:0.##}g, AccelY={accel.Y.Gravity:0.##}g, AccelZ={accel.Z.Gravity:0.##}g, MagX={mag.X.Gauss:0.##}gauss, MagY={mag.Y.Gauss:0.##}gauss, MagZ={mag.Z.Gauss:0.##}gauss");
}