From a0231d4397de913f328829cead587ca6ef355e27 Mon Sep 17 00:00:00 2001 From: Rich Lander Date: Mon, 9 Nov 2020 17:26:29 -0800 Subject: [PATCH] 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 --- .../Iot.Device.Bindings.csproj | 3 +- src/devices/Bh1745/Bh1745.cs | 13 ++++--- src/devices/Bh1745/Bh1745.csproj | 1 + src/devices/Bh1745/Bh1745Extensions.cs | 8 +++++ .../Bh1745/ChannelCompensationMultipliers.cs | 32 ----------------- src/devices/Bh1745/MeasurementTime.cs | 2 ++ .../samples/Program.CustomConfiguration.cs | 36 +++++++++---------- src/devices/Bh1745/samples/Program.cs | 7 ++-- .../CompilerServices/IsExternalInit.cs | 9 +++++ 9 files changed, 52 insertions(+), 59 deletions(-) delete mode 100644 src/devices/Bh1745/ChannelCompensationMultipliers.cs create mode 100644 src/devices/Common/System/Runtime/CompilerServices/IsExternalInit.cs diff --git a/src/Iot.Device.Bindings/Iot.Device.Bindings.csproj b/src/Iot.Device.Bindings/Iot.Device.Bindings.csproj index c4a1cc7f..9af00a6f 100644 --- a/src/Iot.Device.Bindings/Iot.Device.Bindings.csproj +++ b/src/Iot.Device.Bindings/Iot.Device.Bindings.csproj @@ -13,11 +13,12 @@ $(TargetsForTfmSpecificContentInPackage) - + <_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'" /> diff --git a/src/devices/Bh1745/Bh1745.cs b/src/devices/Bh1745/Bh1745.cs index 6ebe94d9..ff4600eb 100644 --- a/src/devices/Bh1745/Bh1745.cs +++ b/src/devices/Bh1745/Bh1745.cs @@ -9,6 +9,13 @@ using System.Linq; namespace Iot.Device.Bh1745 { +// Tracking https://github.com/dotnet/roslyn/issues/44571 +#pragma warning disable CS1591 + /// + /// Channel compensation multipliers used to compensate the 4 color channels of the Bh1745. + /// + public record ChannelCompensationMultipliers(double Red, double Green, double Blue, double Clear); + /// /// Digital color sensor Bh1745. /// @@ -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(); diff --git a/src/devices/Bh1745/Bh1745.csproj b/src/devices/Bh1745/Bh1745.csproj index 492e6ff8..02dc3a98 100644 --- a/src/devices/Bh1745/Bh1745.csproj +++ b/src/devices/Bh1745/Bh1745.csproj @@ -8,6 +8,7 @@ + diff --git a/src/devices/Bh1745/Bh1745Extensions.cs b/src/devices/Bh1745/Bh1745Extensions.cs index 6bc5684a..62fcaf96 100644 --- a/src/devices/Bh1745/Bh1745Extensions.cs +++ b/src/devices/Bh1745/Bh1745Extensions.cs @@ -27,5 +27,13 @@ namespace Iot.Device.Bh1745 MeasurementTime.Ms5120 => 5120, _ => throw new ArgumentOutOfRangeException() }; + + /// + /// Converts the enum Measurement time to a TimeSpan. + /// + /// The BH1745 device. + /// Thrown when a not supported MeasurementTime is used. + /// + public static TimeSpan MeasurementTimeAsTimeSpan(this Bh1745 bh1745) => new TimeSpan(bh1745.MeasurementTime.ToMilliseconds()); } } diff --git a/src/devices/Bh1745/ChannelCompensationMultipliers.cs b/src/devices/Bh1745/ChannelCompensationMultipliers.cs deleted file mode 100644 index c7a2bd53..00000000 --- a/src/devices/Bh1745/ChannelCompensationMultipliers.cs +++ /dev/null @@ -1,32 +0,0 @@ -// 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 -{ - /// - /// Channel compensation multipliers used to compensate the 4 color channels of the Bh1745. - /// - public class ChannelCompensationMultipliers - { - /// - /// Multiplier for the red color channel. - /// - public double Red { get; set; } - - /// - /// Multiplier for the green color channel. - /// - public double Green { get; set; } - - /// - /// Multiplier for the blue color channel. - /// - public double Blue { get; set; } - - /// - /// Multiplier for the clear color channel. - /// - public double Clear { get; set; } - - } -} diff --git a/src/devices/Bh1745/MeasurementTime.cs b/src/devices/Bh1745/MeasurementTime.cs index 92485a79..e070944d 100644 --- a/src/devices/Bh1745/MeasurementTime.cs +++ b/src/devices/Bh1745/MeasurementTime.cs @@ -1,6 +1,8 @@ // 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 { /// diff --git a/src/devices/Bh1745/samples/Program.CustomConfiguration.cs b/src/devices/Bh1745/samples/Program.CustomConfiguration.cs index d8cb2a4d..33265273 100644 --- a/src/devices/Bh1745/samples/Program.CustomConfiguration.cs +++ b/src/devices/Bh1745/samples/Program.CustomConfiguration.cs @@ -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()); } diff --git a/src/devices/Bh1745/samples/Program.cs b/src/devices/Bh1745/samples/Program.cs index b08db60c..c2fc6601 100644 --- a/src/devices/Bh1745/samples/Program.cs +++ b/src/devices/Bh1745/samples/Program.cs @@ -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()); } diff --git a/src/devices/Common/System/Runtime/CompilerServices/IsExternalInit.cs b/src/devices/Common/System/Runtime/CompilerServices/IsExternalInit.cs new file mode 100644 index 00000000..a8aaf76d --- /dev/null +++ b/src/devices/Common/System/Runtime/CompilerServices/IsExternalInit.cs @@ -0,0 +1,9 @@ +// 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 + { + } +} -- GitLab