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/07/31 12:56] – [Bit functions] emozolyak | useful_programs [2020/01/13 10:17] – [Debug printing] emozolyak | ||
---|---|---|---|
Line 47: | Line 47: | ||
end | end | ||
- | function outbit(condition, | + | function outbit(condition, |
if condition then | if condition then | ||
| | ||
Line 53: | Line 53: | ||
| | ||
| | ||
+ | end | ||
+ | |||
+ | function outBit(condition, | ||
+ | local new_value = outbit(condition, | ||
+ | | ||
end | end | ||
Line 135: | Line 140: | ||
<code lua> | <code lua> | ||
- | function DBG ( ... ) | + | function DBG(...) |
- | -- ... means variable number of arguments in lua, passed in the ' | + | local tc = tabToStr |
- | if ENABLE_DEBUG then -- should be global in the calling script | + | local function align2s(s1, s2) -- appends space to smaller string |
+ | local d, s = (#s1 - #s2), ' ' | ||
+ | if (d ~= 0) then return (((d > 0) and {s1, s2 .. s:rep(d)}) or ({s1 .. s: | ||
+ | else | ||
+ | return {s1, s2} | ||
+ | end | ||
+ | end | ||
+ | |||
+ | | ||
+ | local arg_str = tc(arg) | ||
+ | local t_s_pos = string.find(arg_str, | ||
| | ||
- | | + | if not t_s_pos then |
- | for i = 1 , #arg do | + | INFO(arg_str) |
- | -- glue all and add spaces, use tostring to protect from nil argument | + | else |
- | s = s .. tostring ( arg [i] | + | |
- | end | + | -- header |
- | | + | for w in string.gmatch(arg[1], |
- | end -- if | + | h_row[# |
+ | end | ||
+ | | ||
+ | for i = 3, #arg do | ||
+ | v_row[# | ||
+ | end | ||
+ | |||
+ | if (#h_row ~= #v_row) then | ||
+ | ERROR(" | ||
+ | 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 | ||
+ | |||
+ | function tabToStr(t) | ||
+ | local s = "" | ||
+ | for i = 1, #t do | ||
+ | local e = t[i] ; if (e == nil) then e = ' | ||
+ | | ||
+ | end -- for | ||
+ | | ||
+ | end | ||
- | end -- DBG | ||
</ | </ | ||
+ | Then the print output can be enhanced like this: | ||
+ | {{ : | ||
The // | The // | ||
Line 223: | 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 | ||
- | | + | |
- | WriteReg(tmrAlias, | + | if (bool_input == curTmrState) then |
+ | 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