未验证 提交 a0231d43 编写于 作者: R Rich Lander 提交者: GitHub

Use records with Bh1745 (#1258)

* Add records to Bh1745:

* Add tracking issue for /// docs

* Move record to primary class file

* Define records first

* Simplify sleep

* Add extension file

* Add object initializers

* Remove space

* Change extension method name

* Move IsExternalInit

* Update handling of IsExternalInit
上级 981d33f0
......@@ -13,11 +13,12 @@
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage)</TargetsForTfmSpecificContentInPackage>
<!--Disabling default items so samples source won't get build by the main library-->
</PropertyGroup>
<ItemGroup>
<_ExcludeFromCompile Include="$(DeviceRoot)**/samples/**/*.cs" />
<_ExcludeFromCompile Include="$(DeviceRoot)**/tests/**/*.cs" />
<_ExcludeFromCompile Include="$(DeviceRoot)**/obj/**/*.cs" />
<_ExcludeFromCompile Include="$(DeviceRoot)/Common/System/Runtime/CompilerServices/IsExternalInit.cs" Condition="'$(TargetFramework)' != 'netcoreapp2.1'" />
<Compile Include="$(DeviceRoot)**/*.cs" Exclude="@(_ExcludeFromCompile)" />
</ItemGroup>
......
......@@ -9,6 +9,13 @@ using System.Linq;
namespace Iot.Device.Bh1745
{
// Tracking https://github.com/dotnet/roslyn/issues/44571
#pragma warning disable CS1591
/// <summary>
/// Channel compensation multipliers used to compensate the 4 color channels of the Bh1745.
/// </summary>
public record ChannelCompensationMultipliers(double Red, double Green, double Blue, double Clear);
/// <summary>
/// Digital color sensor Bh1745.
/// </summary>
......@@ -35,10 +42,8 @@ namespace Iot.Device.Bh1745
public Bh1745(I2cDevice device)
{
_i2cDevice = device;
ChannelCompensationMultipliers = new ChannelCompensationMultipliers
{
Red = 2.2, Green = 1.0, Blue = 1.8, Clear = 10.0
};
// ChannelCompensationMultipliers: Red, Green, Blue, Clear
ChannelCompensationMultipliers = new (2.2, 1.0, 1.8, 10.0);
// reset device and set default configuration
InitDevice();
......
......@@ -8,6 +8,7 @@
<ItemGroup>
<Compile Include="*.cs" />
<Compile Include="$(MSBuildThisFileDirectory)/../Common/System/Runtime/CompilerServices/IsExternalInit.cs" Condition="'$(TargetFramework)' == 'netcoreapp2.1'" />
<None Include="README.md" />
<ProjectReference Include="$(MainLibraryPath)System.Device.Gpio.csproj" />
</ItemGroup>
......
......@@ -27,5 +27,13 @@ namespace Iot.Device.Bh1745
MeasurementTime.Ms5120 => 5120,
_ => throw new ArgumentOutOfRangeException()
};
/// <summary>
/// Converts the enum Measurement time to a TimeSpan.
/// </summary>
/// <param name="bh1745">The BH1745 device.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when a not supported MeasurementTime is used.</exception>
/// <returns></returns>
public static TimeSpan MeasurementTimeAsTimeSpan(this Bh1745 bh1745) => new TimeSpan(bh1745.MeasurementTime.ToMilliseconds());
}
}
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Iot.Device.Bh1745
{
/// <summary>
/// Channel compensation multipliers used to compensate the 4 color channels of the Bh1745.
/// </summary>
public class ChannelCompensationMultipliers
{
/// <summary>
/// Multiplier for the red color channel.
/// </summary>
public double Red { get; set; }
/// <summary>
/// Multiplier for the green color channel.
/// </summary>
public double Green { get; set; }
/// <summary>
/// Multiplier for the blue color channel.
/// </summary>
public double Blue { get; set; }
/// <summary>
/// Multiplier for the clear color channel.
/// </summary>
public double Clear { get; set; }
}
}
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
namespace Iot.Device.Bh1745
{
/// <summary>
......
......@@ -3,7 +3,7 @@
using System;
using System.Device.I2c;
using System.Threading.Tasks;
using System.Threading;
using Iot.Device.Bh1745;
// bus id on the raspberry pi 3
......@@ -13,27 +13,27 @@ const int busId = 1;
var i2cSettings = new I2cConnectionSettings(busId, Bh1745.DefaultI2cAddress);
var i2cDevice = I2cDevice.Create(i2cSettings);
using var i2cBh1745 = new Bh1745(i2cDevice);
// multipliers affect the compensated values
i2cBh1745.ChannelCompensationMultipliers.Red = 2.5;
i2cBh1745.ChannelCompensationMultipliers.Green = 0.9;
i2cBh1745.ChannelCompensationMultipliers.Blue = 1.9;
i2cBh1745.ChannelCompensationMultipliers.Clear = 9.5;
using Bh1745 i2cBh1745 = new Bh1745(i2cDevice)
{
// multipliers affect the compensated values
// ChannelCompensationMultipliers: Red, Green, Blue, Clear
ChannelCompensationMultipliers = new (2.5, 0.9, 1.9, 9.5),
// set custom measurement time
i2cBh1745.MeasurementTime = MeasurementTime.Ms1280;
// set custom measurement time
MeasurementTime = MeasurementTime.Ms1280,
// interrupt functionality is detailed in the datasheet
// Reference: https://www.mouser.co.uk/datasheet/2/348/bh1745nuc-e-519994.pdf (page 13)
i2cBh1745.LowerInterruptThreshold = 0xABFF;
i2cBh1745.HigherInterruptThreshold = 0x0A10;
// interrupt functionality is detailed in the datasheet
// Reference: https://www.mouser.co.uk/datasheet/2/348/bh1745nuc-e-519994.pdf (page 13)
LowerInterruptThreshold = 0xABFF,
HigherInterruptThreshold = 0x0A10,
i2cBh1745.LatchBehavior = LatchBehavior.LatchEachMeasurement;
i2cBh1745.InterruptPersistence = InterruptPersistence.UpdateMeasurementEnd;
i2cBh1745.InterruptIsEnabled = true;
LatchBehavior = LatchBehavior.LatchEachMeasurement,
InterruptPersistence = InterruptPersistence.UpdateMeasurementEnd,
InterruptIsEnabled = true,
};
// wait for first measurement
Task.Delay(i2cBh1745.MeasurementTime.ToMilliseconds()).Wait();
Thread.Sleep(i2cBh1745.MeasurementTimeAsTimeSpan());
while (true)
{
......@@ -48,5 +48,5 @@ while (true)
Console.WriteLine("RGB color read: #{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);
Console.WriteLine($"Raw illumination value: {i2cBh1745.ReadClearDataRegister()}");
Task.Delay(i2cBh1745.MeasurementTime.ToMilliseconds()).Wait();
Thread.Sleep(i2cBh1745.MeasurementTimeAsTimeSpan());
}
......@@ -3,7 +3,7 @@
using System;
using System.Device.I2c;
using System.Threading.Tasks;
using System.Threading;
using Iot.Device.Bh1745;
// bus id on the raspberry pi 3
......@@ -12,10 +12,9 @@ const int busId = 1;
// create device
I2cConnectionSettings i2cSettings = new (busId, Bh1745.DefaultI2cAddress);
using I2cDevice i2cDevice = I2cDevice.Create(i2cSettings);
using Bh1745 i2cBh1745 = new Bh1745(i2cDevice);
// wait for first measurement
Task.Delay(i2cBh1745.MeasurementTime.ToMilliseconds()).Wait();
Thread.Sleep(i2cBh1745.MeasurementTimeAsTimeSpan());
while (true)
{
......@@ -23,5 +22,5 @@ while (true)
Console.WriteLine("RGB color read: #{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);
Console.WriteLine($"Raw illumination value: {i2cBh1745.ReadClearDataRegister()}");
Task.Delay(i2cBh1745.MeasurementTime.ToMilliseconds()).Wait();
Thread.Sleep(i2cBh1745.MeasurementTimeAsTimeSpan());
}
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace System.Runtime.CompilerServices
{
internal class IsExternalInit
{
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册