X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=decoders%2Fcommon%2Fsrdhelper%2Fmod.py;h=e37345a0b34f1a416c5c6b407cc746ad9b80ae4c;hb=03a986ea61ec565b5e366b950759d4ad753d3aea;hp=e65ab17e5ae590ae6854f6edddd7b4acd1dfc143;hpb=135b790c851c52400279e7c932e1f900d91ae5cc;p=libsigrokdecode.git diff --git a/decoders/common/srdhelper/mod.py b/decoders/common/srdhelper/mod.py index e65ab17..e37345a 100644 --- a/decoders/common/srdhelper/mod.py +++ b/decoders/common/srdhelper/mod.py @@ -14,10 +14,23 @@ ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License -## along with this program; if not, write to the Free Software -## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +## along with this program; if not, see . ## # Return the specified BCD number (max. 8 bits) as integer. def bcd2int(b): return (b & 0x0f) + ((b >> 4) * 10) + +def bin2int(s: str): + return int('0b' + s, 2) + +def bitpack(bits): + return sum([b << i for i, b in enumerate(bits)]) + +def bitunpack(num, minbits=0): + res = [] + while num or minbits > 0: + res.append(num & 1) + num >>= 1 + minbits -= 1 + return tuple(res)