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/06/20 11:20] – [Detecting rise / fall edge of the signals] emozolyakuseful_programs [2019/09/17 08:23] – [Bit functions] emozolyak
Line 22: Line 22:
 To access idividual bits, you can use following handy functions: To access idividual bits, you can use following handy functions:
 <code lua> <code lua>
--- to check, set,clear individual bits in numbers 
 function bw(n) function bw(n)
   return 2 ^ (n - 1)  -- returns weight of the bit in pos. n   return 2 ^ (n - 1)  -- returns weight of the bit in pos. n
 end end
- +  
-function hasbit(x, p) +function hasbit(x, b)  
-  return x % (p + p) >= p  -- returns if is true/false; if hasbit(value, bw(2)) then ...+  local = bw(b
 +  return x % (p + p) >= p  -- returns if is true/false; if hasbit(value, b) then ...
 end end
- +  
-function setbit(x, p+function setbit(x, b)  
-  return hasbit(x, p) and x or x + -- sets bit #p in х example:  х = setbit(х, bit(p))+  return hasbit(x, b) and x or x + bw(b) -- sets bit in х example:  х = setbit(х, b))
 end end
- +  
-function clearbit(x, p+function clearbit(x, b
-  return hasbit(x, p) and x - or x --  clears bit #p in х +  return hasbit(x, b) and x - bw(b) or x --  clears bit in х 
 end end
- +  
-function togglebit(x, b) -- toggles bit #b in x  +function togglebit(x, b) -- toggles bit b in x  
-    local r  +    if hasbit(x, b) then  
-     +        return clearbit(x, b)
-    if hasbit(x,bw(b)) then  +
-        r = clearbit(x, bw(b))+
     else      else 
-        r = setbit(x, bw(b))+        return setbit(x, b)
     end      end 
-    return r  
 end  end 
- +  
--- output bool conditoin as 0 /1 into a bit  +function outbit(condition, x, b) -- output bool condition as 0 /1 into a bit  
-function outbit(x, p, condition) +     if condition then      
-     if condition   then      +         return hasbit(x, b) and x or x + bw(b
-         return hasbit(x, p) and x or x + p -- sets bit #p in х example:  х = setbit(х, bw(p))+
      else       else 
-         return hasbit(x, p) and x - or x --  clears bit #p in х      +         return hasbit(x, b) and x - bw(b) or x 
      end       end 
 end  end 
  
--- uses alias and sets bit in it  +function outBit(condition, alias, b) -- output bool condition as 0 /1 into a bit of a internal register  
-function setBit(alias, b) +     local new_value outbit(condition, R(alias), b) 
-    local value = R(alias) +     W(alias, new_value)
-    value = setbit(valuebw(b)+
-    W(alias, value)+
 end  end 
 + 
 +function setBit(alias, b) -- sets bit in the register using alias or id   
 +    local old_value = R(alias) 
 +    local new_value = setbit(old_value, b) 
  
--- uses alias and crears bit in it  +    if (new_value ~= old_valuethen  
-function clearBit(alias, b+        WriteReg(alias, new_value -- to prevent unnecessary writing 
-    local value = R(alias+    end 
-    value = clearbit(valuebw(b)+
-    W(alias, value)+
 end  end 
  
 +function clearBit(alias, b) -- uses alias or id and crears bit in it 
 +    
 +    local old_value = R(alias)
 +    local new_value = clearbit(old_value, b)
 +    
 +    if (new_value ~= old_value) then 
 +        WriteReg(alias, new_value)
 +    end 
 +end 
 +
 +function toggleBit(alias, b) -- toggles bit b in reg by alias or id 
 +     local cur_value = R(alias)
 +    
 +    if hasbit(cur_value, b) then 
 +        clearBit(alias, b)
 +    else 
 +        setBit(alias, b)
 +    end 
 +    return true 
 +end 
 + 
 -- return bit from the position as a  1 / 0  -- return bit from the position as a  1 / 0 
-function getBit(xp+function getBit(aliasb) 
-    if hasbit ( p) then +     
 +    if hasbit(R(alias)b) then 
         return 1          return 1 
     else      else 
Line 122: Line 140:
  
 <code lua> <code lua>
-function DBG ( ... ) +function DBG( ...) -- ... accepts multiple arguments in a table arg  
-            -- ... means variable number of arguments in lua, passed in the 'arg' table+ 
 +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 ""            -- string buffer to concatenate all arguments +    if not table_sign_found then   
-        for = 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  
-            s .. tostring ( arg [i ) .. ' ' +        local header_t, value_t {}, {} -- prepare header and value rows 
 +          
 +        for = 1, tab_s_index - 1 do  
 +            header_t[k] = arg[k]
         end          end 
-    DEBUG +        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  
 +             ERROR("column count differs!"
 +        else  
 +            for g = 1, #header_t do  
 +                local delta = #header_t[g] #value_t[g] -- makes aligment by adding spaces to shorter strings... 
 +                if delta ~= 0 then  
 +                    if (delta > 0) then  
 +                        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  
 +end -- if ENABLE_DEBUG 
 + 
 +return true  
 +end -- DBG 
 + 
 +function tabToStr(t)  -- glue all and add spaces, use tostring to protect from nil and bool argument  
 +    local s = ""             
 +    for i = 1, #t do  
 +        s = s .. tostring(t[i]) .. ' '  
 +    end -- for  
 +    return s  
 +end 
  
-end -- DBG  
 </code> </code>
 +Then the print output can be enhanced like this:
 +{{ :dbg_table_out.png?direct&600 |}}
  
 The //ENABLE_DEBUG// flag should be global boolean variable in the calling script. You may just to set it when needed and save the script.  The //ENABLE_DEBUG// flag should be global boolean variable in the calling script. You may just to set it when needed and save the script. 
useful_programs.txt · Last modified: 2024/05/27 13:23 by emozolyak

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki