test_config.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # python-cc1101 - Python Library to Transmit RF Signals via CC1101 Transceivers
  2. #
  3. # Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import unittest.mock
  18. import warnings
  19. import pytest
  20. import cc1101
  21. # pylint: disable=protected-access
  22. _FREQUENCY_CONTROL_WORD_HERTZ_PARAMS = [
  23. ([0x10, 0xA7, 0x62], 433000000),
  24. ([0x10, 0xAB, 0x85], 433420000),
  25. ([0x10, 0xB1, 0x3B], 434000000),
  26. ([0x21, 0x62, 0x76], 868000000),
  27. ]
  28. @pytest.mark.parametrize(
  29. ("control_word", "hertz"), _FREQUENCY_CONTROL_WORD_HERTZ_PARAMS
  30. )
  31. def test__frequency_control_word_to_hertz(control_word, hertz):
  32. assert cc1101.CC1101._frequency_control_word_to_hertz(
  33. control_word
  34. ) == pytest.approx(hertz, abs=200)
  35. @pytest.mark.parametrize(
  36. ("control_word", "hertz"), _FREQUENCY_CONTROL_WORD_HERTZ_PARAMS
  37. )
  38. def test__hertz_to_frequency_control_word(control_word, hertz):
  39. assert cc1101.CC1101._hertz_to_frequency_control_word(hertz) == control_word
  40. _FILTER_BANDWIDTH_MANTISSA_EXPONENT_REAL_PARAMS = [
  41. # > The default values give 203 kHz channel filter bandwidth,
  42. # > assuming a 26.0 MHz crystal.
  43. (0, 2, 203e3),
  44. # "Table 26: Channel Filter Bandwidths [kHz] (assuming a 26 MHz crystal)"
  45. (0, 0, 812e3),
  46. (0, 1, 406e3),
  47. (0, 2, 203e3),
  48. (1, 0, 650e3),
  49. (1, 1, 325e3),
  50. (3, 0, 464e3),
  51. (3, 1, 232e3),
  52. (3, 2, 116e3),
  53. (3, 3, 58e3),
  54. ]
  55. @pytest.mark.parametrize(
  56. ("mantissa", "exponent", "real"), _FILTER_BANDWIDTH_MANTISSA_EXPONENT_REAL_PARAMS
  57. )
  58. def test__filter_bandwidth_floating_point_to_real(mantissa, exponent, real):
  59. assert cc1101.CC1101._filter_bandwidth_floating_point_to_real(
  60. mantissa=mantissa, exponent=exponent
  61. ) == pytest.approx(real, rel=1e-3)
  62. @pytest.mark.parametrize(
  63. ("mdmcfg4", "real"),
  64. [
  65. (0b10001100, 203e3),
  66. (0b10001010, 203e3),
  67. (0b10001110, 203e3),
  68. (0b11111100, 58e3),
  69. (0b01011100, 325e3),
  70. ],
  71. )
  72. def test__get_filter_bandwidth_hertz(transceiver, mdmcfg4, real):
  73. transceiver._spi.xfer.return_value = [15, mdmcfg4]
  74. assert transceiver._get_filter_bandwidth_hertz() == pytest.approx(real, rel=1e-3)
  75. transceiver._spi.xfer.assert_called_once_with([0x10 | 0x80, 0])
  76. @pytest.mark.parametrize(
  77. ("mdmcfg4_before", "mdmcfg4_after", "exponent", "mantissa"),
  78. [
  79. (0b00001010, 0b10111010, 0b10, 0b11),
  80. (0b00001100, 0b01001100, 0b01, 0b00),
  81. (0b00001100, 0b10111100, 0b10, 0b11),
  82. (0b00001100, 0b11011100, 0b11, 0b01),
  83. (0b01011100, 0b11011100, 0b11, 0b01),
  84. (0b11111100, 0b11011100, 0b11, 0b01),
  85. ],
  86. )
  87. def test__set_filter_bandwidth(
  88. transceiver, mdmcfg4_before, mdmcfg4_after, exponent, mantissa
  89. ):
  90. transceiver._spi.xfer.return_value = [15, 15]
  91. with unittest.mock.patch.object(
  92. transceiver, "_read_single_byte", return_value=mdmcfg4_before
  93. ):
  94. transceiver._set_filter_bandwidth(mantissa=mantissa, exponent=exponent)
  95. transceiver._spi.xfer.assert_called_once_with([0x10 | 0x40, mdmcfg4_after])
  96. @pytest.mark.parametrize(
  97. ("mdmcfg3", "symbol_rate_mantissa"), [(0b00100010, 34), (0b10101010, 170)]
  98. )
  99. def test__get_symbol_rate_mantissa(transceiver, mdmcfg3, symbol_rate_mantissa):
  100. transceiver._spi.xfer.return_value = [15, mdmcfg3]
  101. assert transceiver._get_symbol_rate_mantissa() == symbol_rate_mantissa
  102. transceiver._spi.xfer.assert_called_once_with([0x11 | 0x80, 0])
  103. _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS = [
  104. # > The default values give a data rate of 115.051 kBaud
  105. # > (closest setting to 115.2 kBaud), assuming a 26.0 MHz crystal.
  106. (34, 12, 115051),
  107. (34, 12 + 1, 115051 * 2),
  108. (34, 12 - 1, 115051 / 2),
  109. ]
  110. @pytest.mark.parametrize(
  111. ("mantissa", "exponent", "real"), _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS
  112. )
  113. def test__symbol_rate_floating_point_to_real(mantissa, exponent, real):
  114. assert cc1101.CC1101._symbol_rate_floating_point_to_real(
  115. mantissa=mantissa, exponent=exponent
  116. ) == pytest.approx(real, rel=1e-5)
  117. @pytest.mark.parametrize(
  118. ("mantissa", "exponent", "real"), _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS
  119. )
  120. def test__symbol_rate_real_to_floating_point(mantissa, exponent, real):
  121. assert cc1101.CC1101._symbol_rate_real_to_floating_point(real) == (
  122. mantissa,
  123. exponent,
  124. )
  125. @pytest.mark.parametrize(
  126. ("freq_hz", "warn"),
  127. (
  128. (100e6, True),
  129. (281.6e6, True),
  130. (281.65e6, False), # within tolerance
  131. (281.7e6, False),
  132. (433.92e6, False),
  133. ),
  134. )
  135. def test_set_base_frequency_hertz_low_warning(transceiver, freq_hz, warn):
  136. with unittest.mock.patch.object(
  137. transceiver, "_set_base_frequency_control_word"
  138. ) as set_control_word_mock:
  139. with warnings.catch_warnings(record=True) as caught_warnings:
  140. transceiver.set_base_frequency_hertz(freq_hz)
  141. assert set_control_word_mock.call_count == 1
  142. if warn:
  143. assert len(caught_warnings) == 1
  144. assert (
  145. str(caught_warnings[0].message)
  146. == "CC1101 is unable to transmit at frequencies below 281.7 MHz"
  147. )
  148. else:
  149. assert not caught_warnings