Skip to content

Commit

Permalink
PCT2075 temp sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
ctacke committed Jan 29, 2024
1 parent 22a8546 commit b10e7c8
Show file tree
Hide file tree
Showing 9 changed files with 360 additions and 0 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
namespace Meadow.Foundation.Sensors.Temperature;

public partial class Pct2075
{
/// <summary>
/// Valid I2C addresses for the PCT2075 sensor.
/// </summary>
public enum Addresses : byte
{
/// <summary>
/// I2C address: 0x48 (1001000)
/// </summary>
Address_0x48 = 0b1001000,

/// <summary>
/// I2C address: 0x49 (1001001)
/// </summary>
Address_0x49 = 0b1001001,

/// <summary>
/// I2C address: 0x4A (1001010)
/// </summary>
Address_0x4A = 0b1001010,

/// <summary>
/// I2C address: 0x4B (1001011)
/// </summary>
Address_0x4B = 0b1001011,

/// <summary>
/// I2C address: 0x4C (1001100)
/// </summary>
Address_0x4C = 0b1001100,

/// <summary>
/// I2C address: 0x4D (1001101)
/// </summary>
Address_0x4D = 0b1001101,

/// <summary>
/// I2C address: 0x4E (1001110)
/// </summary>
Address_0x4E = 0b1001110,

/// <summary>
/// I2C address: 0x4F (1001111)
/// </summary>
Address_0x4F = 0b1001111,

/// <summary>
/// I2C address: 0x70 (1110000)
/// </summary>
Address_0x70 = 0b1110000,

/// <summary>
/// I2C address: 0x71 (1110001)
/// </summary>
Address_0x71 = 0b1110001,

/// <summary>
/// I2C address: 0x72 (1110010)
/// </summary>
Address_0x72 = 0b1110010,

/// <summary>
/// I2C address: 0x73 (1110011)
/// </summary>
Address_0x73 = 0b1110011,

/// <summary>
/// I2C address: 0x74 (1110100)
/// </summary>
Address_0x74 = 0b1110100,

/// <summary>
/// I2C address: 0x75 (1110101)
/// </summary>
Address_0x75 = 0b1110101,

/// <summary>
/// I2C address: 0x76 (1110110)
/// </summary>
Address_0x76 = 0b1110110,

/// <summary>
/// I2C address: 0x77 (1110111)
/// </summary>
Address_0x77 = 0b1110111,

/// <summary>
/// I2C address: 0x28 (0101000)
/// </summary>
Address_0x28 = 0b0101000,

/// <summary>
/// I2C address: 0x29 (0101001)
/// </summary>
Address_0x29 = 0b0101001,

/// <summary>
/// I2C address: 0x2A (0101010)
/// </summary>
Address_0x2A = 0b0101010,

/// <summary>
/// I2C address: 0x2B (0101011)
/// </summary>
Address_0x2B = 0b0101011,

/// <summary>
/// I2C address: 0x2C (0101100)
/// </summary>
Address_0x2C = 0b0101100,

/// <summary>
/// I2C address: 0x2D (0101101)
/// </summary>
Address_0x2D = 0b0101101,

/// <summary>
/// I2C address: 0x2E (0101110)
/// </summary>
Address_0x2E = 0b0101110,

/// <summary>
/// I2C address: 0x2F (0101111)
/// </summary>
Address_0x2F = 0b0101111,

/// <summary>
/// I2C address: 0x35 (0110101)
/// </summary>
Address_0x35 = 0b0110101,

/// <summary>
/// I2C address: 0x36 (0110110)
/// </summary>
Address_0x36 = 0b0110110,

/// <summary>
/// I2C address: 0x37 (0110111)
/// </summary>
Address_0x37 = 0b0110111,

/// <summary>
/// Default I2C address: 0x37 (0110111)
/// </summary>
Default = Address_0x37
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Meadow.Foundation.Sensors.Temperature;

public partial class Pct2075
{
internal enum Registers
{
Temp = 0x00,
Conf = 0x01,
Thyst = 0x02,
Tos = 0x03,
Tidle = 0x04
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using Meadow.Hardware;
using Meadow.Peripherals.Sensors;
using Meadow.Units;
using System;
using System.Threading.Tasks;

namespace Meadow.Foundation.Sensors.Temperature;

/// <summary>
/// Represents a PCT2075 temperature sensor, implementing the ITemperatureSensor and II2cPeripheral interfaces.
/// </summary>
public partial class Pct2075 : PollingSensorBase<Units.Temperature>,
ITemperatureSensor, II2cPeripheral
{
private II2cCommunications _comms;

/// <inheritdoc/>
public Units.Temperature? Temperature { get; private set; }
/// <inheritdoc/>
public byte DefaultI2cAddress => (byte)Addresses.Default;

/// <summary>
/// Initializes a new instance of the Pct2075 class.
/// </summary>
/// <param name="bus">The I2C bus to which the sensor is connected.</param>
/// <param name="address">The I2C address of the sensor (optional, default is Addresses.Default).</param>
public Pct2075(II2cBus bus, Addresses address = Addresses.Default)
{
_comms = new I2cCommunications(bus, (byte)address);
}

/// <inheritdoc/>
protected override Task<Units.Temperature> ReadSensor()
{
var tempRegister = _comms.ReadRegisterAsUShort((byte)Registers.Temp, ByteOrder.BigEndian);
Units.Temperature result;
if ((tempRegister & (1 << 15)) != 0)
{
// negative temp
var t = (tempRegister >> 5); // shift
t = (~t & 0b111111111) + 1; // invert, mask to 9-bits, add 1 (9-bit twos complement)
result = (-1 * t * 0.125).Celsius();
}
else
{
result = ((tempRegister >> 5) * 0.125).Celsius();
}
Temperature = result;
return Task.FromResult(result);
}

/// <summary>
/// Sets the interrupt temperatures for the PCT2075 sensor.
/// </summary>
/// <param name="interruptOnTemperature">The temperature threshold that triggers the interrupt.</param>
/// <param name="interruptOffTemperature">The temperature threshold that deactivates the interrupt.</param>
/// <param name="interruptActiveHigh">
/// Indicates whether the interrupt is active-high (true) or active-low (false).
/// Default is active-high.
/// </param>
public void SetInterruptTemperatures(Units.Temperature interruptOnTemperature, Units.Temperature interruptOffTemperature, bool interruptActiveHigh = true)
{
if (interruptOffTemperature > interruptOnTemperature) throw new ArgumentException("Off temperature must be below On temeprature");

// require 6 readings to exceed the interrupt temp to trigger
byte cfg = 0b00011000;
if (interruptActiveHigh) cfg |= 1 << 2; // interrupt polarity bit
_comms.WriteRegister((byte)Registers.Conf, cfg);

// on temp
var tos = (ushort)(((short)interruptOnTemperature.Celsius / 2) << 7);
_comms.WriteRegister((byte)Registers.Tos, tos);

// off temp
var thyst = (ushort)(((short)interruptOnTemperature.Celsius / 2) << 7);
_comms.WriteRegister((byte)Registers.Thyst, thyst);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Meadow.Sdk/1.1.0">
<PropertyGroup>
<Version>1.8.0</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageIcon>icon.png</PackageIcon>
<Authors>Wilderness Labs, Inc</Authors>
<TargetFramework>netstandard2.1</TargetFramework>
<OutputType>Library</OutputType>
<AssemblyName>Pct2075</AssemblyName>
<Company>Wilderness Labs, Inc</Company>
<PackageProjectUrl>http://developer.wildernesslabs.co/Meadow/Meadow.Foundation/</PackageProjectUrl>
<PackageId>Meadow.Foundation.Sensors.Temperature.Mcp960x</PackageId>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.Foundation</RepositoryUrl>
<PackageTags>Meadow.Foundation,Temperature,PCT2075</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Description>Pct2075 I2C temperature sensor</Description>
<Nullable>enable</Nullable>
<RootNamespace>Meadow.Foundation.Sensors.Temperature</RootNamespace>
</PropertyGroup>
<PropertyGroup>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<LangVersion>10.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<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,38 @@
using Meadow;
using Meadow.Foundation.ICs.IOExpanders;
using Meadow.Foundation.Sensors.Temperature;
using System;
using System.Diagnostics;
using System.Threading.Tasks;

namespace Sensors.Temperature.Pct2075_Sample;

public class MeadowApp : App<Windows>
{
//<!=SNIP=>
private Ft232h _expander;
private Pct2075 _sensor;

public override Task Initialize()
{
_expander = new Ft232h(false);
_sensor = new Pct2075(_expander.CreateI2cBus());

_sensor.Updated += OnUpdated;
_sensor.StartUpdating(TimeSpan.FromSeconds(1));

return base.Initialize();
}

private void OnUpdated(object sender, IChangeResult<Meadow.Units.Temperature> e)
{
Debug.WriteLine($"Temp: {e.New.Celsius:n1}C {e.New.Fahrenheit:n1}F");
}

//<!=SNOP=>

public static async Task Main(string[] args)
{
await MeadowOS.Start(args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.Foundation</RepositoryUrl>
<Company>Wilderness Labs, Inc</Company>
<Authors>Wilderness Labs, Inc</Authors>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<TargetFramework>net7</TargetFramework>
<OutputType>Exe</OutputType>
<AssemblyName>App</AssemblyName>
<LangVersion>10</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\..\..\Meadow.Contracts\Source\Meadow.Contracts\Meadow.Contracts.csproj" />
<ProjectReference Include="..\..\..\..\..\..\Meadow.Core\source\implementations\windows\Meadow.Windows\Meadow.Windows.csproj" />
<ProjectReference Include="..\..\..\..\..\..\Meadow.Core\source\Meadow.Core\Meadow.Core.csproj" />
<ProjectReference Include="..\..\..\..\..\..\Meadow.Logging\Source\Meadow.Logging\lib\Meadow.Logging.csproj" />
<ProjectReference Include="..\..\..\..\..\..\Meadow.Units\Source\Meadow.Units\Meadow.Units.csproj" />
<ProjectReference Include="..\..\..\ICs.IOExpanders.Ft232h\Driver\ICs.IOExpanders.Ft232h.csproj" />
<ProjectReference Include="..\..\Driver\Sensors.Temperature.Pct2075.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="meadow.config.yaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MonoControl:
Options: --jit
24 changes: 24 additions & 0 deletions Source/Meadow.Foundation.sln
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{80EC
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinForms_Sample", "Meadow.Foundation.Peripherals\Displays.WinForms\Samples\WinForms_Sample\WinForms_Sample.csproj", "{A5734B07-64C0-4C93-9EB0-390B0B6A075C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Pct2075", "Pct2075", "{7471C6BF-1995-4E56-91E9-86DAAA46C386}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sensors.Temperature.Pct2075", "Meadow.Foundation.Peripherals\Sensors.Temperature.Pct2075\Driver\Sensors.Temperature.Pct2075.csproj", "{3C999A94-227A-470F-935E-966E375E40BB}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{FC2EEFA9-030B-4EF0-AFB7-1CC61876D1EE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pct2075_Sample", "Meadow.Foundation.Peripherals\Sensors.Temperature.Pct2075\Samples\Pct2075_Sample\Pct2075_Sample.csproj", "{2B29B1A8-8903-4335-A5CC-6FFBD9069C2D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -3501,6 +3509,18 @@ Global
{A5734B07-64C0-4C93-9EB0-390B0B6A075C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5734B07-64C0-4C93-9EB0-390B0B6A075C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A5734B07-64C0-4C93-9EB0-390B0B6A075C}.Release|Any CPU.Build.0 = Release|Any CPU
{3C999A94-227A-470F-935E-966E375E40BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3C999A94-227A-470F-935E-966E375E40BB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C999A94-227A-470F-935E-966E375E40BB}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{3C999A94-227A-470F-935E-966E375E40BB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C999A94-227A-470F-935E-966E375E40BB}.Release|Any CPU.Build.0 = Release|Any CPU
{3C999A94-227A-470F-935E-966E375E40BB}.Release|Any CPU.Deploy.0 = Release|Any CPU
{2B29B1A8-8903-4335-A5CC-6FFBD9069C2D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2B29B1A8-8903-4335-A5CC-6FFBD9069C2D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2B29B1A8-8903-4335-A5CC-6FFBD9069C2D}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{2B29B1A8-8903-4335-A5CC-6FFBD9069C2D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2B29B1A8-8903-4335-A5CC-6FFBD9069C2D}.Release|Any CPU.Build.0 = Release|Any CPU
{2B29B1A8-8903-4335-A5CC-6FFBD9069C2D}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -4224,6 +4244,10 @@ Global
{27589062-708E-44CD-9DF4-0D16899265FE} = {E775FF9F-8AC5-4994-B45A-20B50E2B03E2}
{80EC3729-DB91-4F94-B11D-B611A214BA31} = {BBF136B7-361C-4FC2-AACA-6B6ED79BDCF1}
{A5734B07-64C0-4C93-9EB0-390B0B6A075C} = {80EC3729-DB91-4F94-B11D-B611A214BA31}
{7471C6BF-1995-4E56-91E9-86DAAA46C386} = {DBC6C89D-A932-4F99-9382-7405A0045988}
{3C999A94-227A-470F-935E-966E375E40BB} = {7471C6BF-1995-4E56-91E9-86DAAA46C386}
{FC2EEFA9-030B-4EF0-AFB7-1CC61876D1EE} = {7471C6BF-1995-4E56-91E9-86DAAA46C386}
{2B29B1A8-8903-4335-A5CC-6FFBD9069C2D} = {FC2EEFA9-030B-4EF0-AFB7-1CC61876D1EE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AF7CA16F-8C38-4546-87A2-5DAAF58A1520}
Expand Down

0 comments on commit b10e7c8

Please sign in to comment.