User Tools

Site Tools


useful_programs

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
useful_programs [2019/09/17 08:23] – [Bit functions] emozolyakuseful_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 '|' -- if there is divider sign, the values will be grouped in 2 rows +  
-local table_sign_found = false         +function align2s(s1, s2) -- appends space to smaller string  
-local tab_s_index = 0  +    local d, s = (#s1 #s2)' ' ; local ad math.abs(d)
- +
-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 +
          
-    if not table_sign_found then   +    if (d ~0then 
-        INFO(tabToStr(arg)) -- outputs table values in a single row  +
-    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  +        local Lp, Rp = 0, 0 
-             ERROR("column count differs!")+         
 +        if ((ad % 2) == 0) then  
 +            Lp = ad / 2 ; Rp = Lp ; INFO("even parts, L R = .. Lp .. s .. Rp)
         else          else 
-            for g 1, #header_t do  +            Lp math.floor(ad / 2; Rp Lp + 1 ; INFO("not even L R " .. Lp .. .. Rp)
-                local delta = #header_t[g] - #value_t[g] -- makes aligment by adding spaces to shorter strings... +
-                if delta ~= 0 then  +
-                    if (delta > 0then  +
-                        value_t[g] value_t[g] .. string.rep(' ', delta)  +
-                    else  +
-                        header_t[g] = header_t[g] .. string.rep(' ', math.abs(delta))  +
-                    end  +
-                end  +
-            end -- for  +
-            INFO(tabToStr(header_t)) ; INFO(tabToStr(value_t))+
         end          end 
 +        return (((d > 0) and {s1, s:rep(Lp) .. s2 .. s:rep(Rp)}) 
 +                        or
 +             ({s:rep(Lp) .. s1 .. s:rep(Rp), s2}))
 +    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)  -- Just printing single line 
 +        else 
 +            local h_row, v_row = {}, {} 
 +            -- header 
 +            for w in string.gmatch(arg[1], "%S+") do
 +                h_row[#h_row + 1] = w      
 +            end 
 +            -- values 
 +            for i = 3, #arg do 
 +                v_row[#v_row + 1] = tostring(arg[i])
 +            end 
 + 
 +            if (#h_row ~= #v_row) then 
 +                ERROR("Inconsistent header and value rows in DBG!")
 +                INFO("h_row:" .. tc(h_row)) ; INFO("v_row" .. tc("v_row"))
 +                return 
 +            else 
 +                for g = 1, #h_row do 
 +                    h_row[g], v_row[g] = unpack(align2s(h_row[g], v_row[g]))
 +                end 
 +            end 
 +            INFO(tc(h_row)) ; INFO(tc(v_row))
 +        end    
 +    end 
 +end  
 + 
 function tabToStr(t)  -- glue all and add spaces, use tostring to protect from nil and bool argument  function tabToStr(t)  -- glue all and add spaces, use tostring to protect from nil and bool argument 
     local s = ""                 local s = ""            
     for i = 1, #t do      for i = 1, #t do 
-        s = s .. tostring(t[i]) .. ' ' +        local e = t[i] ; if (e == nil) then e = 'nil' end  
 +        s = s .. tostring(e) .. ' ' 
     end -- for      end -- for 
     return s      return s 
-end +end
  
 </code> </code>
Line 274: Line 279:
  
 <code lua> <code lua>
- function Timer (bool_input, onDelay, offDelay, tmrAlias)+function Timer (bool_input, onDelay, offDelay, tmrAlias)
 --                bool      sec to ON  sec to OFF  string alias --                bool      sec to ON  sec to OFF  string alias
 + 
 +local now , nowString, curTimeStamp, curTmrState =  
 +os.time(), os.date("%c", now), R(tmrAlias), TRUE(tmrAlias .."_out"    
  
-local now , nowString, curTimeStamp, curTmrState =  os.time() +    DBG("Timer State Stamp", "|", tmrAlias, curTmrState, os.date("%c", curTimeStamp)) 
-                                                    os.date("%c", now),  +
-                                                    R(tmrAlias), +
-                                                    TRUE(tmrAlias .."_out"     +
-                                           +
-DEBUG("curTimeStamp - ", os.date("%c", curTimeStamp) ) +
-DEBUG ("cur "..tmrAlias.." State =  "..tostring(curTmrState))                  +
 -- protects from malfunctions on very first run  -- protects from malfunctions on very first run 
 if curTimeStamp == 0 then  if curTimeStamp == 0 then 
-    DEBUG("curTimeStamp was zero in this timer!")+    DBG("curTimeStamp was zero in this timer!")
     WriteReg(tmrAlias, now)     WriteReg(tmrAlias, now)
     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  
-    if (bool_input == curTmrState) then  +        WriteReg(tmrAlias, now) ; DBG("timer inputs match"
-        WriteReg(tmrAlias, now)                           DEBUG("timer input match state "+
         return curTmrState, 0   -- as bool          return curTmrState, 0   -- as bool 
-     +         
-    -- TON      +                            -- TON      
-    elseif bool_input then  +elseif bool_input then  
-         if ((now - GetReg(tmrAlias)) > onDelay) then  +         if ((now - curTimeStamp) > onDelay) then  
-             WriteReg(tmrAlias .."_out", 1 +             SET(tmrAlias .."_out") ; WriteReg(tmrAliasnow; DBG("detected ON state after delay")
-                                                        DEBUG("detected ON state after delay (input, now, tmr, onDelay ):", bool_input, nowString, onDelay)+
              return true, 0                return true, 0  
         else          else 
-            local countDown = onDelay - (now - curTimeStamp) +            local countDown = onDelay - (now - curTimeStamp) ; DBG("countdown to On in "countDown)
-            DEBUG("countdown to On in " .. tmrAlias .. " ".. countDown)+
             return curTmrState, countDown             return curTmrState, countDown
         end          end 
 + 
 +                            -- TOFF     
 +elseif not bool_input then 
          
-    -- TOFF      +        if ((now - curTimeStamp) > offDelay) then    
-    elseif not bool_input then  +            RESET(tmrAlias .."_out") ; WriteReg (tmrAliasnow; DBG("detected OFF state after delay"
-        if (now - GetReg ( tmrAlias ) > offDelay) then    +            return false, 0   
-            WriteReg (tmrAlias .."_out", 0) +
-            DEBUG("detected OFF state after delay (input, now, tmr, offDelay ):", bool_input, nowString, offDelay+
-            return false  +
         else          else 
-            local countDown = offDelay - (now - curTimeStamp) +            local countDown = offDelay - (now - curTimeStamp) ; DBG("countdown to OFF in "countDown)
-            DEBUG ("countdown to OFF in " .. tmrAlias .." ".. countDown)+
             return curTmrState, countDown             return curTmrState, countDown
         end          end 
useful_programs.txt · Last modified: 2024/03/19 09:39 by emozolyak

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki