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.
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.
Sample & Assay Technologies
3
Initial Situation
3
Initial Situation
Robotic Hardware (HAL)
movexy(x,y) ….
Biological
Protocol
Protocol
Interpreter ….
Robotic Platform
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
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?
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?
Sample & Assay Technologies
7
Lua Documentation
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
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
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
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
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
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
Sample & Assay Technologies
17
Lua Stack – LIFO principle
C-> Lua Lua -> C
void lua_pushnil() lua_isnil()
void lua_pushboolean() int lua_toboolean
void lua_pushnumber() double lua_tonumber
void lua_pushstring() const char* lua_tostring
void lua_pushlstring() size_t lua_strlen
void lua_gettable
void lua_settable
Sample & Assay Technologies
18
Lua Stack – Pushing elements
void lua_pushnil (lua_State *L)
void lua_pushboolean (lua_State *L, int bool);
void lua_pushnumber (lua_State *L, double n);
void lua_pushlstring (lua_State *L, const char *s, size_t length);
void lua_pushstring (lua_State *L, const char *s);
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);
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);
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
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.
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)
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

Use of Lua in Lab Devices

  • 1.
    Sample & AssayTechnologies 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 & AssayTechnologies 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.
  • 3.
    Sample & AssayTechnologies 3 Initial Situation 3 Initial Situation Robotic Hardware (HAL) movexy(x,y) …. Biological Protocol Protocol Interpreter …. Robotic Platform
  • 4.
    Sample & AssayTechnologies 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 & AssayTechnologies 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 & AssayTechnologies 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?
  • 7.
    Sample & AssayTechnologies 7 Lua Documentation
  • 8.
    Sample & AssayTechnologies 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 & AssayTechnologies 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 & AssayTechnologies 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 & AssayTechnologies 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 & AssayTechnologies 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 & AssayTechnologies 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
  • 14.
    Sample & AssayTechnologies 17 Lua Stack – LIFO principle C-> Lua Lua -> C void lua_pushnil() lua_isnil() void lua_pushboolean() int lua_toboolean void lua_pushnumber() double lua_tonumber void lua_pushstring() const char* lua_tostring void lua_pushlstring() size_t lua_strlen void lua_gettable void lua_settable
  • 15.
    Sample & AssayTechnologies 18 Lua Stack – Pushing elements void lua_pushnil (lua_State *L) void lua_pushboolean (lua_State *L, int bool); void lua_pushnumber (lua_State *L, double n); void lua_pushlstring (lua_State *L, const char *s, size_t length); void lua_pushstring (lua_State *L, const char *s);
  • 16.
    Sample & AssayTechnologies 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 & AssayTechnologies 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 & AssayTechnologies 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 & AssayTechnologies 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 & AssayTechnologies 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 & AssayTechnologies 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