Appendix G: Lua Scripts
X-600M Users Manual
Appendix G: Lua Scripts
The functionality of the X-600M can be enhanced by writing custom Lua scripts. Registers can take on
the resulting value of a Lua expression. Complex logic can be used to determine the state of a
Conditional Event
through the use of Lua expressions. Actions can perform complex functions through
the use of Lua expressions. Lua scripts can be created that run continuously in the background to
provide the exact logic and control needed for an application. Lua scripts essentially remove any
limitations as to what the X-600M can do. The following section provides details about how the Lua
scripting language is used with the X-600M. The Lua Reference Manual can be found at:
www.ControlByWeb.com/x600m/downloads.html
The X-600M has an embedded Lua interpreter (version 5.2) built into it. Almost all the functionality of a
generic Lua interpreter can be found in the X-600M. The only functionality that has been removed from
the Lua interpreter in the X-600M are things that would directly affect the security of the operating
system, or pose a threat of resource misuse that would cause the X-600M to have an undefined
behavior. The following libraries are usable from a Lua script running on the X-600M: the base library,
table library, string library, bit32 library, and math library. The 'io' library is partially available and has
been renamed to the 'file' library. There is also support for reading and writing to SQLite databases
using the LuaSQLite3 library.
Be aware that Lua expressions that are written for Registers, Conditional Events, and Actions are event
driven and have a limit on the number of Lua opcodes that will be executed. This prevents the X-600M
from hanging when Lua expressions are written with infinite loops, and improves performance as these
expressions are only run when an event triggers them to do so. These events are generally a change in
state of an I/O that the expression is dependent on, or in the case of Actions, a change in state of the
event that the action depends on.
The X-600M also supports non-event driven Lua scripts that can have infinite loops. These scripts run
continuously in the background and are configured on the
Control/Logic > Scripts
menu tab. Up to 5
loop scripts can run continuously in the background. Each script can be up to 8-Kbytes. These scripts
can provide complex and flexible control logic. In this manual, Lua Scripts that are configured under the
Registers
,
Events
, and
Actions
menu tabs are called
Lua Expressions
and the Lua scripts configured
under
Control/Logic >
Scripts
menu tab are called
Scripts
.
Global variables and functions are accessible by all lua scripts and expressions on the X-600M.
Accessing I/O, Registers, and Events in Lua scripts
Tables are the sole data structuring mechanism in Lua. This structuring fits perfectly with the X-600M.
While custom Lua scripts can create custom tables, the X-600M has three special tables built into it.
These are the io table, the register table, and the event table. The io table is indexed by the names of
the I/O configured on the X-600M. To read the current state of an I/O, a Lua script can reference the io
table. To change the state of an I/O, a Lua script can write to the io table. The same is true for the
register table, which is indexed by the names of the configured registers, and the event table, which is
indexed by the names of the configured events. As an example of this, let's create a new variable called
outdoorTemp and set it to the current value of a 1-wire temperature sensor which has been configured
under the I/O tab and named “
owSensor1
”:
outdoorTemp = io.owSensor1
You'll notice that we are using the dot notation to access the element
owSensor1
from the io table. This
could also be written as:
outdoorTemp = io["owSensor1"]
Page 124
Xytronix Research & Design, Inc.
Содержание X-600M
Страница 1: ...X 600MTitle Page Goes Here...
Страница 6: ...X 600M Users Manual Page 4 Xytronix Research Design Inc...
Страница 99: ...X 600M Users Manual Setup Pages Xytronix Research Design Inc Page 97...
Страница 116: ...Appendix B Installing New Firmware X 600M Users Manual Page 114 Xytronix Research Design Inc...