cec-controller
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.
Controller scans devices on startup. It takes a while (scan is done async and result is returned in "ready" event).
Usage Examples
var CecController = require('cec-controller');
var cecCtl = new CecController();
cecCtl.on('ready', (controller) => console.log(controller));
cecCtl.on('error', console.error);
Send TV remote key presses
Send key press to your TV, player or receiver. Get the list of available key names with cecCtl.getKeyNames()
.
var CecController = require('cec-controller');
var cecCtl = new CecController();
cecCtl.on('ready', readyHandler);
cecCtl.on('error', console.error);
function readyHandler(controller)
{
controller.dev1.sendKey('up').then((success) =>
{
if(success)
console.log('Successfully send "up" key to decoder');
else
console.error('Could not send input key!');
});
}
Receive TV remote input
Use keypress
, keydown
or keyup
events to implement code logic that depends on the pressed TV remote button.
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}`));
}
Asynchronous execution
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
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);
Additional pre-made runnable scripts can be found inside "test" folder.
Donation
If you like my work please support it by buying me a cup of coffee :-)