Skip to content

Commit

Permalink
feat: add XKeys.uniqueId property, to be used with automaticUnitIdMode
Browse files Browse the repository at this point in the history
  • Loading branch information
nytamin committed Sep 6, 2021
1 parent 352e477 commit a2e6d7a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/xkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,10 @@ export class XKeys extends EventEmitter {
public _getDeviceInfo(): DeviceInfo {
return this.deviceInfo
}
/** The unique id of the xkeys-panel. Note: This is only available if options.automaticUnitIdMode is set for the Watcher */
public get uniqueId(): string {
return `${this.info.productId}_${this.unitId}`
}
/**
* Writes a Buffer to the X-keys device
*
Expand Down
67 changes: 67 additions & 0 deletions packages/node/examples/multiplePanels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const { XKeysWatcher } = require('../dist')

/*
This example shows how multiple devices should be handled, using automaticUnitIdMode.
*/

const memory = {}


// Set up the watcher for xkeys:
const watcher = new XKeysWatcher({
automaticUnitIdMode: true
})

watcher.on('connected', (xkeysPanel) => {
console.log(`A new X-keys panel of type ${xkeysPanel.info.name} connected`)

const newName = 'HAL ' + (Object.keys(memory).length + 1)

// Store the name in a persistent store:
memory[xkeysPanel.uniqueId] = {
name: newName
}
console.log(`I'm going to call this panel "${newName}", it has productId=${xkeysPanel.info.productId}, unitId=${xkeysPanel.info.unitId}`)


xkeysPanel.on('disconnected', () => {
console.log(`X-keys panel ${memory[xkeysPanel.uniqueId].name} was disconnected`)
})
xkeysPanel.on('error', (...errs) => {
console.log('X-keys error:', ...errs)
})

xkeysPanel.on('reconnected', () => {
console.log(`Hello again, ${memory[xkeysPanel.uniqueId].name}!`)
})

// Listen to pressed buttons:
xkeysPanel.on('down', (keyIndex, metadata) => {
console.log(`Button ${keyIndex} pressed`)
})
// Listen to released buttons:
// xkeysPanel.on('up', (keyIndex, metadata) => {
// console.log('Button released', keyIndex, metadata)
// })

// // Listen to jog wheel changes:
// xkeysPanel.on('jog', (index, deltaPos, metadata) => {
// console.log(`Jog ${index} position has changed`, deltaPos, metadata)
// })
// // Listen to shuttle changes:
// xkeysPanel.on('shuttle', (index, shuttlePos, metadata) => {
// console.log(`Shuttle ${index} position has changed`, shuttlePos, metadata)
// })
// // Listen to joystick changes:

// xkeysPanel.on('joystick', (index, position, metadata) => {
// console.log(`Joystick ${index} position has changed`, position, metadata) // {x, y, z}
// })
// // Listen to t-bar changes:
// xkeysPanel.on('tbar', (index, position, metadata) => {
// console.log(`T-bar ${index} position has changed`, position, metadata)
// })
})

// To stop watching, call
// watcher.stop().catch(console.error)
4 changes: 2 additions & 2 deletions packages/node/src/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export class XKeysWatcher extends EventEmitter {
this.debugLog('handleNewDevice', devicePath)

setupXkeysPanel(devicePath)
.then((xkeysPanel) => {
.then((xkeysPanel: XKeys) => {
this.setupXkeysPanels.push(xkeysPanel)
// Since this is async, check if the panel is still connected
if (this.seenDevicePaths[devicePath]) {
Expand All @@ -215,7 +215,7 @@ export class XKeysWatcher extends EventEmitter {
xkeysPanel.setUnitId(this._getNextUniqueId(xkeysPanel)) // the lookup-cache is stored either in memory, or preferrably on disk
}
// the PID+UID pair is enough to uniquely identify a panel.
const uniqueIdentifier = `${xkeysPanel.info.productId}_${xkeysPanel.unitId}`
const uniqueIdentifier: string = xkeysPanel.uniqueId
const previousXKeysPanel = this.prevConnectedIdentifiers[uniqueIdentifier]
if (previousXKeysPanel) {
// This panel has been connected before.
Expand Down

0 comments on commit a2e6d7a

Please sign in to comment.