Skip to content

Commit

Permalink
Merge pull request #906 from WildernessLabs/scd4x_events
Browse files Browse the repository at this point in the history
Fix events
  • Loading branch information
jorgedevs authored Feb 20, 2024
2 parents c8bca57 + caca3af commit 72bf8d3
Showing 1 changed file with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ namespace Meadow.Foundation.Sensors.Environmental
/// Base class for SCD4x series of C02 sensors
/// </summary>
public abstract partial class Scd4xBase
: ByteCommsSensorBase<(Concentration? Concentration,
Units.Temperature? Temperature,
RelativeHumidity? Humidity)>,
ITemperatureSensor, IHumiditySensor, ICO2ConcentrationSensor, II2cPeripheral
: PollingSensorBase<(Concentration? Concentration, Units.Temperature? Temperature, RelativeHumidity? Humidity)>,
ITemperatureSensor, IHumiditySensor, ICO2ConcentrationSensor, II2cPeripheral
{
private event EventHandler<IChangeResult<Units.Temperature>> _temperatureHandlers = default!;
private event EventHandler<IChangeResult<RelativeHumidity>> _humidityHandlers = default!;
Expand Down Expand Up @@ -60,6 +58,13 @@ event EventHandler<IChangeResult<Concentration>> ISamplingSensor<Concentration>.
/// </summary>
public byte DefaultI2cAddress => (byte)Addresses.Default;

/// <summary>
/// I2C Communication bus used to communicate with the peripheral
/// </summary>
protected readonly II2cCommunications i2cComms;

private byte[] readBuffer;

/// <summary>
/// Create a new Scd4xBase object
/// </summary>
Expand All @@ -70,8 +75,10 @@ event EventHandler<IChangeResult<Concentration>> ISamplingSensor<Concentration>.
/// <param name="i2cBus">The I2C bus</param>
/// <param name="address">The I2C address</param>
public Scd4xBase(II2cBus i2cBus, byte address = (byte)Addresses.Default)
: base(i2cBus, address, readBufferSize: 9, writeBufferSize: 9)
{
readBuffer = new byte[9];

i2cComms = new I2cCommunications(i2cBus, address, 9);
StopPeriodicUpdates().Wait();
}

Expand Down Expand Up @@ -119,7 +126,7 @@ public byte[] GetSerialNumber()
Thread.Sleep(1);

var data = new byte[9];
BusComms.Read(data);
i2cComms.Read(data);

var ret = new byte[6];

Expand All @@ -143,7 +150,7 @@ protected bool IsDataReady()
SendCommand(Commands.GetDataReadyStatus);
Thread.Sleep(1);
var data = new byte[3];
BusComms.Read(data);
i2cComms.Read(data);

if (data[1] == 0 && (data[0] & 0x07) == 0)
{
Expand Down Expand Up @@ -197,7 +204,7 @@ private void SendCommand(Commands command)
data[0] = (byte)((ushort)command >> 8);
data[1] = (byte)(ushort)command;

BusComms.Write(data);
i2cComms.Write(data);
}

/// <summary>
Expand All @@ -215,14 +222,14 @@ private void SendCommand(Commands command)

SendCommand(Commands.ReadMeasurement);
Thread.Sleep(1);
BusComms.Read(ReadBuffer.Span[0..9]);
i2cComms.Read(readBuffer);

int value = ReadBuffer.Span[0] << 8 | ReadBuffer.Span[1];
conditions.Concentration = new Concentration(value, Units.Concentration.UnitType.PartsPerMillion);
int value = readBuffer[0] << 8 | readBuffer[1];
conditions.Concentration = new Concentration(value, Concentration.UnitType.PartsPerMillion);

conditions.Temperature = CalcTemperature(ReadBuffer.Span[3], ReadBuffer.Span[4]);
conditions.Temperature = CalcTemperature(readBuffer[3], readBuffer[4]);

value = ReadBuffer.Span[6] << 8 | ReadBuffer.Span[8];
value = readBuffer[6] << 8 | readBuffer[8];
double humidiy = 100 * value / 65536.0;
conditions.Humidity = new RelativeHumidity(humidiy, RelativeHumidity.UnitType.Percent);

Expand All @@ -240,7 +247,7 @@ private Units.Temperature CalcTemperature(byte valueHigh, byte valueLow)
/// Raise change events for subscribers
/// </summary>
/// <param name="changeResult">The change result with the current sensor data</param>
protected void RaiseChangedAndNotify(IChangeResult<(Concentration? Concentration, Units.Temperature? Temperature, RelativeHumidity? Humidity)> changeResult)
protected override void RaiseEventsAndNotify(IChangeResult<(Concentration? Concentration, Units.Temperature? Temperature, RelativeHumidity? Humidity)> changeResult)
{
if (changeResult.New.Temperature is { } temperature)
{
Expand Down

0 comments on commit 72bf8d3

Please sign in to comment.