-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
78 lines (66 loc) · 1.84 KB
/
Copy pathindex.js
File metadata and controls
78 lines (66 loc) · 1.84 KB
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
class WebUsbCp2102 {
constructor(options = {}) {
const defaultSettings = {
baudRate: 115200,
interfaceNumber: 0,
endpointNumber: 2,
configurationNumber: 1,
filters: [ { 'vendorId': 0x10c4, 'productId': 0xea60 } ],
onDisconnect: () => {},
};
this.usb = null
this.settings = { ...defaultSettings, ...options };
}
async connect() {
const filters = this.settings.filters;
navigator.usb.ondisconnect = this.settings.onDisconnect;
const devices = await navigator.usb.getDevices()
if (devices.length > 0) {
this.usb = devices[0]
} else {
this.usb = await navigator.usb.requestDevice({ filters })
}
}
async close() {
if (this.usb) {
await this.usb.close();
navigator.usb.ondisconnect = null;
this.usb = null;
}
}
async initialize() {
await this.usb.open();
console.log('USB device', this.usb);
await this.usb.selectConfiguration(this.settings.configurationNumber);
await this.usb.claimInterface(this.settings.interfaceNumber);
await this.usb.controlTransferOut({
requestType: 'vendor',
recipient: 'device',
request: 0x00,
index: 0x00,
value: 0x01
});
await this.usb.controlTransferOut({
requestType: 'vendor',
recipient: 'device',
request: 0x07,
index: 0x00,
value: 0x03 | 0x0100 | 0x0200
});
await this.usb.controlTransferOut({
requestType: 'vendor',
recipient: 'device',
request: 0x01,
index: 0x00,
value: 0x384000 / this.settings.baudRate
});
}
async read(length=64) {
const r = await this.usb.transferIn(this.settings.endpointNumber, length);
return new Uint8Array(r.data.buffer);
}
async write(data) {
await this.usb.transferIn(this.settings.endpointNumber, data);
}
}
export default WebUsbCp2102;