Browse Source

test symbol rate conversion

Fabian Peter Hammerle 3 years ago
parent
commit
5b87f62d78
2 changed files with 27 additions and 4 deletions
  1. 11 4
      cc1101/__init__.py
  2. 16 0
      tests/test_cc1101.py

+ 11 - 4
cc1101/__init__.py

@@ -158,15 +158,22 @@ class CC1101:
         """
         return self._read_single_byte(self._SPIAddress.MDMCFG3)
 
-    def get_symbol_rate_baud(self) -> float:
+    @classmethod
+    def _symbol_rate_floating_point_to_real(cls, mantissa: int, exponent: int) -> float:
         # see "12 Data Rate Programming"
         return (
-            (256 + self._get_symbol_rate_mantissa())
-            * (2 ** self._get_symbol_rate_exponent())
-            * self._CRYSTAL_OSCILLATOR_FREQUENCY_HERTZ
+            (256 + mantissa)
+            * (2 ** exponent)
+            * cls._CRYSTAL_OSCILLATOR_FREQUENCY_HERTZ
             / (2 ** 28)
         )
 
+    def get_symbol_rate_baud(self) -> float:
+        return self._symbol_rate_floating_point_to_real(
+            mantissa=self._get_symbol_rate_mantissa(),
+            exponent=self._get_symbol_rate_exponent(),
+        )
+
     def get_modulation_format(self) -> ModulationFormat:
         mdmcfg2 = self._read_single_byte(self._SPIAddress.MDMCFG2)
         return self.ModulationFormat((mdmcfg2 >> 4) & 0b111)

+ 16 - 0
tests/test_cc1101.py

@@ -24,3 +24,19 @@ def test__frequency_control_word_to_hertz(control_word, hertz):
 )
 def test__hertz_to_frequency_control_word(control_word, hertz):
     assert cc1101.CC1101._hertz_to_frequency_control_word(hertz) == control_word
+
+
+@pytest.mark.parametrize(
+    ("mantissa", "exponent", "real"),
+    [
+        # > The default values give a data rate of 115.051 kBaud
+        # > (closest setting to 115.2 kBaud), assuming a 26.0 MHz crystal.
+        (34, 12, 115051),
+        (34, 12 + 1, 115051 * 2),
+        (34, 12 - 1, 115051 / 2),
+    ],
+)
+def test__symbol_rate_floating_point_to_real(mantissa, exponent, real):
+    assert cc1101.CC1101._symbol_rate_floating_point_to_real(
+        mantissa=mantissa, exponent=exponent
+    ) == pytest.approx(real, rel=1e-5)