Skip to content

Commit

Permalink
Fix node-hid requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
thegecko committed Jul 8, 2020
1 parent 274bc20 commit 27d83e2
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 32 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion examples/daplink-flash/hid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion examples/daplink-serial/hid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion examples/execute/hid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion examples/read-registers/hid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
30 changes: 4 additions & 26 deletions src/transport/hid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 './';

/**
Expand All @@ -31,54 +31,36 @@ 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) {
}

/**
* Open device
* @returns Promise
*/
public async open(): Promise<void> {
if (!this.path.length) {
throw new Error('No path specified');
}

const hid = require('node-hid');
this.device = new hid.HID(this.path);
return;
}

/**
* Close device
* @returns Promise
*/
public async close(): Promise<void> {
if (this.device) {
this.device.close();
}
this.device.close();
}

/**
* Read from device
* @returns Promise of DataView
*/
public async read(): Promise<DataView> {
if (!this.device) {
throw new Error('No device opened');
}

const array = await new Promise<number[]>((resolve, reject) => {
this.device!.read((error: string, data: number[]) => {
if (error) {
Expand All @@ -99,10 +81,6 @@ export class HID implements Transport {
* @returns Promise
*/
public async write(data: BufferSource): Promise<void> {
if (!this.device) {
throw new Error('No device opened');
}

const isView = (source: ArrayBuffer | ArrayBufferView): source is ArrayBufferView => {
return (source as ArrayBufferView).buffer !== undefined;
};
Expand Down

0 comments on commit 27d83e2

Please sign in to comment.