misc_functions
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionLast revisionBoth sides next revision | ||
misc_functions [2019/05/14 10:27] – created akuzmuk | misc_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 | ||
Example: | Example: | ||
- | <code lua> | + | <code lua GetTime> |
function main (userId) | function main (userId) | ||
local time = GetTime(); | local time = GetTime(); | ||
Line 12: | Line 14: | ||
end | end | ||
</ | </ | ||
+ | |||
+ | ==== Delay ==== | ||
+ | |||
+ | <code lua Delay> | ||
+ | function main (userId) | ||
+ | | ||
+ | -- Uncomment this part to measure the time spent | ||
+ | -- local start_time = GetTime(); | ||
+ | -- | ||
+ | Delay(100.25, | ||
+ | | ||
+ | | ||
+ | -- Uncomment this part to measure the time spent | ||
+ | -- local cur_time = GetTime(); | ||
+ | -- local time_spent_sec, | ||
+ | -- local millseconds_spent = time_spent_sec * 1000 + time_spent_ns / 1000000 | ||
+ | -- ERROR(" | ||
+ | -- | ||
+ | | ||
+ | end | ||
+ | -- Additional functions can be defined after or before main() loop | ||
+ | function GetTimePrecise() | ||
+ | local start_time = GetTime(); | ||
+ | return | ||
+ | end | ||
+ | |||
+ | function Delay (milliseconds, | ||
+ | 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) ==== | ||
+ | <code lua GetReg filtering> | ||
+ | function main (userId) | ||
+ | | ||
+ | val1 = GetReg(1) or 0 -- if GetReg is nil, then it will be equal to 0 | ||
+ | TRACE(" | ||
+ | val2 = GetReg(" | ||
+ | TRACE(" | ||
+ | | ||
+ | |||
+ | |||
+ | val2_filtered = (val2 or 0) -- if val2 is nil, then it will be equal to 0 too. | ||
+ | TRACE(" | ||
+ | sum = val1 + val2_filtered | ||
+ | |||
+ | TRACE(sum) | ||
+ | | ||
+ | end | ||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ |
misc_functions.txt · Last modified: 2021/04/14 08:42 by atolstov