From 66520523b7de1c57276268936613a16d5cb49533 Mon Sep 17 00:00:00 2001 From: Adrian Stevens Date: Wed, 24 Jul 2024 00:12:52 -0700 Subject: [PATCH] Add IColorSensor to Bh1745 --- .../Sensors.Light.Bh1745/Driver/Bh1745.cs | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/Source/Meadow.Foundation.Peripherals/Sensors.Light.Bh1745/Driver/Bh1745.cs b/Source/Meadow.Foundation.Peripherals/Sensors.Light.Bh1745/Driver/Bh1745.cs index 0d2260769..1beb9caf0 100644 --- a/Source/Meadow.Foundation.Peripherals/Sensors.Light.Bh1745/Driver/Bh1745.cs +++ b/Source/Meadow.Foundation.Peripherals/Sensors.Light.Bh1745/Driver/Bh1745.cs @@ -1,5 +1,6 @@ using Meadow.Hardware; using Meadow.Peripherals.Sensors; +using Meadow.Peripherals.Sensors.Color; using Meadow.Peripherals.Sensors.Light; using Meadow.Units; using System; @@ -12,9 +13,10 @@ namespace Meadow.Foundation.Sensors.Light /// public partial class Bh1745 : ByteCommsSensorBase<(Illuminance? AmbientLight, Color? Color)>, - ILightSensor, II2cPeripheral + ILightSensor, IColorSensor, II2cPeripheral { private event EventHandler> _lightHandlers = default!; + private event EventHandler> _colorHandlers = default!; event EventHandler> ISamplingSensor.Updated { @@ -22,11 +24,22 @@ event EventHandler> ISamplingSensor.Upda remove => _lightHandlers -= value; } + event EventHandler> ISamplingSensor.Updated + { + add => _colorHandlers += value; + remove => _colorHandlers -= value; + } + /// /// The current Illuminance value /// public Illuminance? Illuminance => Conditions.AmbientLight; + /// + /// The current color value + /// + public Color? Color => Conditions.Color; + /// /// Interrupt reset status /// @@ -267,7 +280,7 @@ public Bh1745(II2cBus i2cBus, byte address = (byte)Addresses.Default) // get the ambient light var clearData = ReadClearDataRegister(); - if (clearData == 0) { conditions.Color = Color.Black; } + if (clearData == 0) { conditions.Color = Meadow.Color.Black; } if (ReadMeasurementIsValid() == false) { @@ -287,7 +300,7 @@ public Bh1745(II2cBus i2cBus, byte address = (byte)Addresses.Default) int green = (int)Math.Min(255, compensatedGreen / compensatedClear * 255); int blue = (int)Math.Min(255, compensatedBlue / compensatedClear * 255); - conditions.Color = Color.FromRgb(red, green, blue); + conditions.Color = Meadow.Color.FromRgb(red, green, blue); conditions.AmbientLight = new Illuminance(compensatedClear, Units.Illuminance.UnitType.Lux); @@ -374,5 +387,8 @@ protected bool ReadMeasurementIsValid() async Task ISensor.Read() => (await Read()).AmbientLight!.Value; + + async Task ISensor.Read() + => (await Read()).Color!.Value; } } \ No newline at end of file