Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make BME68x and BMI270 sleep-aware #895

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Source/Meadow.Foundation.Core/PollingSensorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public override void StartUpdating(TimeSpan? updateInterval = null)
{
lock (samplingLock)
{
if (IsSampling) { return; }
if (IsSampling && updateInterval == UpdateInterval) { return; }

IsSampling = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ public abstract partial class Bme68x :
RelativeHumidity? Humidity,
Pressure? Pressure,
Resistance? GasResistance)>,
ITemperatureSensor, IHumiditySensor, IBarometricPressureSensor, IGasResistanceSensor, ISpiPeripheral, II2cPeripheral, IDisposable
ITemperatureSensor, IHumiditySensor, IBarometricPressureSensor, IGasResistanceSensor, ISpiPeripheral, II2cPeripheral, ISleepAwarePeripheral, IDisposable
{
private event EventHandler<IChangeResult<Units.Temperature>> _temperatureHandlers = default!;
private event EventHandler<IChangeResult<RelativeHumidity>> _humidityHandlers = default!;
private event EventHandler<IChangeResult<Pressure>> _pressureHandlers = default!;
private event EventHandler<IChangeResult<Resistance>> _gasResistanceHandlers = default!;
private PowerMode _lastRunningPowerMode;

/// <summary>
/// The temperature oversampling mode
Expand Down Expand Up @@ -373,6 +374,7 @@ public void SetPowerMode(PowerMode powerMode)
byte mask = 0x03;
status = (byte)((status & (byte)~mask) | (byte)powerMode);
busComms.WriteRegister((byte)Registers.CTRL_MEAS, status);
_lastRunningPowerMode = powerMode;
}

/// <summary>
Expand Down Expand Up @@ -675,5 +677,19 @@ async Task<Pressure> ISensor<Pressure>.Read()

async Task<Resistance> ISensor<Resistance>.Read()
=> (await Read()).GasResistance!.Value;

/// <inheritdoc/>
public Task BeforeSleep(CancellationToken cancellationToken)
{
SetPowerMode(PowerMode.Sleep);
return Task.CompletedTask;
}

/// <inheritdoc/>
public Task AfterWake(CancellationToken cancellationToken)
{
SetPowerMode(_lastRunningPowerMode);
return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,12 @@
/// </summary>
public partial class Bmi270 :
PollingSensorBase<(Acceleration3D? Acceleration3D, AngularVelocity3D? AngularVelocity3D, Units.Temperature? Temperature)>,
II2cPeripheral, IGyroscope, IAccelerometer, ITemperatureSensor
II2cPeripheral, IGyroscope, IAccelerometer, ITemperatureSensor, ISleepAwarePeripheral
{
private event EventHandler<IChangeResult<AngularVelocity3D>> _angularVelocityHandlers = default!;

Check warning on line 19 in Source/Meadow.Foundation.Peripherals/Sensors.Motion.Bmi270/Driver/Bmi270.cs

View workflow job for this annotation

GitHub Actions / build

The field 'Bmi270._angularVelocityHandlers' is assigned but its value is never used
private event EventHandler<IChangeResult<Acceleration3D>> _accelerationHandlers = default!;

Check warning on line 20 in Source/Meadow.Foundation.Peripherals/Sensors.Motion.Bmi270/Driver/Bmi270.cs

View workflow job for this annotation

GitHub Actions / build

The field 'Bmi270._accelerationHandlers' is assigned but its value is never used
private event EventHandler<IChangeResult<Units.Temperature>> _temperatureHandlers = default!;

Check warning on line 21 in Source/Meadow.Foundation.Peripherals/Sensors.Motion.Bmi270/Driver/Bmi270.cs

View workflow job for this annotation

GitHub Actions / build

The field 'Bmi270._temperatureHandlers' is assigned but its value is never used

/// <summary>
/// Event raised when linear acceleration changes
/// </summary>
public event EventHandler<IChangeResult<Acceleration3D>> Acceleration3DUpdated = default!;

/// <summary>
/// Event raised when angular velocity (gyro) changes
/// </summary>
public event EventHandler<IChangeResult<AngularVelocity3D>> AngularVelocity3DUpdated = default!;

/// <summary>
/// Event raised when temperature changes
/// </summary>
public event EventHandler<IChangeResult<Units.Temperature>> TemperatureUpdated = default!;

/// <summary>
/// Current Acceleration 3D
/// </summary>
Expand Down Expand Up @@ -186,17 +171,20 @@
/// <param name="changeResult">The updated sensor data</param>
protected override void RaiseEventsAndNotify(IChangeResult<(Acceleration3D? Acceleration3D, AngularVelocity3D? AngularVelocity3D, Units.Temperature? Temperature)> changeResult)
{
Resolver.Log.Info($"RaiseAndNotify");
ctacke marked this conversation as resolved.
Show resolved Hide resolved

if (changeResult.New.AngularVelocity3D is { } angular)
{
AngularVelocity3DUpdated?.Invoke(this, new ChangeResult<AngularVelocity3D>(angular, changeResult.Old?.AngularVelocity3D));
_angularVelocityHandlers?.Invoke(this, new ChangeResult<AngularVelocity3D>(angular, changeResult.Old?.AngularVelocity3D));
}
if (changeResult.New.Acceleration3D is { } accel)
{
Acceleration3DUpdated?.Invoke(this, new ChangeResult<Acceleration3D>(accel, changeResult.Old?.Acceleration3D));
Resolver.Log.Info($"RaiseAndNotify Acceleration");
_accelerationHandlers?.Invoke(this, new ChangeResult<Acceleration3D>(accel, changeResult.Old?.Acceleration3D));
}
if (changeResult.New.Temperature is { } temp)
{
TemperatureUpdated?.Invoke(this, new ChangeResult<Units.Temperature>(temp, changeResult.Old?.Temperature));
_temperatureHandlers?.Invoke(this, new ChangeResult<Units.Temperature>(temp, changeResult.Old?.Temperature));
}
base.RaiseEventsAndNotify(changeResult);
}
Expand Down Expand Up @@ -336,5 +324,19 @@

async Task<Units.Temperature> ISensor<Units.Temperature>.Read()
=> (await Read()).Temperature!.Value;

/// <inheritdoc/>
public Task BeforeSleep(CancellationToken cancellationToken)
{
SetPowerMode(PowerMode.Suspend);
return Task.CompletedTask;
}

/// <inheritdoc/>
public Task AfterWake(CancellationToken cancellationToken)
{
SetPowerMode(PowerMode.Normal);
return Task.CompletedTask;
}
}
}
Loading