[chirp_devel] [PATCH] bitwise: add "bit" type to for reading single-bit flags. #727
Tom Hayward
Mon Mar 25 14:24:52 PDT 2013
# HG changeset patch
# User Tom Hayward <tom at tomh.us>
# Date 1364246672 25200
# Node ID bf2e993f7f56a47780dbedc6ebc51213e524d511
# Parent a077b257ba116a08f5d9478d0b4219cd9dc2a640
bitwise: add "bit" type to for reading single-bit flags. #727
diff -r a077b257ba11 -r bf2e993f7f56 chirp/bitwise.py
--- a/chirp/bitwise.py Sun Mar 03 13:48:01 2013 -0800
+++ b/chirp/bitwise.py Mon Mar 25 14:24:32 2013 -0700
@@ -628,6 +628,7 @@
class Processor:
_types = {
+ "bit" : bitDataElement,
"u8" : u8DataElement,
"u16" : u16DataElement,
"ul16" : ul16DataElement,
@@ -693,9 +694,20 @@
res = arrayDataElement(self._offset)
size = 0
for i in range(0, count):
- gen = self._types[dtype](self._data, self._offset)
+ if dtype == "bit":
+ if count % 8 != 0:
+ raise ValueError("bit array must be divisible by 8.")
+
+ class bitDE(bitDataElement):
+ _nbits = 1
+ _shift = 8 - i % 8
+
+ gen = bitDE(self._data, self._offset)
+ self._offset += int((i+1) % 8 == 0)
+ else:
+ gen = self._types[dtype](self._data, self._offset)
+ self._offset += (gen.size() / 8)
res.append(gen)
- self._offset += (gen.size() / 8)
if count == 1:
self._generators[name] = res[0]
diff -r a077b257ba11 -r bf2e993f7f56 chirp/bitwise_grammar.py
--- a/chirp/bitwise_grammar.py Sun Mar 03 13:48:01 2013 -0800
+++ b/chirp/bitwise_grammar.py Mon Mar 25 14:24:32 2013 -0700
@@ -16,7 +16,7 @@
import re
from chirp.pyPEG import keyword, parse as pypeg_parse
-TYPES = ["u8", "u16", "ul16", "u24", "ul24", "u32", "ul32", "char",
+TYPES = ["bit", "u8", "u16", "ul16", "u24", "ul24", "u32", "ul32", "char",
"lbcd", "bbcd"]
DIRECTIVES = ["seekto", "seek", "printoffset"]
diff -r a077b257ba11 -r bf2e993f7f56 tests/unit/test_bitwise.py
--- a/tests/unit/test_bitwise.py Sun Mar 03 13:48:01 2013 -0800
+++ b/tests/unit/test_bitwise.py Mon Mar 25 14:24:32 2013 -0700
@@ -92,6 +92,16 @@
def test_bitfield_ul24(self):
self._test_bitfield_24("l", "\xC2\x40\x00")
+class TestBitType(BaseTest):
+ def test_bit_array(self):
+ defn = "bit foo[24];"
+ obj = bitwise.parse(defn, "\x00\x80\x01")
+ for i, v in [(0, False), (8, True), (23, True)]:
+ self.assertEqual(bool(obj.foo[i]), v)
+
+ def test_bit_array_fail(self):
+ self.assertRaises(ValueError, bitwise.parse, "bit foo[23];", "000")
+
class TestBitwiseBCDTypes(BaseTest):
def _test_def(self, definition, name, data, value):
obj = bitwise.parse(definition, data)
More information about the chirp_devel
mailing list