Skip to content

Instantly share code, notes, and snippets.

@DarranShepherd
Created September 28, 2014 18:10
Show Gist options
  • Save DarranShepherd/f11efef7de934ae985dc to your computer and use it in GitHub Desktop.
Save DarranShepherd/f11efef7de934ae985dc to your computer and use it in GitHub Desktop.
C# USB HID example for mbed using hidlibrary
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="hidlibrary" version="3.2.29.0" targetFramework="net45" />
</packages>
namespace UsbHid
{
using System;
using System.Linq;
using System.Threading;
using HidLibrary;
public class Program
{
private const int VendorId = 0x1234;
private const int ProductId = 0x0006;
private const int ReportLength = 8;
private static readonly Random Random = new Random();
private static HidDevice device;
public static void Main()
{
device = HidDevices.Enumerate(VendorId, ProductId).FirstOrDefault();
if (device == null)
{
Console.WriteLine("No device connected");
return;
}
Console.WriteLine("mbed found");
device.ReadReport(ReadReportCallback);
while (true)
{
var data = new byte[ReportLength];
Random.NextBytes(data);
device.WriteReport(new HidReport(ReportLength, new HidDeviceData(data, HidDeviceData.ReadStatus.Success)));
Thread.Sleep(TimeSpan.FromSeconds(0.2));
}
}
private static void ReadReportCallback(HidReport report)
{
Console.WriteLine("recv: {0}", string.Join(", ", report.Data.Select(b => b.ToString("X2"))));
device.ReadReport(ReadReportCallback);
}
}
}
@DarranShepherd
Copy link
Author

See https://mbed.org/cookbook/USBHID-bindings- for the mbed side of things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment