[chirp_devel] [PATCH] bitwise: add "bit" type for reading arrays of single-bit flags. #727

Tom Hayward
Thu Mar 28 15:20:15 PDT 2013


# HG changeset patch
# User Tom Hayward <tom at tomh.us>
# Date 1364507912 25200
# Node ID ad3c7a7a778a0166c3808a22fa7b9448715bf042
# Parent  778c02c81ca1cb92f081303f536a66a9539cd40a
bitwise: add "bit" type for reading arrays of single-bit flags. #727

Example usage:
struct {
  char  name[8];
  bit   ch_enable_flag[128];
  u8    unknown[8];
} scan_list[17];

diff -r 778c02c81ca1 -r ad3c7a7a778a chirp/bitwise.py
--- a/chirp/bitwise.py	Sun Mar 03 13:48:01 2013 -0800
+++ b/chirp/bitwise.py	Thu Mar 28 14:58:32 2013 -0700
@@ -674,6 +674,16 @@
 
         return bytes
 
+    def do_bitarray(self, i, count):
+        if count % 8 != 0:
+            raise ValueError("bit array must be divisible by 8.")
+
+        class bitDE(bitDataElement):
+            _nbits = 1
+            _shift = 8 - i % 8
+
+        return bitDE(self._data, self._offset)
+
     def parse_defn(self, defn):
         dtype = defn[0]
 
@@ -693,9 +703,13 @@
             res = arrayDataElement(self._offset)
             size = 0
             for i in range(0, count):
-                gen = self._types[dtype](self._data, self._offset)
+                if dtype == "bit":
+                    gen = self.do_bitarray(i, count)
+                    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 778c02c81ca1 -r ad3c7a7a778a chirp/bitwise_grammar.py
--- a/chirp/bitwise_grammar.py	Sun Mar 03 13:48:01 2013 -0800
+++ b/chirp/bitwise_grammar.py	Thu Mar 28 14:58: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 778c02c81ca1 -r ad3c7a7a778a 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	Thu Mar 28 14:58: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