echo '' ;

Lua Language

  • Lua as an interpreted language.
  • Lua is case-sensitive:
  • Lua is a dynamically typed language.
  • Note :
    • No need to use the terminate;  symbol in Lua
    • The node needs closures { }  for functions like c.

Commands

  • Double hyphen  —
    • If you want to insert a single line command in the program use a double hyphen –.
    • Example  ArunEworld –Variable
  • Double hyphen with double bracket –[[ ]]–
    • If you want to insert more line commands in the program use double bracket–[[ More lines ]]–
    • Example –[[ www.ArunEworld.com www.facebook.com/ArunEworld www.twitter.com/ArunEworld www.plus.google.com/+ArunEworld ]]–

Identifiers in Lua Language

These identifiers are fundamental in many programming languages, particularly in Lua.

IdentifierDescription
andA logical operator representing conjunction
breakUsed to terminate the execution of a loop
doInitiates a block of code to be executed
elseExecutes a block of code if the preceding condition is false
elseifUsed to define alternative conditions in an ‘if’ statement
endMarks the end of a block of code, such as a loop or function
falseA boolean value representing false
forUsed to iterate over a range of values in a loop
functionDefines a reusable block of code with a name
ifExecutes a block of code if a condition is true
inUsed to iterate over elements in a collection
localDeclares a local variable, limiting its scope
nilA value representing the absence of a value
notA logical operator representing negation
orA logical operator representing disjunction
repeatInitiates a loop that repeats until a condition is met
returnUsed to exit a function and optionally return a value
thenMarks the beginning of the block of code to execute if a condition is true
trueA boolean value representing true
untilMarks the end of a ‘repeat’ loop, with a condition to check
whileInitiates a loop that continues while a condition is true

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

These types are fundamental in Lua programming and are used to define the nature of variables and data structures.

TypeDescription
nilRepresents the absence of a value
booleanRepresents true or false values
numberRepresents numerical values
stringRepresents sequences of characters
userdataRepresents arbitrary C data stored in Lua variables
functionRepresents executable code
threadRepresents independent threads of execution
tableRepresents an associative array or collection of data

Variable in Lua Language

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 print [[ ArunEworld sites contains technical tutorials, examples and videos. Lean programmings, micro-controllers, and RTOS ]]
  • Printing strings (Lua consider only string if it’s char also string –print string value Facebook = “www.Facebook.com/ArunEworld” — Facebook is a variable and initialize string to that print(facebook) — print the string value –print char value Name_1 = ‘A’ Name_2 = ‘R’ Name_3 = ‘U’ Name_4 = ‘N’ print(Name_1) print(Name_2) print(Name_3) print(Name_4)
  • Printing numbers (Lua considers only number not like integer, float) –print integer value mobile_number = 8300026060 — initialize mobile_number variable is 8300026060 integer value print(mobile_number ) — print the integer number value –print Float value F_value = 83000.26060 — initialize mobile_number variable is 83000.26060 float value print(F_value) — print the float number value
  • 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 operatordouble 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. –www.ArunEworld.com print(type(“Hello world”)) –> string print(type(10.4*3)) –> number print(type(print)) –> function print(type(type)) –> function print(type(true)) –> boolean print(type(nil)) –> nil print(type(type(X))) –> string –Result –[[ string number lightfunction lightfunction boolean nil string > ]]–  

Example 2 : a = print — yes, this is valid! a(type(a)) –> function

Function tonumber

  • If you need to convert a string to a number explicitly, you can use the function tonumber , print(tostring(10) == “10”) –> true print(10 .. “” == “10”) –> true  

Table

Example i = 10; j = “10”; k = “+10” a = {} — create a table and store its reference in `a’ a[i] = “one value” a[j] = “another value” a[k] = “yet another value” print(a[j]) –> another value print(a[k]) –> yet another value print(a[tonumber(j)]) –> one value print(a[tonumber(k)]) –> one value

Statements

if then else

  • if then else Loop ArunEworld = True if (wifi.sta.sethostname(ArunEworld == true) then print(“ArunEworld is true”) else print(“ArunEworld is false”) end

if then elseif then end

  • if then elseif then loo ArunEworld = 1 if (ArunEworld == 1) then print(“ArunEworld is 1”) elseif (ArunEworld == 2) then print(“ArunEworld is 2”) elseif (ArunEworld == 3) then print(“ArunEworld is 3”) end

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. local i = 1 while a[i] do print(a[i]) i = i + 1 end

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. — print the first non-empty line repeat line = os.read() until line ~= “” print(line)  

for

  • The for statement has two variants: the numeric for and the generic for.
  • Numeric for : A numeric for has the following syntax: for var=exp1,exp2,exp3 do something end  

break and return

  • break and return statement local i = 1 while a[i] do if a[i] == v then break end i = i + 1 end  

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

Syntaxfunction fuction_name() –do somthing end

Example :function ArunEworld() — Function print(“www.ArunEword.com”) print(“www.twitter.com/ArunEworld”) print(“www.facebook.com/ArunEworld”) end ArunEworld() — Function Call

File

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

Lua Online Compiler

Install Lua in windows


 Lua Notes

Lua Examples

Factorial example

–www.aruneworld.com — defines a factorial function function fact (n) if n == 0 then return 1 else return n * fact(n-1) end end print(“enter a number:”) a = io.read(“*number”) — read a number print(fact(a))

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


Lua Android : Run Lua Scrip in Android Mobile

  • https://play.google.com/store/apps/details?id=com.nvreformat.luascripting

How to Manually Install Lua on Windows


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.