提交 26e1b075 编写于 作者: Z Zhang Yuexin 提交者: Jose Perez Rodriguez

Added WriteRead() method in I2cDevice (#452)

* added WriteRead()

* add comments
上级 b829312e
......@@ -214,6 +214,33 @@ namespace System.Device.I2c.Drivers
}
}
/// <summary>
/// Performs an atomic operation to write data to and then read data from the I2C bus on which the device is connected,
/// and sends a restart condition between the write and read operations.
/// </summary>
/// <param name="writeBuffer">
/// The buffer that contains the data to be written to the I2C device.
/// The data should not include the I2C device address.</param>
/// <param name="readBuffer">
/// The buffer to read the data from the I2C device.
/// The length of the buffer determines how much data to read from the I2C device.
/// </param>
public override unsafe void WriteRead(ReadOnlySpan<byte> writeBuffer, Span<byte> readBuffer)
{
if (readBuffer.Length == 0)
throw new ArgumentException($"{nameof(readBuffer)} cannot be empty.");
Initialize();
fixed (byte* writeBufferPointer = writeBuffer)
{
fixed (byte* readBufferPointer = readBuffer)
{
Transfer(writeBufferPointer, readBufferPointer, writeBuffer.Length, readBuffer.Length);
}
}
}
public override void Dispose(bool disposing)
{
if (_deviceFileDescriptor >= 0)
......
......@@ -19,5 +19,7 @@ namespace System.Device.I2c.Drivers
public override void Write(ReadOnlySpan<byte> buffer) => throw new PlatformNotSupportedException();
public override void WriteByte(byte value) => throw new PlatformNotSupportedException();
public override void WriteRead(ReadOnlySpan<byte> writeBuffer, Span<byte> readBuffer) => throw new PlatformNotSupportedException();
}
}
......@@ -18,5 +18,7 @@ namespace System.Device.I2c.Drivers
public override void WriteByte(byte value) => throw new PlatformNotSupportedException();
public override void Write(ReadOnlySpan<byte> buffer) => throw new PlatformNotSupportedException();
public override void WriteRead(ReadOnlySpan<byte> writeBuffer, Span<byte> readBuffer) => throw new PlatformNotSupportedException();
}
}
......@@ -94,6 +94,27 @@ namespace System.Device.I2c.Drivers
_winI2cDevice.Write(buffer.ToArray());
}
/// <summary>
/// Performs an atomic operation to write data to and then read data from the I2C bus on which the device is connected,
/// and sends a restart condition between the write and read operations.
/// </summary>
/// <param name="writeBuffer">
/// The buffer that contains the data to be written to the I2C device.
/// The data should not include the I2C device address.</param>
/// <param name="readBuffer">
/// The buffer to read the data from the I2C device.
/// The length of the buffer determines how much data to read from the I2C device.
/// </param>
public override void WriteRead(ReadOnlySpan<byte> writeBuffer, Span<byte> readBuffer)
{
if (readBuffer.Length == 0)
throw new ArgumentException($"{nameof(readBuffer)} cannot be empty.");
byte[] byteArray = new byte[readBuffer.Length];
_winI2cDevice.WriteRead(writeBuffer.ToArray(), byteArray);
new Span<byte>(byteArray).CopyTo(readBuffer);
}
public override void Dispose(bool disposing)
{
_winI2cDevice?.Dispose();
......
......@@ -44,6 +44,19 @@ namespace System.Device.I2c
/// </param>
public abstract void Write(ReadOnlySpan<byte> buffer);
/// <summary>
/// Performs an atomic operation to write data to and then read data from the I2C bus on which the device is connected,
/// and sends a restart condition between the write and read operations.
/// </summary>
/// <param name="writeBuffer">
/// The buffer that contains the data to be written to the I2C device.
/// The data should not include the I2C device address.</param>
/// <param name="readBuffer">
/// The buffer to read the data from the I2C device.
/// The length of the buffer determines how much data to read from the I2C device.
/// </param>
public abstract void WriteRead(ReadOnlySpan<byte> writeBuffer, Span<byte> readBuffer);
public void Dispose()
{
Dispose(true);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册