-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathsimple.js
More file actions
60 lines (51 loc) · 1.87 KB
/
Copy pathsimple.js
File metadata and controls
60 lines (51 loc) · 1.87 KB
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
'use strict';
// Minimal smoke test, mirroring the other language samples: open the first
// adapter, log traffic, list the devices on the bus, then power the TV on.
//
// cd src/nodejs && npm install && node example/simple.js
const cec = require('..');
const adapter = new cec.CecAdapter({
deviceName: 'CECNode',
deviceType: cec.CecDeviceType.RecordingDevice,
activateSource: false,
});
adapter.on('log', (m) => {
// level is a CecLogLevel bit; skip the very chatty debug traffic here.
if (m.level <= cec.CecLogLevel.Notice)
console.log(`[cec] ${m.message}`);
});
adapter.on('keyPress', (k) =>
console.log(`key: ${cec.userControlKeyToString(k.keycode)} (${k.duration}ms)`));
adapter.on('command', (c) =>
console.log(`cmd: ${cec.opcodeToString(c.opcode)} ${c.initiator}->${c.destination}`));
adapter.on('sourceActivated', (addr, on) =>
console.log(`source ${cec.logicalAddressToString(addr)} ${on ? 'activated' : 'deactivated'}`));
adapter.on('alert', (a) => console.log(`alert: ${a}`));
const found = adapter.detectAdapters();
if (found.length === 0) {
console.error('no CEC adapters found');
adapter.close();
process.exit(1);
}
const port = found[0].path;
console.log(`opening ${port} ...`);
if (!adapter.open(port)) {
console.error('failed to open adapter');
adapter.close();
process.exit(1);
}
console.log(adapter.getLibInfo());
console.log('devices on the bus:');
for (const addr of adapter.getActiveDevices()) {
const name = adapter.getDeviceOSDName(addr);
const power = cec.powerStatusToString(adapter.getDevicePowerStatus(addr));
console.log(` ${cec.logicalAddressToString(addr)}: ${name} [${power}]`);
}
console.log('powering on the TV ...');
adapter.powerOnDevices(cec.CecLogicalAddress.TV);
// Keep the process alive to stream events; Ctrl-C to quit.
process.on('SIGINT', () => {
console.log('\nclosing ...');
adapter.close();
process.exit(0);
});