Browse Source

representations of PATABLE settings: replace "0x0" with "0" for brevity

Fabian Peter Hammerle 3 years ago
parent
commit
760808ec73
2 changed files with 15 additions and 13 deletions
  1. 12 5
      cc1101/__init__.py
  2. 3 8
      cc1101/_cli.py

+ 12 - 5
cc1101/__init__.py

@@ -109,6 +109,16 @@ class _ReceivedPacket:  # unstable
         )
 
 
+def _format_patable(settings: typing.Iterable[int], insert_spaces: bool) -> str:
+    # "Table 39: Optimum PATABLE Settings" uses hexadecimal digits
+    # "0" for brevity
+    settings_hex = tuple(map(lambda s: "0" if s == 0 else "0x{:x}".format(s), settings))
+    if len(settings_hex) == 1:
+        return "({},)".format(settings_hex[0])
+    delimiter = ", " if insert_spaces else ","
+    return "({})".format(delimiter.join(settings_hex))
+
+
 class CC1101:
 
     # pylint: disable=too-many-public-methods
@@ -643,7 +653,6 @@ class CC1101:
 
     def __str__(self) -> str:
         sync_mode = self.get_sync_mode()
-        output_power_settings_hex = tuple(map("0x{:x}".format, self.get_output_power()))
         attrs = (
             "marcstate={}".format(
                 self.get_main_radio_control_state_machine_state().name.lower()
@@ -666,10 +675,8 @@ class CC1101:
                 else "=",
                 self.get_packet_length_bytes(),
             ),
-            "output_power=({})".format(
-                ",".join(output_power_settings_hex)
-                if len(output_power_settings_hex) > 1
-                else output_power_settings_hex[0] + ","
+            "output_power={}".format(
+                _format_patable(self.get_output_power(), insert_spaces=False)
             ),
         )
         return "CC1101({})".format(", ".join(filter(None, attrs)))

+ 3 - 8
cc1101/_cli.py

@@ -112,14 +112,9 @@ def _export_config():
             )
         print("]")
         print(
-            "# PATABLE = ({})".format(
-                # pylint: disable=protected-access; internal method
-                ", ".join(
-                    map(
-                        lambda s: "0" if s == 0 else "0x{:x}".format(s),
-                        transceiver._get_patable(),
-                    )
-                )
+            "# PATABLE = {}".format(
+                # pylint: disable=protected-access; internal function & method
+                cc1101._format_patable(transceiver._get_patable(), insert_spaces=True)
             )
         )