-
Notifications
You must be signed in to change notification settings - Fork 610
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
Changes from all commits
329906f
6e98a2d
69b0b93
2583e29
69dc380
6a0b617
72c3786
c654da0
2398d40
80eb488
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,285 @@ | ||||||
| // Licensed to the .NET Foundation under one or more agreements. | ||||||
| // The .NET Foundation licenses this file to you under the MIT license. | ||||||
|
|
||||||
| using System; | ||||||
| using System.Collections.Generic; | ||||||
| using System.Globalization; | ||||||
| using System.IO; | ||||||
| using System.Linq; | ||||||
| using System.Numerics; | ||||||
| using System.Runtime.InteropServices; | ||||||
| using System.Text; | ||||||
| using System.Threading.Tasks; | ||||||
| using UnitsNet; | ||||||
|
|
||||||
| namespace System.Device.I2c; | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// This class can be used to create a simulated I2C device. | ||||||
| /// Derive from it and implement the <see cref="Write"/> and <see cref="Read"/> commands | ||||||
| /// to behave as expected. | ||||||
| /// Can also serve as base for a testing mock. | ||||||
| /// </summary> | ||||||
| public abstract class I2cSimulatedDeviceBase : I2cDevice | ||||||
| { | ||||||
| private bool _disposed; | ||||||
| private Dictionary<byte, RegisterBase> _registerMap; | ||||||
| private byte _currentRegister; | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Default constructor | ||||||
| /// </summary> | ||||||
| /// <param name="settings">The connection settings for this device.</param> | ||||||
| public I2cSimulatedDeviceBase(I2cConnectionSettings settings) | ||||||
| { | ||||||
| ConnectionSettings = settings; | ||||||
| _registerMap = new Dictionary<byte, RegisterBase>(); | ||||||
| _disposed = false; | ||||||
| _currentRegister = 0; | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// The registermap of this device. | ||||||
| /// This should only be accessed from a derived class, except for test purposes. | ||||||
| /// </summary> | ||||||
| public Dictionary<byte, RegisterBase> RegisterMap => _registerMap; | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// The active connection settings | ||||||
| /// </summary> | ||||||
| public override I2cConnectionSettings ConnectionSettings { get; } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// The active register. | ||||||
| /// Can be set to mimic some non-standard behavior of setting a register (or if reading increases | ||||||
| /// the register pointer, which is the case on some chips) | ||||||
| /// </summary> | ||||||
| protected byte CurrentRegister | ||||||
| { | ||||||
| get | ||||||
| { | ||||||
| return _currentRegister; | ||||||
| } | ||||||
| set | ||||||
| { | ||||||
| _currentRegister = value; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Reads a byte from the bus | ||||||
| /// </summary> | ||||||
| /// <returns></returns> | ||||||
| /// <exception cref="ObjectDisposedException">The instance is disposed already</exception> | ||||||
| /// <exception cref="IOException"></exception> | ||||||
| public override byte ReadByte() | ||||||
| { | ||||||
| if (_disposed) | ||||||
| { | ||||||
| throw new ObjectDisposedException("This instance is disposed"); | ||||||
| } | ||||||
|
|
||||||
| byte[] buffer = new byte[1]; | ||||||
| if (WriteRead([], buffer) == 1) | ||||||
| { | ||||||
| return buffer[0]; | ||||||
| } | ||||||
|
|
||||||
| throw new IOException("Unable to read a byte from the device"); | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// This method implements the read operation from the device. | ||||||
| /// </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> | ||||||
|
||||||
| /// <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> |
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.
why not just creating an empty array of the same size? As seems that when you read, you don't need the data inside the existing array?
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.
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.
I though our coding style is about having 1 file per class.
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.
Does this also apply to nested classes?
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.
no, just the main classes, enums, interfaces. It's to easily find a class with the name!
Nested ones are ok in the class itself. as they are used only in the class. So, they don't need to be exposed outside of the class in a separated file.
That's the logic around all this.
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.