Skip to content

Commit

Permalink
Merge pull request #105 from badcel/restore-read-method-signatures
Browse files Browse the repository at this point in the history
Restore read method signatures
  • Loading branch information
badcel authored Aug 18, 2024
2 parents e7c2f31 + fa20f87 commit ca300b2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
37 changes: 37 additions & 0 deletions src/HidApi.Net/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ public void Write(ReadOnlySpan<byte> data)
HidException.Throw(handle);
}

/// <summary>
/// Returns an input report.
/// </summary>
/// <param name="maxLength">Max length of the expected data. The value can be greater than the actual report.</param>
/// <param name="milliseconds">timeout in milliseconds. -1 for blocking mode.</param>
/// <returns>The received data of the HID device. If the timeout is exceeded an empty result is returned.</returns>
/// <exception cref="ArgumentOutOfRangeException">Raised if maxlength is smaller than 0</exception>
/// <exception cref="HidException">Raised on failure</exception>
public ReadOnlySpan<byte> ReadTimeout(int maxLength, int milliseconds)
{
if (maxLength < 0)
throw new ArgumentOutOfRangeException(nameof(maxLength), maxLength, "Please provide a positive value");

var buffer = new Span<byte>(new byte[maxLength]);
var bytesRead = ReadTimeout(buffer, milliseconds);

return buffer[..bytesRead];
}

/// <summary>
/// Returns an input report.
/// </summary>
Expand All @@ -82,6 +101,24 @@ public int ReadTimeout(Span<byte> buffer, int milliseconds)
return result;
}

/// <summary>
/// Returns an input report.
/// </summary>
/// <param name="maxLength">Max length of the expected data. The value can be greater than the actual report.</param>
/// <returns>The received data of the HID device. If non-blocking mode is enabled and no data is available an empty result will be returned.</returns>
/// <exception cref="ArgumentOutOfRangeException">Raised if maxlength is smaller than 0</exception>
/// <exception cref="HidException">Raised on failure</exception>
public ReadOnlySpan<byte> Read(int maxLength)
{
if (maxLength < 0)
throw new ArgumentOutOfRangeException(nameof(maxLength), maxLength, "Please provide a positive value");

var buffer = new Span<byte>(new byte[maxLength]);
var bytesRead = Read(buffer);

return buffer[..bytesRead];
}

/// <summary>
/// Returns an input report.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/HidApi.Net/HidApi.Net.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>2.0.0</VersionPrefix>
<VersionPrefix>1.1.0</VersionPrefix>

<RootNamespace>HidApi</RootNamespace>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down

0 comments on commit ca300b2

Please sign in to comment.