Skip to content

Conversation

@pgrawehr
Copy link
Contributor

@pgrawehr pgrawehr commented Dec 20, 2025

Add a new Binding for INA236 voltage and current sensor. It's similar to the existing binding INA219 in functionality, but the registers are quite different, so that a common base class is not useful. Could think of a common interface, though.

Alarm features are not implemented as the breakouts from most suppliers don't seem to have the ALRT pin wired out.

Microsoft Reviewers: Open in CodeFlow

@raffaeler
Copy link
Contributor

Could you please split the PRs by differentiating the tasks? I see changes to the lang versions along with other stuff not related to the binding.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request introduces a new device binding for the INA236 voltage and current sensor, along with significant improvements to the repository's testing infrastructure and language version standardization.

Key Changes:

  • New INA236 device binding with complete implementation including tests, samples, and documentation
  • Introduction of I2cSimulatedDeviceBase class to provide a reusable testing infrastructure for I2C devices
  • Standardization of C# language version to 12 across the entire repository
  • Refactoring of existing Tca955x tests to use the new simulated device infrastructure

Reviewed changes

Copilot reviewed 79 out of 79 changed files in this pull request and generated 19 comments.

Show a summary per file
File Description
src/devices/Ina236/Ina236.cs Main INA236 device implementation with voltage/current/power monitoring capabilities
src/devices/Ina236/Ina236OperatingMode.cs Operating mode enumeration for device configuration
src/devices/Ina236/Ina236Register.cs Register address definitions
src/devices/Ina236/README.md Device documentation and usage examples
src/devices/Ina236/samples/Program.cs Sample code demonstrating device usage
src/devices/Ina236/tests/Ina236Tests.cs Unit tests for INA236 device
src/devices/Ina236/tests/SimulatedIna236.cs Simulated device for testing
src/devices/Common/System/Device/I2c/I2cSimulatedDeviceBase.cs New base class for simulating I2C devices in tests
src/devices/Tca955x/tests/Tca955xSimulatedDevice.cs New simulated device using the base infrastructure
src/devices/Tca955x/tests/Tca9554Tests.cs Refactored tests using new infrastructure
src/devices/Tca955x/tests/Tca9555Tests.cs Refactored tests using new infrastructure
Directory.Build.props Updated default LangVersion to 12
samples/Directory.Build.props Updated default LangVersion to 12
Multiple .csproj files Removed explicit LangVersion settings to use default
src/devices/Gpio/README.md Fixed table formatting and typos
src/devices/Tca955x/README.md Fixed spelling errors
samples/README.md Updated .NET version requirements

Comment on lines +116 to +123
public override void WriteByte(byte value)
{
if (WriteRead([value], []) == 1)
{
return;
}

throw new IOException("Unable to write a byte to the device");
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WriteByte method checks if WriteRead returns 1, but WriteRead returns the number of bytes read (from outputBuffer), not written. When writing a single byte with no read operation (empty output buffer), WriteRead should return 0, not 1. The condition should check for 0 or not check the return value at all.

Copilot uses AI. Check for mistakes.
Comment on lines +8 to +10
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Several using directives appear to be unused: System.Linq, System.Text, and System.Threading.Tasks. These should be removed to keep the code clean.

Copilot uses AI. Check for mistakes.
ContinuousBusVoltage = 0b110,

/// <summary>
/// Continuously measure both bus and shut voltages.
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a spelling error in the comment - "shut voltages" should be "shunt voltages".

Copilot uses AI. Check for mistakes.
@@ -0,0 +1,46 @@
# INA219 - Bidirectional Current/Power Monitor
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The title of the README incorrectly says "INA219" when it should say "INA236". The device being documented is the INA236, not the INA219.

Copilot uses AI. Check for mistakes.
Comment on lines +326 to +347
// set a value in the buffer representing the register that we want to read and send it to the INA219
_i2cDevice.WriteRead(new ReadOnlySpan<byte>(ref registerNumber), buffer);

// massage the big endian value read from the INA219 unto a ushort.
return BinaryPrimitives.ReadUInt16BigEndian(buffer);
}

/// <summary>
/// Read a register from the INA236 device
/// </summary>
/// <param name="register">The register to read.</param>
/// <returns>A signed short integer representing the regsiter contents.</returns>
private short ReadRegisterSigned(Ina236Register register)
{
Span<byte> buffer = stackalloc byte[2];

byte registerNumber = (byte)register;
// set a value in the buffer representing the register that we want to read and send it to the INA219
_i2cDevice.WriteRead(new ReadOnlySpan<byte>(ref registerNumber), buffer);

// massage the big endian value read from the INA219 unto a ushort.
return BinaryPrimitives.ReadInt16BigEndian(buffer);
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comments in the ReadRegisterUnsigned and ReadRegisterSigned methods reference "INA219" instead of "INA236". These should be updated to match the actual device being implemented.

Copilot uses AI. Check for mistakes.
Comment on lines +5 to +10
using System.Buffers.Binary;
using System.Collections.Generic;
using System.Device.I2c;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Several using directives appear to be unused: System.Collections.Generic, System.Linq, System.Text, and System.Threading.Tasks. These should be removed to keep the code clean.

Copilot uses AI. Check for mistakes.
Comment on lines +8 to +13
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using UnitsNet;
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Several using directives appear to be unused: System.Linq, System.Runtime.InteropServices, System.Text, System.Threading.Tasks, and UnitsNet. These should be removed to keep the code clean.

Suggested change
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using UnitsNet;
using System.Numerics;

Copilot uses AI. Check for mistakes.
Assert.Equal(PinValue.High, value);
pin8.Dispose();
pin0.Dispose();
Assert.False(tcaController.IsPinOpen(8));
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertion checks if pin 8 is closed after closing pin 0. This appears to be a copy-paste error - it should check if pin 0 is closed instead.

Copilot uses AI. Check for mistakes.
/// </summary>
/// <param name="inputBuffer">Buffer with input data to the device, buffer[0] is usually the command byte</param>
/// <param name="outputBuffer">The return data from the device</param>
/// <returns>How many bytes where read. Should usually match the length of the output buffer</returns>
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a spelling error in the XML comment - "where read" should be "were read".

Suggested change
/// <returns>How many bytes where read. Should usually match the length of the output buffer</returns>
/// <returns>How many bytes were read. Should usually match the length of the output buffer</returns>

Copilot uses AI. Check for mistakes.
SingeShuntVoltage = 0b001,

/// <summary>
/// Generate s single measurement of the shunt voltage value, then wait for a command again
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a spelling error in the comment - "Generate s single" should be "Generate a single" (lowercase 's' should be 'a').

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants