This document discusses using the Lua programming language in lab devices. It provides background on Lua, describing it as a lightweight scripting language designed for extending applications. Lua can be embedded in C/C++ applications and extended with C functions. The document gives an example of using Lua as an interpreter layer between a biological protocol and a robotic platform. It also provides code samples of integrating Lua into a lab device, with Lua running procedures and enhanced by C/C++ functions for hardware access.
1. Sample & Assay Technologies
Use of Lua in Lab Devices
Claus Kühnel, Daniel Zwirner, QIAGEN Instruments AGJanuary 2015
Lua is a powerful,
light-weight programming
language designed for
extending applications.
The language engine is
accessible as a library,
having a C API which
allows the application to
exchange data with Lua
programs and also to
extend Lua with C
functions.
Lua is also used as a
general-purpose,
stand-alone language
through the simple
command line interpreter
provided.
2. Sample & Assay Technologies
2
Initial Situation
2
Origin of Lua
Lua is designed, implemented, and maintained by
a team at PUC-Rio, the Pontifical Catholic
University of Rio de Janeiro in Brazil.
Lua was born in 1993 and raised in Tecgraf,
formerly the Computer Graphics Technology
Group of PUC-Rio.
LabLua was founded on May 2004 by Prof.
Roberto Ierusalimschy.
Lua is now housed at LabLua, a laboratory of
the Department of Computer Science of PUC-
Rio.
"Lua" (pronounced LOO-ah) means "Moon" in
Portuguese.
4. Sample & Assay Technologies
4
• To execute “biological protocols” we need an
protocol interpreter as a layer over the HAL
• Should we develop the 1001. proprietary
interpreter now?
• Searching for a suitable solution leads us to Lua
• A Friday afternoon was enough for installation
and first tests
• Culture shock for the proprietary fraction
Initial Situation
5. Sample & Assay Technologies
5
• Lua is a powerful, fast, lightweight, embeddable
scripting language.
• Lua combines simple procedural syntax with
powerful data description constructs based on
associative arrays and extensible semantics.
• Lua is dynamically typed, runs by interpreting
byte code for a register-based virtual machine,
and has automatic memory management with
incremental garbage collection, making it ideal
for configuration, scripting, and rapid
prototyping.
What is Lua?
6. Sample & Assay Technologies
6
• Lua is a proven, robust language used in
applications like Adobe's Photoshop Lightroom,
World of Warcraft & Angry Birds.
• Lua is fast, portable, embeddable
• Lua is powerful (but simple)
• Lua is small. Under Linux, the Lua interpreter
built with all standard Lua libraries takes 182 KB
and the Lua library takes 244 KB.
• Lua is well documented
• Lua is free open-source software, distributed
under the well-known MIT license
• Runs on all common operating systems
Why choose Lua?
8. Sample & Assay Technologies
8
Installing Lua (to get a playground)
Lua for Windows
Lua for Windows (LfW) combines Lua binaries, Lua libraries with a
Lua-capable editor in a single install package for the MS Windows
operating system.
LfW contains everything you need to write, run and debug Lua scripts
on Windows. A wide variety of libraries and examples are included
that are ready to use with MS Windows.
https://code.google.com/p/luaforwindows/
Lua for Linux (Debian)
apt-get install lua5.1
apt-get install lua5.1-0-dev
http://ckuehnel.ch/dokuwiki/doku.php?id=lua_on_bananapi
9. Sample & Assay Technologies
10
Some Lua code
function p(a)
io.write(a .. ", type is " .. type(a) .. "n")
end
a = 123.45
p(a)
-> 123.45, type is number
a = "hello"
p(a)
-> hello, type is string
a = 123+ "12"
p(a)
-> 135, type is number
10. Sample & Assay Technologies
12
Returning multiple variables
function f(a,b)
return a, a*b, 5
end
print(f(1,2))
-> 1 2 5
a,b,c = f(3,4)
print(a,b,c)
-> 3 12 5
t = {1,2}
print(f(unpack(t)))
-> 1 2 5
11. Sample & Assay Technologies
14
Variable number of arguments
function f(a,b, ...)
print(a,b, unpack(arg))
end
print(f(1,2))
-> 1 2
print(f(1,2,3))
-> 1 2 3
print(f(1,2, "Hi", " there"))
-> 1 2 Hi there
12. Sample & Assay Technologies
15
Set of functions that allow C to interact with Lua
• Functions to read and write Lua global variables
• Functions to call Lua functions
• Functions to register C functions within Lua
Stack-based parameter passing
C Interface API
13. Sample & Assay Technologies
16
Embedding Lua:
• Lua is packed as a library.
• The application calls lua_open to create and
initializes a Lua state.
• When the application is finished running Lua script it
calls lua_close to finalize and destroy Lua state.
Extending Lua:
• The Lua API supports the extension of Lua state by
the possibility to call C functions from Lua scripts.
Embedding Lua vs. Extending Lua
16. Sample & Assay Technologies
19
Lua Stack – Query elements
int lua_toboolean (lua_State *L, int index)
double lua_tonumber (lua_State *L, int index)
const char * lua_tostring (lua_State *L, int index)
size_t lua_strlen (lua_State *L, int index);
17. Sample & Assay Technologies
20
Lua Stack – other stack operations
void lua_pop (lua_State *L, int number)
int lua_gettop (lua_State *L)
void lua_settop (lua_State *L, int index)
void lua_pushvalue (lua_State *L, int index)
void lua_remove (lua_State *L, int index)
void lua_insert (lua_State *L, int index)
void lua_replace (lua_State *L, int index);
18. Sample & Assay Technologies
21
Enhancing Lua by C functions
http://ckuehnel.ch/dokuwiki/doku.php?id=lua_erweiterung
Embedding Lua in a C application
http://ckuehnel.ch/dokuwiki/doku.php?id=lua_embedding
19. Sample & Assay Technologies
22
Wrapper
SWIG is a software development tool that connects
programs written in C and C++ with a variety of high-level
programming languages.
http://www.swig.org/
tolua is a tool that greatly simplifies the integration of
C/C++ code with Lua. Based on a cleaned header
file, tolua automatically generates the binding code to
access C/C++ features from Lua
http://webserver2.tecgraf.puc-rio.br/~celes/tolua/
Look for examples to both wrappers in our book.
20. Sample & Assay Technologies
23
Lua in a Lab Device
HAL
movexy(x,y) ….
GUI
movexy(x,y) ….
Lua VM
….
Robotic Platform
Lua enhanced
by C/C++
functions (HAL)
Lua embedded
in C/C++
application (GUI)
21. Sample & Assay Technologies
INSTRUMENT
init()…
24
Lua in a Lab Device – some code
HAL
init()
GUI
pushButtonInitPressed() ….
LuaVM
run()… luaopen_cube() …
Robotic Platform
Lua embedded
in C/C++
application (GUI)
Lua enhanced
by C/C++
functions (HAL)
Code sample:http://ckuehnel.ch/dokuwiki/doku.php?id=lua_lab_device