User Tools

Site Tools


misc_functions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
misc_functions [2019/05/14 10:28] akuzmukmisc_functions [2021/04/14 08:38] atolstov
Line 2: Line 2:
  
 ==== GetTime ==== ==== GetTime ====
-The GetTime function returns table with local time information. **sec** field contains seconds in Unixtime format. **ns** field contains nanoseconds passed since current second.+The GetTime function returns table with local time information. **sec** field contains seconds in Unixtime format. **ns** field contains nanoseconds passed since begin of current second.
  
 This function is available since WebHMI version 3.6 This function is available since WebHMI version 3.6
  
 Example: Example:
-<code lua>+<code lua GetTime>
 function main (userId) function main (userId)
     local time = GetTime();     local time = GetTime();
Line 14: Line 14:
 end end
 </code> </code>
 +
 +==== Delay ====
 +
 +<code lua Delay>
 +function main (userId)
 +    
 +        --  Uncomment this part to measure the time spent
 +                        -- local start_time = GetTime();
 +        --
 +    Delay(100.25, GetTimePrecise())    
 +        
 +        
 +        --  Uncomment this part to measure the time spent
 +                        -- local cur_time = GetTime();     local cur_sec, cur_ns =  cur_time.sec, cur_time.ns
 +                        -- local time_spent_sec, time_spent_ns = cur_sec - start_time.sec, cur_ns - start_time.ns
 +                        -- local millseconds_spent = time_spent_sec * 1000 + time_spent_ns / 1000000
 +                        -- ERROR("Time spent ms = " .. millseconds_spent)
 +        -- 
 +        
 +end
 + -- Additional functions can be defined after or before main() loop
 +function GetTimePrecise()
 +    local start_time = GetTime();
 +    return   start_time.sec * 1000^3  + start_time.ns  
 +end
 +
 +function Delay (milliseconds, precise_start_time)
 +    repeat
 +        delay_ns = milliseconds * 1000000
 +        local cur_time = GetTime();
 +        time_over = GetTimePrecise() > (precise_start_time + delay_ns)
 +    until time_over
 +end
 +
 +</code>
 +
 +==== GetReg filter nil (if nil then zero) ==== 
 +<code lua GetReg filtering>
 +function main (userId)
 +    
 +    val1 = GetReg(1) or 0 -- if GetReg is nil, then it will be equal to 0
 +    TRACE("val1 " ..tostring(val1))
 + val2 = GetReg("temperature")
 + TRACE("val2 " ..tostring(val2))
 +    
 +
 +
 + val2_filtered = (val2 or 0) -- if val2 is nil, then it will be equal to 0 too.
 + TRACE("val2_filtered " ..tostring(val2_filtered))
 +    sum = val1 + val2_filtered
 +
 +    TRACE(sum)
 +  
 +end
 +
 +
 +</code>
 +
 +
misc_functions.txt · Last modified: 2021/04/14 08:42 by atolstov

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki