Browse Source

add option/kwargs "spi_max_speed_hz" to CC1101's constructor (@matteo-briani)

https://github.com/fphammerle/python-cc1101/pull/129
https://github.com/fphammerle/python-cc1101/issues/128
Fabian Peter Hammerle 1 year ago
parent
commit
51ae4123f4
3 changed files with 25 additions and 2 deletions
  1. 3 0
      CHANGELOG.md
  2. 7 2
      cc1101/__init__.py
  3. 15 0
      tests/test_spi.py

+ 3 - 0
CHANGELOG.md

@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ## [Unreleased]
 ### Added
 - declare compatibility with `python3.11`
+- allow parametrization of `spi_max_speed_hz` during `C1101` class instantiation
+  to solve [issue-128] 
 
 ### Changed
 - `CC1101.transmit`: raise `RuntimeError` instead of `Exception` when
@@ -133,3 +135,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 [1.2.0]: https://github.com/fphammerle/python-cc1101/compare/v1.1.0...v1.2.0
 [1.1.0]: https://github.com/fphammerle/python-cc1101/compare/v1.0.0...v1.1.0
 [1.0.0]: https://github.com/fphammerle/python-cc1101/releases/tag/v1.0.0
+[issue-128]: https://github.com/fphammerle/python-cc1101/issues/128

+ 7 - 2
cc1101/__init__.py

@@ -156,7 +156,11 @@ class CC1101:
     _PATABLE_LENGTH_BYTES = 8
 
     def __init__(
-        self, spi_bus: int = 0, spi_chip_select: int = 0, lock_spi_device: bool = False
+        self,
+        spi_bus: int = 0,
+        spi_chip_select: int = 0,
+        lock_spi_device: bool = False,
+        spi_max_speed_hz: int = 55700,
     ) -> None:
         """
         lock_spi_device:
@@ -177,6 +181,7 @@ class CC1101:
             >>>     # lock removed
         """
         self._spi = spidev.SpiDev()
+        self._spi_max_speed_hz = spi_max_speed_hz
         self._spi_bus = int(spi_bus)
         # > The BCM2835 core common to all Raspberry Pi devices has 3 SPI Controllers:
         # > SPI0, with two hardware chip selects, [...]
@@ -558,7 +563,7 @@ class CC1101:
             # lock removed in __exit__ by SpiDev.close()
             fcntl.flock(self._spi.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
         try:
-            self._spi.max_speed_hz = 55700  # empirical
+            self._spi.max_speed_hz = self._spi_max_speed_hz
             self._reset()
             self._verify_chip()
             self._configure_defaults()

+ 15 - 0
tests/test_spi.py

@@ -35,6 +35,7 @@ def test___init__select_device(bus, chip_select):
     assert transceiver._spi_bus == bus
     assert transceiver._spi_chip_select == chip_select
     assert transceiver._spi_device_path == f"/dev/spidev{bus}.{chip_select}"
+    assert transceiver._spi_max_speed_hz == 55700
     transceiver._spi.open.side_effect = SystemExit
     with pytest.raises(SystemExit):
         with transceiver:
@@ -87,6 +88,7 @@ def test___enter__(transceiver, chip_version):
             assert transceiver == transceiver_context
             transceiver._spi.open.assert_called_once_with(0, 0)
             assert transceiver._spi.max_speed_hz == 55700
+            assert transceiver._spi.max_speed_hz == transceiver._spi_max_speed_hz
             reset_mock.assert_called_once_with()
             set_modulation_format_mock.assert_called_once_with(
                 cc1101.options.ModulationFormat.ASK_OOK
@@ -99,6 +101,19 @@ def test___enter__(transceiver, chip_version):
             ]
 
 
+@pytest.mark.parametrize("spi_max_speed_hz", [55700, 500000])
+def test___enter__spi_max_speed(spi_max_speed_hz):
+    with unittest.mock.patch("spidev.SpiDev"):
+        transceiver = cc1101.CC1101(spi_max_speed_hz=spi_max_speed_hz)
+    assert transceiver._spi_max_speed_hz == spi_max_speed_hz
+    with unittest.mock.patch.object(
+        transceiver, "_reset", side_effect=SystemExit
+    ), pytest.raises(SystemExit):
+        with transceiver:
+            pass
+    assert transceiver._spi.max_speed_hz == spi_max_speed_hz
+
+
 def test___enter___unsupported_partnum(transceiver):
     with unittest.mock.patch.object(
         transceiver, "_read_status_register"