Skip to content

Commit 27d83e2

Browse files
committed
Fix node-hid requirement
1 parent 274bc20 commit 27d83e2

File tree

7 files changed

+15
-32
lines changed

7 files changed

+15
-32
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ const DAPjs = require('dapjs');
134134
let devices = hid.devices();
135135
devices = devices.filter(device => device.vendorId === 0xD28);
136136

137-
const transport = new DAPjs.HID(devices[0]);
137+
const device = new hid.HID(devices[0].path);
138+
const transport = new DAPjs.HID(device);
138139
const daplink = new DAPjs.DAPLink(transport);
139140

140141
try {

examples/daplink-flash/hid.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ const getDevices = (vendorID) => {
3939
try {
4040
const program = await common.getFile();
4141
const devices = getDevices(common.DAPLINK_VENDOR);
42-
const device = await common.selectDevice(devices);
42+
const selected = await common.selectDevice(devices);
43+
const device = new hid.HID(selected.path);
4344
const transport = new DAPjs.HID(device);
4445
await common.flash(transport, program);
4546
} catch(error) {

examples/daplink-serial/hid.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ const getDevices = (vendorID) => {
3838
(async () => {
3939
try {
4040
const devices = getDevices(common.DAPLINK_VENDOR);
41-
const device = await common.selectDevice(devices);
41+
const selected = await common.selectDevice(devices);
42+
const device = new hid.HID(selected.path);
4243
const transport = new DAPjs.HID(device);
4344
await common.listen(transport);
4445
} catch(error) {

examples/execute/hid.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ const getDevices = (vendorID) => {
4040
(async () => {
4141
try {
4242
const devices = getDevices(common.DAPLINK_VENDOR);
43-
const device = await common.selectDevice(devices);
43+
const selected = await common.selectDevice(devices);
44+
const device = new hid.HID(selected.path);
4445
const transport = new DAPjs.HID(device);
4546

4647
const deviceHash = await common.deviceHash(transport, DATA);

examples/read-registers/hid.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ const getDevices = (vendorID) => {
3838
(async () => {
3939
try {
4040
const devices = getDevices(common.DAPLINK_VENDOR);
41-
const device = await common.selectDevice(devices);
41+
const selected = await common.selectDevice(devices);
42+
const device = new hid.HID(selected.path);
4243
const transport = new DAPjs.HID(device);
4344
await common.readRegisters(transport);
4445
} catch(error) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dapjs",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"description": "JavaScript interface to on-chip debugger (CMSIS-DAP)",
55
"homepage": "https://github.com/ARMmbed/dapjs",
66
"license": "MIT",

src/transport/hid.ts

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*/
2323

2424
import { platform } from 'os';
25-
import type { HID as nodeHID, Device } from 'node-hid';
25+
import type { HID as nodeHID } from 'node-hid';
2626
import { Transport } from './';
2727

2828
/**
@@ -31,54 +31,36 @@ import { Transport } from './';
3131
export class HID implements Transport {
3232

3333
private os: string = platform();
34-
private path: string;
35-
private device?: nodeHID;
3634
public readonly packetSize = 64;
3735

3836
/**
3937
* HID constructor
4038
* @param path Path to HID device to use
4139
*/
42-
constructor(deviceOrPath: Device | string) {
43-
const isDevice = (source: Device | string): source is Device => {
44-
return (source as Device).path !== undefined;
45-
};
46-
47-
this.path = isDevice(deviceOrPath) ? deviceOrPath.path! : deviceOrPath;
40+
constructor(private device: nodeHID) {
4841
}
4942

5043
/**
5144
* Open device
5245
* @returns Promise
5346
*/
5447
public async open(): Promise<void> {
55-
if (!this.path.length) {
56-
throw new Error('No path specified');
57-
}
58-
59-
const hid = require('node-hid');
60-
this.device = new hid.HID(this.path);
48+
return;
6149
}
6250

6351
/**
6452
* Close device
6553
* @returns Promise
6654
*/
6755
public async close(): Promise<void> {
68-
if (this.device) {
69-
this.device.close();
70-
}
56+
this.device.close();
7157
}
7258

7359
/**
7460
* Read from device
7561
* @returns Promise of DataView
7662
*/
7763
public async read(): Promise<DataView> {
78-
if (!this.device) {
79-
throw new Error('No device opened');
80-
}
81-
8264
const array = await new Promise<number[]>((resolve, reject) => {
8365
this.device!.read((error: string, data: number[]) => {
8466
if (error) {
@@ -99,10 +81,6 @@ export class HID implements Transport {
9981
* @returns Promise
10082
*/
10183
public async write(data: BufferSource): Promise<void> {
102-
if (!this.device) {
103-
throw new Error('No device opened');
104-
}
105-
10684
const isView = (source: ArrayBuffer | ArrayBufferView): source is ArrayBufferView => {
10785
return (source as ArrayBufferView).buffer !== undefined;
10886
};

0 commit comments

Comments
 (0)