misc_functions
This is an old revision of the document!
Table of Contents
Miscellaneous functions
GetTime
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
Example:
- GetTime
function main (userId) local time = GetTime(); INFO("Seconds: " .. time.sec); INFO("Nanoseconds:" .. time.ns); end
Delay
- 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
GetReg filter nil (if nil then zero)
- 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
misc_functions.1618389500.txt.gz ยท Last modified: 2021/04/14 08:38 by atolstov