Browse Source

added method get_output_power_levels

Fabian Peter Hammerle 3 years ago
parent
commit
a6d73ebb68
3 changed files with 36 additions and 0 deletions
  1. 3 0
      CHANGELOG.md
  2. 15 0
      cc1101/__init__.py
  3. 18 0
      tests/test_config.py

+ 3 - 0
CHANGELOG.md

@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
 ## [Unreleased]
+### Added
+- method `get_output_power_levels`
+
 ### Fixed
 - default config via private/unstable method `_set_power_amplifier_setting_index`:
   no longer set `FREND0.LODIV_BUF_CURRENT_TX` to `0` (default: `1`)

+ 15 - 0
cc1101/__init__.py

@@ -664,6 +664,9 @@ class CC1101:
                 else "=",
                 self.get_packet_length_bytes(),
             ),
+            "output_power_levels=({})".format(
+                "".join(map("0x{:X},".format, self.get_output_power_levels()))
+            ),
         )
         return "CC1101({})".format(", ".join(filter(None, attrs)))
 
@@ -805,6 +808,18 @@ class CC1101:
         assert 0 < len(setting) <= self._PATABLE_LENGTH_BYTES, setting
         self._write_burst(start_register=PatableAddress.PATABLE, values=setting)
 
+    def get_output_power_levels(self) -> typing.Tuple[int, ...]:
+        """
+        Returns the enabled PATABLE output power levels (up to 8 bytes).
+
+        > [PATABLE] provides flexible PA power ramp up and ramp down
+        > at the start and end of transmission when using 2-FSK, GFSK,
+        > 4-FSK, and MSK modulation as well as ASK modulation shaping.
+
+        See "24 Output Power Programming"
+        """
+        return self._get_patable()[: self._get_power_amplifier_setting_index() + 1]
+
     def _flush_tx_fifo_buffer(self) -> None:
         # > Only issue SFTX in IDLE or TXFIFO_UNDERFLOW states.
         _LOGGER.debug("flushing tx fifo buffer")

+ 18 - 0
tests/test_config.py

@@ -156,3 +156,21 @@ def test_set_base_frequency_hertz_low_warning(transceiver, freq_hz, warn):
         )
     else:
         assert not caught_warnings
+
+
+@pytest.mark.parametrize(
+    ("patable", "patable_index", "power_levels"),
+    (
+        ((198, 0, 0, 0, 0, 0, 0, 0), 0, (198,)),  # CC1101's default
+        ((198, 0, 0, 0, 0, 0, 0, 0), 1, (198, 0)),  # library's default
+        ((0, 198, 0, 0, 0, 0, 0, 0), 1, (0, 198)),
+        ((0, 1, 2, 3, 4, 5, 21, 42), 7, (0, 1, 2, 3, 4, 5, 21, 42)),
+    ),
+)
+def test_get_output_power_levels(transceiver, patable, patable_index, power_levels):
+    with unittest.mock.patch.object(
+        transceiver, "_get_patable", return_value=patable
+    ), unittest.mock.patch.object(
+        transceiver, "_get_power_amplifier_setting_index", return_value=patable_index
+    ):
+        assert transceiver.get_output_power_levels() == power_levels