Skip to content

Commit b5b8e90

Browse files
committed
Reworked daplink-serial example
1 parent 70257ba commit b5b8e90

File tree

9 files changed

+272
-253
lines changed

9 files changed

+272
-253
lines changed

examples/daplink-flash/common.js

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,30 @@ const readline = require('readline');
2727
const progress = require('progress');
2828
const DAPjs = require('../../');
2929

30+
// Handle single character input from the user
31+
const readHandler = (inputHandler, stream = process.stdin) => {
32+
return new Promise(resolve => {
33+
stream.setRawMode(true);
34+
stream.setEncoding('utf8');
35+
36+
const onResolve = result => {
37+
stream.off('readable', read);
38+
stream.setRawMode(false);
39+
resolve(result);
40+
}
41+
42+
const read = () => {
43+
let input;
44+
while (input = stream.read()) {
45+
inputHandler(input, onResolve);
46+
}
47+
}
48+
49+
stream.on('readable', read);
50+
});
51+
}
52+
53+
// Determine file to grab
3054
const getFile = async () => {
3155
const fileName = await getFileName();
3256
if (!fileName) {
@@ -107,22 +131,15 @@ const selectDevice = async (devices) => {
107131
console.log(`${index + 1}: ${device.name}`);
108132
});
109133

110-
const device = await new Promise(resolve => {
111-
process.stdin.setRawMode(true);
112-
process.stdin.setEncoding('utf8');
113-
process.stdin.on('readable', () => {
114-
let input;
115-
while (input = process.stdin.read()) {
116-
if (input === '\u0003') {
117-
process.exit();
118-
} else if (input !== null) {
119-
let index = parseInt(input);
120-
if (index <= devices.length) {
121-
resolve(devices[index - 1]);
122-
}
123-
}
134+
const device = await readHandler((input, resolve) => {
135+
if (input === '\u0003') {
136+
process.exit();
137+
} else if (input !== null) {
138+
let index = parseInt(input);
139+
if (index <= devices.length) {
140+
resolve(devices[index - 1]);
124141
}
125-
});
142+
}
126143
});
127144

128145
return device;

examples/daplink-serial/common.js

Lines changed: 64 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,56 +20,83 @@
2020
* SOFTWARE.
2121
*/
2222

23-
const EventEmitter = require("events");
24-
const DAPjs = require("../../");
23+
const DAPjs = require('../../');
2524

26-
const inputEmitter = new EventEmitter();
27-
process.stdin.setRawMode(true);
28-
process.stdin.setEncoding("utf8");
29-
process.stdin.on("readable", () => {
30-
let input;
31-
while (input = process.stdin.read()) {
32-
if (input !== null) {
33-
inputEmitter.emit("input", input);
25+
// Handle single character input from the user
26+
const readHandler = (inputHandler, stream = process.stdin) => {
27+
return new Promise(resolve => {
28+
stream.setRawMode(true);
29+
stream.setEncoding('utf8');
30+
31+
const onResolve = result => {
32+
stream.off('readable', read);
33+
stream.setRawMode(false);
34+
resolve(result);
35+
}
36+
37+
const read = () => {
38+
let input;
39+
while (input = stream.read()) {
40+
inputHandler(input, onResolve);
41+
}
3442
}
43+
44+
stream.on('readable', read);
45+
});
46+
}
47+
48+
// Select a device from the list
49+
const selectDevice = async (devices) => {
50+
if (devices.length === 0) {
51+
throw new Error('No devices found');
3552
}
36-
});
53+
54+
console.log('Select a device to listen to serial output:');
55+
devices.forEach((device, index) => {
56+
console.log(`${index + 1}: ${device.name}`);
57+
});
58+
59+
const device = await readHandler((input, resolve) => {
60+
if (input === '\u0003') {
61+
process.exit();
62+
} else if (input !== null) {
63+
let index = parseInt(input);
64+
if (index <= devices.length) {
65+
resolve(devices[index - 1]);
66+
}
67+
}
68+
});
69+
70+
return device;
71+
}
3772

3873
// Listen to serial output from the device
39-
function listen(transport) {
74+
const listen = async transport => {
4075
const target = new DAPjs.DAPLink(transport);
4176

77+
await target.connect();
78+
const baud = await target.getSerialBaudrate();
79+
console.log(`Listening at ${baud} baud...`);
80+
4281
target.on(DAPjs.DAPLink.EVENT_SERIAL_DATA, data => {
4382
process.stdout.write(data);
4483
});
84+
target.startSerialRead();
4585

46-
return target.connect()
47-
.then(() => {
48-
return target.getSerialBaudrate();
49-
})
50-
.then(baud => {
51-
target.startSerialRead();
52-
console.log(`Listening at ${baud} baud...`);
53-
54-
inputEmitter.addListener("input", input => {
55-
if (input === "\u0003") {
56-
process.stdin.setRawMode(false);
57-
target.stopSerialRead()
58-
59-
return target.disconnect()
60-
.then(() => {
61-
process.exit();
62-
})
63-
}
64-
65-
if (input !== null) {
66-
target.serialWrite(input);
67-
}
68-
});
86+
await readHandler((input, resolve) => {
87+
if (input === '\u0003') {
88+
resolve();
89+
} else if (input !== null) {
90+
target.serialWrite(input);
91+
}
6992
});
93+
94+
target.stopSerialRead();
95+
await target.disconnect();
7096
}
7197

7298
module.exports = {
73-
inputEmitter: inputEmitter,
74-
listen: listen
99+
DAPLINK_VENDOR: 0xD28,
100+
selectDevice,
101+
listen
75102
};

examples/daplink-serial/hid.js

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,30 @@
2020
* SOFTWARE.
2121
*/
2222

23-
const hid = require("node-hid");
24-
const common = require("./common");
25-
const DAPjs = require("../../");
23+
const hid = require('node-hid');
24+
const common = require('./common');
25+
const DAPjs = require('../../');
2626

27-
// Allow user to select a device
28-
function selectDevice(vendorID) {
29-
return new Promise((resolve, reject) => {
30-
let devices = hid.devices();
31-
devices = devices.filter(device => device.vendorId === vendorID);
27+
// List all devices
28+
const getDevices = (vendorID) => {
29+
let devices = hid.devices();
30+
devices = devices.filter(device => device.vendorId === vendorID);
3231

33-
if (devices.length === 0) {
34-
return reject("No devices found");
35-
}
36-
37-
function inputHandler(input) {
38-
if (input === "\u0003") {
39-
process.exit();
40-
}
41-
42-
let index = parseInt(input);
43-
if (index && index <= devices.length) {
44-
common.inputEmitter.removeListener("input", inputHandler);
45-
resolve(devices[index - 1]);
46-
}
47-
}
48-
common.inputEmitter.addListener("input", inputHandler);
49-
50-
console.log("Select a device to listen to serial output:");
51-
devices.forEach((device, index) => {
52-
console.log(`${index + 1}: ${device.product}`);
53-
});
54-
});
32+
return devices.map(device => ({
33+
...device,
34+
name: device.product
35+
}));
5536
}
5637

57-
selectDevice(0xD28)
58-
.then(device => {
59-
const transport = new DAPjs.HID(device);
60-
return common.listen(transport);
61-
})
62-
.catch(error => {
63-
console.error(error.message || error);
64-
process.exit();
65-
});
38+
(async () => {
39+
try {
40+
const devices = getDevices(common.DAPLINK_VENDOR);
41+
const device = await common.selectDevice(devices);
42+
const transport = new DAPjs.HID(device);
43+
await common.listen(transport);
44+
} catch(error) {
45+
console.error(error.message || error);
46+
} finally {
47+
process.exit();
48+
}
49+
})();

examples/daplink-serial/usb.js

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,65 +20,51 @@
2020
* SOFTWARE.
2121
*/
2222

23-
const usb = require("usb");
24-
const common = require("./common");
25-
const DAPjs = require("../../");
23+
const usb = require('usb');
24+
const common = require('./common');
25+
const DAPjs = require('../../');
2626

2727
// Read USB device descriptor
28-
function getStringDescriptor(device, index) {
28+
const getStringDescriptor = async (device, index) => {
29+
try {
30+
device.open();
31+
} catch (_e) {
32+
return '';
33+
}
34+
2935
return new Promise((resolve, reject) => {
30-
try {
31-
device.open();
32-
} catch (_e) {
33-
resolve("");
34-
}
3536
device.getStringDescriptor(index, (error, buffer) => {
3637
device.close();
37-
if (error) return reject(error);
38-
resolve(buffer.toString());
38+
if (error) {
39+
reject(new Error(error));
40+
} else {
41+
resolve(buffer.toString());
42+
}
3943
});
4044
});
4145
}
4246

43-
// Allow user to select a device
44-
function selectDevice(vendorID) {
45-
return new Promise((resolve, reject) => {
46-
let devices = usb.getDeviceList();
47-
devices = devices.filter(device => device.deviceDescriptor.idVendor === vendorID);
48-
49-
if (devices.length === 0) {
50-
return reject("No devices found");
51-
}
47+
// List all devices
48+
const getDevices = async (vendorID) => {
49+
let devices = usb.getDeviceList();
50+
devices = devices.filter(device => device.deviceDescriptor.idVendor === vendorID);
5251

53-
function inputHandler(input) {
54-
if (input === "\u0003") {
55-
process.exit();
56-
}
57-
58-
let index = parseInt(input);
59-
if (index && index <= devices.length) {
60-
common.inputEmitter.removeListener("input", inputHandler);
61-
resolve(devices[index - 1]);
62-
}
63-
}
64-
common.inputEmitter.addListener("input", inputHandler);
52+
for (device of devices) {
53+
device.name = await getStringDescriptor(device, device.deviceDescriptor.iProduct);
54+
}
6555

66-
console.log("Select a device to listen to serial output:");
67-
devices.forEach((device, index) => {
68-
getStringDescriptor(device, device.deviceDescriptor.iProduct)
69-
.then(name => {
70-
console.log(`${index + 1}: ${name}`);
71-
});
72-
});
73-
});
56+
return devices;
7457
}
7558

76-
selectDevice(0xD28)
77-
.then(device => {
78-
const transport = new DAPjs.USB(device);
79-
return common.listen(transport);
80-
})
81-
.catch(error => {
82-
console.error(error.message || error);
83-
process.exit();
84-
});
59+
(async () => {
60+
try {
61+
const devices = await getDevices(common.DAPLINK_VENDOR);
62+
const device = await common.selectDevice(devices);
63+
const transport = new DAPjs.USB(device);
64+
await common.listen(transport);
65+
} catch(error) {
66+
console.error(error.message || error);
67+
} finally {
68+
process.exit();
69+
}
70+
})();

0 commit comments

Comments
 (0)