Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IColorSensor to Bh1745 #1056

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,21 +13,33 @@ namespace Meadow.Foundation.Sensors.Light
/// </summary>
public partial class Bh1745
: ByteCommsSensorBase<(Illuminance? AmbientLight, Color? Color)>,
ILightSensor, II2cPeripheral
ILightSensor, IColorSensor, II2cPeripheral
{
private event EventHandler<IChangeResult<Illuminance>> _lightHandlers = default!;
private event EventHandler<IChangeResult<Color>> _colorHandlers = default!;

event EventHandler<IChangeResult<Illuminance>> ISamplingSensor<Illuminance>.Updated
{
add => _lightHandlers += value;
remove => _lightHandlers -= value;
}

event EventHandler<IChangeResult<Color>> ISamplingSensor<Color>.Updated
{
add => _colorHandlers += value;
remove => _colorHandlers -= value;
}

/// <summary>
/// The current Illuminance value
/// </summary>
public Illuminance? Illuminance => Conditions.AmbientLight;

/// <summary>
/// The current color value
/// </summary>
public Color? Color => Conditions.Color;

/// <summary>
/// Interrupt reset status
/// </summary>
Expand Down Expand Up @@ -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)
{
Expand All @@ -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);

Expand Down Expand Up @@ -374,5 +387,8 @@ protected bool ReadMeasurementIsValid()

async Task<Illuminance> ISensor<Illuminance>.Read()
=> (await Read()).AmbientLight!.Value;

async Task<Color> ISensor<Color>.Read()
=> (await Read()).Color!.Value;
}
}
Loading