Device.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Linq;
  3. using HidLibrary;
  4. namespace DreamCheeky
  5. {
  6. class Device : IDisposable
  7. {
  8. private readonly byte[] statusCommand = { 0, 0, 0, 0, 0, 0, 0, 0, 2 };
  9. private readonly int vendorId = 0x1D34;
  10. private readonly int productId = 0x000D;
  11. private readonly HidDevice device;
  12. public Device()
  13. {
  14. device = HidDevices.Enumerate(vendorId, productId).FirstOrDefault();
  15. if (device == null)
  16. throw new InvalidOperationException("Device not found");
  17. }
  18. public void Open()
  19. {
  20. device.OpenDevice();
  21. }
  22. public void Close()
  23. {
  24. device.CloseDevice();
  25. }
  26. public DeviceStatus GetStatus()
  27. {
  28. if (!device.Write(statusCommand, 100))
  29. {
  30. return DeviceStatus.Errored;
  31. }
  32. HidDeviceData data = device.Read(100);
  33. if (data.Status != HidDeviceData.ReadStatus.Success)
  34. {
  35. return DeviceStatus.Errored;
  36. }
  37. return (DeviceStatus)data.Data[1];
  38. }
  39. public void Dispose()
  40. {
  41. Close();
  42. }
  43. }
  44. }