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

Fix compensation data assignment after refactor #953

Merged
merged 5 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ partial class Bmx280
/// <summary>
/// Reads the sensor compensation data
/// </summary>
internal static void ReadCompensationData(IByteCommunications bmx280Comms, Memory<byte> readBuffer, CompensationData compensationData)
internal static void ReadCompensationData(IByteCommunications bmx280Comms, Memory<byte> readBuffer, ref CompensationData compensationData)
{
// read the temperature and pressure data into the internal read buffer
bmx280Comms.ReadRegister(0x88, readBuffer.Span[0..24]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public Bme280(ISpiBus spiBus, IDigitalOutputPort chipSelectPort)
/// </summary>
protected void Initialize()
{
Bmx280.ReadCompensationData(bme280Comms, readBuffer, compensationData);
Bmx280.ReadCompensationData(bme280Comms, readBuffer, ref compensationData);

configuration.Mode = Modes.Sleep;
configuration.Filter = FilterCoefficient.Off;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public Bmp280(ISpiBus spiBus, IDigitalOutputPort chipSelectPort)
/// </summary>
protected void Initialize()
{
Bmx280.ReadCompensationData(bmp280Comms, readBuffer, compensationData);
Bmx280.ReadCompensationData(bmp280Comms, readBuffer, ref compensationData);

configuration.Mode = Modes.Sleep;
configuration.Filter = FilterCoefficient.Off;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
<TargetFramework>netstandard2.1</TargetFramework>
<OutputType>Library</OutputType>
<AssemblyName>App</AssemblyName>
</PropertyGroup>
<PropertyGroup>
<LangVersion>8.0</LangVersion>
<LangVersion>10</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\..\..\Meadow.Core\Source\implementations\f7\Meadow.F7\Meadow.F7.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,90 +1,103 @@
using Meadow;
using Meadow.Devices;
using Meadow.Foundation.Sensors.Atmospheric;
using Meadow.Hardware;
using System;
using System.Threading.Tasks;

namespace Sensors.Atmospheric.BME280_Sample
namespace Sensors.Atmospheric.BME280_Sample;

public class CoreComputeApp : MeadowApp<F7CoreComputeV2>
{
public class MeadowApp : App<F7FeatherV2>
{
//<!=SNIP=>
protected override IPin SpiChipSelect => Device.Pins.D00;
}

Bme280 sensor;
public class FeatherApp : MeadowApp<F7FeatherV2>
{
protected override IPin SpiChipSelect => Device.Pins.D00;
}

public override Task Initialize()
{
Resolver.Log.Info("Initializing...");
public abstract class MeadowApp<T> : App<T>
where T : F7MicroBase
{
//<!=SNIP=>

//CreateSpiSensor();
CreateI2CSensor();
protected Bme280 sensor;

var consumer = Bme280.CreateObserver(
handler: result =>
{
Resolver.Log.Info($"Observer: Temp changed by threshold; new temp: {result.New.Temperature?.Celsius:N2}C, old: {result.Old?.Temperature?.Celsius:N2}C");
},
filter: result =>
{
if (result.Old?.Temperature is { } oldTemp &&
result.Old?.Humidity is { } oldHumidity &&
result.New.Temperature is { } newTemp &&
result.New.Humidity is { } newHumidity)
{
return
(newTemp - oldTemp).Abs().Celsius > 0.5 &&
(newHumidity - oldHumidity).Percent > 0.05;
}
return false;
}
);
sensor.Subscribe(consumer);
protected virtual IPin SpiChipSelect { get; }

public override Task Initialize()
{
Resolver.Log.Info("Initializing...");

//CreateSpiSensor();
CreateI2CSensor();

sensor.Updated += (sender, result) =>
var consumer = Bme280.CreateObserver(
handler: result =>
{
try
{
Resolver.Log.Info($" Temperature: {result.New.Temperature?.Celsius:N2}C");
Resolver.Log.Info($" Relative Humidity: {result.New.Humidity:N2}%");
Resolver.Log.Info($" Pressure: {result.New.Pressure?.Millibar:N2}mbar ({result.New.Pressure?.Pascal:N2}Pa)");
}
catch (Exception ex)
Resolver.Log.Info($"Observer: Temp changed by threshold; new temp: {result.New.Temperature?.Celsius:N1}C, old: {result.Old?.Temperature?.Celsius:N1}C");
},
filter: result =>
{
if (result.Old?.Temperature is { } oldTemp &&
result.Old?.Humidity is { } oldHumidity &&
result.New.Temperature is { } newTemp &&
result.New.Humidity is { } newHumidity)
{
Resolver.Log.Error(ex, "Error reading sensor");
return
(newTemp - oldTemp).Abs().Celsius > 0.5 &&
(newHumidity - oldHumidity).Percent > 0.05;
}
};
return false;
}
);
sensor.Subscribe(consumer);

return Task.CompletedTask;
}

public override async Task Run()
sensor.Updated += (sender, result) =>
{
var conditions = await sensor.Read();
Resolver.Log.Info("Initial Readings:");
Resolver.Log.Info($" Temperature: {conditions.Temperature?.Celsius:N2}C");
Resolver.Log.Info($" Pressure: {conditions.Pressure?.Bar:N2}hPa");
Resolver.Log.Info($" Relative Humidity: {conditions.Humidity?.Percent:N2}%");
try
{
Resolver.Log.Info($" Temperature: {result.New.Temperature?.Celsius:N1}C");
Resolver.Log.Info($" Relative Humidity: {result.New.Humidity:N1}%");
Resolver.Log.Info($" Pressure: {result.New.Pressure?.Millibar:N1}mbar ({result.New.Pressure?.Pascal:N1}Pa)");
}
catch (Exception ex)
{
Resolver.Log.Error(ex, "Error reading sensor");
}
};

sensor.StartUpdating(TimeSpan.FromSeconds(1));
}
return Task.CompletedTask;
}

void CreateSpiSensor()
{
Resolver.Log.Info("Create BME280 sensor with SPI...");
public override async Task Run()
{
var conditions = await sensor.Read();
Resolver.Log.Info("Initial Readings:");
Resolver.Log.Info($" Temperature: {conditions.Temperature?.Celsius:N1}C");
Resolver.Log.Info($" Pressure: {conditions.Pressure?.Bar:N1}hPa");
Resolver.Log.Info($" Relative Humidity: {conditions.Humidity?.Percent:N1}%");

var spi = Device.CreateSpiBus();
sensor = new Bme280(spi, Device.Pins.D00.CreateDigitalOutputPort());
}
sensor.StartUpdating(TimeSpan.FromSeconds(1));
}

void CreateI2CSensor()
{
Resolver.Log.Info("Create BME280 sensor with I2C...");
private void CreateSpiSensor()
{
Resolver.Log.Info("Create BME280 sensor with SPI...");

var spi = Device.CreateSpiBus();
sensor = new Bme280(spi, SpiChipSelect.CreateDigitalOutputPort());
}

var i2c = Device.CreateI2cBus();
sensor = new Bme280(i2c, (byte)Bmx280.Addresses.Default); // SDA pulled up
private void CreateI2CSensor()
{
Resolver.Log.Info("Create BME280 sensor with I2C...");

}
var i2c = Device.CreateI2cBus();
sensor = new Bme280(i2c, (byte)Bmx280.Addresses.Default); // SDA pulled up

//<!=SNOP=>
}

//<!=SNOP=>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Meadow.Sdk/1.1.0">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<OutputType>Library</OutputType>
<AssemblyName>App</AssemblyName>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\..\..\Meadow.Core\source\implementations\f7\Meadow.F7\Meadow.F7.csproj" />
<ProjectReference Include="..\..\Driver\Sensors.Atmospheric.Bmx280.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
Imports Meadow
Imports Meadow.Devices
Imports Meadow.Foundation.Sensors.Atmospheric

Public Class FeatherV2App
Inherits MeadowApp(Of F7FeatherV2)

End Class

Public Class CoreComputeApp
Inherits MeadowApp(Of F7CoreComputeV2)

End Class

Public Class MeadowApp(Of T As F7MicroBase)
Inherits App(Of T)

Private sensor As Bme280

Public Overrides Function Initialize() As Task
Resolver.Log.Info("Initializing...")

Dim bus = Device.CreateI2cBus()
sensor = New Bme280(bus)

Dim consumer = Bme280.CreateObserver(
handler:=Sub(result)
Resolver.Log.Info($"Observer: Temp changed by threshold; new temp: {result.New.Temperature?.Celsius:N1}C, old: {result.Old?.Temperature?.Celsius:N1}C")
End Sub,
filter:=Function(result)
If result.Old?.Temperature IsNot Nothing AndAlso
result.Old?.Humidity IsNot Nothing AndAlso
result.New.Temperature IsNot Nothing AndAlso
result.New.Humidity IsNot Nothing Then

Dim oldTemp = result.Old.Value.Temperature.Value.Celsius
Dim oldHumidity = result.Old.Value.Humidity.Value.Percent
Dim newTemp = result.New.Temperature.Value.Celsius
Dim newHumidity = result.New.Humidity.Value.Percent

Return Math.Abs(newTemp - oldTemp) > 0.5 AndAlso
(newHumidity - oldHumidity) / oldHumidity > 0.05
End If
Return False
End Function)

Return Task.CompletedTask
End Function

Public Overrides Async Function Run() As Task
Dim conditions = Await sensor.Read()
Resolver.Log.Info("Initial Readings:")
Resolver.Log.Info($" Temperature: {conditions.Temperature?.Celsius:N2}C")
Resolver.Log.Info($" Pressure: {conditions.Pressure?.Bar:N2}hPa")
Resolver.Log.Info($" Relative Humidity: {conditions.Humidity?.Percent:N2}%")

sensor.StartUpdating(TimeSpan.FromSeconds(1))
End Function
End Class
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override Task Initialize()
var consumer = Bmp280.CreateObserver(
handler: result =>
{
Resolver.Log.Info($"Observer: Temp changed by threshold; new temp: {result.New.Temperature?.Celsius:N2}C, old: {result.Old?.Temperature?.Celsius:N2}C");
Resolver.Log.Info($"Observer: Temp changed by threshold; new temp: {result.New.Temperature?.Celsius:N1}C, old: {result.Old?.Temperature?.Celsius:N1}C");
},
filter: result =>
{
Expand All @@ -40,8 +40,8 @@ public override Task Initialize()
{
try
{
Resolver.Log.Info($" Temperature: {result.New.Temperature?.Celsius:N2}C");
Resolver.Log.Info($" Pressure: {result.New.Pressure?.Millibar:N2}mbar ({result.New.Pressure?.Pascal:N2}Pa)");
Resolver.Log.Info($" Temperature: {result.New.Temperature?.Celsius:N1}C");
Resolver.Log.Info($" Pressure: {result.New.Pressure?.Millibar:N1}mbar ({result.New.Pressure?.Pascal:N1}Pa)");
}
catch (Exception ex)
{
Expand All @@ -56,8 +56,8 @@ public override async Task Run()
{
var conditions = await sensor.Read();
Resolver.Log.Info("Initial Readings:");
Resolver.Log.Info($" Temperature: {conditions.Temperature?.Celsius:N2}C");
Resolver.Log.Info($" Pressure: {conditions.Pressure?.Bar:N2}hPa");
Resolver.Log.Info($" Temperature: {conditions.Temperature?.Celsius:N1}C");
Resolver.Log.Info($" Pressure: {conditions.Pressure?.Bar:N1}hPa");

sensor.StartUpdating(TimeSpan.FromSeconds(1));
}
Expand Down
7 changes: 7 additions & 0 deletions Source/Meadow.Foundation.sln
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AsciiConsole_Sample", "Mead
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BarChart_Sample", "Meadow.Foundation.Libraries_and_Frameworks\Graphics.MicroLayout\Samples\BarChart_Sample\BarChart_Sample.csproj", "{DEE00EAF-617D-4768-A2CB-A04729ADF426}"
EndProject
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "Bme280_VB_Sample", "Meadow.Foundation.Peripherals\Sensors.Atmospheric.Bmx280\Samples\Bme280_VB_Sample\Bme280_VB_Sample.vbproj", "{933A1748-3481-497E-94BC-8F93D5A6E9BA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -3627,6 +3629,10 @@ Global
{DEE00EAF-617D-4768-A2CB-A04729ADF426}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DEE00EAF-617D-4768-A2CB-A04729ADF426}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DEE00EAF-617D-4768-A2CB-A04729ADF426}.Release|Any CPU.Build.0 = Release|Any CPU
{933A1748-3481-497E-94BC-8F93D5A6E9BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{933A1748-3481-497E-94BC-8F93D5A6E9BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{933A1748-3481-497E-94BC-8F93D5A6E9BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{933A1748-3481-497E-94BC-8F93D5A6E9BA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -4376,6 +4382,7 @@ Global
{1A156008-49C6-4ADD-BED9-1CF98D8FB076} = {21C511DD-43DD-486E-86D7-18D8F6810ACD}
{85383653-A9EC-4023-BBA1-419A5CC30A78} = {40687DAC-F3EF-4F70-850A-0429B9586648}
{DEE00EAF-617D-4768-A2CB-A04729ADF426} = {E762EEFF-E646-4517-BAE6-4E23A34854DC}
{933A1748-3481-497E-94BC-8F93D5A6E9BA} = {21C511DD-43DD-486E-86D7-18D8F6810ACD}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AF7CA16F-8C38-4546-87A2-5DAAF58A1520}
Expand Down
Loading