7 Scripting
27
7.6.2
Modern API
Modern API allows more object-oriented approach to scripting. You need to explicitly use the
delay()
function if
you use the modern API and need a delay. Note that the outlet power-on sequence delay applies anyway.
Lua objects can have fields (data contained in the object) and methods (functions which affect the object's state).
Object fields are accessed with a dot
.
, like
meter.reading
. However, different object implementations in Lua
may use the colon
:
or the dot
.
to access the object's methods (
outlet:cycle()
or
outlet.cycle()
).
In the modern API, all objects use the dot
.
to access their methods, to prevent confusion.
Modern API objects and functions are grouped into several categories for convenience.
7.6.2.1
Core Lua functions
To make scripting safer, only a limited subset of Lua features is supported by sandboxing. The following Lua
standard library features are supported:
Globals:
_VERSION
,
assert
,
error
,
next
,
ipairs
,
pairs
,
pcall
,
xpcall
,
select
,
tonumber
,
tostring
,
type
,
unpack
.
string
library:
string.byte
,
string.char
,
string.find
,
string.format
,
string.gmatch
,
string.gsub
,
string.len
,
string.lower
,
string.match
,
string.rep
,
string.reverse
,
string.sub
,
string.upper
.
table
library:
table.insert
,
table.concat
,
table.maxn
,
table.remove
,
table.sort
.
math
library:
math.abs
,
math.acos
,
math.asin
,
math.atan
,
math.atan2
,
math.ceil
,
math.
←
-
cos
,
math.cosh
,
math.deg
,
math.exp
,
math.floor
,
math.fmod
,
math.frexp
,
math.huge
,
math.ldexp
,
math.log
,
math.log10
,
math.max
,
math.min
,
math.modf
,
math.pi
,
math.pow
,
math.rad
,
math.random
,
math.sin
,
math.sinh
,
math.sqrt
,
math.tan
,
math.tanh
.
os
library:
os.clock
,
os.difftime
,
os.date
,
os.time
.
Additionally,
_G
points to the sandbox environment.
Unlike most APIs, core Lua functions are available in the global context.
7.6.2.2
Delay functions
The
delay
function accepts the number of seconds to wait as an argument (it is assumed to be the script step
delay if not specified). Only a single script thread is active at any given time; switching to a different thread is not
performed until you call delay() (or other function possibly introducing a delay). If a scripting thread doesn't call
delay()
or one of the legacy API functions every now and then, it can't be terminated by
thread.kill
and
will eventually be shut down by the runtime.
The
wait_until
function can be used to wait for an arbitrary number of conditions on local time. It may have
any number of arguments, each named a condition. It waits for one of the conditions to be satisfied, and returns its
1-based index. If several conditions would be satisfied simultaneously, the first match wins.
A condition is a table which may (but does not have to) contain any set of the following keys:
•
year
, which stands for the year;
•
month
, which stands for month (1 is January);
•
day
, which stands for day of month;
DLI LPC9 User’s Guide: 1.7.24.0
Summary of Contents for LPC9
Page 1: ...DLI LPC9 User s Guide 1 7 24 0 ...
Page 81: ......