Skip to content

Commit 016880e

Browse files
committed
feat: PE-1037: Add example for roCecInterface
- Added examples for sending CEC commands in brightscript and javascript
1 parent df21dc3 commit 016880e

File tree

7 files changed

+228
-0
lines changed

7 files changed

+228
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# BrightScript CEC Example
2+
3+
This example demonstrates how to send CEC (Consumer Electronics Control) commands using BrightScript on BrightSign devices.
4+
5+
## What it does
6+
- Sends an "Image View On" (display on) CEC command
7+
- Waits 15 seconds
8+
- Sends a "Standby" (display off) CEC command
9+
10+
## How to use
11+
1. Copy the `autorun.brs` and `index.html` files to the root of the SD card of your BrightSign player.
12+
2. Reboot the player.
13+
3. The script will automatically run and send the CEC commands as described.
14+
15+
## Reference
16+
- [BrightSign roCecInterface documentation](https://docs.brightsign.biz/developers/rocecinterface)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
sub Main()
2+
print "BrightScript CEC Example: Display On/Off"
3+
4+
mp = createobject("roMessagePort")
5+
cec = CreateObject("roCecInterface", "HDMI-1") ' Modify this to be 'HDMI-1', 'HDMI-2', 'HDMI-3', or 'HDMI-4' as needed
6+
if cec = invalid then
7+
print "Failed to create roCecInterface object."
8+
return
9+
end if
10+
11+
' Create a HTML widget to display something on the screen
12+
r = CreateObject("roRectangle", 0, 0, 1920, 1080)
13+
config = {
14+
url: "file:///sd:/index.html",
15+
inspector_server: {
16+
port: 2999
17+
}
18+
}
19+
htmlWidget = CreateObject("roHtmlWidget", r, config)
20+
htmlWidget.SetPort(mp)
21+
htmlWidget.Show()
22+
23+
cec.SetPort(mp)
24+
25+
' Optionally enable debugging for CEC messages
26+
' cec.EnableCecDebug("SD:/cec_debug.log")
27+
28+
' Send Image View On (4f0d)
29+
bufferImageViewOn = CreateObject("roByteArray")
30+
bufferImageViewOn.FromHexString("4f0d")
31+
cec.SendRawMessage(bufferImageViewOn)
32+
print "Sent Image View On: " + bufferImageViewOn.ToHexString()
33+
34+
' Wait 15 seconds, then send Standby
35+
sleep(15000)
36+
bufferStandby = CreateObject("roByteArray")
37+
bufferStandby.FromHexString("4f36")
38+
cec.SendRawMessage(bufferStandby)
39+
print "Sent Standby: " + bufferStandby.ToHexString()
40+
41+
' Optionally, uncomment the block below to repeat the cycle every 15 seconds
42+
' while true
43+
' sleep(15000)
44+
' cec.SendRawMessage(bufferImageViewOn)
45+
' print "Sent Image View On: " + bufferImageViewOn.ToHexString()
46+
47+
' sleep(15000)
48+
' cec.SendRawMessage(bufferStandby)
49+
' print "Sent Standby: " + bufferStandby.ToHexString()
50+
' end while
51+
52+
' Message loop to handle any incoming messages (if needed)
53+
while true
54+
msg = wait(0, mp)
55+
if type(msg) = "roMessagePortEvent"
56+
? "Message received: ";msg
57+
end if
58+
end while
59+
end sub
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html lang='en'>
3+
<head>
4+
<title>BrightScript CEC Example</title>
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<meta charset='utf-8'>
7+
<style>
8+
/* CSS to style the background image and text */
9+
html,
10+
body {
11+
width: 100%;
12+
height: 100%;
13+
margin: 0;
14+
padding: 0;
15+
color: white;
16+
}
17+
h2 {
18+
text-align: center;
19+
margin-top: 25%;
20+
}
21+
</style>
22+
</head>
23+
<body>
24+
<h2>BrightScript CEC Example to turn display on and off</h2>
25+
</body>
26+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# JavaScript CEC Example
2+
3+
This example demonstrates how to send and receive CEC (Consumer Electronics Control) commands using JavaScript on BrightSign devices.
4+
5+
## What it does
6+
- Uses the `@brightsign/cec` package to send CEC commands
7+
- Listens for CEC receive events
8+
- Sends an "Image View On" (display on) command, waits 15 seconds, then sends a "Standby" (display off) command
9+
10+
## How to use
11+
1. Copy the `autorun.brs`, `index.html` and `index.js` files to the root of the SD card of your BrightSign player.
12+
2. Reboot the player.
13+
3. The script will automatically run and send the CEC commands as described.
14+
15+
## Reference
16+
- [BrightSign @brightsign/cec documentation](https://docs.brightsign.biz/developers/cec)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function main()
2+
' Create directory to store crash-dumps (optional)
3+
dir = CreateDirectory("SD:/brightsign-dumps")
4+
if not dir then
5+
print "Could not create directory"
6+
end if
7+
8+
mp = CreateObject("roMessagePort")
9+
r = CreateObject("roRectangle", 0, 0, 1920, 1080)
10+
11+
config = {
12+
nodejs_enabled: true,
13+
security_params: {
14+
websecurity: false
15+
},
16+
url: "file:///sd:/index.html",
17+
brightsign_js_objects_enabled: true,
18+
javascript_enabled: true,
19+
inspector_server: {
20+
port: 2999
21+
}
22+
}
23+
24+
htmlWidget = CreateObject("roHtmlWidget", r, config)
25+
htmlWidget.SetPort(mp)
26+
htmlWidget.Show()
27+
28+
while true
29+
msg = wait(0, mp)
30+
if type(msg) = "roMessagePortEvent"
31+
? "Message received: ";msg
32+
end if
33+
end while
34+
35+
end function
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html lang='en'>
3+
<head>
4+
<title>JavaScript CEC Example</title>
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<meta charset='utf-8'>
7+
<style>
8+
/* CSS to style the background image and text */
9+
html,
10+
body {
11+
width: 100%;
12+
height: 100%;
13+
margin: 0;
14+
padding: 0;
15+
color: white;
16+
}
17+
h2 {
18+
text-align: center;
19+
margin-top: 25%;
20+
}
21+
</style>
22+
<script src='index.js'></script>
23+
</head>
24+
<body onload="main()">
25+
<h2>JavaScript CEC Example to turn display on and off</h2>
26+
</body>
27+
</html>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const CecClass = require('@brightsign/cec');
2+
const cec1 = new CecClass('HDMI-1'); // Modify this to be 'HDMI-1', 'HDMI-2', 'HDMI-3', or 'HDMI-4' as needed
3+
4+
cec1.addEventListener("receive", function(packet) {
5+
const frame = packet.data;
6+
console.log("frame from HDMI-1: " + toHexString(frame));
7+
});
8+
9+
const toHexString = (cecBytes) => {
10+
return Array.from(cecBytes)
11+
.map((byte) => byte.toString(16).padStart(2, '0'))
12+
.join(' ');
13+
};
14+
15+
async function sendCecCommand(buffer) {
16+
try {
17+
console.log("Sending CEC command ..." + toHexString(buffer));
18+
await cec1.send(buffer);
19+
} catch (error) {
20+
console.log("Error while sending CEC command: " + JSON.stringify(error));
21+
}
22+
}
23+
24+
function main() {
25+
// * Developer Note: Varying display manufacturers require direct addressing.
26+
// * There may be settings to configure direct address or not.
27+
28+
// Turn display "on" - image's view to on
29+
const bufferImageViewOn = new Uint8Array(2);
30+
bufferImageViewOn[0] = 0x4f;
31+
bufferImageViewOn[1] = 0x0D;
32+
33+
// Turn display "off". Some displays will go to standby mode.
34+
const bufferStandby = new Uint8Array(2);
35+
bufferStandby[0] = 0x4f;
36+
bufferStandby[1] = 0x36;
37+
38+
sendCecCommand(Array.from(bufferImageViewOn));
39+
40+
// Wait 15 seconds, then turn display off
41+
setTimeout(() => {
42+
sendCecCommand(Array.from(bufferStandby));
43+
44+
// Uncomment below to repeat the cycle every 15 seconds
45+
// setTimeout(main, 15000);
46+
}, 15000);
47+
}
48+
49+
window.main = main;

0 commit comments

Comments
 (0)