Skip to content

Commit 51ae412

Browse files
committed
add option/kwargs "spi_max_speed_hz" to CC1101's constructor (@matteo-briani)
#129 #128
2 parents 1c9a5bb + 0f90b4c commit 51ae412

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88
### Added
99
- declare compatibility with `python3.11`
10+
- allow parametrization of `spi_max_speed_hz` during `C1101` class instantiation
11+
to solve [issue-128]
1012

1113
### Changed
1214
- `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
133135
[1.2.0]: https://github.com/fphammerle/python-cc1101/compare/v1.1.0...v1.2.0
134136
[1.1.0]: https://github.com/fphammerle/python-cc1101/compare/v1.0.0...v1.1.0
135137
[1.0.0]: https://github.com/fphammerle/python-cc1101/releases/tag/v1.0.0
138+
[issue-128]: https://github.com/fphammerle/python-cc1101/issues/128

cc1101/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,11 @@ class CC1101:
156156
_PATABLE_LENGTH_BYTES = 8
157157

158158
def __init__(
159-
self, spi_bus: int = 0, spi_chip_select: int = 0, lock_spi_device: bool = False
159+
self,
160+
spi_bus: int = 0,
161+
spi_chip_select: int = 0,
162+
lock_spi_device: bool = False,
163+
spi_max_speed_hz: int = 55700,
160164
) -> None:
161165
"""
162166
lock_spi_device:
@@ -177,6 +181,7 @@ def __init__(
177181
>>> # lock removed
178182
"""
179183
self._spi = spidev.SpiDev()
184+
self._spi_max_speed_hz = spi_max_speed_hz
180185
self._spi_bus = int(spi_bus)
181186
# > The BCM2835 core common to all Raspberry Pi devices has 3 SPI Controllers:
182187
# > SPI0, with two hardware chip selects, [...]
@@ -558,7 +563,7 @@ def __enter__(self) -> CC1101:
558563
# lock removed in __exit__ by SpiDev.close()
559564
fcntl.flock(self._spi.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
560565
try:
561-
self._spi.max_speed_hz = 55700 # empirical
566+
self._spi.max_speed_hz = self._spi_max_speed_hz
562567
self._reset()
563568
self._verify_chip()
564569
self._configure_defaults()

tests/test_spi.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def test___init__select_device(bus, chip_select):
3535
assert transceiver._spi_bus == bus
3636
assert transceiver._spi_chip_select == chip_select
3737
assert transceiver._spi_device_path == f"/dev/spidev{bus}.{chip_select}"
38+
assert transceiver._spi_max_speed_hz == 55700
3839
transceiver._spi.open.side_effect = SystemExit
3940
with pytest.raises(SystemExit):
4041
with transceiver:
@@ -87,6 +88,7 @@ def test___enter__(transceiver, chip_version):
8788
assert transceiver == transceiver_context
8889
transceiver._spi.open.assert_called_once_with(0, 0)
8990
assert transceiver._spi.max_speed_hz == 55700
91+
assert transceiver._spi.max_speed_hz == transceiver._spi_max_speed_hz
9092
reset_mock.assert_called_once_with()
9193
set_modulation_format_mock.assert_called_once_with(
9294
cc1101.options.ModulationFormat.ASK_OOK
@@ -99,6 +101,19 @@ def test___enter__(transceiver, chip_version):
99101
]
100102

101103

104+
@pytest.mark.parametrize("spi_max_speed_hz", [55700, 500000])
105+
def test___enter__spi_max_speed(spi_max_speed_hz):
106+
with unittest.mock.patch("spidev.SpiDev"):
107+
transceiver = cc1101.CC1101(spi_max_speed_hz=spi_max_speed_hz)
108+
assert transceiver._spi_max_speed_hz == spi_max_speed_hz
109+
with unittest.mock.patch.object(
110+
transceiver, "_reset", side_effect=SystemExit
111+
), pytest.raises(SystemExit):
112+
with transceiver:
113+
pass
114+
assert transceiver._spi.max_speed_hz == spi_max_speed_hz
115+
116+
102117
def test___enter___unsupported_partnum(transceiver):
103118
with unittest.mock.patch.object(
104119
transceiver, "_read_status_register"

0 commit comments

Comments
 (0)