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/06/20 11:20] – [Detecting rise / fall edge of the signals] emozolyak | useful_programs [2019/09/16 15:10] – [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, |
- | return x % (p + p) >= p -- returns if p is true/false; if hasbit(value, | + | local p = bw(b) |
+ | return x % (p + p) >= p -- returns if b is true/false; if hasbit(value, | ||
end | end | ||
- | + | ||
- | function setbit(x, | + | function setbit(x, |
- | return hasbit(x, | + | return hasbit(x, |
end | end | ||
- | + | ||
- | function clearbit(x, | + | function clearbit(x, |
- | return hasbit(x, | + | return hasbit(x, |
end | end | ||
- | + | ||
- | function togglebit(x, | + | function togglebit(x, |
- | local r | + | if hasbit(x, b) then |
- | | + | |
- | if hasbit(x,bw(b)) then | + | |
- | | + | |
else | else | ||
- | | + | |
end | end | ||
- | return r | ||
end | end | ||
- | + | ||
- | -- output bool conditoin | + | function outbit(condition, |
- | function outbit(x, p, condition) | + | if condition then |
- | if condition | + | |
- | | + | |
| | ||
- | | + | |
| | ||
end | end | ||
- | -- uses alias and sets bit in it | + | function |
- | function | + | |
- | local value = R(alias) | + | return new_value |
- | value = setbit(value, bw(b)) | + | |
- | | + | |
end | end | ||
+ | |||
+ | function setBit(alias, | ||
+ | local old_value = R(alias) | ||
+ | local new_value = setbit(old_value, | ||
- | -- uses alias and crears bit in it | + | if (new_value ~= old_value) then |
- | function clearBit(alias, b) | + | |
- | local value = R(alias) | + | |
- | value = clearbit(value, bw(b)) | + | |
- | | + | |
end | end | ||
+ | function clearBit(alias, | ||
+ | | ||
+ | local old_value = R(alias) | ||
+ | local new_value = clearbit(old_value, | ||
+ | | ||
+ | if (new_value ~= old_value) then | ||
+ | WriteReg(alias, | ||
+ | end | ||
+ | end | ||
+ | |||
+ | function toggleBit(alias, | ||
+ | local cur_value = R(alias) | ||
+ | | ||
+ | if hasbit(cur_value, | ||
+ | clearBit(alias, | ||
+ | else | ||
+ | setBit(alias, | ||
+ | end | ||
+ | return true | ||
+ | end | ||
+ | |||
-- return bit from the position as a 1 / 0 | -- return bit from the position as a 1 / 0 | ||
- | function getBit(x, p) | + | function getBit(alias, b) |
- | if hasbit ( x , 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 |
- | | + | |
+ | 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 // |
useful_programs.txt · Last modified: 2024/05/27 13:23 by emozolyak