diff --git a/Source/Meadow.Foundation.Peripherals/Sensors.Environmental.Scd4x/Driver/Scd4xBase.cs b/Source/Meadow.Foundation.Peripherals/Sensors.Environmental.Scd4x/Driver/Scd4xBase.cs index 64d10ee814..b79bb0a8e2 100644 --- a/Source/Meadow.Foundation.Peripherals/Sensors.Environmental.Scd4x/Driver/Scd4xBase.cs +++ b/Source/Meadow.Foundation.Peripherals/Sensors.Environmental.Scd4x/Driver/Scd4xBase.cs @@ -13,10 +13,8 @@ namespace Meadow.Foundation.Sensors.Environmental /// Base class for SCD4x series of C02 sensors /// 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> _temperatureHandlers = default!; private event EventHandler> _humidityHandlers = default!; @@ -60,6 +58,13 @@ event EventHandler> ISamplingSensor. /// public byte DefaultI2cAddress => (byte)Addresses.Default; + /// + /// I2C Communication bus used to communicate with the peripheral + /// + protected readonly II2cCommunications i2cComms; + + private byte[] readBuffer; + /// /// Create a new Scd4xBase object /// @@ -70,8 +75,10 @@ event EventHandler> ISamplingSensor. /// The I2C bus /// The I2C address 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(); } @@ -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]; @@ -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) { @@ -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); } /// @@ -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); @@ -240,7 +247,7 @@ private Units.Temperature CalcTemperature(byte valueHigh, byte valueLow) /// Raise change events for subscribers /// /// The change result with the current sensor data - 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) {