Skip to content

Commit

Permalink
Abstraction for USB Connection
Browse files Browse the repository at this point in the history
This abstracts the method for getting the USB device. Right now we care
about HIDUSB but we could use another library at another time.
  • Loading branch information
mikejr83 committed Jul 5, 2018
1 parent e82d032 commit 228f337
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 25 deletions.
5 changes: 5 additions & 0 deletions POC/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const keyboard = new Keyboard();
* Now, this actually finds the keyboard. Internally the keyboard object will track
* the device, but if you really want access to the low-level HID device this will
* return it.
*
* NOTE: THE FIND METHOD CAN TAKE THE VENDOR AND PRODUCT IDS ALONG WITH A HOST OF USB
* INFO TO MAKE THE CONNECTION. THE DEFAULT CALL USES THE INFO NECESSARY TO CONNECT
* TO THE Q5.
*/
const hidDevice = keyboard.find();

Expand All @@ -19,6 +23,7 @@ const hidDevice = keyboard.find();
keyboard.initialize();

allColor("#FF0000");
allColor("#00FF00");
allColor("#0000FF");

// sparkle();
Expand Down
27 changes: 10 additions & 17 deletions src/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,21 @@ import { FreezePacket } from "./internal/models/packets/freeze-packet";
import { InitializePacket } from "./internal/models/packets/initialize-packet";
import { TriggerPacket } from "./internal/models/packets/trigger-packet";
import { KeyState } from "./key-state";
import { USBHID } from "./usb/hid";
import { Usb } from "./usb/usb";

export class Keyboard {
private interface: number;
private vendorId: number;
private productId: number;
private usage: number;
import { findUsbDevice, Usb } from "./usb";

export class Keyboard {
private usbDevice: Usb | undefined;
private sequence: number = 0;

constructor(vendorId?: number, productId?: number, deviceInterface?: number, usage?: number) {
this.vendorId = vendorId || 0x24f0;
this.productId = productId || 0x2020;
this.interface = deviceInterface || 2;
this.usage = usage || 165;
}
public find(vendorId?: number, productId?: number, deviceInterface?: number, usage?: number): Usb {
this.usbDevice = findUsbDevice(vendorId || 0x24f0,
productId || 0x2020,
deviceInterface || 2,
usage || 165);

this.usbDevice.connect();

public find(): Usb {
this.usbDevice = new USBHID();
this.usbDevice.connect(this.vendorId, this.productId, this.interface, this.usage);
return this.usbDevice;
}

Expand Down Expand Up @@ -80,7 +73,7 @@ export class Keyboard {
this.featureReports(new FirmwarePacket().buildPacketBytes());
const fwVer = this.readDataFromDevice();
return {
firmware: fwVer[4] + "." + fwVer[5] + "." + fwVer[6] + "." + fwVer[7],
firmware: fwVer[4] + "." + fwVer[5] + "." + fwVer[6] + "." + fwVer[7],
packetCount: fwVer[3],
};
}
Expand Down
18 changes: 12 additions & 6 deletions src/usb/hid.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
import { Device, devices, HID } from "node-hid";
import { Usb } from "./usb";

export class USBHID extends Usb {
export class UsbHid extends Usb {

private hidDevice: HID | undefined;

public connect(vendorId: number, productId: number, usbInterface: number, usage: number) {
constructor(protected vendorId: number, protected productId: number, protected deviceInterface: number, protected usage: number) {
super(vendorId, productId, deviceInterface, usage);
}

public connect() {
const device = devices().find((d: Device) => {
if (d.vendorId === vendorId && d.productId === productId) {
if (d.vendorId === this.vendorId && d.productId === this.productId) {
if (process.platform === "darwin") {
return d.usage === usage;
return d.usage === this.usage;
} else {
return d.interface === usbInterface;
return d.interface === this.deviceInterface;
}
}
return false;
});

if (device === undefined) {
throw new Error("no deviceInfo");
}
if (device.path === undefined) {
throw new Error("Unable to find device path");
}

this.hidDevice = new HID(device.path);
}

Expand Down Expand Up @@ -52,4 +58,4 @@ export class USBHID extends Usb {
this.hidDevice = undefined;
}
}
}
}
2 changes: 2 additions & 0 deletions src/usb/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./locator";
export * from "./usb";
9 changes: 9 additions & 0 deletions src/usb/locator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { UsbHid } from "./hid";
import { Usb } from "./usb";

export function findUsbDevice(vendorId: number, productId: number, deviceInterface: number, usage: number): Usb {
// At some point we will have logic to determine which library to use...
// Right now just use the HID one...

return new UsbHid(vendorId, productId, deviceInterface, usage);
}
8 changes: 6 additions & 2 deletions src/usb/usb.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export abstract class Usb {
public abstract connect(vendorId: number, productId: number, usbInterface: number, usage: number): void;
constructor(protected vendorId: number, protected productId: number, protected deviceInterface: number, protected usage: number) {

}

public abstract connect(): void;
public abstract read(): number[];
public abstract write(data: number[]): void;
public abstract disconnect(): void;
}
}

0 comments on commit 228f337

Please sign in to comment.