Remarks

Tcs3472x
Status Status badge: in-progress
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for Meadow.Foundation.Sensors.Light.Tcs3472x

Code Example

Tcs3472x sensor;
RgbPwmLed rgbLed;

public override Task Initialize()
{
    Console.WriteLine("Initialize...");

    sensor = new Tcs3472x(Device.CreateI2cBus());

    // instantiate our onboard LED
    rgbLed = new RgbPwmLed(
        Device,
        Device.Pins.OnboardLedRed,
        Device.Pins.OnboardLedGreen,
        Device.Pins.OnboardLedBlue,
        commonType: CommonType.CommonAnode);

    // Example that uses an IObservable subscription to only be notified when the filter is satisfied
    var consumer = Tcs3472x.CreateObserver(
        handler: result => Console.WriteLine($"Observer: filter satisifed: {result.New.AmbientLight?.Lux:N2}Lux, old: {result.Old?.AmbientLight?.Lux:N2}Lux"),

        // only notify if the visible light changes by 100 lux (put your hand over the sensor to trigger)
        filter: result => {
            if (result.Old is { } old) { //c# 8 pattern match syntax. checks for !null and assigns var.
                // returns true if > 100lux change
                return ((result.New.AmbientLight.Value - old.AmbientLight.Value).Abs().Lux > 100);
            }
            return false;
        });
    sensor.Subscribe(consumer);

    // classical .NET events can also be used:
    sensor.Updated += (sender, result) => {
        Console.WriteLine($" Ambient Light: {result.New.AmbientLight?.Lux:N2}Lux");
        Console.WriteLine($" Color: {result.New.Color}");
        if (result.New.Color is { } color) { rgbLed.SetColor(color); }
    };

    return Task.CompletedTask;
}

public override async Task Run()
{
    var (AmbientLight, Color, Valid) = await sensor.Read();

    Console.WriteLine("Initial Readings:");
    Console.WriteLine($" Visible Light: {AmbientLight?.Lux:N2}Lux");
    Console.WriteLine($" Color: {Color}");
    if (Color is { } color) { rgbLed.SetColor(color); }

    sensor.StartUpdating(TimeSpan.FromSeconds(1));
}

Sample project(s) available on GitHub

Characteristic Locus
Inheritance System.Object ObservableBase<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Color>, System.Boolean>> SensorBase<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Color>, System.Boolean>> SamplingSensorBase<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Color>, System.Boolean>> ByteCommsSensorBase<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Color>, System.Boolean>> > Tcs3472x
Implements IDisposable ILightSensor
Inherited Members ByteCommsSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.Peripheral ByteCommsSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.ReadBuffer ByteCommsSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.WriteBuffer ByteCommsSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.Init(Int32, Int32) ByteCommsSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.Dispose(Boolean) ByteCommsSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.Dispose() SamplingSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.StartUpdating(Nullable<TimeSpan>) SamplingSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.StopUpdating() SensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.Updated SensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.samplingLock SensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.SamplingTokenSource SensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.Conditions SensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.IsSampling SensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.UpdateInterval SensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.ReadSensor() SensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.RaiseEventsAndNotify(IChangeResult<>) SensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.Read() ObservableBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.observers ObservableBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.NotifyObservers(IChangeResult<>) Meadow.Foundation.ObservableBase<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Meadow.Foundation.Color>, System.Boolean>>.Subscribe(IObserver<>) Meadow.Foundation.ObservableBase<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Meadow.Foundation.Color>, System.Boolean>>.CreateObserver(Action<>, System.Nullable<Predicate<IChangeResult<UNIT>>>)
Namespace Meadow.Foundation.Sensors.Light
Assembly Tcs3472x.dll

Syntax

public class Tcs3472x : ByteCommsSensorBase<(Illuminance? AmbientLight, Color? Color, bool Valid)>, IDisposable, ILightSensor

Constructors

Tcs3472x(II2cBus, Byte, Double, Tcs3472x.GainType)

Declaration
public Tcs3472x(II2cBus i2cBus, byte address = null, double integrationTime = 0.7, Tcs3472x.GainType gain = default(Tcs3472x.GainType))

Parameters

Type Name Description
II2cBus i2cBus
System.Byte address
System.Double integrationTime
Tcs3472x.GainType gain

Properties

Device

Get the type of sensor

Declaration
public Tcs3472x.DeviceType Device { get; }

Property Value

Type Description
Tcs3472x.DeviceType

Gain

Set/Get the gain

Declaration
public Tcs3472x.GainType Gain { get; set; }

Property Value

Type Description
Tcs3472x.GainType

Illuminance

Declaration
public Illuminance? Illuminance { get; }

Property Value

Type Description
System.Nullable<Illuminance>

IntegrationTime

Set/Get the time to wait for the sensor to read the data Minimum time is 0.0024 s Maximum time is 7.4 s Be aware that it is not a linear function

Declaration
public double IntegrationTime { get; set; }

Property Value

Type Description
System.Double

IsClearInterrupt

Get true if RGBC is clear channel interrupt

Declaration
public bool IsClearInterrupt { get; }

Property Value

Type Description
System.Boolean

Methods

I2cRead16(Tcs3472x.Registers)

Declaration
protected ushort I2cRead16(Tcs3472x.Registers reg)

Parameters

Type Name Description
Tcs3472x.Registers reg

Returns

Type Description
System.UInt16

IsValidData()

Get true is there are valid data

Declaration
protected bool IsValidData()

Returns

Type Description
System.Boolean

RaiseEventsAndNotify(IChangeResult<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>)

Declaration
protected override void RaiseEventsAndNotify(IChangeResult<(Illuminance? AmbientLight, Color? Color, bool Valid)> changeResult)

Parameters

Type Name Description
IChangeResult<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Color>, System.Boolean>> changeResult

ReadSensor()

Declaration
protected override Task<(Illuminance? AmbientLight, Color? Color, bool Valid)> ReadSensor()

Returns

Type Description
Task<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Color>, System.Boolean>>

Overrides

Meadow.Foundation.SensorBase<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Meadow.Foundation.Color>, System.Boolean>>.ReadSensor()

SetInterrupt(Tcs3472x.InterruptState, Boolean)

Set/clear a specific interrupt persistence This is used to have more than 1 cycle before generating an interruption.

Declaration
public void SetInterrupt(Tcs3472x.InterruptState interupt, bool state)

Parameters

Type Name Description
Tcs3472x.InterruptState interupt

The percistence cycles

System.Boolean state

True to set the interrupt, false to clear

SetInterrupt(Boolean)

Set/Clear the colors and clear interrupts

Declaration
public void SetInterrupt(bool state)

Parameters

Type Name Description
System.Boolean state

true to set all interrupts, false to clear

Events

LuminosityUpdated

Declaration
public event EventHandler<IChangeResult<Illuminance>> LuminosityUpdated

Event Type

Type Description
EventHandler<IChangeResult<Illuminance>>