Code Examples#

Browse our collection of ready-to-use code samples for common BrightSign development tasks.

BrightScript Examples#

JavaScript Examples#


BrightScript Examples#

Basic Video Player#

A simple video player that loops a single video.

Sub Main()
    msgPort = CreateObject("roMessagePort")

    player = CreateObject("roVideoPlayer")
    player.SetPort(msgPort)
    player.PlayFile("SD:/videos/content.mp4")

    while true
        msg = wait(0, msgPort)
        if type(msg) = "roVideoEvent" then
            if msg.GetInt() = 8 then  ' Media ended
                player.PlayFile("SD:/videos/content.mp4")
            end if
        end if
    end while
End Sub

Playlist Player#

Play through a list of videos in sequence.

Sub Main()
    msgPort = CreateObject("roMessagePort")

    playlist = [
        "SD:/videos/video1.mp4",
        "SD:/videos/video2.mp4",
        "SD:/videos/video3.mp4"
    ]
    currentIndex = 0

    player = CreateObject("roVideoPlayer")
    player.SetPort(msgPort)
    player.PlayFile(playlist[currentIndex])

    while true
        msg = wait(0, msgPort)
        if type(msg) = "roVideoEvent" then
            if msg.GetInt() = 8 then
                currentIndex = (currentIndex + 1) MOD playlist.Count()
                player.PlayFile(playlist[currentIndex])
            end if
        end if
    end while
End Sub

HTTP Client#

Fetch data from a REST API.

Function FetchJson(url$ As String) As Object
    xfer = CreateObject("roUrlTransfer")
    xfer.SetUrl(url$)
    xfer.AddHeader("Accept", "application/json")

    response$ = xfer.GetToString()
    if response$ = "" then
        return invalid
    end if

    return ParseJson(response$)
End Function

Sub Main()
    data = FetchJson("https://api.example.com/config")
    if data <> invalid then
        print "Fetched: " + FormatJson(data)
    else
        print "Failed to fetch data"
    end if
End Sub

Timer Events#

Execute code at regular intervals.

Sub Main()
    msgPort = CreateObject("roMessagePort")

    timer = CreateObject("roTimer")
    timer.SetPort(msgPort)
    timer.SetElapsed(30, 0)  ' 30 seconds
    timer.Start()

    print "Timer started"

    while true
        msg = wait(0, msgPort)
        if type(msg) = "roTimerEvent" then
            print "Timer fired at " + CreateObject("roDateTime").ToIsoString()
            PerformScheduledTask()
            timer.Start()  ' Restart timer
        end if
    end while
End Sub

Sub PerformScheduledTask()
    print "Executing scheduled task..."
End Sub

GPIO Input#

Handle button presses from GPIO.

Sub Main()
    msgPort = CreateObject("roMessagePort")

    gpio = CreateObject("roGpioControlPort")
    gpio.SetPort(msgPort)

    print "Listening for GPIO events..."

    while true
        msg = wait(0, msgPort)
        if type(msg) = "roGpioButton" then
            button = msg.GetInt()
            print "Button pressed: " + button.ToStr()
            HandleButton(button)
        end if
    end while
End Sub

Sub HandleButton(buttonId As Integer)
    if buttonId = 0 then
        print "Action: Next"
    else if buttonId = 1 then
        print "Action: Previous"
    else if buttonId = 2 then
        print "Action: Pause"
    end if
End Sub

Configuration Manager#

Load and save JSON configuration.

Function newConfigManager(path$ As String) As Object
    return {
        path: path$,
        config: {},

        load: Function() As Object
            json$ = ReadAsciiFile(m.path)
            if json$ <> "" then
                m.config = ParseJson(json$)
                if m.config = invalid then
                    m.config = {}
                end if
            end if
            return m.config
        End Function,

        save: Function() As Boolean
            json$ = FormatJson(m.config)
            return WriteAsciiFile(m.path, json$)
        End Function,

        get: Function(key$ As String, default$ = "" As String) As String
            if m.config.DoesExist(key$) then
                return m.config[key$]
            end if
            return default$
        End Function,

        set: Function(key$ As String, value As Dynamic) As Void
            m.config[key$] = value
        End Function
    }
End Function

' Usage
Sub Main()
    config = newConfigManager("SD:/config.json")
    config.load()

    serverUrl$ = config.get("serverUrl", "https://default.com")
    print "Server: " + serverUrl$

    config.set("lastRun", CreateObject("roDateTime").ToIsoString())
    config.save()
End Sub

JavaScript Examples#

REST API Server#

Create a simple REST API with Node.js.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const http = require('http');

const server = http.createServer((req, res) => {
    res.setHeader('Content-Type', 'application/json');
    res.setHeader('Access-Control-Allow-Origin', '*');

    if (req.url === '/api/status') {
        res.end(JSON.stringify({
            status: 'ok',
            uptime: process.uptime()
        }));
    }
    else if (req.url === '/api/info') {
        res.end(JSON.stringify({
            platform: 'BrightSign',
            time: new Date().toISOString()
        }));
    }
    else {
        res.statusCode = 404;
        res.end(JSON.stringify({ error: 'Not found' }));
    }
});

server.listen(8080, () => {
    console.log('API server running on port 8080');
});

Interactive Touch UI#

HTML interface with touch support.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!DOCTYPE html>
<html>
<head>
    <style>
        .button {
            display: inline-block;
            padding: 30px 60px;
            margin: 20px;
            background: #311C6B;
            color: white;
            font-size: 24px;
            border-radius: 10px;
            cursor: pointer;
        }
        .button:active {
            background: #701896;
        }
    </style>
</head>
<body>
    <div class="button" onclick="sendCommand('play')">Play</div>
    <div class="button" onclick="sendCommand('pause')">Pause</div>
    <div class="button" onclick="sendCommand('next')">Next</div>

    <script>
        function sendCommand(cmd) {
            if (typeof BSMessagePort !== 'undefined') {
                BSMessagePort.postMessage({ command: cmd });
            }
        }
    </script>
</body>
</html>

Download Examples#

All examples are available for download:

Contributing#

Have a useful example to share? Submit it to our GitHub repository.