From effcea8b4d211f76425ca3010f4d49ceb0782f36 Mon Sep 17 00:00:00 2001 From: EonaCat Date: Mon, 14 Jul 2025 20:26:13 +0200 Subject: [PATCH] Updated README.md --- Analyzer/AboutBox.cs | 3 + Analyzer/MainForm.cs | 3 + Analyzer/Program.cs | 2 + EonaCat.HID.Console/Program.cs | 3 + EonaCat.HID/EonaCat.HID.csproj | 2 +- EonaCat.HID/Models/HidReport.cs | 2 + README.md | 186 ++++++++++++++++---------------- 7 files changed, 110 insertions(+), 91 deletions(-) diff --git a/Analyzer/AboutBox.cs b/Analyzer/AboutBox.cs index 7eeca73..3b8ada5 100644 --- a/Analyzer/AboutBox.cs +++ b/Analyzer/AboutBox.cs @@ -5,6 +5,9 @@ using System.Windows.Forms; namespace EonaCat.HID.Analyzer { + // This file is part of the EonaCat project(s) which is released under the Apache License. + // See the LICENSE file or go to https://EonaCat.com/license for full license details. + internal partial class AboutBox : Form { public AboutBox() diff --git a/Analyzer/MainForm.cs b/Analyzer/MainForm.cs index 5933258..a8b0c2e 100644 --- a/Analyzer/MainForm.cs +++ b/Analyzer/MainForm.cs @@ -13,6 +13,9 @@ using System.Windows.Forms; namespace EonaCat.HID.Analyzer { + // This file is part of the EonaCat project(s) which is released under the Apache License. + // See the LICENSE file or go to https://EonaCat.com/license for full license details. + public partial class MainForm : Form { IHidManager _deviceManager; diff --git a/Analyzer/Program.cs b/Analyzer/Program.cs index a65eab1..2f3d061 100644 --- a/Analyzer/Program.cs +++ b/Analyzer/Program.cs @@ -3,6 +3,8 @@ using System.Windows.Forms; namespace EonaCat.HID.Analyzer { + // This file is part of the EonaCat project(s) which is released under the Apache License. + // See the LICENSE file or go to https://EonaCat.com/license for full license details. internal static class Program { /// diff --git a/EonaCat.HID.Console/Program.cs b/EonaCat.HID.Console/Program.cs index a7bc3fd..c78b68e 100644 --- a/EonaCat.HID.Console/Program.cs +++ b/EonaCat.HID.Console/Program.cs @@ -4,6 +4,9 @@ using System.Globalization; namespace EonaCat.HID.Example { + // This file is part of the EonaCat project(s) which is released under the Apache License. + // See the LICENSE file or go to https://EonaCat.com/license for full license details. + public class Program { static IHidManager _deviceManager; diff --git a/EonaCat.HID/EonaCat.HID.csproj b/EonaCat.HID/EonaCat.HID.csproj index c7e3a77..7da64d7 100644 --- a/EonaCat.HID/EonaCat.HID.csproj +++ b/EonaCat.HID/EonaCat.HID.csproj @@ -7,7 +7,7 @@ Copyright 2024 EonaCat (Jeroen Saey) latest EonaCat.HID - 1.0.3 + 1.0.4 EonaCat.HID EonaCat (Jeroen Saey) HID Devices diff --git a/EonaCat.HID/Models/HidReport.cs b/EonaCat.HID/Models/HidReport.cs index a8fc471..0f3e032 100644 --- a/EonaCat.HID/Models/HidReport.cs +++ b/EonaCat.HID/Models/HidReport.cs @@ -2,6 +2,8 @@ namespace EonaCat.HID.Models { + // This file is part of the EonaCat project(s) which is released under the Apache License. + // See the LICENSE file or go to https://EonaCat.com/license for full license details. public class HidReport { public byte ReportId { get; } diff --git a/README.md b/README.md index 8a942d0..3ff23f3 100644 --- a/README.md +++ b/README.md @@ -6,114 +6,120 @@ Works on Windows, Linux and macOS. Example Code: ```csharp -using EonaCat.HID.EventArguments; -using EonaCat.HID.Helpers; +using EonaCat.HID; +using EonaCat.HID.Models; +using System.Globalization; -class Program +namespace EonaCat.HID.Example { - static IHidManager _deviceManager; - static IHid _device; - static IEnumerable _deviceList; + // This file is part of the EonaCat project(s) which is released under the Apache License. + // See the LICENSE file or go to https://EonaCat.com/license for full license details. - static async Task Main(string[] args) + public class Program { - try + static IHidManager _deviceManager; + static IHid _device; + static IEnumerable _deviceList; + + static async Task Main(string[] args) { - _deviceManager = HidFactory.CreateDeviceManager(); - if (_deviceManager == null) + try { - Console.WriteLine("Failed to create HID manager."); - return; + _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 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.Report.Data)}"); + }; + + _device.OnError += (s, e) => + { + Console.WriteLine($"Error: {e.Exception.Message}"); + }; + + _device.Open(); + Console.WriteLine($"Connected to {_device.ProductName}"); + + await _device.StartListeningAsync(default); + + Console.WriteLine("Listening... Press [Enter] to send test output report."); + Console.ReadLine(); + + // Example: Send output report using HidReport + var data = ByteHelper.HexStringToByteArray("01-02-03"); + var reportId = (byte)0x00; // Report ID + var outputReport = new HidReport(reportId, data); + + await _device.WriteOutputReportAsync(outputReport); + Console.WriteLine($"Sent output report: Report ID: {reportId}, Data: {BitConverter.ToString(data)}"); + + Console.WriteLine("Press [Enter] to exit..."); + Console.ReadLine(); } - - _deviceManager.OnDeviceInserted += (s, e) => + catch (Exception ex) { - 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 found."); - return; + Console.WriteLine($"[EXCEPTION] {ex.Message}"); } - - 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) + + static void RefreshDevices(ushort? vendorId = null, ushort? productId = null) { - Console.WriteLine($"[EXCEPTION] {ex.Message}"); + _deviceList = _deviceManager.Enumerate(vendorId, productId); } - } - 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) + static void DisplayDevices() { - Console.WriteLine($"[{i++}] {dev.ProductName} | VID:PID = {dev.VendorId:X4}:{dev.ProductId:X4}"); + 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) + public static class ByteHelper { - 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(); + 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(); + } } } ``` \ No newline at end of file