test_config.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. # python-cc1101 - Python Library to Transmit RF Signals via C1101 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 pytest
  19. import cc1101
  20. import cc1101.options
  21. # pylint: disable=protected-access
  22. @pytest.mark.parametrize(
  23. ("xfer_return_value", "sync_word"),
  24. [([64, 211, 145], b"\xd3\x91"), ([64, 0, 0], b"\0\0")],
  25. )
  26. def test_get_sync_word(transceiver, xfer_return_value, sync_word):
  27. transceiver._spi.xfer.return_value = xfer_return_value
  28. assert transceiver.get_sync_word() == sync_word
  29. transceiver._spi.xfer.assert_called_once_with([0x04 | 0xC0, 0, 0])
  30. _FREQUENCY_CONTROL_WORD_HERTZ_PARAMS = [
  31. ([0x10, 0xA7, 0x62], 433000000),
  32. ([0x10, 0xAB, 0x85], 433420000),
  33. ([0x10, 0xB1, 0x3B], 434000000),
  34. ([0x21, 0x62, 0x76], 868000000),
  35. ]
  36. @pytest.mark.parametrize(
  37. ("control_word", "hertz"), _FREQUENCY_CONTROL_WORD_HERTZ_PARAMS
  38. )
  39. def test__frequency_control_word_to_hertz(control_word, hertz):
  40. assert cc1101.CC1101._frequency_control_word_to_hertz(
  41. control_word
  42. ) == pytest.approx(hertz, abs=200)
  43. @pytest.mark.parametrize(
  44. ("control_word", "hertz"), _FREQUENCY_CONTROL_WORD_HERTZ_PARAMS
  45. )
  46. def test__hertz_to_frequency_control_word(control_word, hertz):
  47. assert cc1101.CC1101._hertz_to_frequency_control_word(hertz) == control_word
  48. _FILTER_BANDWIDTH_MANTISSA_EXPONENT_REAL_PARAMS = [
  49. # > The default values give 203 kHz channel filter bandwidth,
  50. # > assuming a 26.0 MHz crystal.
  51. (0, 2, 203e3),
  52. # "Table 26: Channel Filter Bandwidths [kHz] (assuming a 26 MHz crystal)"
  53. (0, 0, 812e3),
  54. (0, 1, 406e3),
  55. (0, 2, 203e3),
  56. (1, 0, 650e3),
  57. (1, 1, 325e3),
  58. (3, 0, 464e3),
  59. (3, 1, 232e3),
  60. (3, 2, 116e3),
  61. (3, 3, 58e3),
  62. ]
  63. @pytest.mark.parametrize(
  64. ("mantissa", "exponent", "real"), _FILTER_BANDWIDTH_MANTISSA_EXPONENT_REAL_PARAMS
  65. )
  66. def test__filter_bandwidth_floating_point_to_real(mantissa, exponent, real):
  67. assert cc1101.CC1101._filter_bandwidth_floating_point_to_real(
  68. mantissa=mantissa, exponent=exponent
  69. ) == pytest.approx(real, rel=1e-3)
  70. @pytest.mark.parametrize(
  71. ("mdmcfg4", "real"),
  72. [
  73. (0b10001100, 203e3),
  74. (0b10001010, 203e3),
  75. (0b10001110, 203e3),
  76. (0b11111100, 58e3),
  77. (0b01011100, 325e3),
  78. ],
  79. )
  80. def test__get_filter_bandwidth_hertz(transceiver, mdmcfg4, real):
  81. transceiver._spi.xfer.return_value = [15, mdmcfg4]
  82. assert transceiver._get_filter_bandwidth_hertz() == pytest.approx(real, rel=1e-3)
  83. transceiver._spi.xfer.assert_called_once_with([0x10 | 0x80, 0])
  84. _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS = [
  85. # > The default values give a data rate of 115.051 kBaud
  86. # > (closest setting to 115.2 kBaud), assuming a 26.0 MHz crystal.
  87. (34, 12, 115051),
  88. (34, 12 + 1, 115051 * 2),
  89. (34, 12 - 1, 115051 / 2),
  90. ]
  91. @pytest.mark.parametrize(
  92. ("mantissa", "exponent", "real"), _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS
  93. )
  94. def test__symbol_rate_floating_point_to_real(mantissa, exponent, real):
  95. assert cc1101.CC1101._symbol_rate_floating_point_to_real(
  96. mantissa=mantissa, exponent=exponent
  97. ) == pytest.approx(real, rel=1e-5)
  98. @pytest.mark.parametrize(
  99. ("mantissa", "exponent", "real"), _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS
  100. )
  101. def test__symbol_rate_real_to_floating_point(mantissa, exponent, real):
  102. assert cc1101.CC1101._symbol_rate_real_to_floating_point(real) == (
  103. mantissa,
  104. exponent,
  105. )
  106. def test_get_packet_length_bytes(transceiver):
  107. xfer_mock = transceiver._spi.xfer
  108. xfer_mock.return_value = [0, 8]
  109. assert transceiver.get_packet_length_bytes() == 8
  110. xfer_mock.assert_called_once_with([0x06 | 0x80, 0])
  111. @pytest.mark.parametrize("packet_length", [21])
  112. def test_set_packet_length_bytes(transceiver, packet_length):
  113. xfer_mock = transceiver._spi.xfer
  114. xfer_mock.return_value = [15, 15]
  115. transceiver.set_packet_length_bytes(packet_length)
  116. xfer_mock.assert_called_once_with([0x06 | 0x40, packet_length])
  117. @pytest.mark.parametrize("packet_length", [-21, 0, 256, 1024])
  118. def test_set_packet_length_bytes_fail(transceiver, packet_length):
  119. with pytest.raises(Exception):
  120. transceiver.set_packet_length_bytes(packet_length)
  121. transceiver._spi.xfer.assert_not_called()
  122. @pytest.mark.parametrize(
  123. ("pktctrl0_before", "pktctrl0_after"),
  124. (
  125. # unchanged
  126. (0b00000000, 0b00000000),
  127. (0b00010000, 0b00010000),
  128. (0b00010001, 0b00010001),
  129. (0b01000000, 0b01000000),
  130. (0b01000010, 0b01000010),
  131. (0b01110000, 0b01110000),
  132. (0b01110010, 0b01110010),
  133. # disabled
  134. (0b00010100, 0b00010000),
  135. (0b01000100, 0b01000000),
  136. (0b01000110, 0b01000010),
  137. (0b01110110, 0b01110010),
  138. ),
  139. )
  140. def test_disable_checksum(transceiver, pktctrl0_before, pktctrl0_after):
  141. xfer_mock = transceiver._spi.xfer
  142. xfer_mock.return_value = [15, 15]
  143. with unittest.mock.patch.object(
  144. transceiver, "_read_single_byte", return_value=pktctrl0_before
  145. ):
  146. transceiver.disable_checksum()
  147. xfer_mock.assert_called_once_with([0x08 | 0x40, pktctrl0_after])
  148. @pytest.mark.parametrize(
  149. ("pktctrl0", "expected_mode"),
  150. (
  151. (0b00000000, cc1101.options.PacketLengthMode.FIXED),
  152. (0b00000001, cc1101.options.PacketLengthMode.VARIABLE),
  153. (0b01000100, cc1101.options.PacketLengthMode.FIXED),
  154. (0b01000101, cc1101.options.PacketLengthMode.VARIABLE),
  155. ),
  156. )
  157. def test_get_packet_length_mode(transceiver, pktctrl0, expected_mode):
  158. xfer_mock = transceiver._spi.xfer
  159. xfer_mock.return_value = [0, pktctrl0]
  160. assert transceiver.get_packet_length_mode() == expected_mode
  161. xfer_mock.assert_called_once_with([0x08 | 0x80, 0])
  162. @pytest.mark.parametrize(
  163. ("pktctrl0_before", "pktctrl0_after", "mode"),
  164. (
  165. (0b00000000, 0b00000000, cc1101.options.PacketLengthMode.FIXED),
  166. (0b00000001, 0b00000000, cc1101.options.PacketLengthMode.FIXED),
  167. (0b00000001, 0b00000001, cc1101.options.PacketLengthMode.VARIABLE),
  168. (0b00000010, 0b00000000, cc1101.options.PacketLengthMode.FIXED),
  169. (0b00000010, 0b00000001, cc1101.options.PacketLengthMode.VARIABLE),
  170. (0b01000100, 0b01000100, cc1101.options.PacketLengthMode.FIXED),
  171. (0b01000100, 0b01000101, cc1101.options.PacketLengthMode.VARIABLE),
  172. (0b01000101, 0b01000100, cc1101.options.PacketLengthMode.FIXED),
  173. (0b01000101, 0b01000101, cc1101.options.PacketLengthMode.VARIABLE),
  174. ),
  175. )
  176. def test_set_packet_length_mode(transceiver, pktctrl0_before, pktctrl0_after, mode):
  177. xfer_mock = transceiver._spi.xfer
  178. xfer_mock.return_value = [15, 15]
  179. with unittest.mock.patch.object(
  180. transceiver, "_read_single_byte", return_value=pktctrl0_before
  181. ):
  182. transceiver.set_packet_length_mode(mode)
  183. xfer_mock.assert_called_once_with([0x08 | 0x40, pktctrl0_after])