Browse Source

added method `get_preamble_length_bytes()`

Fabian Peter Hammerle 3 years ago
parent
commit
7ae1bb8240
4 changed files with 41 additions and 8 deletions
  1. 2 1
      CHANGELOG.md
  2. 16 0
      cc1101/__init__.py
  3. 4 7
      examples/transmit_variable_length.py
  4. 19 0
      tests/test_config.py

+ 2 - 1
CHANGELOG.md

@@ -8,7 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ### Added
 - method `.get_sync_word()`
 - method `.set_sync_word()`
-- sync word in string representation
+- method `.get_preamble_length_bytes()`
+- preamble length & sync word in string representation
 
 ## [2.0.0] - 2020-12-03
 ### Changed

+ 16 - 0
cc1101/__init__.py

@@ -69,6 +69,8 @@ class MainRadioControlStateMachineState(enum.IntEnum):
 
 class CC1101:
 
+    # pylint: disable=too-many-public-methods
+
     # > All transfers on the SPI interface are done
     # > most significant bit first.
     # > All transactions on the SPI interface start with
@@ -293,6 +295,17 @@ class CC1101:
         mdmcfg2 |= mode
         self._write_burst(ConfigurationRegisterAddress.MDMCFG2, [mdmcfg2])
 
+    def get_preamble_length_bytes(self) -> int:
+        """
+        Minimum number of preamble bytes to be transmitted.
+
+        See "15.2 Packet Format"
+        """
+        index = (
+            self._read_single_byte(ConfigurationRegisterAddress.MDMCFG1) >> 4
+        ) & 0b111
+        return int(2 ** int(index / 2 + 1) * (1 + (index & 0b1) * 0.5))
+
     def _set_power_amplifier_setting_index(self, setting_index: int) -> None:
         """
         FREND0.PA_POWER
@@ -417,6 +430,9 @@ class CC1101:
             "symbol_rate={:.2f}kBaud".format(self.get_symbol_rate_baud() / 1000),
             "modulation_format={}".format(self.get_modulation_format().name),
             "sync_mode={}".format(sync_mode.name),
+            "preamble_length={}B".format(self.get_preamble_length_bytes())
+            if sync_mode != SyncMode.NO_PREAMBLE_AND_SYNC_WORD
+            else None,
             "sync_word=0x{:02x}{:02x}".format(*self.get_sync_word())
             if sync_mode != SyncMode.NO_PREAMBLE_AND_SYNC_WORD
             else None,

+ 4 - 7
examples/transmit_variable_length.py

@@ -20,13 +20,10 @@ with cc1101.CC1101() as transceiver:
     print("modulation format", transceiver.get_modulation_format().name)
     sync_mode = transceiver.get_sync_mode()
     print("sync mode", sync_mode)
-    print(
-        "sync word",
-        transceiver.get_sync_word()
-        if sync_mode != cc1101.SyncMode.NO_PREAMBLE_AND_SYNC_WORD
-        else "disabled",
-    )
-    print("starting transmission")
+    if sync_mode != cc1101.SyncMode.NO_PREAMBLE_AND_SYNC_WORD:
+        print("preamble length", transceiver.get_preamble_length_bytes(), "bytes")
+        print("sync word", transceiver.get_sync_word())
+    print("\nstarting transmission")
     transceiver.transmit(b"\xff\xaa\x00 message")
     time.sleep(1.0)
     transceiver.transmit(bytes([0, 0b10101010, 0xFF]))

+ 19 - 0
tests/test_config.py

@@ -141,6 +141,25 @@ def test__symbol_rate_real_to_floating_point(mantissa, exponent, real):
     )
 
 
+@pytest.mark.parametrize(
+    ("mdmcfg1", "length"),
+    [
+        (0b00000010, 2),
+        (0b00010010, 3),
+        (0b00100010, 4),
+        (0b00110010, 6),
+        (0b01000010, 8),
+        (0b01010010, 12),
+        (0b01100010, 16),
+        (0b01110010, 24),
+    ],
+)
+def test_get_preamble_length_bytes(transceiver, mdmcfg1, length):
+    transceiver._spi.xfer.return_value = [0, mdmcfg1]
+    assert transceiver.get_preamble_length_bytes() == length
+    transceiver._spi.xfer.assert_called_once_with([0x13 | 0x80, 0])
+
+
 def test_get_packet_length_bytes(transceiver):
     xfer_mock = transceiver._spi.xfer
     xfer_mock.return_value = [0, 8]