test_cc1101.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 pytest
  18. import cc1101
  19. # pylint: disable=protected-access
  20. _FREQUENCY_CONTROL_WORD_HERTZ_PARAMS = [
  21. ([0x10, 0xA7, 0x62], 433000000),
  22. ([0x10, 0xAB, 0x85], 433420000),
  23. ([0x10, 0xB1, 0x3B], 434000000),
  24. ([0x21, 0x62, 0x76], 868000000),
  25. ]
  26. @pytest.mark.parametrize(
  27. ("control_word", "hertz"), _FREQUENCY_CONTROL_WORD_HERTZ_PARAMS
  28. )
  29. def test__frequency_control_word_to_hertz(control_word, hertz):
  30. assert cc1101.CC1101._frequency_control_word_to_hertz(
  31. control_word
  32. ) == pytest.approx(hertz, abs=200)
  33. @pytest.mark.parametrize(
  34. ("control_word", "hertz"), _FREQUENCY_CONTROL_WORD_HERTZ_PARAMS
  35. )
  36. def test__hertz_to_frequency_control_word(control_word, hertz):
  37. assert cc1101.CC1101._hertz_to_frequency_control_word(hertz) == control_word
  38. _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS = [
  39. # > The default values give a data rate of 115.051 kBaud
  40. # > (closest setting to 115.2 kBaud), assuming a 26.0 MHz crystal.
  41. (34, 12, 115051),
  42. (34, 12 + 1, 115051 * 2),
  43. (34, 12 - 1, 115051 / 2),
  44. ]
  45. @pytest.mark.parametrize(
  46. ("mantissa", "exponent", "real"), _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS
  47. )
  48. def test__symbol_rate_floating_point_to_real(mantissa, exponent, real):
  49. assert cc1101.CC1101._symbol_rate_floating_point_to_real(
  50. mantissa=mantissa, exponent=exponent
  51. ) == pytest.approx(real, rel=1e-5)
  52. @pytest.mark.parametrize(
  53. ("mantissa", "exponent", "real"), _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS
  54. )
  55. def test__symbol_rate_real_to_floating_point(mantissa, exponent, real):
  56. assert cc1101.CC1101._symbol_rate_real_to_floating_point(real) == (
  57. mantissa,
  58. exponent,
  59. )
  60. def test_get_packet_length_bytes(transceiver):
  61. xfer_mock = transceiver._spi.xfer
  62. xfer_mock.return_value = [0, 8]
  63. assert transceiver.get_packet_length_bytes() == 8
  64. xfer_mock.assert_called_once_with([0x06 | 0x80, 0])
  65. @pytest.mark.parametrize("packet_length", [21])
  66. def test_set_packet_length_bytes(transceiver, packet_length):
  67. xfer_mock = transceiver._spi.xfer
  68. xfer_mock.return_value = [15, 15]
  69. transceiver.set_packet_length_bytes(packet_length)
  70. xfer_mock.assert_called_once_with([0x06 | 0x40, packet_length])