Updated readme.md
This commit is contained in:
parent
8529cdc43f
commit
f6c0250fe6
115
README.md
115
README.md
|
@ -2,3 +2,118 @@
|
||||||
|
|
||||||
Cross platform library for HID devices.
|
Cross platform library for HID devices.
|
||||||
Works on Windows, Linux and macOS.
|
Works on Windows, Linux and macOS.
|
||||||
|
|
||||||
|
Example Code:
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
using EonaCat.HID.EventArguments;
|
||||||
|
using EonaCat.HID.Helpers;
|
||||||
|
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
static IHidManager _deviceManager;
|
||||||
|
static IHid _device;
|
||||||
|
static IEnumerable<IHid> _deviceList;
|
||||||
|
|
||||||
|
static async Task Main(string[] args)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_deviceManager = HidFactory.CreateDeviceManager();
|
||||||
|
if (_deviceManager == null)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Failed to create HID manager.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_deviceManager.OnDeviceInserted += (s, e) =>
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Inserted Device --> VID: {e.Device.VendorId:X4}, PID: {e.Device.ProductId:X4}");
|
||||||
|
};
|
||||||
|
|
||||||
|
_deviceManager.OnDeviceRemoved += (s, e) =>
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Removed Device --> VID: {e.Device.VendorId:X4}, PID: {e.Device.ProductId:X4}");
|
||||||
|
};
|
||||||
|
|
||||||
|
RefreshDevices();
|
||||||
|
|
||||||
|
if (!_deviceList.Any())
|
||||||
|
{
|
||||||
|
Console.WriteLine("No HID devices found.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
DisplayDevices();
|
||||||
|
|
||||||
|
Console.Write("Select a device index to connect: ");
|
||||||
|
int index = int.Parse(Console.ReadLine());
|
||||||
|
_device = _deviceList.ElementAt(index);
|
||||||
|
|
||||||
|
_device.OnDataReceived += (s, e) =>
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Rx Data: {BitConverter.ToString(e.Data)}");
|
||||||
|
};
|
||||||
|
|
||||||
|
_device.OnError += (s, e) =>
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Error: {e.Exception.Message}");
|
||||||
|
};
|
||||||
|
|
||||||
|
_device.Open();
|
||||||
|
Console.WriteLine($"Connected to {_device.ProductName}");
|
||||||
|
|
||||||
|
await _device.StartListeningAsync();
|
||||||
|
|
||||||
|
Console.WriteLine("Listening... Press [Enter] to send test output report.");
|
||||||
|
Console.ReadLine();
|
||||||
|
|
||||||
|
// Example: Send output report
|
||||||
|
var data = ByteHelper.HexStringToByteArray("01-02-03");
|
||||||
|
byte[] outputReport = new byte[data.Length + 1];
|
||||||
|
outputReport[0] = 0x00; // Report ID
|
||||||
|
Array.Copy(data, 0, outputReport, 1, data.Length);
|
||||||
|
|
||||||
|
await _device.WriteOutputReportAsync(outputReport);
|
||||||
|
Console.WriteLine($"Sent output report: {BitConverter.ToString(outputReport)}");
|
||||||
|
|
||||||
|
Console.WriteLine("Press [Enter] to exit...");
|
||||||
|
Console.ReadLine();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"[EXCEPTION] {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void RefreshDevices(ushort? vendorId = null, ushort? productId = null)
|
||||||
|
{
|
||||||
|
_deviceList = _deviceManager.Enumerate(vendorId, productId);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void DisplayDevices()
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
foreach (var dev in _deviceList)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"[{i++}] {dev.ProductName} | VID:PID = {dev.VendorId:X4}:{dev.ProductId:X4}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class ByteHelper
|
||||||
|
{
|
||||||
|
public static byte[] HexStringToByteArray(string hex)
|
||||||
|
{
|
||||||
|
return hex
|
||||||
|
.Replace("-", "")
|
||||||
|
.Replace(" ", "")
|
||||||
|
.ToUpper()
|
||||||
|
.Where(c => Uri.IsHexDigit(c))
|
||||||
|
.Select((c, i) => new { c, i })
|
||||||
|
.GroupBy(x => x.i / 2)
|
||||||
|
.Select(g => byte.Parse($"{g.First().c}{g.Last().c}", NumberStyles.HexNumber))
|
||||||
|
.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
Loading…
Reference in New Issue