Skip to content

Commit

Permalink
Merge pull request #859 from WildernessLabs/sensor_update_pattern
Browse files Browse the repository at this point in the history
Sensor Update pattern cleanup
  • Loading branch information
adrianstevens authored Dec 11, 2023
2 parents b2245b0 + 374d35c commit dc6978f
Show file tree
Hide file tree
Showing 72 changed files with 46 additions and 613 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public override Task Initialize()
analogLightSensor.Subscribe(consumer);

// classical .NET events can also be used:
analogLightSensor.IlluminanceUpdated += (sender, result) =>
analogLightSensor.Updated += (sender, result) =>
Resolver.Log.Info($"Lux changed: {result.New.Lux:N2} lux, old: {result.Old?.Lux:N2} lux");

//==== One-off reading use case/pattern
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public override Task Initialize()
analogTemperature.Subscribe(consumer);

// classical .NET events can also be used:
analogTemperature.TemperatureUpdated += (sender, result) =>
analogTemperature.Updated += (sender, result) =>
{
Resolver.Log.Info($"Temp Changed, temp: {result.New.Celsius:N2}C, old: {result.Old?.Celsius:N2}C");
};
Expand Down
15 changes: 0 additions & 15 deletions Source/Meadow.Foundation.Core/Sensors/Light/AnalogLightSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ public partial class AnalogLightSensor
/// </summary>
protected IAnalogInputPort AnalogInputPort { get; }

/// <summary>
/// Raised when the value of the reading changes.
/// </summary>
public event EventHandler<IChangeResult<Illuminance>> IlluminanceUpdated = default!;

/// <summary>
/// Illuminance sensor calibration
/// </summary>
Expand Down Expand Up @@ -125,16 +120,6 @@ public override void StopUpdating()
AnalogInputPort.StopUpdating();
}

/// <summary>
/// Notify subscribers of IlluminanceUpdated event handler
/// </summary>
/// <param name="changeResult">Change result with old and new Illuminance</param>
protected override void RaiseEventsAndNotify(IChangeResult<Illuminance> changeResult)
{
IlluminanceUpdated?.Invoke(this, changeResult);
base.RaiseEventsAndNotify(changeResult);
}

/// <summary>
/// Converts a voltage value to a level in centimeters, based on the current
/// calibration values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ namespace Meadow.Foundation.Sensors.Temperature
/// </remarks>
public partial class AnalogTemperature : SamplingSensorBase<Units.Temperature>, ITemperatureSensor, IDisposable
{
/// <summary>
/// Raised when the value of the reading changes.
/// </summary>
public event EventHandler<IChangeResult<Units.Temperature>> TemperatureUpdated = default!;

///<Summary>
/// AnalogInputPort connected to temperature sensor
///</Summary>
Expand Down Expand Up @@ -252,16 +247,6 @@ public override void StopUpdating()
}
}

/// <summary>
/// Method to notify subscribers to TemperatureUpdated event handler
/// </summary>
/// <param name="changeResult"></param>
protected override void RaiseEventsAndNotify(IChangeResult<Units.Temperature> changeResult)
{
TemperatureUpdated?.Invoke(this, changeResult);
base.RaiseEventsAndNotify(changeResult);
}

