Scripting Examples. Pharos LPC X, DESIGNER, LPC 1/2/4, TPC, BPS, Rio
Oglas
Oglas
Pharos Designer User Manual
Scripting Examples
In this section we will go through a number of practical examples of how script can be used with a Controller. These examples are all based on real projects that are installed and working. They do get progressively more involved, so do not worry if you don't follow the later ones - you will still be able to use script successfully to solve many problems.
If you are working through this document on your own then look out for where I ask a question and rather than reading straight on I recommend stopping and trying to answer it yourself. You will only get truly comfortable with writing scripts by doing it!
Conditions
Running A Trigger 50% Of The Time
The script below can be used to only run the trigger 50% of the time randomly
-- returns true randomly, 50% of the time return math.random(1,2) == 1
Actions
Cycling Through Different Timelines
We are installing a wall of RGB LED fixtures in a children's play area. There is a single large button that the kids are supposed to press. Each time they press it they should get a different colour or effect on the wall.
Each colour or effect would be programmed as a different timeline in Designer. The button will connect to a contact closure and so we will have a single Digital Input trigger. Rather than starting a timeline directly we will instead run the following script:
-- which timelines should we cycle through?
timeline = { 22, 14, 24, 16, 15, 17, 21 }
-- on first time of running, initialise index if not index then index = 1 end
-- start the timeline whose number is at entry 'index' get_timeline(timeline[index]):start()
-- increment index index = index + 1
-- should we go back to the beginning of the table?
if index > #timeline then -- #timeline returns the number of values in the table
- 342 -
Scripting Examples end index = 1
How would this change if we wanted each button press to choose a timeline at random rather than cycling through them in order?
-- which timelines should we cycle through?
timeline = { 22, 14, 24, 16, 15, 17, 21 }
-- use the random function to set index index = math.random(1,#timeline)
-- start the timeline whose number is at entry 'index' get_timeline(timeline[index]):start()
Of course if the timeline selection is truly random then it will sometimes select the same timeline twice in a row. If we wanted to prevent this from happening how could we do it?
-- which timelines should we cycle through?
timeline = { 22, 14, 24, 16, 15, 17, 21 }
-- find an index different from the old one while index == oldIndex do
-- use the random function to set index index = math.random(1,#timeline) end
-- store the index for next time round oldIndex = index
-- start the timeline whose number is at entry 'index' get_timeline(timeline[index]):start()
Stopping A Range Of Timelines
We need to stop a large number of timelines in one go, but not all of them.
You can use up to 32 Actions on a Trigger, so if you need to stop more than 32 Timelines at once, you will need to use a script.
You can stop a single timeline from a script with the following: get_timeline(1):stop()
This allows you to stop a single timeline from a script, but if you have a large number to stop, adding this for each timeline is a lot of work.
A FOR loop can be used to reduce the amount of scripting required.
for i=1,10 do -- run through the values 1-10 get_timeline(i):stop() -- Stop the timeline defined by i end
- 343 -
Pharos Designer User Manual
This script can be used to run through from 1 to 10 (or a different range by changing the values), and will stop the timeline with those numbers. To make this more useful, you can put it in a function which allows you to call it with any range of timeline numbers.
function stop_range(a, b) -- this defines the script as a function with two variables (a and b) for i=a,b do -- a FOR loop which runs through from a to b get_timeline(i):stop() -- stop the timeline defined by i end end stop_range(1,10) -- call the function with the variables 1 and 10
Note: It is generally best practice to define the function in a script that is run at startup, and then call the function when it is needed
Make A Timeline Loop N Times
The designer has requested that a particular timeline runs once at sunset on a Monday, but twice at sunset on a
Tuesday, three times at sunset on Wednesday, etc. He is planning to keep changing the timeline so does not want to have lots of copies.
There are actually lots of perfectly reasonable ways to solve this using script. Let's assume we have a single astronomical clock trigger that fires at sunset and runs the following script:
N = get_current_time().weekday -- 0 is Sunday, 1 is Monday,...
-- we want Sunday to be 7 rather than 0 if N == 0 then
N = 7 end get_timeline(1):start()
The timeline would be set to loop when it was programmed. We also put a flag on the timeline at the end and make a flag trigger that runs a second script:
-- decrement N
N = N - 1 if N == 0 then
-- release timeline 1 in time 5s get_timeline(1):stop(5) end
Note how this works by setting the value of the variable N in one script and then using that variable in another script, which is often a useful technique.
I have used two scripts here, but it is possible to do the same job using only one - can you see how?
In this case you would have the sunset trigger start the timeline directly and use the following script on the flag trigger:
-- is this the first time round?
- 344 -
Scripting Examples if not N or N == 0 then
N = get_current_time().weekday -- 0 is Sunday, 1 is Monday,...
end
-- we want Sunday to be 7 rather than 0 if N == 0 then
N = 7 end
-- decrement N
N = N - 1 if N == 0 then enqueue_trigger(2) -- runs action on trigger 2 end
The trick here is to detect whether it is the first time round the loop - if the Controller has started up today then N will have no value and so not N will be true, otherwise N will have been left with the value zero when the script ran yesterday. When we detect it is the first time then we set its initial value in the same way as before.
I have also used a different method to do the timeline release. Rather than calling get_timeline(num):stop() directly from the script I am causing trigger number 2 to fire. We can then configure trigger number 2 to have an action that releases the correct timeline. It is sometimes easier to write scripts like this when they are going to be sent out to a customer who does not know how to modify them. In this case all the customer needs to know is to modify the start and release timeline actions in the trigger window if they want to change which timeline is run - they do not need to modify the script.
Storing Data To The Memory Card
In the event that our controller reboots, we want it to start running the timeline that was running prior to the reboot.
The Lua library contains functions that make it possible to read and write files to the device the Lua is running on.
This includes reading and writing files on the Controller's Memory card.
running = 1
This variable will be used to store the number of the timeline that was started most recently,using a Timeline Started
Trigger (set to Any) with a Run Script action as below: running = get_trigger_variable(1)
Storing data on the memory card involves two steps, writing to the card and reading back from the card.
function writeToCard() -- Write the running timelines table to the memory card file = io.open(get_resource_path("timeline.txt"), "w+") -- Open or create a file (in write mode) called timeline.txt
if (file ~= nil) then -- Ensure the file has been opened io.output(file) -- Set the open file as the default output location for the io library io.write("running = " .. running) -- Write the line "running = [value]" to the file. The value will be the value of the variable io.flush() -- Clear the output buffer
- 345 -
Pharos Designer User Manual io.close() -- Close the file end end
Whenever the function writeToCard is called it will store the current value of the variable running to the memory card in a file called timeline.txt. The file will be in the format: running = [value]
This is the syntax for defining a variable in Lua and means that if we run the file on startup, it will set the variable running to be the value stored in the file (with no parsing required) function readFromCard() -- Read the stored running timelines table and start the timelines specified file = io.open(get_resource_path("timeline.txt"),"r") -- Open the file timeline.txt (in read mode) if it exists if file ~= nil then -- Ensure the file has been opened dofile(get_resource_path("timeline.txt")) -- Run the file to set the variable variable end end get_timeline(running):start() -- Start the timeline stored in the running
Whenever the function readFromCard is run, it will find the file called timeline.txt, run it so that the stored variable is set on the controller and then start the relevant timeline
Note: Functions should be placed in a Run Script action at Startup to ensure they are declared. They can then be used at any time within the show file.
Push Data To The Web Interface
If you are using a Custom Web Interface, it is possible to push data to it from the project file e.g. when a TPC Slider is moved. You will then need to set up some Javascript within your custom web interface to read the data in.
If we want to send the level of a TPC slider to the web interface, we would use a TPC Slider Move Trigger set to match the Slider's Key. This would have a Run Script Action attached with the following Lua Script level = get_trigger_variable(1) -- Capture the level set on the slider push_to_web("slider_level",level) -- creates a JSON packet in the form {slider_ level:level}, where level is the value stored previously
Then within the web interface, we need to use the
function to process this data.
Implementing An Interactive Game For A Science Museum
In an exhibit children are posed questions and have to select answers from an array of numbered buttons. The buttons are large with RGB backlights that are controlled by a Controller to highlight choices and indicate right and wrong answers. Questions are displayed by a slide projector which is under RS232 control from the Controller. The buttons are wired to contact closures on the Controller and on RIOs, so that the Controller can check answers and determine the progress of the game accordingly. The lighting in the rest of the room is designed to mimic a popular
TV quiz show to retain the children's interest, with different timelines for each stage of the game.
- 346 -
Scripting Examples
I am not going to work through this example - but the key point is that it should now be clear to you that a Controller could be used to implement this sort of advanced interactive exhibit with the use of script. Try breaking down the problem into discrete parts and you will find that no individual part of this is difficult - although getting it all to function together reliably would no doubt require a lot of work. The Controller is a viable alternative to custom software running on a PC and has clear advantages in terms of durability and cost.
Examples
Some Trigger Scripting examples are available on our website .
- 347 -
Prenesi
Oglas