[chirp_devel] [PATCH 1 of 2] bitwise: add signed integer support

Marco Filippi IZ3GME
Thu May 9 05:58:19 PDT 2013


# HG changeset patch
# User Marco Filippi <iz3gme.marco at gmail.com>
# Date 1368103529 -7200
# Node ID f161e27eb3dd26f857e92b5c85d579fcde78ac31
# Parent  113d314ca49639e98f09dbf800422bcfadc2924b
bitwise: add signed integer support
needed for #863

diff --git a/chirp/bitwise.py b/chirp/bitwise.py
--- a/chirp/bitwise.py
+++ b/chirp/bitwise.py
@@ -25,6 +25,13 @@
 #  ul24 foo;     /* Unsigned 24-bit value (LE)              */
 #  u32  foo;     /* Unsigned 32-bit value                   */
 #  ul32 foo;     /* Unsigned 32-bit value (LE)              */
+#  i8   foo;     /* Signed 8-bit value                      */
+#  i16  foo;     /* Signed 16-bit value                     */
+#  il16 foo;     /* Signed 16-bit value (LE)                */
+#  i24  foo;     /* Signed 24-bit value                     */
+#  il24 foo;     /* Signed 24-bit value (LE)                */
+#  i32  foo;     /* Signed 32-bit value                     */
+#  il32 foo;     /* Signed 32-bit value (LE)                */
 #  char foo;     /* Character (single-byte                  */
 #  lbcd foo;     /* BCD-encoded byte (LE)                   */
 #  bbcd foo;     /* BCD-encoded byte (BE)                   */
@@ -401,7 +408,7 @@
 
 class ul16DataElement(u16DataElement):
     _endianess = "<"
-
+    
 class u24DataElement(intDataElement):
     _size = 3
     _endianess = ">"
@@ -438,6 +445,65 @@
 class ul32DataElement(u32DataElement):
     _endianess = "<"
 
+class i8DataElement(u8DataElement):
+    _size = 1
+
+    def _get_value(self, data):
+        return struct.unpack("b", data)[0]
+
+    def set_value(self, value):
+        self._data[self._offset] = struct.pack("b", int(value) )
+        
+class i16DataElement(intDataElement):
+    _size = 2
+    _endianess = ">"
+
+    def _get_value(self, data):
+        return struct.unpack(self._endianess + "h", data)[0]
+
+    def set_value(self, value):
+        self._data[self._offset] = struct.pack(self._endianess + "h",
+                                               int(value) )
+
+class il16DataElement(i16DataElement):
+    _endianess = "<"
+
+class i24DataElement(intDataElement):
+    _size = 3
+    _endianess = ">"
+
+    def _get_value(self, data):
+        pre = self._endianess == ">" and "\x00" or ""
+        post = self._endianess == "<" and "\x00" or ""
+        return struct.unpack(self._endianess + "i", pre+data+post)[0]
+
+    def set_value(self, value):
+        if self._endianess == "<":
+            start = 0
+            end = 3
+        else:
+            start = 1
+            end = 4
+        self._data[self._offset] = struct.pack(self._endianess + "i",
+                                               int(value) )[start:end]
+
+class il24DataElement(i24DataElement):
+    _endianess = "<"
+
+class i32DataElement(intDataElement):
+    _size = 4
+    _endianess = ">"
+
+    def _get_value(self, data):
+        return struct.unpack(self._endianess + "i", data)[0]
+
+    def set_value(self, value):
+        self._data[self._offset] = struct.pack(self._endianess + "i",
+                                               int(value) )
+
+class il32DataElement(i32DataElement):
+    _endianess = "<"
+
 class charDataElement(DataElement):
     _size = 1
 
@@ -629,6 +695,12 @@
         "ul24" : ul24DataElement,
         "u32"  : u32DataElement,
         "ul32" : ul32DataElement,
+        "i8"   : i8DataElement,
+        "i16"  : i16DataElement,
+        "il16" : il16DataElement,
+        "i24"  : i24DataElement,
+        "il24" : il24DataElement,
+        "i32"  : i32DataElement,
         "char" : charDataElement,
         "lbcd" : lbcdDataElement,
         "bbcd" : bbcdDataElement,
diff --git a/chirp/bitwise_grammar.py b/chirp/bitwise_grammar.py
--- a/chirp/bitwise_grammar.py
+++ b/chirp/bitwise_grammar.py
@@ -16,7 +16,8 @@
 import re
 from chirp.pyPEG import keyword, parse as pypeg_parse
 
-TYPES = ["bit", "u8", "u16", "ul16", "u24", "ul24", "u32", "ul32", "char",
+TYPES = ["bit", "u8", "u16", "ul16", "u24", "ul24", "u32", "ul32",
+         "i8", "i16", "il16", "i24", "il24", "i32", "il32", "char",
          "lbcd", "bbcd"]
 DIRECTIVES = ["seekto", "seek", "printoffset"]
 



More information about the chirp_devel mailing list