Difference between revisions of "NETIO Lua Manual"

From wiki.netio-products.com
Jump to navigation Jump to search
(Periodical scipts)
(Tag: New redirect)
 
Line 1: Line 1:
== Script starting ==
+
#REDIRECT [[Welcome to NETIO resources & projects & documentation overview]]
LUA Scripts in NETIO are event driven. That means if some [[NETIO Lua Reference| event]] is triggered the script run exactly once. One of "triggers" is for example [[System started up|System started up]]. You can find list of all events called "triggers" in [[NETIO Lua Reference|NETIO Lua Reference]].
 
 
 
== Periodical scipts ==
 
In NETIO there is no way for triggering scripts periodically (e.g. every 10 seconds) usng standard triggers. For such scripts the trigger [[System started up|System started up]] and the function [[delay()|delay()]] must be used. This function calls itself and allows to run some procedures periodically.
 
*Example switching output 1 every 10 seconds:
 
 
 
DoPeriod = 10 -- repeat period in seconds
 
-------------------------------------------------
 
function doPeriodically()
 
      -- your periodical code
 
      -- your periodical code
 
      -- your periodical code
 
      -- your periodical code
 
      -- your periodical code
 
      -- your periodical code
 
  delay(DoPeriod,function() doPeriodically() end)
 
end
 
 
doPeriodically()
 
 
 
*This script can be launch using e.g. [[System started up|System started up]] trigger.
 
*Only way how to disable these scripts is restart of NETIO, or usage of global variables.
 
 
 
== Debugging ==
 
To debug your code, use [[Function log()|log()]] and [[Function logf()|logf()]] functions. See the output in system events log.
 
 
 
== Data types and variables ==
 
Lua supports these data types:
 
* <i>nil</i> (to assign <i>nil</i> value effectively deletes the variable)
 
* <i>boolean</i> (falsy values are <i>nil</i> and <i>false</i> only; "" and 0 evaluates as <i>true</i>)
 
* <i>number</i> (decimals only in standard Lua, integers only in Netio Lua)
 
* <i>string</i> (same strings share same address, so equality operator measures both address and value)
 
* <i>tables</i> are associative arrays. (Tables with numeric indices are, well, just arrays.)
 
 
 
local numbers = {2,3,7,5} -- array (first index = 1)
 
local dictionary = {one=1, two=2, three=3} -- table
 
local numbersZeroBased = {[0]=2,3,7,5} -- array (first index = 0)
 
 
 
<i>local</i> keyword limits the variable scope to the block (ended by <i>end</i>) where it is declared. To access an <i>upvalue</i> (variable defined outside of the scope), just omit the <i>local</i> keyword.
 
 
 
== Operators ==
 
Lua has its own manners:
 
* <code>~=</code> is inequality operator
 
* <code>..</code> is string concatenation (don't use plus)
 
* <code><s>i++</s></code> no increment/decrement operator <code>i = i + 1</code>
 
* <code><s>max = a<b ? b : a</s></code> no ternary operator. Use (and get used to) <i>and idiom</i> instead: <code>max = (a<b) and b or a</code>
 
* <i>or idiom</i> works: <code>x = x or 42</code> is equivalent to <code>if not x then x = 42 end</code>
 
 
 
== Conditions ==
 
Lua conditions has casual syntax, see [https://www.lua.org/pil/4.3.1.html here]. Just keep in mind to merge <code>else</code> and <code>if</code> to <code>elseif</code> or multiple <code>end</code>s are required.
 
 
 
Note that falsy values are <i>nil</i> and <i>false</i> only. Empty string and 0 evaluates as true. If you come from C, you might be surprised by following snippet:
 
 
 
if devices.system.output1_consumption then
 
  -- always happens, even if the consumption is 0
 
  log("Outlet 1 supplies power")
 
end
 
 
 
if not devices.system.output1_consumption then
 
  -- never happens, even if the consumption is 0
 
  log("Outlet 1 is idle")
 
end
 
 
 
== Loops ==
 
Lua has <code>break</code> statement, but no <code>continue</code> (use condition inside loop instead). Additionally, Netio Lua implementation limits the loop iterations to 32k.
 
 
 
=== While ===
 
While syntax is not different to other languages:
 
local i = 0
 
while i~=3 do
 
  i = i + 1
 
  logf("%d",i)
 
end
 
 
 
=== Repeat until ===
 
Repeat until syntax is the same as Pascal's, other languages have similar do-while loops:
 
local i = 0
 
repeat
 
  i = i + 1
 
  logf("%d",i)
 
until i==3
 
 
 
=== Numeric for ===
 
Arrays in Lua are one-based (in indices are not specified, the first one is 1, not 0).
 
local arr = {2,3,7,5}
 
for i=1,#arr do logf("%d",arr[i]) end
 
<code><b>for</b> initVar,limit,increment <b>do</b></code>
 
* <i>number assignment</i> <code>initVar</code> inits loop-local variable
 
* <i>number</i> <code>limit</code> loops until initVar reaches this value
 
* <i>optional number</i> <code>increment</code> after each loop initVar increment by this value (default 1)
 
 
 
=== Generic for ===
 
Is rather sophisticated. Most common example (the order of elements in <code>pairs()</code> is not guaranteed):
 
local tab = {one=1, two=2, three=3}
 
for key,val in pairs(tab) do logf("%s:%d",key,val) end
 
Generic for syntax
 
<b>for</b> var_1, ..., var_n <b>in</b> explist <b>do</b> block end
 
is equivalent to (Full explanation [https://www.lua.org/pil/7.2.html here].)
 
do
 
  local _f, _s, _var = explist
 
  while true do
 
    local var_1, ... , var_n = _f(_s, _var)
 
    _var = var_1
 
    if _var == nil then break end
 
    block
 
  end
 
end
 
 
 
Iterator closure that holds its state
 
function iter(a)
 
  local i = 0
 
  return function()
 
    i = i+1
 
    return a[i]
 
  end
 
end
 
 
local arr = {2,3,7,5}
 
for value in iter(arr) do
 
  logf("%d",value)
 
end
 
 
 
Stateless iterator (in this case returns variable list: key and value)
 
function iter(a,i)
 
  i = i+1
 
  if a[i] then return i,a[i] end
 
end
 
 
local arr = {2,3,7,5}
 
for k,v in iter,arr,0 do
 
  logf("%d:%d",k,v)
 
end
 
 
 
Same effect using <code>ipairs()</code> Lua function (without initial state)
 
for k,v in ipairs(arr) do
 
  logf("%d:%d",k,v)
 
end
 
 
 
Now return to the most common example above using <code>pairs()</code> Lua function and read about <code>pairs()</code> and <code>ipairs()</code> implementation [https://www.lua.org/pil/7.3.html here].
 
 
 
== Functions ==
 
 
 
Lua allows multiple results (comma separated):
 
 
 
function diskSpace()
 
  return devices.system.freeSpace, devices.system.totalSpace
 
end
 
 
logf("Free space: %d MB", diskSpace()) -- only first return value is used
 
local free,total = diskSpace() -- values are stored into LHS var-list
 
logf("Free space: %d %%", 100*free/total)
 
 
 
Variable-length arguments are also available:
 
 
 
function sum(...)
 
  local result = 0
 
  for _,v in ipairs(arg) do
 
    result = result + v
 
  end
 
  return result
 
end
 
 
logf("Sum: %d", sum(2,3,5))
 

Latest revision as of 18:01, 11 July 2019