-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmultiple-panels.js
64 lines (49 loc) · 2.01 KB
/
multiple-panels.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const { XKeysWatcher } = require('xkeys')
/*
This example shows how multiple devices should be handled, using automaticUnitIdMode.
The main reason to use automaticUnitIdMode is that it enables us to track re-connections of the same device,
vs a new device being connected to the system.
The best way to test how it works is to have 2 panels of the same type. Connect one, then disconnect it and
notice the difference between reconnecting the same one vs a new one.
To reset the unitId of the panels, run the reset-unitId.js example.
*/
/** A persistent memory to store data for connected panels */
const memory = {}
// Set up the watcher for xkeys:
const watcher = new XKeysWatcher({
automaticUnitIdMode: true,
// If running on a system (such as some linux flavors) where the 'usb' library doesn't work, enable usePolling instead:
// usePolling: true,
// pollingInterval: 1000,
})
watcher.on('error', (e) => {
console.log('Error in XKeysWatcher', e)
})
watcher.on('connected', (xkeysPanel) => {
// This callback is called when a panel is initially connected.
// It won't be called again on reconnection (use the 'reconnected' event instead).
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`)
})
})
// To stop watching, call
// watcher.stop().catch(console.error)