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/02/21 13:48] – [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 table_sign_found = false | + | function align2s(s1, s2) -- appends space to smaller string |
- | local tab_s_index = 0 | + | |
- | + | ||
- | if ENABLE_DEBUG then -- should be global in the calling script | + | |
- | + | ||
- | -- find divider | + | |
- | for i = 1, #arg do | + | |
- | if arg[i] | + | |
- | table_sign_found = true | + | |
- | tab_s_index = i | + | |
- | break | + | |
- | end | + | |
- | end | + | |
| | ||
- | if not table_sign_found then | + | if (d ~= 0) then |
- | INFO(tabToStr(arg)) -- outputs table values in a single row | + | |
- | else | + | |
- | local header_t, value_t | + | |
- | + | ||
- | 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 | + | |
- | ERROR("column count differs!") | + | |
+ | | ||
+ | Lp = ad / 2 ; Rp = Lp ; INFO("even parts, L R = " | ||
else | else | ||
- | | + | |
- | local delta = # | + | |
- | if delta ~= 0 then | + | |
- | if (delta > 0) then | + | |
- | value_t[g] | + | |
- | else | + | |
- | header_t[g] = header_t[g] | + | |
- | end | + | |
- | end | + | |
- | end -- for | + | |
- | INFO(tabToStr(header_t)) ; INFO(tabToStr(value_t)) | + | |
end | end | ||
+ | return (((d > 0) and {s1, s:rep(Lp) .. s2 .. s: | ||
+ | or | ||
+ | | ||
+ | else | ||
+ | return {s1, s2} | ||
end | end | ||
- | end -- if ENABLE_DEBUG | + | end |
- | + | ||
- | return true | + | |
- | end -- DBG | + | |
+ | if ENABLE_DEBUG then | ||
+ | local arg_str = tc(arg) | ||
+ | local t_s_pos = string.find(arg_str, | ||
+ | |||
+ | if not t_s_pos then | ||
+ | INFO(arg_str) | ||
+ | else | ||
+ | local h_row, v_row = {}, {} | ||
+ | -- header | ||
+ | for w in string.gmatch(arg[1], | ||
+ | h_row[# | ||
+ | end | ||
+ | -- values | ||
+ | for i = 3, #arg do | ||
+ | v_row[# | ||
+ | end | ||
+ | |||
+ | if (#h_row ~= #v_row) then | ||
+ | ERROR(" | ||
+ | INFO(" | ||
+ | return | ||
+ | else | ||
+ | for g = 1, #h_row do | ||
+ | h_row[g], v_row[g] = unpack(align2s(h_row[g], | ||
+ | end | ||
+ | end | ||
+ | INFO(tc(h_row)) ; INFO(tc(v_row)) | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | |||
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 279: | ||
<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