[chirp_devel] [PATCH 0 of 2] [RFC] Bitwise named structure definitions
This is a change to the bitwise core, which I don't touch very often. However, I think this is a significant improvement for drivers like the FT-8x7 ones. I've included as an example a quick conversion of the FT-817 driver, which still passes all the tests (and thus I assume is good).
Marco, what do you think of this?
--Dan
# HG changeset patch # User Dan Smith dsmith@danplanet.com # Date 1334113255 25200 # Node ID ce7239963bc4dd7bef2b8dc1a3bdb6fbe26aab75 # Parent c3b8a85d6e235eb493c790ddac982683414dd251 [RFC] Add ability to define structure types in bitwise
This adds the ability to define structure types in bitwise formats and then use them in multiple places. Right now, we have a few places where this is needed and it's badly faked like this:
struct_format = "struct { u8 foo; u8 bar; }" mem_format = struct_format + "struct1;" + struct_format + "struct2;"
This new change to bitwise will allow us to behave more like C and do:
mem_format = """ struct mytype { u8 foo; u8 bar; }; struct mytype struct1; struct mytype struct2; """
The bitwise grammar changed a little, but it's still compatible with the existing definitions and all of the tests still pass.
#93
diff -r c3b8a85d6e23 -r ce7239963bc4 chirp/bitwise.py --- a/chirp/bitwise.py Tue Apr 10 19:10:20 2012 -0700 +++ b/chirp/bitwise.py Tue Apr 10 20:00:55 2012 -0700 @@ -590,6 +590,7 @@ self._data = data self._offset = offset self._obj = None + self._user_types = {}
def do_symbol(self, symdef, gen): name = symdef[1] @@ -647,8 +648,11 @@ else: self._generators[name] = res
- def parse_struct(self, struct): + def parse_struct_decl(self, struct): block = struct[:-1] + if block[0][0] == "symbol": + # This is a pre-defined struct + block = self._user_types[block[0][1]] deftype = struct[-1] if deftype[0] == "array": name = deftype[1][0][1] @@ -672,6 +676,19 @@ else: self._generators[name] = result
+ def parse_struct_defn(self, struct): + name = struct[0][1] + block = struct[1:] + self._user_types[name] = block + + def parse_struct(self, struct): + if struct[0][0] == "struct_defn": + return self.parse_struct_defn(struct[0][1]) + elif struct [0][0] == "struct_decl": + return self.parse_struct_decl(struct[0][1]) + else: + raise Exception("Internal error: What is `%s'?" % struct[0][0]) + def parse_directive(self, directive): name = directive[0][0] if name == "seekto": @@ -713,6 +730,8 @@
if __name__ == "__main__": defn = """ +struct mytype { u8 foo; }; +struct mytype bar; struct { u8 foo; u8 highbit:1, @@ -722,9 +741,11 @@ bbcd fourdigits[2]; } mystruct; """ - data = "\x7F\x81abc\x12\x34" + data = "\xab\x7F\x81abc\x12\x34" tree = parse(defn, data)
+ print repr(tree) + print "Foo %i" % tree.mystruct.foo print "Highbit: %i SixZeros: %i: Lowbit: %i" % (tree.mystruct.highbit, tree.mystruct.sixzeros, diff -r c3b8a85d6e23 -r ce7239963bc4 chirp/bitwise_grammar.py --- a/chirp/bitwise_grammar.py Tue Apr 10 19:10:20 2012 -0700 +++ b/chirp/bitwise_grammar.py Tue Apr 10 20:00:55 2012 -0700 @@ -64,8 +64,14 @@ def _block(): return "{", _block_inner, "}"
+def struct_defn(): + return symbol, _block + +def struct_decl(): + return [symbol, _block], [array, symbol] + def struct(): - return keyword("struct"), _block, [array, symbol], ";" + return keyword("struct"), [struct_defn, struct_decl], ";"
def _language(): return _block_inner
# HG changeset patch # User Dan Smith dsmith@danplanet.com # Date 1334113627 25200 # Node ID 2921c559d78ef63031ac6a19b6a0c8d805e52d2e # Parent ce7239963bc4dd7bef2b8dc1a3bdb6fbe26aab75 [RFC] Convert the FT-817 driver to use named struct definitions
This is an example usage of the previous patch which makes the definition of the FT-817 memory format significantly simpler. The diff doesn't do it justice, so look at the before and after files to see the difference.
#93
diff -r ce7239963bc4 -r 2921c559d78e chirp/ft817.py --- a/chirp/ft817.py Tue Apr 10 20:00:55 2012 -0700 +++ b/chirp/ft817.py Tue Apr 10 20:07:07 2012 -0700 @@ -110,8 +110,8 @@
print "Clone completed in %i seconds" % (time.time() - start)
-mem_struct = """ -struct { +mem_format = """ +struct mem_struct { u8 tag_on_off:1, tag_default:1, unknown1:3, @@ -142,33 +142,26 @@ u32 freq; u32 offset; u8 name[8]; -} -""" +};
-# there is a bug in bitwise_grammar that prevent the definition of single structures -# qmb should be only one mem_struct followed by -#""" + mem_struct + """ mtqmb; -# but both qmb and qmb[1] raise an exception so I had to define it as qmb[2] +#seekto 0x2A; +struct mem_struct vfoa[15]; +struct mem_struct vfob[15]; +struct mem_struct home[4]; +struct mem_struct qmb[2]; +struct mem_struct mtune;
-mem_format = """ -#seekto 0x2a; -""" + mem_struct + """ vfoa[15]; -""" + mem_struct + """ vfob[15]; -""" + mem_struct + """ home[4]; -""" + mem_struct + """ qmb[2]; -""" + mem_struct + """ mtune; - -#seekto 0x3fd; +#seekto 0x3FD; u8 visible[25];
#seekto 0x417; u8 filled[25];
#seekto 0x431; -""" + mem_struct + """ memory[200]; +struct mem_struct memory[200];
#seekto 0x1979; -""" + mem_struct + """ sixtymeterchannels[5]; +struct mem_struct sixtymeterchannels[5]; """
@directory.register
participants (1)
-
Dan Smith