Skip to content
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

Made I2cDevice/SpiDevice ReadByte/WriteByte methods virtual #1937

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions src/System.Device.Gpio/System/Device/I2c/Devices/UnixI2cDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,11 @@ public UnixI2cDevice(UnixI2cBus bus, int deviceAddress, bool shouldDisposeBus =

public override I2cConnectionSettings ConnectionSettings => new I2cConnectionSettings(_bus.BusId, _deviceAddress);

public override unsafe byte ReadByte()
{
Span<byte> toRead = stackalloc byte[1];
Read(toRead);
return toRead[0];
}

public override unsafe void Read(Span<byte> buffer)
{
_bus.Read(_deviceAddress, buffer);
}

public override unsafe void WriteByte(byte value)
{
Span<byte> toWrite = stackalloc byte[1]
{
value
};
Write(toWrite);
}

public override unsafe void Write(ReadOnlySpan<byte> buffer)
{
_bus.Write(_deviceAddress, buffer);
Expand Down
14 changes: 12 additions & 2 deletions src/System.Device.Gpio/System/Device/I2c/I2cDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ public static I2cDevice Create(I2cConnectionSettings settings)
/// Reads a byte from the I2C device.
/// </summary>
/// <returns>A byte read from the I2C device.</returns>
public abstract byte ReadByte();
public virtual unsafe byte ReadByte()
Jimmys20 marked this conversation as resolved.
Show resolved Hide resolved
{
byte value = 0;
Span<byte> toRead = new Span<byte>(&value, 1);
Read(toRead);
return value;
}

/// <summary>
/// Reads data from the I2C device.
Expand All @@ -50,7 +56,11 @@ public static I2cDevice Create(I2cConnectionSettings settings)
/// Writes a byte to the I2C device.
/// </summary>
/// <param name="value">The byte to be written to the I2C device.</param>
public abstract void WriteByte(byte value);
public virtual unsafe void WriteByte(byte value)
{
ReadOnlySpan<byte> toWrite = new ReadOnlySpan<byte>(&value, 1);
Write(toWrite);
}

/// <summary>
/// Writes data to the I2C device.
Expand Down
14 changes: 12 additions & 2 deletions src/System.Device.Gpio/System/Device/Spi/SpiDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ public static SpiDevice Create(SpiConnectionSettings settings)
/// Reads a byte from the SPI device.
/// </summary>
/// <returns>A byte read from the SPI device.</returns>
public abstract byte ReadByte();
public virtual unsafe byte ReadByte()
{
byte value = 0;
Span<byte> toRead = new Span<byte>(&value, 1);
Read(toRead);
return value;
}

/// <summary>
/// Reads data from the SPI device.
Expand All @@ -50,7 +56,11 @@ public static SpiDevice Create(SpiConnectionSettings settings)
/// Writes a byte to the SPI device.
/// </summary>
/// <param name="value">The byte to be written to the SPI device.</param>
public abstract void WriteByte(byte value);
public virtual unsafe void WriteByte(byte value)
{
ReadOnlySpan<byte> toWrite = new ReadOnlySpan<byte>(&value, 1);
Write(toWrite);
}

/// <summary>
/// Writes data to the SPI device.
Expand Down
16 changes: 0 additions & 16 deletions src/devices/Arduino/ArduinoSpiDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,13 @@ public ArduinoBoard Board
}

public override SpiConnectionSettings ConnectionSettings { get; }
public override byte ReadByte()
{
Span<byte> dummy = stackalloc byte[1];
Read(dummy);
return dummy[0];
}

public override void Read(Span<byte> buffer)
{
ReadOnlySpan<byte> dummy = stackalloc byte[buffer.Length];
Board.Firmata.SpiTransfer(ConnectionSettings.ChipSelectLine, dummy, buffer);
}

public override void WriteByte(byte value)
{
ReadOnlySpan<byte> span = stackalloc byte[1]
{
value
};

Write(span);
}

public override void Write(ReadOnlySpan<byte> buffer)
{
Board.Firmata.SpiWrite(ConnectionSettings.ChipSelectLine, buffer, !Board.StreamUsesHardwareFlowControl);
Expand Down
18 changes: 0 additions & 18 deletions src/devices/Ft232H/Ft232HI2c.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,12 @@ public override void Read(Span<byte> buffer)
_i2cBus.Read(_deviceAddress, buffer);
}

/// <inheritdoc/>
public override byte ReadByte()
{
Span<byte> toRead = stackalloc byte[1];
Read(toRead);
return toRead[0];
}

/// <inheritdoc/>
public override void Write(ReadOnlySpan<byte> buffer)
{
_i2cBus.Write(_deviceAddress, buffer);
}

/// <inheritdoc/>
public override void WriteByte(byte value)
{
Span<byte> toWrite = stackalloc byte[1]
{
value
};
Write(toWrite);
}

/// <inheritdoc/>
public override void WriteRead(ReadOnlySpan<byte> writeBuffer, Span<byte> readBuffer)
{
Expand Down
18 changes: 0 additions & 18 deletions src/devices/Ft4222/Ft4222I2c.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,12 @@ public override void Read(Span<byte> buffer)
_i2cBus.Read(_deviceAddress, buffer);
}

/// <inheritdoc/>
public override byte ReadByte()
{
Span<byte> toRead = stackalloc byte[1];
Read(toRead);
return toRead[0];
}

/// <inheritdoc/>
public override void Write(ReadOnlySpan<byte> buffer)
{
_i2cBus.Write(_deviceAddress, buffer);
}

/// <inheritdoc/>
public override void WriteByte(byte value)
{
Span<byte> toWrite = stackalloc byte[1]
{
value
};
Write(toWrite);
}

/// <inheritdoc/>
public override void WriteRead(ReadOnlySpan<byte> writeBuffer, Span<byte> readBuffer)
{
Expand Down
18 changes: 0 additions & 18 deletions src/devices/Ft4222/Ft4222Spi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,6 @@ public override void Read(Span<byte> buffer)
}
}

/// <inheritdoc/>
public override byte ReadByte()
{
Span<byte> toRead = stackalloc byte[1];
Read(toRead);
return toRead[0];
}

/// <inheritdoc/>
public override void TransferFullDuplex(ReadOnlySpan<byte> writeBuffer, Span<byte> readBuffer)
{
Expand All @@ -168,16 +160,6 @@ public override void Write(ReadOnlySpan<byte> buffer)
}
}

/// <inheritdoc/>
public override void WriteByte(byte value)
{
Span<byte> toWrite = stackalloc byte[1]
{
value
};
Write(toWrite);
}

/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
Expand Down
16 changes: 0 additions & 16 deletions src/devices/SoftwareSpi/SoftwareSpi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,22 +220,6 @@ public override void Read(Span<byte> data)
/// <inheritdoc />
public override void Write(ReadOnlySpan<byte> data) => TransferWriteOnly(data);

/// <inheritdoc />
public override void WriteByte(byte data)
{
Span<byte> outData = stackalloc byte[1];
outData[0] = data;
Write(outData);
}

/// <inheritdoc />
public override byte ReadByte()
{
Span<byte> data = stackalloc byte[1];
Read(data);
return data[0];
}

/// <inheritdoc />
protected override void Dispose(bool disposing)
{
Expand Down