Advanced scripting - Timers
A timer is a real-time object that counts milliseconds while it runs. With timers, you can create scripts that respond to timed events in various ways. For example, this can enable a script button to perform different actions based on whether it is pressed, double-pressed, or triple-pressed.
Timers have a syntax similar to custom variables but are prefixed "@t_...", "@timer_...", or "@lt_...".
Timers prefixed with @t_ or @timer_ are global, while timers prefixed with @lt_ are local. Similar to local variables, local timers can be defined with the same name for multiple buttons or dials, with each button or dial having its own instance of the timer. Local timers will be deleted when the button or dial is unloaded, whereas global timers will continue to exist (and run, if applicable).
Timer creation
A timer is created the first time it is referenced, regardless of whether the reference is in an event or an action.
Timer actions
A timer can be controlled using four distinct actions:
{@t_mytimer:run} will start a timer. If it has been running before and not being reset, it will continue to count from where it was paused. If it has not been run before or has been reset, it will start counting from zero.
{@t_mytimer:pause} will stop/pause a timer at its current time value.
{@t_mytimer:restart} will reset a timer and start counting from zero. A restart action can be the first action for a timer; it doesn't need to have been run before in order to be restarted.
{@t_mytimer:reset} will stop/pause a timer and reset the time to zero.
Timer events
(@t_mytimer:1000) will trigger when the timer reaches 1000 milliseconds. An exact time must be defined; timer events cannot be defined using transitions or "*".
In a multi-event command, the timer event is the only event that can trigger the command; it cannot be defined together with, e.g., a (press) event since a press event and a timer event, by definition, cannot happen simultaneously.
Timer references
Timer values can be referenced in the same way as variables. In events and actions, you reference the timer name "@t_..."; the value is the number of milliseconds counted by the timer since its start. A paused timer will return the static value it had when it stopped.
Timer example
To have press events react differently depending on time, you can use variables like this simple example script. It will display whether you press, double-press or triple-press the button within 2 seconds.
[
(init)
{@pressCounter:0} {text:Counter\nInitial\nValue\n#@pressCounter#} {@t_pressCounter:reset}
]
[
(press)
@pressCounter:#@pressCounter+1#} {text:Counter\n#@pressCounter#} {@t_pressCounter:run}
]
[
(@t_pressCounter:2000) (@pressCounter:1)
{@t_pressCounter:reset} {@pressCounter:0} {text:Single\nPress}
]
[
(@t_pressCounter:2000) (@pressCounter:2)
{@t_pressCounter:reset} {@pressCounter:0} {text:Double\nPress}
]
[
(@t_pressCounter:2000) (@pressCounter:3)
{@t_pressCounter:reset} {@pressCounter:0} {text:Triple\nPress}
]