Last active
March 17, 2022 02:25
-
-
Save happycodelucky/a9a165f9a799ddc826d2996bedb2d598 to your computer and use it in GitHub Desktop.
Using Ortho as HID in Node.js (Elided, untested code)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { HID as OrthoHID, setDriverType, devices } from 'node-hid' | |
// Command functions | |
const commandMap = new Map<number, (data: Uint8Array) => void>() | |
/** | |
* HID Controller for Ortho | |
*/ | |
export class OrthoRemoteHIDController { | |
/** | |
* HID | |
*/ | |
readonly hid: OrthoHID | |
/** | |
* | |
*/ | |
constructor(hid: OrthoHID) { | |
this.hid = hid | |
} | |
/** | |
* Connect to Ortho Remote | |
*/ | |
async connect(): Promise<boolean> { | |
// Do not wait to read events | |
this.hid.setNonBlocking(true) | |
// Subscribe to all events | |
this.bindToOrthoRemoteEvents(this.hid) | |
// Do other connecting (the async stuff) | |
return true | |
} | |
// | |
// Private functions | |
// | |
private bindToOrthoRemoteEvents(hid: OrthoHID) { | |
hid.on('data', (data: Uint8Array) => { | |
const command = commandMap.get(data[0]) | |
if (command) { | |
command.call(this, data.slice(1)) | |
} | |
}) | |
hid.on('error', (err: Error) => { | |
console.error(`Ortho HID error ${err.message}`) | |
}) | |
} | |
} | |
// | |
// Command functions | |
// | |
// Volume Down | |
commandMap.set(0x01, function (this: OrthoRemoteHIDController, data) { | |
console.log("Volume Down") | |
}) | |
// Volume Up | |
commandMap.set(0x02, function (this: OrthoRemoteHIDController, data) { | |
console.log("Volume Up") | |
}) | |
// Toggle play pause | |
commandMap.set(0x04, function (this: OrthoRemoteHIDController, data) { | |
console.log("Toggle play pause") | |
}) | |
// Next track | |
commandMap.set(0x08, function (this: OrthoRemoteHIDController, data) { | |
console.log("Next track") | |
}) | |
// Prev track | |
commandMap.set(0x10, function (this: OrthoRemoteHIDController, data) { | |
console.log("Prev track") | |
}) | |
// Long press | |
commandMap.set(0x20, function (this: OrthoRemoteHIDController, data) { | |
console.log("Long press") | |
}) | |
async function main() { | |
setDriverType('hidraw') | |
let orthoDevice: OrthoHID | undefined | |
do { | |
const hidDevice = devices().find(device => { | |
return (device.vendorId === 1104 && device.productId === 4) | |
}) | |
if (hidDevice) { | |
try { | |
orthoDevice = new OrthoHID(hidDevice.path!) | |
} catch (err) { | |
// Do nothing... | |
} | |
break | |
} | |
console.log('Waiting for Ortho Remote device (retrying in 5 second)...\n') | |
await (new Promise(r => setTimeout(r, 5000))) | |
} while (!orthoDevice) | |
if (!orthoDevice) { | |
console.error('\nNo Ortho Remote connected!\n') | |
process.exit(1) | |
} | |
const orthoController = new OrthoRemoteHIDController(orthoDevice) | |
await orthoController.connect() | |
orthoDevice.once('disconnect', () => { | |
process.exit(1) | |
}) | |
} | |
// Call main | |
main().catch(err => { | |
console.error(`Connection error: ${err.message}`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment