using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using HidSharp;
using HidSharp.Utility;
using HidSharp.Reports;
namespace ConsoleApp1
{
class Program
{
static void WriteDeviceItemInputParserResult(HidSharp.Reports.Input.DeviceItemInputParser parser)
{
while (parser.HasChanged)
{
int changedIndex = parser.GetNextChangedIndex();
DataValue previousDataValue = parser.GetPreviousValue(changedIndex);
DataValue dataValue = parser.GetValue(changedIndex);
Console.WriteLine(string.Format(" {0}: {1} -> {2}", (Usage)dataValue.Usages.FirstOrDefault(),
previousDataValue.GetPhysicalValue(), dataValue.GetPhysicalValue()));
}
}
static void Main(string[] args)
{
HidSharpDiagnostics.EnableTracing = true;
HidSharpDiagnostics.PerformStrictChecks = true;
DeviceList list = DeviceList.Local;
list.Changed += (sender, e) => Console.WriteLine("Device list changed.");
Device[] allDeviceList = list.GetAllDevices().ToArray();
Console.WriteLine("■ All device list:");
foreach (Device dev in allDeviceList)
{
Console.WriteLine("- Name: " + dev.ToString() + "\n" + " Path: " + dev.DevicePath);
}
Console.WriteLine("-----------------------------------------------");
Stopwatch stopwatch = Stopwatch.StartNew();
HidDevice[] hidDeviceList = list.GetHidDevices().ToArray();
Console.WriteLine("■ HID device list (took {0} ms to get {1} devices):",
stopwatch.ElapsedMilliseconds, hidDeviceList.Length);
foreach (HidDevice dev in hidDeviceList)
{
Console.WriteLine("- Name: " + dev);
Console.WriteLine(" Path: " + dev.DevicePath);
try
{
ReportDescriptor reportDescriptor = dev.GetReportDescriptor();
// Lengths should match.
Debug.Assert(dev.GetMaxInputReportLength() == reportDescriptor.MaxInputReportLength);
Debug.Assert(dev.GetMaxOutputReportLength() == reportDescriptor.MaxOutputReportLength);
Debug.Assert(dev.GetMaxFeatureReportLength() == reportDescriptor.MaxFeatureReportLength);
foreach (DeviceItem deviceItem in reportDescriptor.DeviceItems)
{
foreach (uint usage in deviceItem.Usages.GetAllValues())
{
Console.WriteLine(string.Format(" Usage: {0:X4} {1}", usage, (Usage)usage));
}
{
Console.WriteLine("Opening device for 20 seconds...");
HidStream hidStream;
if (dev.TryOpen(out hidStream))
{
Console.WriteLine("Opened device.");
hidStream.ReadTimeout = Timeout.Infinite;
using (hidStream)
{
byte[] inputReportBuffer = new byte[dev.GetMaxInputReportLength()];
HidSharp.Reports.Input.HidDeviceInputReceiver inputReceiver =
reportDescriptor.CreateHidDeviceInputReceiver();
HidSharp.Reports.Input.DeviceItemInputParser inputParser =
deviceItem.CreateDeviceItemInputParser();
inputReceiver.Start(hidStream);
int startTime = Environment.TickCount;
while (true)
{
if (inputReceiver.WaitHandle.WaitOne(1000))
{
if (!inputReceiver.IsRunning) { break; } // Disconnected?
Report report;
while (inputReceiver.TryRead(inputReportBuffer, 0, out report))
{
// Parse the report if possible.
// This will return false if (for example) the report applies to a different DeviceItem.
if (inputParser.TryParseReport(inputReportBuffer, 0, report))
{
WriteDeviceItemInputParserResult(inputParser);
}
}
}
uint elapsedTime = (uint)(Environment.TickCount - startTime);
if (elapsedTime >= 20000) { break; } // Stay open for 20 seconds.
}
}
Console.WriteLine("Closed device.");
}
else
{
Console.WriteLine("Failed to open device.");
}
Console.WriteLine(" ---------------------------------------------");
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
}