useful_programs
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
useful_programs [2019/09/17 08:23] – [Bit functions] emozolyak | useful_programs [2020/01/13 10:17] – [Debug printing] emozolyak | ||
---|---|---|---|
Line 140: | Line 140: | ||
<code lua> | <code lua> | ||
- | function DBG( ...) -- ... accepts multiple arguments in a table arg | + | function DBG(...) |
+ | local tc = tabToStr | ||
- | local table_sign = ' | + | local function align2s(s1, s2) -- appends space to smaller string |
- | local table_sign_found | + | local d, s = (#s1 - #s2), ' ' |
- | local tab_s_index | + | if (d ~= 0) then return (((d > 0) and {s1, s2 .. s:rep(d)}) or ({s1 .. s: |
+ | else | ||
+ | return {s1, s2} | ||
+ | end | ||
+ | end | ||
- | if ENABLE_DEBUG then -- should be global in the calling script | + | |
- | + | local arg_str | |
- | -- find divider | + | |
- | for i = 1, #arg do | + | |
- | | + | |
- | table_sign_found = true | + | |
- | tab_s_index = i | + | |
- | break | + | |
- | end | + | |
- | end | + | |
| | ||
- | | + | |
- | INFO(tabToStr(arg)) -- outputs table values in a single | + | INFO(arg_str) -- Just printing |
- | else | + | |
- | local header_t, value_t = {}, {} -- prepare header and value rows | + | |
- | + | ||
- | for k = 1, tab_s_index - 1 do | + | |
- | header_t[k] = arg[k] | + | |
- | end | + | |
- | for j = tab_s_index + 1, #arg do | + | |
- | value_t[j - tab_s_index] = tostring(arg[j]) | + | |
- | end | + | |
- | + | ||
- | if (#header_t ~= #value_t) then | + | |
- | | + | |
else | else | ||
- | | + | |
- | | + | -- header |
- | if delta ~= 0 then | + | for w in string.gmatch(arg[1], " |
- | | + | |
- | | + | end |
- | else | + | |
- | header_t[g] = header_t[g] .. string.rep(' | + | for i = 3, #arg do |
- | end | + | |
+ | end | ||
+ | |||
+ | | ||
+ | ERROR(" | ||
+ | | ||
+ | else | ||
+ | for g = 1, #h_row do | ||
+ | h_row[g], v_row[g] = unpack(align2s(h_row[g], v_row[g])) | ||
end | end | ||
- | end -- for | + | end |
- | INFO(tabToStr(header_t)) ; INFO(tabToStr(value_t)) | + | INFO(tc(h_row)) ; INFO(tc(v_row)) |
- | end | + | end |
end | end | ||
- | end -- if ENABLE_DEBUG | + | end |
- | + | ||
- | return true | + | |
- | end -- DBG | + | |
function tabToStr(t) | function tabToStr(t) | ||
local s = "" | local s = "" | ||
for i = 1, #t do | for i = 1, #t do | ||
- | s = s .. tostring(t[i]) .. ' ' | + | |
+ | | ||
end -- for | end -- for | ||
return s | return s | ||
- | end | + | end |
</ | </ | ||
Line 274: | Line 266: | ||
<code lua> | <code lua> | ||
- | function Timer (bool_input, | + | function Timer (bool_input, |
-- bool sec to ON sec to OFF string alias | -- bool sec to ON sec to OFF string alias | ||
+ | |||
+ | local now , nowString, curTimeStamp, | ||
+ | os.time(), os.date(" | ||
- | local now , nowString, curTimeStamp, | + | DBG("Timer State Stamp", "|", tmrAlias, |
- | os.date("%c", | + | |
- | R(tmrAlias), | + | |
- | TRUE(tmrAlias .." | + | |
- | + | ||
- | DEBUG(" | + | |
- | DEBUG ("cur " | + | |
-- protects from malfunctions on very first run | -- protects from malfunctions on very first run | ||
if curTimeStamp == 0 then | if curTimeStamp == 0 then | ||
- | | + | |
WriteReg(tmrAlias, | WriteReg(tmrAlias, | ||
return nil, 0 -- countdown | return nil, 0 -- countdown | ||
end | end | ||
+ | |||
+ | -- in and output are equal | ||
- | -- in and output are equal | + | if (bool_input == curTmrState) then |
- | | + | WriteReg(tmrAlias, |
- | WriteReg(tmrAlias, | + | |
return curTmrState, | return curTmrState, | ||
- | | + | |
- | -- TON | + | -- TON |
- | elseif bool_input then | + | elseif bool_input then |
- | if ((now - GetReg(tmrAlias)) > onDelay) then | + | if ((now - curTimeStamp) > onDelay) then |
- | WriteReg(tmrAlias .." | + | SET(tmrAlias .." |
- | DEBUG(" | + | |
| | ||
else | else | ||
- | local countDown = onDelay - (now - curTimeStamp) | + | local countDown = onDelay - (now - curTimeStamp) |
- | DEBUG(" | + | |
return curTmrState, | return curTmrState, | ||
end | end | ||
+ | |||
+ | -- TOFF | ||
+ | elseif not bool_input then | ||
| | ||
- | -- TOFF | + | |
- | elseif not bool_input then | + | |
- | | + | return false, 0 |
- | | + | |
- | DEBUG(" | + | |
- | return false | + | |
else | else | ||
- | local countDown = offDelay - (now - curTimeStamp) | + | local countDown = offDelay - (now - curTimeStamp) |
- | DEBUG (" | + | |
return curTmrState, | return curTmrState, | ||
end | end |
useful_programs.txt · Last modified: 2024/03/19 09:39 by emozolyak