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 SubPlaylist 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 SubHTTP 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 SubTimer 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 SubGPIO 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 SubConfiguration 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 SubJavaScript Examples#
REST API Server#
Create a simple REST API with Node.js.
| |
Interactive Touch UI#
HTML interface with touch support.
| |
Download Examples#
All examples are available for download:
Contributing#
Have a useful example to share? Submit it to our GitHub repository.