-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduinoUsbDevice.cs
166 lines (133 loc) · 4.77 KB
/
ArduinoUsbDevice.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using LibUsbDotNet;
using LibUsbDotNet.DeviceNotify;
using LibUsbDotNet.Main;
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
namespace MonitorAutoBrightness
{
public class ArduinoUsbDevice : IDisposable
{
private readonly int _productId;
private readonly int _vendorId;
private readonly int? _portNumber;
private UsbDevice _usbDevice;
public bool IsAvailable { get; private set; }
public ArduinoUsbDevice()
: this(0x16c0, 0x05df) // Default values for the DigiSpark
{
}
public ArduinoUsbDevice(int vendorId, int productId, int? portNumber = null)
{
_vendorId = vendorId;
_productId = productId;
_portNumber = portNumber;
ConnectUsbDevice();
}
private void ConnectUsbDevice()
{
_usbDevice = null;
var devices = UsbDevice.AllDevices;
foreach (UsbRegistry usbRegistry in devices.Cast<UsbRegistry>())
{
var found = usbRegistry.Vid == _vendorId && usbRegistry.Pid == _productId;
if (_portNumber != null)
found = found && (int)usbRegistry.DeviceProperties["Address"] == _portNumber;
if (found)
{
usbRegistry.Open(out _usbDevice);
break;
}
}
IsAvailable = _usbDevice != null;
}
public string GetStringDescriptor(byte index)
{
if (IsAvailable == false)
return null;
var packet = new UsbSetupPacket((byte)UsbEndpointDirection.EndpointIn,
(byte)UsbStandardRequest.GetDescriptor,
(short)(0x0300 | index), // (usb.util.DESC_TYPE_STRING << 8) | index
0, //Language ID
255); //Length
var byteArray = new byte[256];
_usbDevice.ControlTransfer(ref packet, byteArray, byteArray.Length, out _);
return Encoding.Unicode.GetString(byteArray);
}
public bool WriteByte(byte value)
{
if (IsAvailable == false)
return false;
var packet = new UsbSetupPacket(
(byte)(UsbCtrlFlags.RequestType_Class | UsbCtrlFlags.Recipient_Device | UsbCtrlFlags.Direction_Out),
0x09, // USBRQ_HID_SET_REPORT
0x300, // (USB_HID_REPORT_TYPE_FEATURE << 8) | 0,
value, // the byte to write
0); // according to usbdevice.py this is ignored, so passing in 0
return _usbDevice.ControlTransfer(ref packet, null, 0, out _);
}
public bool WriteBytes(byte[] values)
{
if (IsAvailable == false)
return false;
bool result = true;
foreach (byte value in values)
{
result &= WriteByte(value);
}
return result;
}
public bool ReadByte(out byte value)
{
var buffer = new byte[1];
value = byte.MinValue;
if (IsAvailable == false)
return false;
var packet = new UsbSetupPacket(
(byte)(UsbCtrlFlags.RequestType_Class | UsbCtrlFlags.Recipient_Device | UsbCtrlFlags.Direction_In),
0x01, // USBRQ_HID_GET_REPORT
0x300, // (USB_HID_REPORT_TYPE_FEATURE << 8) | 0,
0, // according to usbdevice.py this is ignored, so passing in 0
1); // length
bool sendResult = _usbDevice.ControlTransfer(ref packet, buffer, 1, out int numBytesTransferred);
bool result = sendResult & (numBytesTransferred > 0);
if (result)
value = buffer[0];
return result;
}
public bool ReadBytes(out byte[] buffer, int length, uint timeout)
{
var sw = Stopwatch.StartNew();
int bytesRead = 0;
buffer = new byte[length];
while (sw.ElapsedMilliseconds < timeout && bytesRead < length)
{
while (ReadByte(out byte newByte) && bytesRead < length)
{
buffer[bytesRead++] = newByte;
}
if (bytesRead == length)
return true;
Thread.Sleep(50);
}
return false;
}
#region Dispose implementation
private bool _disposed;
~ArduinoUsbDevice()
{
Dispose();
}
public void Dispose()
{
if (!_disposed)
{
_usbDevice?.Close();
}
_disposed = true;
}
#endregion
}
}