]>
Commit | Line | Data |
---|---|---|
769ed325 UH |
1 | ## |
2 | ## This file is part of the libsigrokdecode project. | |
3 | ## | |
6ca8b2b7 | 4 | ## Copyright (C) 2012-2020 Uwe Hermann <uwe@hermann-uwe.de> |
769ed325 UH |
5 | ## |
6 | ## This program is free software; you can redistribute it and/or modify | |
7 | ## it under the terms of the GNU General Public License as published by | |
8 | ## the Free Software Foundation; either version 2 of the License, or | |
9 | ## (at your option) any later version. | |
10 | ## | |
11 | ## This program is distributed in the hope that it will be useful, | |
12 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ## GNU General Public License for more details. | |
15 | ## | |
16 | ## You should have received a copy of the GNU General Public License | |
4539e9ca | 17 | ## along with this program; if not, see <http://www.gnu.org/licenses/>. |
769ed325 UH |
18 | ## |
19 | ||
6ca8b2b7 | 20 | from enum import Enum, IntEnum, unique |
fe89c3b4 | 21 | from itertools import chain |
6ca8b2b7 | 22 | import re |
fe89c3b4 | 23 | |
769ed325 UH |
24 | # Return the specified BCD number (max. 8 bits) as integer. |
25 | def bcd2int(b): | |
26 | return (b & 0x0f) + ((b >> 4) * 10) | |
c413347e | 27 | |
392a5d1e UH |
28 | def bin2int(s: str): |
29 | return int('0b' + s, 2) | |
30 | ||
c413347e | 31 | def bitpack(bits): |
c7172e5f | 32 | return sum([b << i for i, b in enumerate(bits)]) |
c413347e | 33 | |
4a3c854c GS |
34 | def bitpack_lsb(bits, idx=None): |
35 | '''Conversion from LSB first bit sequence to integer.''' | |
36 | if idx is not None: | |
37 | bits = [b[idx] for b in bits] | |
38 | return bitpack(bits) | |
39 | ||
40 | def bitpack_msb(bits, idx=None): | |
41 | '''Conversion from MSB first bit sequence to integer.''' | |
42 | bits = bits[:] | |
43 | if idx is not None: | |
44 | bits = [b[idx] for b in bits] | |
45 | bits.reverse() | |
46 | return bitpack(bits) | |
47 | ||
c413347e UH |
48 | def bitunpack(num, minbits=0): |
49 | res = [] | |
50 | while num or minbits > 0: | |
51 | res.append(num & 1) | |
52 | num >>= 1 | |
53 | minbits -= 1 | |
54 | return tuple(res) | |
fe89c3b4 | 55 | |
6ca8b2b7 UH |
56 | @unique |
57 | class SrdStrEnum(Enum): | |
58 | @classmethod | |
59 | def from_list(cls, name, l): | |
60 | # Keys are limited/converted to [A-Z0-9_], values can be any string. | |
61 | items = [(re.sub('[^A-Z0-9_]', '_', l[i]), l[i]) for i in range(len(l))] | |
62 | return cls(name, items) | |
63 | ||
64 | @classmethod | |
65 | def from_str(cls, name, s): | |
66 | return cls.from_list(name, s.split()) | |
67 | ||
fe89c3b4 UH |
68 | @unique |
69 | class SrdIntEnum(IntEnum): | |
70 | @classmethod | |
71 | def _prefix(cls, p): | |
72 | return tuple([a.value for a in cls if a.name.startswith(p)]) | |
73 | ||
74 | @classmethod | |
75 | def prefixes(cls, prefix_list): | |
76 | if isinstance(prefix_list, str): | |
77 | prefix_list = prefix_list.split() | |
78 | return tuple(chain(*[cls._prefix(p) for p in prefix_list])) | |
79 | ||
80 | @classmethod | |
81 | def _suffix(cls, s): | |
82 | return tuple([a.value for a in cls if a.name.endswith(s)]) | |
83 | ||
84 | @classmethod | |
85 | def suffixes(cls, suffix_list): | |
86 | if isinstance(suffix_list, str): | |
87 | suffix_list = suffix_list.split() | |
88 | return tuple(chain(*[cls._suffix(s) for s in suffix_list])) | |
89 | ||
90 | @classmethod | |
91 | def from_list(cls, name, l): | |
92 | # Manually construct (Python 3.4 is missing the 'start' argument). | |
93 | # Python defaults to start=1, but we want start=0. | |
94 | return cls(name, [(l[i], i) for i in range(len(l))]) | |
95 | ||
96 | @classmethod | |
97 | def from_str(cls, name, s): | |
98 | return cls.from_list(name, s.split()) |