Browse Source

added method `set_sync_mode()`

Fabian Peter Hammerle 3 years ago
parent
commit
f58e3f370a
4 changed files with 32 additions and 0 deletions
  1. 1 0
      CHANGELOG.md
  2. 10 0
      cc1101/__init__.py
  3. 9 0
      examples/transmit_variable_length.py
  4. 12 0
      tests/test_config.py

+ 1 - 0
CHANGELOG.md

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

+ 10 - 0
cc1101/__init__.py

@@ -459,6 +459,16 @@ class CC1101:
             )
         )
 
+    def set_sync_word(self, sync_word: bytes) -> None:
+        """
+        See .set_sync_word()
+        """
+        if len(sync_word) != 2:
+            raise ValueError("expected two bytes, got {!r}".format(sync_word))
+        self._write_burst(
+            start_register=ConfigurationRegisterAddress.SYNC1, values=list(sync_word)
+        )
+
     def get_packet_length_bytes(self) -> int:
         """
         PKTLEN

+ 9 - 0
examples/transmit_variable_length.py

@@ -11,12 +11,21 @@ with cc1101.CC1101() as transceiver:
     transceiver.set_base_frequency_hertz(433.5e6)
     transceiver.set_symbol_rate_baud(600)
     # transceiver.set_sync_mode(cc1101.SyncMode.NO_PREAMBLE_AND_SYNC_WORD)
+    # transceiver.set_sync_word(b"\x12\x34")
     # transceiver.disable_checksum()
     print(transceiver)
     print("state", transceiver.get_marc_state().name)
     print("base frequency", transceiver.get_base_frequency_hertz(), "Hz")
     print("symbol rate", transceiver.get_symbol_rate_baud(), "Baud")
     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")
     transceiver.transmit(b"\xff\xaa\x00 message")
     time.sleep(1.0)

+ 12 - 0
tests/test_config.py

@@ -35,6 +35,18 @@ def test_get_sync_word(transceiver, xfer_return_value, sync_word):
     transceiver._spi.xfer.assert_called_once_with([0x04 | 0xC0, 0, 0])
 
 
+def test_set_sync_word(transceiver):
+    transceiver._spi.xfer.return_value = [15, 15, 15]
+    transceiver.set_sync_word(b"\x12\x34")
+    transceiver._spi.xfer.assert_called_once_with([0x04 | 0x40, 0x12, 0x34])
+
+
+@pytest.mark.parametrize("sync_word", [b"", b"\0", "\x12\x34\x56"])
+def test_set_sync_word_invalid_length(transceiver, sync_word):
+    with pytest.raises(ValueError, match=r"\bexpected two bytes\b"):
+        transceiver.set_sync_word(sync_word)
+
+
 _FREQUENCY_CONTROL_WORD_HERTZ_PARAMS = [
     ([0x10, 0xA7, 0x62], 433000000),
     ([0x10, 0xAB, 0x85], 433420000),