Player APIs (BrightScript & Ja...
JavaScript APIs

tvcontroller

10min
the tvcontroller object provides a communication interface between televisions and brightsign built in products this object is available as of bos 9 0 168 multiple instances of tvcontroller can access the television from multiple domains (user node application, user html application, etc ), or you can use multiple instances of the javascript object they all access the same television hardware and they all get notified with an event when the television sends a message tvcontroller idl @brightsign/tvcontroller \[exposed=window] interface tvcontroller { promise\<void> send(array data); attribute eventhandler onreceive; }; \[exposed=window] interface receiveevent event { readonly attribute array data; }; object creation to create a tvcontroller object, load the brightsign/tvcontroller module using the require() method and then create an instance of the tvcontroller let tv controller class = require("@brightsign/tvcontroller"); let tv controller = new tv controller class(); tvcontroller use this interface to access a television (for example, to send messages) send() promise\<void> send(array data) attribute onreceive eventhandler eventhandler receiveevent use this interface to get notified with an event when the television sends a message attribute data array array this is a read only attribute messages messages are defined in the protobuf format brightsign firmware includes the javascript bindings of the protocol buffer messages in the firmware image to load the messages, load the following module let messages = require("messages pb") a single protobuf file is used for both television to brightsign built in and brightsign built in to television messages, because the same messages are used both for verification and update notifications when brightsign updates a field (for example, to set up the volume), the television sends the same message back to brightsign that the volume is updated the volume can be updated with the television user interface, via remote control brightsign also gets that as an event from the television there are some exceptions brightsign built in doesn’t send messages back to the television to confirm acknowledgement of receipt of the request message, it only sends the requested information both sides can send powersettings message to each other to prevent confusion with receiving a command and a acknowledgement, we have powersettings and powersettingsack message types powersettingsack is sent when device gets a powersettings message and switches to the requested power mode this is the proto file for the communication interface syntax = "proto3"; package messages; message whitebalance { int32 red gain = 1; // red channel gain (e g , 0 to 100) int32 green gain = 2; // green channel gain (e g , 0 to 100) int32 blue gain = 3; // blue channel gain (e g , 0 to 100) } // message to set the tv hdmi® input message videooutputsettings { enum videooutputselection { hdmi 1 = 0; hdmi 2 = 1; // add more as needed } videooutputselection selected input = 1; } // there may be more powerstatus modes in the future enum powerstatus { standby = 0; on = 1; } // message to set the power status message powersettings { powerstatus status = 1; } // powersettingsack message is an acknowledgement of the power status // change request // the sender should wait for ack after changing powersettings // via the powersettings message, or via gpio when waking the device // up message powersettingsack { powerstatus status = 1; } // message to set or get the usb device connection message usbconnectionsettings { enum usbconnection { connected to tv = 0; connected to set top box = 1; } usbconnection connection = 1; } // message to forward the ir commands received by the tv message ircommandmessage { int32 address = 1; int32 command = 2; } enum request { device info = 0; } message wifiinfo { string mac address = 1; // "xx\ xx\ xx\ xx\ xx\ xx" } message deviceinfo { string mac address = 1; // "xx\ xx\ xx\ xx\ xx\ xx" string serial no = 2; string os version = 3; // "x x x x" int32 hw revision = 4; // wifiinfo wifi info = 5; } // command message that will be sent from the set top box to the tv message tvcommand { oneof command { videooutputsettings video output settings = 1; powersettings power settings = 2; powersettingsack power settings ack = 3; usbconnectionsettings usb connection settings = 4; ircommandmessage ir command = 5; int32 volume = 6; float gamma = 7; int32 brightness = 8; int32 contrast = 9; whitebalance white balance = 10; request request = 11; deviceinfo device info = 12; } } usage examples set the tv gamma value in this one way communication, brightsign makes the request and the television updates and returns the gamma value send takes a javascript array, but serializebinary returns uint8array uint8array should be converted to array using array from() before calling send deserializebinary also expects a uint8array , but receive event returns array array should be converted to uint8array before calling deserializebinary using uint8array from() let tv controller class = require("@brightsign/tvcontroller"); let tv controller = new tv controller class(); tv controller addeventlistener("receive", (event)=>{ // deserialize the binary data let tv = messages tvcommand deserializebinary(uint8array from(event detail)); switch (tv getcommandcase()) { case messages tvcommand gamma let gamma = tv getgamma(); console log("gamma value is updated to " + gamma); break; default console log("unknown command type " + tv getcommandcase()); break; } }); let messages = require("messages pb") let tvcommand = new messages tvcommand(); tvcommand setgamma(2 3); // serialize to binary let bytes = tvcommand serializebinary(); // send it as an array rather than the uint8array returned by serializebinary tv controller send(array from(bytes)); send brightsign device information in the following example, the tv controller is set up to send a device info, when the television requests it the application sets up a receive event listener and waits for a request event only one request, device info , is currently available (anything else will error) a device info request fills the required fields and sends a message back let tv controller class = require("@brightsign/tvcontroller"); let tv controller = new tv controller class(); let messages = require("messages pb") tv controller addeventlistener("receive", (event)=>{ // deserialize the binary data let tv = messages tvcommand deserializebinary(uint8array from(event detail)); switch (tv getcommandcase()) { case messages tvcommand commandcase request let request = tv getrequest(); // we only have device info request for now if (request != messages request device info) throw new error("request is not device info"); let tvcommand = new messages tvcommand(); // create an instance of deviceinfo let deviceinfo = new messages deviceinfo(); deviceinfo setmacaddress("xx\ xx\ xx\ xx\ xx\ xx"); deviceinfo setserialno("1234567890"); deviceinfo setosversion("1 2 3 4"); deviceinfo sethwrevision(1); tvcommand setdeviceinfo(deviceinfo); // serialize to binary let bytes = tvcommand serializebinary(); tv controller send(array from(bytes)); break; default console log("unknown command type " + tv getcommandcase()); break; } });