-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
120 lines (104 loc) · 3.28 KB
/
index.js
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
/*
* == BSD2 LICENSE ==
* Copyright (c) 2018, Tidepool Project
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the associated License, which is identical to the BSD 2-Clause
* License as published by the Open Source Initiative at opensource.org.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the License for more details.
*
* You should have received a copy of the License along with this program; if
* not, you can obtain one from Tidepool Project at tidepool.org.
* == BSD2 LICENSE ==
*/
class cp2102 extends EventTarget {
constructor(usbDevice, opts) {
super();
this.device = usbDevice;
this.opts = opts;
this.device.open();
console.log('Opened:', this.device.opened);
const self = this;
(async () => {
if (this.device.configuration === null) {
console.log('selectConfiguration');
await this.device.selectConfiguration(1);
}
[self.iface] = this.device.configuration.interfaces;
console.log('Claiming interface', self.iface.interfaceNumber);
await this.device.claimInterface(self.iface.interfaceNumber);
if (this.device.configuration.interfaces == null) {
throw new Error('Please unplug device and retry.');
}
console.log('Setting baud rate to', this.opts.baudRate);
await this.device.controlTransferOut({
requestType: 'vendor',
recipient: 'device',
request: 0x00,
index: 0x00,
value: 0x01,
});
await this.device.controlTransferOut({
requestType: 'vendor',
recipient: 'device',
request: 0x07,
index: 0x00,
value: 0x03 | 0x0100 | 0x0200,
});
await this.device.controlTransferOut({
requestType: 'vendor',
recipient: 'device',
request: 0x01,
index: 0x00,
value: 0x384000 / this.opts.baudRate,
});
self.isClosing = false;
self.readLoop();
self.dispatchEvent(new Event('ready'));
})().catch((error) => {
console.log('Error during CP2102 setup:', error);
self.dispatchEvent(new CustomEvent('error', {
detail: error,
}));
});
}
write(data, cb) {
this.device.transferOut(1, data).then(() => {
cb();
}, (err) => cb(err, null));
}
async readLoop() {
let result;
try {
result = await this.device.transferIn(1, 64);
} catch (error) {
if (error.message.indexOf('LIBUSB_TRANSFER_NO_DEVICE')) {
console.log('Device disconnected');
} else {
console.log('Error reading data:', error);
}
}
if (result && result.data && result.data.byteLength) {
console.log(`Received ${result.data.byteLength} byte(s).`);
const uint8buffer = new Uint8Array(result.data.buffer);
this.dispatchEvent(new CustomEvent('data', {
detail: uint8buffer.slice(0),
}));
}
if (!this.isClosing && this.device.opened) {
this.readLoop();
}
}
close(cb) {
this.isClosing = true;
setTimeout(async () => {
await this.device.releaseInterface(0);
await this.device.close();
return cb();
}, 2000);
}
}
module.exports = cp2102;