Lua Languge

Learn Lua

  • Lua as an interpreted language.
  • Lua is case-sensitive:
  • Lua is a dynamically typed language.
  • Note :
    • No need to use terminate  ;  symbol in lua
    • Node need closures { }  for function like c.

 

Commands

  • Double hyphen  --
    • If you want to insert a single line command in program use double hyphen --  .
    • Example  ArunEworld --Variable
  • Double hyphen with double bracket --[[ ]]--
    • If you want to insert a more line commands in program use double bracket --[[ More lines ]]--
    • Example

Identifiers

and
break
do
else
elseif
end
false
for
function
if
in
local
nil
not
or
repeat
return
then
true
until
while

Note  : Lua is case-sensitive:  and  is a reserved word, but And  and AND  are two other different identifiers.

 

Basic types  of values in lua

Note : There are eight basic types in Lua. The type function gives the type name of a given value see here

nil,
boolean,
number,
string,
userdata,
function,
thread,
table

 

Variable

Lua is a dynamically typed language. There are no type definitions in the language; each value carries its own type.

Variable Declare Initialize In Lua

  • Global variable :
    • Do not need declaration.  Ex : a -- a is variable
    • Usually you do not need to delete global variables; if you need to delete a global variable, just assign nil to it: Ex a = nil
  • Local Variable :
    • Your variable is going to have a short life, you should use a local variable.
    • if you use local  keyword its consider as local variable. Ex local b --b is local variable

 

Variable Initialize Initialize In Lua

  • Initialize string  a = "www.ArunEworld.com"
  • Initialize Integer value b = 5
  • Initialize float variable c = 3.47

 

Function : print

  • print  function automatically add the "\n"  new line char at the end of the strings
  • print ArunEworld single line  print("ArunEworld") also same as print "ArunEworld"
  • print the multi lines like
  • Printing strings (Lua consider only string if it’s char also string
  • Printing numbers (Lua considers only number not like integer, float)
  • Print the function returns value
    • For example consider the wifi.sta.getip()  function is return the IP address of the device. So print the the IP address Ex : print(wifi.sta.getip())

 

Concatenation

  • Print the two values in same line using concatenation operator  double dot ..
    • Example 1  :  print(" Hello ".."World")--> Hello World
    • Example 2 :  print("1".."2")-->12

 

Precedence

Operator precedence in Lua follows the table below, from the higher to the lower priority:

^
not – (unary)
* /
+ –
..
< > <= >= ~= ==
and
or

 

Function type

The type function gives the type name of a given value:

Example : 1

  • The last example will result in  "string"  no matter the value of X , because the result of  type  is always a string.

     

Example 2 :

 

Function tonumber

  • If you need to convert a string to a number explicitly, you can use the function tonumber ,

     

Table

Example

 

 

Statements

if then else

  • if then else Loop

if then elseif then end

  • if then elseif then loo

While

  • While loop : As usual, Lua first tests the while condition; if the condition is false, then the loop ends; otherwise, Lua executes the body of the loop and repeats the process.

repeat

  • repeat loop :   As the name implies, a repeatuntil statement repeats its body until its condition is true. The test is done after the body, so the body is always executed at least once.

     

for

  • The for statement has two variants: the numeric for and the generic for.
  • Numeric for : A numeric for has the following syntax:

     

break and return

  • break and return statement

     

Compilation, Execution and Errors

  • Lua always precompiles source code to an intermediate form before running it.
  • dofile function
    • Compile and execute the file using dofile(ArunEworld.lua)  or dofile 'ArunEworld.lua'
    • The dofile  function is useful also when you are testing a piece of code.
  • require  function
    • Ex  : require"ArunEworld.lua"
  • Each piece of code that Lua executes, such as a file or a single line in interactive mode, is a chunk.

 

Function In Lua

Syntax

Example :

 

File

  • Open File
  • Write File
  • Read File
  • Close File
  • Rename File

Lua Online Compiler

 

Install Lua in windows

 


 Lua Notes

Lua Examples

 

Factorial example

 

Real Time Application using lua

  • Lua using VLC Media player

 


Tutorial – eLua

 

What is eLua?

    eLua stands for Embedded Lua and full of Lua programming for embedded projects. eLua is not an Operating System, although it offers some features and characteristics of one, like different File Systems, a command Shell, a remote console for Terminal access,

Why Lua?

  • Fully functional language
  • Scripting language

Supported Embedded Platforms

        eLua supports and implements more than  five architectures includes Cortex-M3, AVRR32, ARM7TDMI, ARM966E-S, x86. more info

eLua Generic Modules

  • pio (Programmable input/output)
  • tmr (Physical and virtual timer)
  • pwm (Pulse With Modulation)
  • uart (Universal asynchronous receiver transmitter)
  • spi (Serial peripheral interface, net9TCP/IP networking)
  • net (TCP/IP networking)
  • adc (analog to digital converter)
  • dac (Digital to analog converter)**
  • cpu (Low level system access)
  • pd (platform data)
  • term (ANSI terminal access)
  • bit (Bit-wise operations)
  • pack (Packet/unpack binary data)
  • cmp (Analog comparator)**
  • i2c (Inter-integrated circuit)
  • cnt (Event counter)**
  • can (Controller area network)*
  • mrpc (Remote procedure call)
  • i2s (Inter-IC sound)**
  • elua (eLua system control)

 *(Implemented, needs more testing), **(Not yet implemented)

Getting stated with eLua

 Tutorials 

Lua Applications

  • Digital Potentiometer
  • Display ( Graphics, TFT LCD Control)
  • Game industry
  • Music (Music, MIDI control and Play piano)
  • Road-map
  • Robotics
  • Simulation
  • Wireless communication (GSM/GPRS, 2.4Ghz)
  • Web services ( Adobe Light room, World of Warcraft)

Links

You can follow on eLua news and updates from Social media


Next :

Previous :