未验证 提交 8b42ce21 编写于 作者: A Andrew Phelps 提交者: GitHub

add sample for si7021 temperature read over i2c (#86)

* add sample for si7021 temperature read over i2c

* add Si7021 device class

* remove ReadTempFromSi7021.cs

* add device sample for Si7021 and humidity read command

* remove reference to README in Si7021 csproj file
上级 10bb59f0
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Device.I2c;
namespace Iot.Device
{
public class Si7021 : IDisposable
{
const byte si7021_cmd_read_temperature = 0xE3;
const byte si7021_cmd_read_humidity = 0xE5;
private I2cDevice _i2cDevice;
public Si7021(I2cDevice i2cDevice)
{
_i2cDevice = i2cDevice;
}
public void Dispose()
{
}
public double ReadTempFahrenheit()
{
double temp_celcius = ReadTempCelcius();
double temp_fahrenheit = (ReadTempCelcius() * (9 / 5)) + 32;
return temp_fahrenheit;
}
public double ReadTempCelcius()
{
byte[] buffer = new byte[2];
// Send temperature command, read back two bytes
_i2cDevice.WriteByte(si7021_cmd_read_temperature);
_i2cDevice.Read(buffer.AsSpan());
// Calculate temperature
double temp_code = buffer[0] << 8 | buffer[1];
double temp_celcius = (((175.72 * temp_code) / 65536) - 46.85);
return temp_celcius;
}
public double ReadHumidity()
{
byte[] buffer = new byte[2];
// Send humidity read command, read back two bytes
_i2cDevice.WriteByte(si7021_cmd_read_humidity);
_i2cDevice.Read(buffer.AsSpan());
// Calculate humidity
double rh_code = buffer[0] << 8 | buffer[1];
double humidity = ((125 * rh_code) / 65536) - 6;
return humidity;
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<EnableDefaultItems>false</EnableDefaultItems> <!--Disabling default items so samples source won't get build by the main library-->
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Device.Gpio" Version="0.1.0-prerelease*" />
<Compile Include="Si7021.cs" />
</ItemGroup>
</Project>
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Device.I2c;
using System.Device.I2c.Drivers;
namespace Iot.Device.Samples
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Si7021!");
const int si7021Address = 0x40;
const int busId = 1;
var i2cSettings = new I2cConnectionSettings(busId, si7021Address);
var i2cDevice = new Windows10I2cDevice(i2cSettings);
var i2CSi7021 = new Si7021(i2cDevice);
using (i2CSi7021)
{
Console.WriteLine($"Temperature in fahrenheit: {i2CSi7021.ReadTempFahrenheit()}");
Console.WriteLine($"Temperature in celcius: {i2CSi7021.ReadTempCelcius()}");
Console.WriteLine($"Relative humidity is: {i2CSi7021.ReadHumidity()}%");
}
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Device.Gpio" Version="0.1.0-prerelease*" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Si7021.csproj" />
</ItemGroup>
</Project>
......@@ -16,7 +16,7 @@ namespace DeviceApiTester.Commands.I2c
[Option('b', "bus-id", HelpText = "The bus id the I2C device to connect to", Required = true)]
public int BusId { get; set; }
[Option('a', "device-address", HelpText = "The device address for the connection to the I2C device", Required = true)]
[Option('a', "device-address", HelpText = "The device address (in decimal format) for the connection to the I2C device", Required = true)]
public int DeviceAddress { get; set; }
protected I2cDevice CreateI2cDevice(I2cConnectionSettings connectionSettings)
......
......@@ -30,8 +30,7 @@ namespace DeviceApiTester.Commands.I2c
// Read bytes of data
var buffer = new byte[ByteCount];
i2c.Read(buffer.AsSpan());
Console.WriteLine($"Bytes read:{Environment.NewLine}{HexStringUtilities.FormatByteData(buffer)}");
Console.WriteLine($"Bytes read:{Environment.NewLine}{BitConverter.ToString(buffer)}");
}
return 0;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册