diff --git a/src/devices/Si7021/Si7021.cs b/src/devices/Si7021/Si7021.cs new file mode 100644 index 0000000000000000000000000000000000000000..c75c22dfd6cccc7320273bf917e0cb63fff30181 --- /dev/null +++ b/src/devices/Si7021/Si7021.cs @@ -0,0 +1,63 @@ +// 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; + } + } +} diff --git a/src/devices/Si7021/Si7021.csproj b/src/devices/Si7021/Si7021.csproj new file mode 100644 index 0000000000000000000000000000000000000000..12d0b40d2f91c049982554e76ad228a08910ebd8 --- /dev/null +++ b/src/devices/Si7021/Si7021.csproj @@ -0,0 +1,13 @@ + + + + netcoreapp2.1 + false + + + + + + + + diff --git a/src/devices/Si7021/samples/Si7021.sample.cs b/src/devices/Si7021/samples/Si7021.sample.cs new file mode 100644 index 0000000000000000000000000000000000000000..88a0414bcc6e6b53eb0478ef830d06477b063fe4 --- /dev/null +++ b/src/devices/Si7021/samples/Si7021.sample.cs @@ -0,0 +1,34 @@ +// 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()}%"); + } + } + } +} diff --git a/src/devices/Si7021/samples/Si7021.sample.csproj b/src/devices/Si7021/samples/Si7021.sample.csproj new file mode 100644 index 0000000000000000000000000000000000000000..eb70e8cd2d2505748403ecabcf82d1e2f4c9b8ad --- /dev/null +++ b/src/devices/Si7021/samples/Si7021.sample.csproj @@ -0,0 +1,17 @@ + + + + Exe + netcoreapp2.1 + latest + + + + + + + + + + + diff --git a/tools/DevicesApiTester/Commands/I2c/I2cCommand.cs b/tools/DevicesApiTester/Commands/I2c/I2cCommand.cs index 47ae82d4d824e9e5bcdb43be7ea46a20d3755bd5..072d2ac153d9129916c0fbf8f10b2b7f2d98da48 100644 --- a/tools/DevicesApiTester/Commands/I2c/I2cCommand.cs +++ b/tools/DevicesApiTester/Commands/I2c/I2cCommand.cs @@ -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) diff --git a/tools/DevicesApiTester/Commands/I2c/ReadBytes.cs b/tools/DevicesApiTester/Commands/I2c/ReadBytes.cs index ad74021184334d8d4569a5c5894024f95aef878f..b0227bf95cea6fb93eafb49723ab8074548e793c 100644 --- a/tools/DevicesApiTester/Commands/I2c/ReadBytes.cs +++ b/tools/DevicesApiTester/Commands/I2c/ReadBytes.cs @@ -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;