From 27d83e27c2df1f463301171f5347f8d0496ee74e Mon Sep 17 00:00:00 2001 From: thegecko Date: Wed, 8 Jul 2020 15:01:58 +0100 Subject: [PATCH] Fix node-hid requirement --- README.md | 3 ++- examples/daplink-flash/hid.js | 3 ++- examples/daplink-serial/hid.js | 3 ++- examples/execute/hid.js | 3 ++- examples/read-registers/hid.js | 3 ++- package.json | 2 +- src/transport/hid.ts | 30 ++++-------------------------- 7 files changed, 15 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index a7a360b..999d6f5 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,8 @@ const DAPjs = require('dapjs'); let devices = hid.devices(); devices = devices.filter(device => device.vendorId === 0xD28); -const transport = new DAPjs.HID(devices[0]); +const device = new hid.HID(devices[0].path); +const transport = new DAPjs.HID(device); const daplink = new DAPjs.DAPLink(transport); try { diff --git a/examples/daplink-flash/hid.js b/examples/daplink-flash/hid.js index b420994..fe0b0f4 100644 --- a/examples/daplink-flash/hid.js +++ b/examples/daplink-flash/hid.js @@ -39,7 +39,8 @@ const getDevices = (vendorID) => { try { const program = await common.getFile(); const devices = getDevices(common.DAPLINK_VENDOR); - const device = await common.selectDevice(devices); + const selected = await common.selectDevice(devices); + const device = new hid.HID(selected.path); const transport = new DAPjs.HID(device); await common.flash(transport, program); } catch(error) { diff --git a/examples/daplink-serial/hid.js b/examples/daplink-serial/hid.js index a680e75..c2235d5 100644 --- a/examples/daplink-serial/hid.js +++ b/examples/daplink-serial/hid.js @@ -38,7 +38,8 @@ const getDevices = (vendorID) => { (async () => { try { const devices = getDevices(common.DAPLINK_VENDOR); - const device = await common.selectDevice(devices); + const selected = await common.selectDevice(devices); + const device = new hid.HID(selected.path); const transport = new DAPjs.HID(device); await common.listen(transport); } catch(error) { diff --git a/examples/execute/hid.js b/examples/execute/hid.js index 16d131f..1d0515c 100644 --- a/examples/execute/hid.js +++ b/examples/execute/hid.js @@ -40,7 +40,8 @@ const getDevices = (vendorID) => { (async () => { try { const devices = getDevices(common.DAPLINK_VENDOR); - const device = await common.selectDevice(devices); + const selected = await common.selectDevice(devices); + const device = new hid.HID(selected.path); const transport = new DAPjs.HID(device); const deviceHash = await common.deviceHash(transport, DATA); diff --git a/examples/read-registers/hid.js b/examples/read-registers/hid.js index 77967ca..380a892 100644 --- a/examples/read-registers/hid.js +++ b/examples/read-registers/hid.js @@ -38,7 +38,8 @@ const getDevices = (vendorID) => { (async () => { try { const devices = getDevices(common.DAPLINK_VENDOR); - const device = await common.selectDevice(devices); + const selected = await common.selectDevice(devices); + const device = new hid.HID(selected.path); const transport = new DAPjs.HID(device); await common.readRegisters(transport); } catch(error) { diff --git a/package.json b/package.json index bd59a04..b4b7fad 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dapjs", - "version": "2.1.0", + "version": "2.2.0", "description": "JavaScript interface to on-chip debugger (CMSIS-DAP)", "homepage": "https://github.com/ARMmbed/dapjs", "license": "MIT", diff --git a/src/transport/hid.ts b/src/transport/hid.ts index 2d0b718..af6aa12 100644 --- a/src/transport/hid.ts +++ b/src/transport/hid.ts @@ -22,7 +22,7 @@ */ import { platform } from 'os'; -import type { HID as nodeHID, Device } from 'node-hid'; +import type { HID as nodeHID } from 'node-hid'; import { Transport } from './'; /** @@ -31,20 +31,13 @@ import { Transport } from './'; export class HID implements Transport { private os: string = platform(); - private path: string; - private device?: nodeHID; public readonly packetSize = 64; /** * HID constructor * @param path Path to HID device to use */ - constructor(deviceOrPath: Device | string) { - const isDevice = (source: Device | string): source is Device => { - return (source as Device).path !== undefined; - }; - - this.path = isDevice(deviceOrPath) ? deviceOrPath.path! : deviceOrPath; + constructor(private device: nodeHID) { } /** @@ -52,12 +45,7 @@ export class HID implements Transport { * @returns Promise */ public async open(): Promise { - if (!this.path.length) { - throw new Error('No path specified'); - } - - const hid = require('node-hid'); - this.device = new hid.HID(this.path); + return; } /** @@ -65,9 +53,7 @@ export class HID implements Transport { * @returns Promise */ public async close(): Promise { - if (this.device) { - this.device.close(); - } + this.device.close(); } /** @@ -75,10 +61,6 @@ export class HID implements Transport { * @returns Promise of DataView */ public async read(): Promise { - if (!this.device) { - throw new Error('No device opened'); - } - const array = await new Promise((resolve, reject) => { this.device!.read((error: string, data: number[]) => { if (error) { @@ -99,10 +81,6 @@ export class HID implements Transport { * @returns Promise */ public async write(data: BufferSource): Promise { - if (!this.device) { - throw new Error('No device opened'); - } - const isView = (source: ArrayBuffer | ArrayBufferView): source is ArrayBufferView => { return (source as ArrayBufferView).buffer !== undefined; };