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/10 09:33] – [Universal Timer (TON / TOFF)] 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( ...) -- ... accepts multiple |
- | | + | |
+ | local table_sign = '|' -- if there is divider sign, the values will be grouped in 2 rows | ||
+ | local table_sign_found = false | ||
+ | local tab_s_index = 0 | ||
if ENABLE_DEBUG then -- should be global in the calling script | if ENABLE_DEBUG then -- should be global in the calling script | ||
+ | |||
+ | -- find divider | ||
+ | for i = 1, #arg do | ||
+ | if arg[i] == table_sign then | ||
+ | table_sign_found = true | ||
+ | tab_s_index = i | ||
+ | break | ||
+ | end | ||
+ | end | ||
| | ||
- | local s = "" | + | |
- | for i = 1 , #arg do | + | INFO(tabToStr(arg)) -- outputs table values in a single row |
- | -- glue all and add spaces, use tostring to protect from nil argument | + | else |
- | | + | |
+ | |||
+ | for k = 1, tab_s_index - 1 do | ||
+ | | ||
end | end | ||
- | DEBUG ( s ) | + | for j = tab_s_index + 1, #arg do |
- | end -- if | + | value_t[j - tab_s_index] = tostring(arg[j]) |
+ | end | ||
+ | |||
+ | if (#header_t ~= #value_t) then | ||
+ | | ||
+ | else | ||
+ | for g = 1, #header_t do | ||
+ | local delta = # | ||
+ | | ||
+ | if (delta > 0) then | ||
+ | value_t[g] = value_t[g] .. string.rep(' | ||
+ | else | ||
+ | header_t[g] = header_t[g] .. string.rep(' | ||
+ | end | ||
+ | end | ||
+ | end -- for | ||
+ | INFO(tabToStr(header_t)) ; INFO(tabToStr(value_t)) | ||
+ | end | ||
+ | end | ||
+ | end -- if ENABLE_DEBUG | ||
+ | |||
+ | return true | ||
+ | end -- DBG | ||
+ | |||
+ | function tabToStr(t) | ||
+ | local s = "" | ||
+ | for i = 1, #t do | ||
+ | s = s .. tostring(t[i]) .. ' ' | ||
+ | end -- for | ||
+ | return s | ||
+ | end | ||
- | end -- DBG | ||
</ | </ | ||
+ | Then the print output can be enhanced like this: | ||
+ | {{ : | ||
The // | The // | ||
Line 223: | Line 274: | ||
<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