Mma7660fc sensor;
public override Task Initialize()
{
Resolver.Log.Info("Initialize...");
sensor = new Mma7660fc(Device.CreateI2cBus());
sensor.Updated += (sender, result) =>
{
Resolver.Log.Info($"Accel: [X:{result.New.X.MetersPerSecondSquared:N2}," +
$"Y:{result.New.Y.MetersPerSecondSquared:N2}," +
$"Z:{result.New.Z.MetersPerSecondSquared:N2} (m/s^2)]" +
$" Direction: {sensor.Direction}" +
$" Orientation: {sensor.Orientation}");
};
var consumer = Mma7660fc.CreateObserver(
handler: result => Resolver.Log.Info($"Observer: [x] changed by threshold; new [x]: X:{result.New.X:N2}, old: X:{result.Old?.X:N2}"),
filter: result =>
{
if (result.Old is { } old)
{
return ((result.New - old).Z > new Acceleration(0.5, AU.Gravity));
}
return false;
});
sensor.Subscribe(consumer);
return Task.CompletedTask;
}
public async override Task Run()
{
var result = await sensor.Read();
Resolver.Log.Info("Initial Readings:");
Resolver.Log.Info($"Accel: [X:{result.X.MetersPerSecondSquared:N2}," +
$"Y:{result.Y.MetersPerSecondSquared:N2}," +
$"Z:{result.Z.MetersPerSecondSquared:N2} (m/s^2)]");
sensor.StartUpdating(TimeSpan.FromMilliseconds(1000));
}