test_config.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS = [
  63. # > The default values give a data rate of 115.051 kBaud
  64. # > (closest setting to 115.2 kBaud), assuming a 26.0 MHz crystal.
  65. (34, 12, 115051),
  66. (34, 12 + 1, 115051 * 2),
  67. (34, 12 - 1, 115051 / 2),
  68. # > If DRATE_M is rounded to the nearest integer and becomes 256,
  69. # > increment DRATE_E and use DRATE_M = 0.
  70. (0, 13, 203124),
  71. ]
  72. @pytest.mark.parametrize(
  73. ("mantissa", "exponent", "real"), _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS
  74. )
  75. def test__symbol_rate_floating_point_to_real(mantissa, exponent, real):
  76. assert cc1101.CC1101._symbol_rate_floating_point_to_real(
  77. mantissa=mantissa, exponent=exponent
  78. ) == pytest.approx(real, rel=1e-5)
  79. @pytest.mark.parametrize(
  80. ("mantissa", "exponent", "real"), _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS
  81. )
  82. def test__symbol_rate_real_to_floating_point(mantissa, exponent, real):
  83. assert cc1101.CC1101._symbol_rate_real_to_floating_point(real) == (
  84. mantissa,
  85. exponent,
  86. )
  87. @pytest.mark.parametrize(
  88. ("mantissa", "exponent", "real"), _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS
  89. )
  90. def test_get_symbol_rate_baud(transceiver, mantissa, exponent, real):
  91. with unittest.mock.patch.object(
  92. transceiver, "_get_symbol_rate_mantissa", return_value=mantissa
  93. ), unittest.mock.patch.object(
  94. transceiver, "_get_symbol_rate_exponent", return_value=exponent
  95. ):
  96. assert transceiver.get_symbol_rate_baud() == pytest.approx(real, rel=1e-5)
  97. @pytest.mark.parametrize(
  98. ("mantissa", "exponent", "real"), _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS
  99. )
  100. def test_set_symbol_rate_baud(transceiver, mantissa, exponent, real):
  101. with unittest.mock.patch.object(
  102. transceiver, "_set_symbol_rate_mantissa"
  103. ) as set_mantissa_mock, unittest.mock.patch.object(
  104. transceiver, "_set_symbol_rate_exponent"
  105. ) as set_exponent_mock:
  106. transceiver.set_symbol_rate_baud(real)
  107. set_mantissa_mock.assert_called_once_with(mantissa)
  108. set_exponent_mock.assert_called_once_with(exponent)
  109. @pytest.mark.parametrize(
  110. ("freq_hz", "warn"),
  111. (
  112. (100e6, True),
  113. (281.6e6, True),
  114. (281.65e6, False), # within tolerance
  115. (281.7e6, False),
  116. (433.92e6, False),
  117. ),
  118. )
  119. def test_set_base_frequency_hertz_low_warning(transceiver, freq_hz, warn):
  120. with unittest.mock.patch.object(
  121. transceiver, "_set_base_frequency_control_word"
  122. ) as set_control_word_mock:
  123. with warnings.catch_warnings(record=True) as caught_warnings:
  124. transceiver.set_base_frequency_hertz(freq_hz)
  125. set_control_word_mock.assert_called_once()
  126. if warn:
  127. assert len(caught_warnings) == 1
  128. assert (
  129. str(caught_warnings[0].message)
  130. == "CC1101 is unable to transmit at frequencies below 281.7 MHz"
  131. )
  132. else:
  133. assert not caught_warnings
  134. @pytest.mark.parametrize(
  135. ("patable", "patable_index", "power_levels"),
  136. (
  137. ((198, 0, 0, 0, 0, 0, 0, 0), 0, (198,)), # CC1101's default
  138. ((198, 0, 0, 0, 0, 0, 0, 0), 1, (198, 0)), # library's default
  139. ((0, 198, 0, 0, 0, 0, 0, 0), 1, (0, 198)),
  140. ((0, 1, 2, 3, 4, 5, 21, 42), 7, (0, 1, 2, 3, 4, 5, 21, 42)),
  141. ),
  142. )
  143. def test_get_output_power(transceiver, patable, patable_index, power_levels):
  144. with unittest.mock.patch.object(
  145. transceiver, "_get_patable", return_value=patable
  146. ), unittest.mock.patch.object(
  147. transceiver, "_get_power_amplifier_setting_index", return_value=patable_index
  148. ):
  149. assert transceiver.get_output_power() == power_levels
  150. @pytest.mark.parametrize(
  151. ("patable_index", "power_levels"),
  152. (
  153. (0, (198,)), # CC1101's default
  154. (1, (198, 0)), # library's default
  155. (1, (0, 198)),
  156. (1, [0, 198]),
  157. (1, b"\0\x6c"),
  158. (7, (0, 1, 2, 3, 4, 5, 21, 42)),
  159. ),
  160. )
  161. def test_set_output_power(transceiver, patable_index, power_levels):
  162. with unittest.mock.patch.object(
  163. transceiver, "_set_patable"
  164. ) as set_patable_mock, unittest.mock.patch.object(
  165. transceiver, "_set_power_amplifier_setting_index"
  166. ) as set_patable_index_mock:
  167. transceiver.set_output_power(power_levels)
  168. set_patable_mock.assert_called_once_with(list(power_levels))
  169. set_patable_index_mock.assert_called_once_with(patable_index)
  170. @pytest.mark.parametrize(
  171. "power_levels", (tuple(), (21, 256), (0, 1, 2, 3, 4, 5, 6, 7, 8))
  172. )
  173. def test_set_output_power_invalid(transceiver, power_levels):
  174. with pytest.raises(Exception):
  175. transceiver.set_output_power(power_levels)