Requires CEC capable device (e.g. Raspberry Pi or USB-CEC adapter).
Additionally cec-client
must be installed. On Raspbian it is included in cec-utils package.
var CecController = require('cec-controller');
var cecCtl = new CecController();
cecCtl.on('ready', (controller) => console.log(controller));
cecCtl.on('error', console.error);
/*
{
dev0: {
name: 'TV',
logicalAddress: '0',
address: '0.0.0.0',
activeSource: 'no',
vendor: 'Samsung',
osdString: 'TV',
cecVersion: '1.4',
powerStatus: 'on',
language: 'eng',
turnOn: [Function: bound changePower], // Turn on dev0 (TV)
turnOff: [Function: bound changePower], // Turn off dev0 (TV)
changeSource: [Function] }, // Switch HDMI input (optional arg is port number)
dev4: {
name: 'Playback 1',
logicalAddress: '4',
address: '3.0.0.0',
activeSource: 'no',
vendor: 'Pulse Eight',
osdString: 'CEC-Control',
cecVersion: '1.4',
powerStatus: 'on',
language: 'eng',
turnOn: [Function: bound changePower],
turnOff: [Function: bound changePower]
},
setActive: [Function: bound changeActive], // Send source active signal (switches TV input)
setInactive: [Function: bound changeActive], // Send source inactive signal
volumeUp: [Function: bound command], // Increase amplifier volume
volumeDown: [Function: bound command], // Decrease amplifier volume
mute: [Function: bound command], // Mute amplifier
command: [Function: command] // Send custom signal (arg is send as input to cec-client)
}
*/
var CecController = require('cec-controller');
var cecCtl = new CecController();
cecCtl.on('ready', readyHandler);
cecCtl.on('error', console.error);
function readyHandler(controller)
{
console.log('Turning ON TV...');
controller.dev0.turnOn().then(() =>
{
controller.setActive();
console.log('Press any button on TV remote');
});
cecCtl.on('keypress', (keyName) => console.log(`User pressed: ${keyName}`));
}
Each function returns a Promise. They are executed asynchronously by default.
controller.dev0.turnOn();
console.log('Sending turn on signal to TV');
setTimeout(() => controller.setActive(), 5000);
console.log('Changing TV input source in 5 sec...');
Synchronous execution can be achieved by using await inside async function.
async function controlTv()
{
await controller.dev0.turnOn();
console.log('Turned on TV');
await controller.setActive();
console.log('Changed TV input source');
}
async function increaseVolume(count)
{
while(count--) await controller.volumeUp();
}
controlTv();
increaseVolume(3); // Increase volume 3 times
Additional pre-made scripts can be found inside "test" folder.
If you like my work please support it by buying me a cup of coffee :-)