/// <summary>
/// Converts voltage to Temperature
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ protected override void RaiseEventsAndNotify(IChangeResult<(Temperature? Interna
{
FanSpeedUpdated?.Invoke(this, new ChangeResult<AngularVelocity>(fanSpeed, changeResult.Old?.FanSpeed));
}

base.RaiseEventsAndNotify(changeResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ namespace Meadow.Foundation.Sensors.Atmospheric
/// </summary>
public partial class Bh1900Nux : ByteCommsSensorBase<Units.Temperature>, ITemperatureSensor, II2cPeripheral
{
/// <summary>
/// Raised when the temperature value changes
/// </summary>
public event EventHandler<IChangeResult<Units.Temperature>> TemperatureUpdated = default!;

/// <summary>
/// The current temperature
/// </summary>
Expand Down Expand Up @@ -223,16 +218,5 @@ private Units.Temperature RegisterToTemp(Memory<byte> data)

return Task.FromResult(RegisterToTemp(ReadBuffer));
}

/// <summary>
/// Raise events for subscribers and notify of value changes
/// </summary>
/// <param name="changeResult">The updated sensor data</param>
protected override void RaiseEventsAndNotify(IChangeResult<Units.Temperature> changeResult)
{
TemperatureUpdated?.Invoke(this, changeResult);

base.RaiseEventsAndNotify(changeResult);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ protected override void RaiseEventsAndNotify(IChangeResult<(Concentration? Co2,
{
VocUpdated?.Invoke(this, new ChangeResult<Concentration>(voc, changeResult.Old?.Voc));
}

base.RaiseEventsAndNotify(changeResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ public Mpl3115a2(II2cBus i2cBus, byte address = (byte)Addresses.Default)
/// <param name="changeResult"></param>
protected override void RaiseEventsAndNotify(IChangeResult<(Units.Temperature? Temperature, Pressure? Pressure)> changeResult)
{
//Updated?.Invoke(this, changeResult);
if (changeResult.New.Temperature is { } temp)
{
_temperatureHandlers?.Invoke(this, new ChangeResult<Units.Temperature>(temp, changeResult.Old?.Temperature));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ namespace Meadow.Foundation.Sensors.Atmospheric
/// </summary>
public partial class Sgp40 : ByteCommsSensorBase<int>, II2cPeripheral
{
/// <summary>
/// </summary>
public event EventHandler<ChangeResult<int>> VocIndexUpdated = default!;

/// <summary>
/// The VOC Index, from the last reading
/// </summary>
Expand Down Expand Up @@ -98,17 +94,6 @@ protected override Task<int> ReadSensor()
return Task.FromResult(data[0] << 8 | data[1]);
}

/// <summary>
/// Inheritance-safe way to raise events and notify observers.
/// </summary>
/// <param name="changeResult"></param>
protected override void RaiseEventsAndNotify(IChangeResult<int> changeResult)
{
VocIndexUpdated?.Invoke(this, new ChangeResult<int>(VocIndex, changeResult.Old));

base.RaiseEventsAndNotify(changeResult);
}

/// <summary>
/// This command turns the hotplate off and stops the measurement. Subsequently, the sensor enters idle mode.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ protected override void RaiseEventsAndNotify(IChangeResult<(Units.Temperature? T
{
_humidityHandlers?.Invoke(this, new ChangeResult<Units.RelativeHumidity>(humidity, changeResult.Old?.Humidity));
}

base.RaiseEventsAndNotify(changeResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ namespace Meadow.Foundation.Sensors.Distance
/// </summary>
public class A02yyuw : PollingSensorBase<Length>, IRangeFinder, ISleepAwarePeripheral, IDisposable
{
/// <summary>
/// Raised when the value of the reading changes
/// </summary>
public event EventHandler<IChangeResult<Length>> DistanceUpdated = default!;

/// <summary>
/// Distance from sensor to object
/// </summary>
Expand Down Expand Up @@ -95,16 +90,6 @@ protected override Task<Length> ReadSensor()
return ReadSingleValue();
}

/// <summary>
/// Raise distance change event for subscribers
/// </summary>
/// <param name="changeResult"></param>
protected override void RaiseEventsAndNotify(IChangeResult<Length> changeResult)
{
DistanceUpdated?.Invoke(this, changeResult);
base.RaiseEventsAndNotify(changeResult);
}

/// <summary>
/// Start updating distances
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override Task Initialize()
);
a02yyuw.Subscribe(consumer);

a02yyuw.DistanceUpdated += A02yyuw_DistanceUpdated;
a02yyuw.Updated += A02yyuw_DistanceUpdated;

return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public override Task Initialize()
);
a02yyuw.Subscribe(consumer);

a02yyuw.DistanceUpdated += A02yyuw_DistanceUpdated;
a02yyuw.Updated += A02yyuw_DistanceUpdated;

return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ namespace Meadow.Foundation.Sensors.Distance
/// </summary>
public class Hcsr04 : SamplingSensorBase<Length>, IRangeFinder, IDisposable
{
/// <summary>
/// Raised when an received a rebound trigger signal
/// </summary>
public event EventHandler<IChangeResult<Length>> DistanceUpdated = default!;

/// <summary>
/// Returns current distance
/// </summary>
Expand Down Expand Up @@ -143,16 +138,6 @@ protected override Task<Length> ReadSensor()
throw new NotImplementedException();
}

/// <summary>
/// Raise events for subscribers and notify of value changes
/// </summary>
/// <param name="changeResult">The updated sensor data</param>
protected override void RaiseEventsAndNotify(IChangeResult<Length> changeResult)
{
DistanceUpdated?.Invoke(this, changeResult);
base.RaiseEventsAndNotify(changeResult);
}

/// <summary>
/// Starts continuously sampling the sensor
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override Task Initialize()
hCSR04 = new Hcsr04(
triggerPin: Device.Pins.D05,
echoPin: Device.Pins.D06);
hCSR04.DistanceUpdated += HCSR04_DistanceUpdated;
hCSR04.Updated += HCSR04_DistanceUpdated;

return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override Task Initialize()
hCSR04 = new Hcsr04(
triggerPin: Device.Pins.D05,
echoPin: Device.Pins.D06);
hCSR04.DistanceUpdated += HCSR04_DistanceUpdated;
hCSR04.Updated += HCSR04_DistanceUpdated;

return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ namespace Meadow.Foundation.Sensors.Distance
/// </summary>
public partial class MaxBotix : ByteCommsSensorBase<Length>, IRangeFinder, IDisposable
{
/// <summary>
/// Raised when the value of the reading changes
/// </summary>
public event EventHandler<IChangeResult<Length>> DistanceUpdated = default!;

/// <summary>
/// Distance from sensor to object
/// </summary>
Expand Down Expand Up @@ -64,16 +59,6 @@ protected override async Task<Length> ReadSensor()
};
}

/// <summary>
/// Raise distance change event for subscribers
/// </summary>
/// <param name="changeResult"></param>
protected override void RaiseEventsAndNotify(IChangeResult<Length> changeResult)
{
DistanceUpdated?.Invoke(this, changeResult);
base.RaiseEventsAndNotify(changeResult);
}

/// <summary>
/// Start updating distances
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public override Task Initialize()
);
maxBotix.Subscribe(consumer);

maxBotix.DistanceUpdated += MaxBotix_DistanceUpdated;
maxBotix.Updated += MaxBotix_DistanceUpdated;

return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ namespace Meadow.Foundation.Sensors.Distance
/// </summary>
public class Me007ys : PollingSensorBase<Length>, IRangeFinder, ISleepAwarePeripheral, IDisposable
{
/// <summary>
/// Raised when the value of the reading changes
/// </summary>
public event EventHandler<IChangeResult<Length>> DistanceUpdated = default!;

/// <summary>
/// Distance from sensor to object
/// </summary>
Expand Down Expand Up @@ -95,16 +90,6 @@ protected override Task<Length> ReadSensor()
return ReadSingleValue();
}

/// <summary>
/// Raise distance change event for subscribers
/// </summary>
/// <param name="changeResult"></param>
protected override void RaiseEventsAndNotify(IChangeResult<Length> changeResult)
{
DistanceUpdated?.Invoke(this, changeResult);
base.RaiseEventsAndNotify(changeResult);
}

/// <summary>
/// Start updating distances
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override Task Initialize()
);
me007ys.Subscribe(consumer);

me007ys.DistanceUpdated += Me007y_DistanceUpdated;
me007ys.Updated += Me007y_DistanceUpdated;

return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public override Task Initialize()
);
me007ys.Subscribe(consumer);

me007ys.DistanceUpdated += Me007y_DistanceUpdated;
me007ys.Updated += Me007y_DistanceUpdated;

return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override Task Initialize()
var i2cBus = Device.CreateI2cBus(I2cBusSpeed.FastPlus);
sensor = new Vl53l0x(i2cBus);

sensor.DistanceUpdated += Sensor_Updated;
sensor.Updated += Sensor_Updated;

return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ namespace Meadow.Foundation.Sensors.Distance
/// </summary>
public partial class Vl53l0x : ByteCommsSensorBase<Length>, IRangeFinder, II2cPeripheral
{
/// <summary>
/// Distance updated event
/// </summary>
public event EventHandler<IChangeResult<Length>> DistanceUpdated = default!;

/// <summary>
/// Is the hardware shutdown / off
/// </summary>
Expand Down Expand Up @@ -85,16 +80,6 @@ public Vl53l0x(II2cBus i2cBus, IPin? shutdownPin, byte address = (byte)Addresses
Initialize().Wait();
}

/// <summary>
/// Raise distance change event and notify subscribers
/// </summary>
/// <param name="changeResult"></param>
protected override void RaiseEventsAndNotify(IChangeResult<Length> changeResult)
{
DistanceUpdated?.Invoke(this, changeResult);
base.RaiseEventsAndNotify(changeResult);
}

/// <summary>
/// Initializes the VL53L0X
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override Task Initialize()
var i2cBus = Device.CreateI2cBus(I2cBusSpeed.FastPlus);
sensor = new Vl53l0x(i2cBus);

sensor.DistanceUpdated += Sensor_Updated;
sensor.Updated += Sensor_Updated;

return Task.CompletedTask;
}
Expand Down
Loading

0 comments on commit dc6978f

Please sign in to comment.