Code Monkey home page Code Monkey logo

cec-controller's Introduction

cec-controller

License npm Downloads Donate Donate

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.

Usage Examples

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)
}
*/

Receive TV remote input

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); // Increase volume 3 times

Additional pre-made scripts can be found inside "test" folder.

Donation

If you like my work please support it by buying me a cup of coffee :-)

PayPal

cec-controller's People

Contributors

rafostar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.