1
0

test_config.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 typing
  18. import unittest.mock
  19. import warnings
  20. import pytest
  21. import cc1101
  22. # pylint: disable=protected-access
  23. _FREQUENCY_CONTROL_WORD_HERTZ_PARAMS = [
  24. ([0x10, 0xA7, 0x62], 433000000),
  25. ([0x10, 0xAB, 0x85], 433420000),
  26. ([0x10, 0xB1, 0x3B], 434000000),
  27. ([0x21, 0x62, 0x76], 868000000),
  28. ]
  29. @pytest.mark.parametrize(
  30. ("control_word", "hertz"), _FREQUENCY_CONTROL_WORD_HERTZ_PARAMS
  31. )
  32. def test__frequency_control_word_to_hertz(control_word, hertz):
  33. assert cc1101.CC1101._frequency_control_word_to_hertz(
  34. control_word
  35. ) == pytest.approx(hertz, abs=200)
  36. @pytest.mark.parametrize(
  37. ("control_word", "hertz"), _FREQUENCY_CONTROL_WORD_HERTZ_PARAMS
  38. )
  39. def test__hertz_to_frequency_control_word(control_word, hertz):
  40. assert cc1101.CC1101._hertz_to_frequency_control_word(hertz) == control_word
  41. _FILTER_BANDWIDTH_MANTISSA_EXPONENT_REAL_PARAMS = [
  42. # > The default values give 203 kHz channel filter bandwidth,
  43. # > assuming a 26.0 MHz crystal.
  44. (0, 2, 203e3),
  45. # "Table 26: Channel Filter Bandwidths [kHz] (assuming a 26 MHz crystal)"
  46. (0, 0, 812e3),
  47. (0, 1, 406e3),
  48. (0, 2, 203e3),
  49. (1, 0, 650e3),
  50. (1, 1, 325e3),
  51. (3, 0, 464e3),
  52. (3, 1, 232e3),
  53. (3, 2, 116e3),
  54. (3, 3, 58e3),
  55. ]
  56. @pytest.mark.parametrize(
  57. ("mantissa", "exponent", "real"), _FILTER_BANDWIDTH_MANTISSA_EXPONENT_REAL_PARAMS
  58. )
  59. def test__filter_bandwidth_floating_point_to_real(mantissa, exponent, real):
  60. assert cc1101.CC1101._filter_bandwidth_floating_point_to_real(
  61. mantissa=mantissa, exponent=exponent
  62. ) == pytest.approx(real, rel=1e-3)
  63. _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS = [
  64. # > The default values give a data rate of 115.051 kBaud
  65. # > (closest setting to 115.2 kBaud), assuming a 26.0 MHz crystal.
  66. (34, 12, 115051),
  67. (34, 12 + 1, 115051 * 2),
  68. (34, 12 - 1, 115051 / 2),
  69. # > If DRATE_M is rounded to the nearest integer and becomes 256,
  70. # > increment DRATE_E and use DRATE_M = 0.
  71. (0, 13, 203124),
  72. ]
  73. @pytest.mark.parametrize(
  74. ("mantissa", "exponent", "real"), _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS
  75. )
  76. def test__symbol_rate_floating_point_to_real(mantissa, exponent, real):
  77. assert cc1101.CC1101._symbol_rate_floating_point_to_real(
  78. mantissa=mantissa, exponent=exponent
  79. ) == pytest.approx(real, rel=1e-5)
  80. @pytest.mark.parametrize(
  81. ("mantissa", "exponent", "real"), _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS
  82. )
  83. def test__symbol_rate_real_to_floating_point(mantissa, exponent, real):
  84. assert cc1101.CC1101._symbol_rate_real_to_floating_point(real) == (
  85. mantissa,
  86. exponent,
  87. )
  88. @pytest.mark.parametrize(
  89. ("mantissa", "exponent", "real"), _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS
  90. )
  91. def test_get_symbol_rate_baud(transceiver, mantissa, exponent, real):
  92. with unittest.mock.patch.object(
  93. transceiver, "_get_symbol_rate_mantissa", return_value=mantissa
  94. ), unittest.mock.patch.object(
  95. transceiver, "_get_symbol_rate_exponent", return_value=exponent
  96. ):
  97. assert transceiver.get_symbol_rate_baud() == pytest.approx(real, rel=1e-5)
  98. @pytest.mark.parametrize(
  99. ("mantissa", "exponent", "real"), _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS
  100. )
  101. def test_set_symbol_rate_baud(transceiver, mantissa, exponent, real):
  102. with unittest.mock.patch.object(
  103. transceiver, "_set_symbol_rate_mantissa"
  104. ) as set_mantissa_mock, unittest.mock.patch.object(
  105. transceiver, "_set_symbol_rate_exponent"
  106. ) as set_exponent_mock:
  107. transceiver.set_symbol_rate_baud(real)
  108. set_mantissa_mock.assert_called_once_with(mantissa)
  109. set_exponent_mock.assert_called_once_with(exponent)
  110. @pytest.mark.parametrize(
  111. ("freq_hz", "warn"),
  112. (
  113. (100e6, True),
  114. (281.6e6, True),
  115. (281.65e6, False), # within tolerance
  116. (281.7e6, False),
  117. (433.92e6, False),
  118. ),
  119. )
  120. def test_set_base_frequency_hertz_low_warning(transceiver, freq_hz, warn):
  121. with unittest.mock.patch.object(
  122. transceiver, "_set_base_frequency_control_word"
  123. ) as set_control_word_mock:
  124. with warnings.catch_warnings(record=True) as caught_warnings:
  125. transceiver.set_base_frequency_hertz(freq_hz)
  126. set_control_word_mock.assert_called_once()
  127. if warn:
  128. assert len(caught_warnings) == 1
  129. assert (
  130. str(caught_warnings[0].message)
  131. == "CC1101 is unable to transmit at frequencies below 281.7 MHz"
  132. )
  133. else:
  134. assert not caught_warnings
  135. @pytest.mark.parametrize(
  136. ("settings", "insert_spaces", "expected_str"),
  137. [
  138. ((198,), True, "(0xc6,)"),
  139. ((198,), False, "(0xc6,)"),
  140. ((0, 198), True, "(0, 0xc6)"),
  141. ((21, 42, 17), False, "(0x15,0x2a,0x11)"),
  142. ],
  143. )
  144. def test__format_patable(
  145. settings: typing.List[int], insert_spaces: bool, expected_str: str
  146. ) -> None:
  147. assert cc1101._format_patable(settings, insert_spaces=insert_spaces) == expected_str
  148. @pytest.mark.parametrize(
  149. ("patable", "patable_index", "power_levels"),
  150. (
  151. ((198, 0, 0, 0, 0, 0, 0, 0), 0, (198,)), # CC1101's default
  152. ((198, 0, 0, 0, 0, 0, 0, 0), 1, (198, 0)), # library's default
  153. ((0, 198, 0, 0, 0, 0, 0, 0), 1, (0, 198)),
  154. ((0, 1, 2, 3, 4, 5, 21, 42), 7, (0, 1, 2, 3, 4, 5, 21, 42)),
  155. ),
  156. )
  157. def test_get_output_power(transceiver, patable, patable_index, power_levels):
  158. with unittest.mock.patch.object(
  159. transceiver, "_get_patable", return_value=patable
  160. ), unittest.mock.patch.object(
  161. transceiver, "_get_power_amplifier_setting_index", return_value=patable_index
  162. ):
  163. assert transceiver.get_output_power() == power_levels
  164. @pytest.mark.parametrize(
  165. ("patable_index", "power_levels"),
  166. (
  167. (0, (198,)), # CC1101's default
  168. (1, (198, 0)), # library's default
  169. (1, (0, 198)),
  170. (1, [0, 198]),
  171. (1, b"\0\x6c"),
  172. (7, (0, 1, 2, 3, 4, 5, 21, 42)),
  173. ),
  174. )
  175. def test_set_output_power(transceiver, patable_index, power_levels):
  176. with unittest.mock.patch.object(
  177. transceiver, "_set_patable"
  178. ) as set_patable_mock, unittest.mock.patch.object(
  179. transceiver, "_set_power_amplifier_setting_index"
  180. ) as set_patable_index_mock:
  181. transceiver.set_output_power(power_levels)
  182. set_patable_mock.assert_called_once_with(list(power_levels))
  183. set_patable_index_mock.assert_called_once_with(patable_index)
  184. @pytest.mark.parametrize(
  185. "power_levels", (tuple(), (21, 256), (0, 1, 2, 3, 4, 5, 6, 7, 8))
  186. )
  187. def test_set_output_power_invalid(transceiver, power_levels):
  188. with pytest.raises(Exception):
  189. transceiver.set_output_power(power_levels)