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 Tcs3472x sensor #1055

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
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
@@ -0,0 +1,87 @@
# Meadow.Foundation.Sensors.Color.Tcs3472x

**Tcs3472x family of I2C color sensors (Tcs34721, Tcs34723, Tcs34725, Tcs34727)**

The **Tcs3472x** library is included in the **Meadow.Foundation.Sensors.Color.Tcs3472x** nuget package and is designed for the [Wilderness Labs](www.wildernesslabs.co) Meadow .NET IoT platform.

This driver is part of the [Meadow.Foundation](https://developer.wildernesslabs.co/Meadow/Meadow.Foundation/) peripherals library, an open-source repository of drivers and libraries that streamline and simplify adding hardware to your C# .NET Meadow IoT applications.

For more information on developing for Meadow, visit [developer.wildernesslabs.co](http://developer.wildernesslabs.co/).

To view all Wilderness Labs open-source projects, including samples, visit [github.com/wildernesslabs](https://github.com/wildernesslabs/).

## Installation

You can install the library from within Visual studio using the the NuGet Package Manager or from the command line using the .NET CLI:

`dotnet add package Meadow.Foundation.Sensors.Color.Tcs3472x`
## Usage

```csharp
private Tcs3472x sensor;

public override Task Initialize()
{
Resolver.Log.Info("Initialize...");

// configure our sensor on the I2C Bus
sensor = new Tcs3472x(Device.CreateI2cBus());

// Example that uses an IObservable subscription to only be notified when the filter is satisfied
var consumer = Tcs3472x.CreateObserver(
handler: result => Resolver.Log.Info($"Observer: filter satisfied: {result.New}, old: {result.Old}"),
filter: result =>
{
if (result.Old is { } old)
{
return Math.Abs(result.New.R - old.R) > 50;
}
return false;
});
sensor.Subscribe(consumer);

// classical .NET events can also be used:
sensor.Updated += (sender, result) =>
{
Resolver.Log.Info($" Color: {result.New}");
};

return Task.CompletedTask;
}

public override async Task Run()
{
var result = await sensor.Read();
Resolver.Log.Info($"Initial reading: {result}");

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

```
## How to Contribute

- **Found a bug?** [Report an issue](https://github.com/WildernessLabs/Meadow_Issues/issues)
- Have a **feature idea or driver request?** [Open a new feature request](https://github.com/WildernessLabs/Meadow_Issues/issues)
- Want to **contribute code?** Fork the [Meadow.Foundation](https://github.com/WildernessLabs/Meadow.Foundation) repository and submit a pull request against the `develop` branch


## Need Help?

If you have questions or need assistance, please join the Wilderness Labs [community on Slack](http://slackinvite.wildernesslabs.co/).
## About Meadow

Meadow is a complete, IoT platform with defense-grade security that runs full .NET applications on embeddable microcontrollers and Linux single-board computers including Raspberry Pi and NVIDIA Jetson.

### Build

Use the full .NET platform and tooling such as Visual Studio and plug-and-play hardware drivers to painlessly build IoT solutions.

### Connect

Utilize native support for WiFi, Ethernet, and Cellular connectivity to send sensor data to the Cloud and remotely control your peripherals.

### Deploy

Instantly deploy and manage your fleet in the cloud for OtA, health-monitoring, logs, command + control, and enterprise backend integrations.


Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Meadow.Sdk/1.1.0">
<PropertyGroup>
<Version>1.11.0</Version>
<PackageReadmeFile>Readme.md</PackageReadmeFile>
<Nullable>enable</Nullable>
<LangVersion>10.0</LangVersion>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageIcon>icon.png</PackageIcon>
<Authors>Wilderness Labs, Inc</Authors>
<TargetFramework>netstandard2.1</TargetFramework>
<OutputType>Library</OutputType>
<AssemblyName>Tcs3472x</AssemblyName>
<Company>Wilderness Labs, Inc</Company>
<PackageProjectUrl>http://developer.wildernesslabs.co/Meadow/Meadow.Foundation/</PackageProjectUrl>
<PackageId>Meadow.Foundation.Sensors.Color.Tcs3472x</PackageId>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.Foundation</RepositoryUrl>
<PackageTags>Meadow.Foundation,Color,Tcs3472x,Tcs34721,Tcs34723,Tcs34725,Tcs34727</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Description>Tcs3472x family of I2C color sensors (Tcs34721, Tcs34723, Tcs34725, Tcs34727)</Description>
</PropertyGroup>
<ItemGroup>
<None Include=".\Readme.md" Pack="true" PackagePath="" />
<None Include="..\..\..\icon.png" Pack="true" PackagePath="" />
<ProjectReference Include="..\..\..\Meadow.Foundation.Core\Meadow.Foundation.Core.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
namespace Meadow.Foundation.Sensors.Color;

public partial class Tcs3472x
{
/// <summary>
/// Valid I2C addresses for the sensor
/// </summary>
public enum Addresses : byte
{
/// <summary>
/// Bus address 0x29
/// </summary>
Address_0x29 = 0x29,
/// <summary>
/// Default bus address
/// </summary>
Default = Address_0x29
}

/// <summary>
/// The gain used to integrate the colors
/// </summary>
public enum Gain
{
/// <summary>1x gain</summary>
Gain1x = 0x00,
/// <summary>4x gain</summary>
Gain4x = 0x01,
/// <summary>16x gain</summary>
Gain16x = 0x02,
/// <summary>60x gain</summary>
Gain60x = 0x03
}

/// <summary>
/// This enum allows selecting how many cycles will be done measuring before
/// raising an interrupt.
/// </summary>
public enum InterruptState
{
/// <summary>Every RGBC cycle generates an interrupt.</summary>
All = 0x00,
/// <summary>1 clear channel value outside of threshold range.</summary>
Persistence01Cycle = 0x01,
/// <summary>2 clear channel consecutive values out of range.</summary>
Persistence02Cycle = 0x02,
/// <summary>3 clear channel consecutive values out of range.</summary>
Persistence03Cycle = 0x03,
/// <summary>5 clear channel consecutive values out of range.</summary>
Persistence05Cycle = 0x04,
/// <summary>10 clear channel consecutive values out of range.</summary>
Persistence10Cycle = 0x05,
/// <summary>15 clear channel consecutive values out of range.</summary>
Persistence15Cycle = 0x06,
/// <summary>20 clear channel consecutive values out of range.</summary>
Persistence20Cycle = 0x07,
/// <summary>25 clear channel consecutive values out of range.</summary>
Persistence25Cycle = 0x08,
/// <summary>30 clear channel consecutive values out of range.</summary>
Persistence30Cycle = 0x09,
/// <summary>35 clear channel consecutive values out of range.</summary>
Persistence35Cycle = 0x0A,
/// <summary>40 clear channel consecutive values out of range.</summary>
Persistence40Cycle = 0x0B,
/// <summary>45 clear channel consecutive values out of range.</summary>
Persistence45Cycle = 0x0C,
/// <summary>50 clear channel consecutive values out of range.</summary>
Persistence50Cycle = 0x0D,
/// <summary>55 clear channel consecutive values out of range.</summary>
Persistence55Cycle = 0x0E,
/// <summary>60 clear channel consecutive values out of range.</summary>
Persistence60Cycle = 0x0F
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
namespace Meadow.Foundation.Sensors.Color;

public partial class Tcs3472x
{
internal enum Registers
{
/// <summary>
/// Enable register.
/// </summary>
ENABLE = 0x00,

/// <summary>
/// RGBC interrupt enable. When asserted, permits RGBC interrupts to be generated.
/// </summary>
ENABLE_AIEN = 0x10,

/// <summary>
/// Wait enable. This bit activates the wait feature. Writing a 1 activates the wait timer. Writing a 0 disables the wait timer.
/// </summary>
ENABLE_WEN = 0x08,

/// <summary>
/// RGBC enable. This bit activates the two-channel ADC. Writing a 1 activates the RGBC. Writing a 0 disables the RGBC.
/// </summary>
ENABLE_AEN = 0x02,

/// <summary>
/// Power ON. This bit activates the internal oscillator to permit the timers and ADC channels to operate. Writing a 1 activates the oscillator. Writing a 0 disables the oscillator.
/// </summary>
ENABLE_PON = 0x01,

/// <summary>
/// The RGBC timing register controls the internal integration time of the RGBC clear and IR channel ADCs in 2.4-ms increments. Max RGBC Count = (256 − ATIME) × 1024 up to a maximum of 65535.
/// </summary>
ATIME = 0x01,

/// <summary>
/// Wait time is set in 2.4 ms increments unless the WLONG bit is asserted, in which case the wait times are 12× longer. WTIME is programmed as a 2’s complement number.
/// </summary>
WTIME = 0x03,

/// <summary>
/// RGBC clear channel low threshold lower byte.
/// </summary>
AILTL = 0x04,

/// <summary>
/// RGBC clear channel low threshold upper byte.
/// </summary>
AILTH = 0x05,

/// <summary>
/// RGBC clear channel high threshold lower byte.
/// </summary>
AIHTL = 0x06,

/// <summary>
/// RGBC clear channel high threshold upper byte.
/// </summary>
AIHTH = 0x07,

/// <summary>
/// The persistence register controls the filtering interrupt capabilities of the device.
/// </summary>
PERS = 0x0C,

/// <summary>
/// The configuration register sets the wait long time.
/// </summary>
CONFIG = 0x0D,

/// <summary>
/// Wait Long. When asserted, the wait cycles are increased by a factor of 12× from that programmed in the WTIME register.
/// </summary>
CONFIG_WLONG = 0x02,

/// <summary>
/// The Control register provides eight bits of miscellaneous control to the analog block. These bits typically control functions such as gain settings and/or diode selection.
/// </summary>
CONTROL = 0x0F,

/// <summary>
/// 0x44 = TCS34721/TCS34725, 0x4D = TCS34723/TCS34727
/// </summary>
ID = 0x12,

/// <summary>
/// The Status Register provides the internal status of the device.
/// </summary>
STATUS = 0x13,

/// <summary>
/// RGBC clear channel Interrupt.
/// </summary>
STATUS_AINT = 0x10,

/// <summary>
/// RGBC Valid. Indicates that the RGBC channels have completed an integration cycle.
/// </summary>
STATUS_AVALID = 0x01,

/// <summary>
/// Clear data low byte.
/// </summary>
CDATAL = 0x14,

/// <summary>
/// Clear data high byte.
/// </summary>
CDATAH = 0x15,

/// <summary>
/// Red data low byte.
/// </summary>
RDATAL = 0x16,

/// <summary>
/// Red data high byte.
/// </summary>
RDATAH = 0x17,

/// <summary>
/// Green data low byte.
/// </summary>
GDATAL = 0x18,

/// <summary>
/// Green data high byte.
/// </summary>
GDATAH = 0x19,

/// <summary>
/// Blue data low byte.
/// </summary>
BDATAL = 0x1A,

/// <summary>
/// Blue data high byte.
/// </summary>
BDATAH = 0x1B,

/// <summary>
/// Command bit.
/// </summary>
COMMAND_BIT = 0x80
}
}
Loading
Loading