test_config.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. _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. _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS = [
  41. # > The default values give a data rate of 115.051 kBaud
  42. # > (closest setting to 115.2 kBaud), assuming a 26.0 MHz crystal.
  43. (34, 12, 115051),
  44. (34, 12 + 1, 115051 * 2),
  45. (34, 12 - 1, 115051 / 2),
  46. ]
  47. @pytest.mark.parametrize(
  48. ("mantissa", "exponent", "real"), _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS
  49. )
  50. def test__symbol_rate_floating_point_to_real(mantissa, exponent, real):
  51. assert cc1101.CC1101._symbol_rate_floating_point_to_real(
  52. mantissa=mantissa, exponent=exponent
  53. ) == pytest.approx(real, rel=1e-5)
  54. @pytest.mark.parametrize(
  55. ("mantissa", "exponent", "real"), _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS
  56. )
  57. def test__symbol_rate_real_to_floating_point(mantissa, exponent, real):
  58. assert cc1101.CC1101._symbol_rate_real_to_floating_point(real) == (
  59. mantissa,
  60. exponent,
  61. )
  62. def test_get_packet_length_bytes(transceiver):
  63. xfer_mock = transceiver._spi.xfer
  64. xfer_mock.return_value = [0, 8]
  65. assert transceiver.get_packet_length_bytes() == 8
  66. xfer_mock.assert_called_once_with([0x06 | 0x80, 0])
  67. @pytest.mark.parametrize("packet_length", [21])
  68. def test_set_packet_length_bytes(transceiver, packet_length):
  69. xfer_mock = transceiver._spi.xfer
  70. xfer_mock.return_value = [15, 15]
  71. transceiver.set_packet_length_bytes(packet_length)
  72. xfer_mock.assert_called_once_with([0x06 | 0x40, packet_length])
  73. @pytest.mark.parametrize("packet_length", [-21, 0, 256, 1024])
  74. def test_set_packet_length_bytes_fail(transceiver, packet_length):
  75. with pytest.raises(Exception):
  76. transceiver.set_packet_length_bytes(packet_length)
  77. transceiver._spi.xfer.assert_not_called()
  78. @pytest.mark.parametrize(
  79. ("pktctrl0_before", "pktctrl0_after"),
  80. (
  81. # unchanged
  82. (0b00000000, 0b00000000),
  83. (0b00010000, 0b00010000),
  84. (0b00010001, 0b00010001),
  85. (0b01000000, 0b01000000),
  86. (0b01000010, 0b01000010),
  87. (0b01110000, 0b01110000),
  88. (0b01110010, 0b01110010),
  89. # disabled
  90. (0b00010100, 0b00010000),
  91. (0b01000100, 0b01000000),
  92. (0b01000110, 0b01000010),
  93. (0b01110110, 0b01110010),
  94. ),
  95. )
  96. def test_disable_checksum(transceiver, pktctrl0_before, pktctrl0_after):
  97. xfer_mock = transceiver._spi.xfer
  98. xfer_mock.return_value = [15, 15]
  99. with unittest.mock.patch.object(
  100. transceiver, "_read_single_byte", return_value=pktctrl0_before
  101. ):
  102. transceiver.disable_checksum()
  103. xfer_mock.assert_called_once_with([0x08 | 0x40, pktctrl0_after])
  104. @pytest.mark.parametrize(
  105. ("pktctrl0", "expected_mode"),
  106. (
  107. (0b00000000, cc1101.options.PacketLengthMode.FIXED),
  108. (0b00000001, cc1101.options.PacketLengthMode.VARIABLE),
  109. (0b01000100, cc1101.options.PacketLengthMode.FIXED),
  110. (0b01000101, cc1101.options.PacketLengthMode.VARIABLE),
  111. ),
  112. )
  113. def test_get_packet_length_mode(transceiver, pktctrl0, expected_mode):
  114. xfer_mock = transceiver._spi.xfer
  115. xfer_mock.return_value = [0, pktctrl0]
  116. assert transceiver.get_packet_length_mode() == expected_mode
  117. xfer_mock.assert_called_once_with([0x08 | 0x80, 0])
  118. @pytest.mark.parametrize(
  119. ("pktctrl0_before", "pktctrl0_after", "mode"),
  120. (
  121. (0b00000000, 0b00000000, cc1101.options.PacketLengthMode.FIXED),
  122. (0b00000001, 0b00000000, cc1101.options.PacketLengthMode.FIXED),
  123. (0b00000001, 0b00000001, cc1101.options.PacketLengthMode.VARIABLE),
  124. (0b00000010, 0b00000000, cc1101.options.PacketLengthMode.FIXED),
  125. (0b00000010, 0b00000001, cc1101.options.PacketLengthMode.VARIABLE),
  126. (0b01000100, 0b01000100, cc1101.options.PacketLengthMode.FIXED),
  127. (0b01000100, 0b01000101, cc1101.options.PacketLengthMode.VARIABLE),
  128. (0b01000101, 0b01000100, cc1101.options.PacketLengthMode.FIXED),
  129. (0b01000101, 0b01000101, cc1101.options.PacketLengthMode.VARIABLE),
  130. ),
  131. )
  132. def test_set_packet_length_mode(transceiver, pktctrl0_before, pktctrl0_after, mode):
  133. xfer_mock = transceiver._spi.xfer
  134. xfer_mock.return_value = [15, 15]
  135. with unittest.mock.patch.object(
  136. transceiver, "_read_single_byte", return_value=pktctrl0_before
  137. ):
  138. transceiver.set_packet_length_mode(mode)
  139. xfer_mock.assert_called_once_with([0x08 | 0x40, pktctrl0_after])