-
Notifications
You must be signed in to change notification settings - Fork 609
New Binding: Ina236 #2459
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
base: main
Are you sure you want to change the base?
New Binding: Ina236 #2459
Conversation
Generic implementation is difficult, one problem is the different argument sizes and the fact that the endianess on the bus may change.
Will make it easier in future.
|
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. |
There was a problem hiding this 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 |
| public override void WriteByte(byte value) | ||
| { | ||
| if (WriteRead([value], []) == 1) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| throw new IOException("Unable to write a byte to the device"); |
Copilot
AI
Jan 8, 2026
There was a problem hiding this comment.
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.
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; |
Copilot
AI
Jan 8, 2026
There was a problem hiding this comment.
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.
| ContinuousBusVoltage = 0b110, | ||
|
|
||
| /// <summary> | ||
| /// Continuously measure both bus and shut voltages. |
Copilot
AI
Jan 8, 2026
There was a problem hiding this comment.
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".
| @@ -0,0 +1,46 @@ | |||
| # INA219 - Bidirectional Current/Power Monitor | |||
Copilot
AI
Jan 8, 2026
There was a problem hiding this comment.
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.
| // 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); |
Copilot
AI
Jan 8, 2026
There was a problem hiding this comment.
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.
| using System.Buffers.Binary; | ||
| using System.Collections.Generic; | ||
| using System.Device.I2c; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; |
Copilot
AI
Jan 8, 2026
There was a problem hiding this comment.
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.
| using System.Linq; | ||
| using System.Numerics; | ||
| using System.Runtime.InteropServices; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using UnitsNet; |
Copilot
AI
Jan 8, 2026
There was a problem hiding this comment.
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.
| using System.Linq; | |
| using System.Numerics; | |
| using System.Runtime.InteropServices; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using UnitsNet; | |
| using System.Numerics; |
| Assert.Equal(PinValue.High, value); | ||
| pin8.Dispose(); | ||
| pin0.Dispose(); | ||
| Assert.False(tcaController.IsPinOpen(8)); |
Copilot
AI
Jan 8, 2026
There was a problem hiding this comment.
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.
| /// </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> |
Copilot
AI
Jan 8, 2026
There was a problem hiding this comment.
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".
| /// <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> |
| SingeShuntVoltage = 0b001, | ||
|
|
||
| /// <summary> | ||
| /// Generate s single measurement of the shunt voltage value, then wait for a command again |
Copilot
AI
Jan 8, 2026
There was a problem hiding this comment.
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').
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