1
0

test_0x10_mdmcfg4.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 pytest
  19. # pylint: disable=protected-access
  20. @pytest.mark.parametrize(
  21. ("mdmcfg4", "real"),
  22. [
  23. (0b10001100, 203e3),
  24. (0b10001010, 203e3),
  25. (0b10001110, 203e3),
  26. (0b11111100, 58e3),
  27. (0b01011100, 325e3),
  28. ],
  29. )
  30. def test__get_filter_bandwidth_hertz(transceiver, mdmcfg4, real):
  31. transceiver._spi.xfer.return_value = [15, mdmcfg4]
  32. assert transceiver._get_filter_bandwidth_hertz() == pytest.approx(real, rel=1e-3)
  33. transceiver._spi.xfer.assert_called_once_with([0x10 | 0x80, 0])
  34. @pytest.mark.parametrize(
  35. ("mdmcfg4_before", "mdmcfg4_after", "exponent", "mantissa"),
  36. [
  37. (0b00001010, 0b10111010, 0b10, 0b11),
  38. (0b00001100, 0b01001100, 0b01, 0b00),
  39. (0b00001100, 0b10111100, 0b10, 0b11),
  40. (0b00001100, 0b11011100, 0b11, 0b01),
  41. (0b01011100, 0b11011100, 0b11, 0b01),
  42. (0b11111100, 0b11011100, 0b11, 0b01),
  43. ],
  44. )
  45. def test__set_filter_bandwidth(
  46. transceiver, mdmcfg4_before, mdmcfg4_after, exponent, mantissa
  47. ):
  48. transceiver._spi.xfer.return_value = [15, 15]
  49. with unittest.mock.patch.object(
  50. transceiver, "_read_single_byte", return_value=mdmcfg4_before
  51. ):
  52. transceiver._set_filter_bandwidth(mantissa=mantissa, exponent=exponent)
  53. transceiver._spi.xfer.assert_called_once_with([0x10 | 0x40, mdmcfg4_after])
  54. @pytest.mark.parametrize(
  55. ("mdmcfg4", "exponent"),
  56. (
  57. (0b10001100, 12),
  58. (0b10001001, 9),
  59. (0b10001111, 15),
  60. (0b10000000, 0),
  61. (0b10111100, 12),
  62. (0b10111111, 15),
  63. (0b10110000, 0),
  64. ),
  65. )
  66. def test__get_symbol_rate_exponent(transceiver, mdmcfg4, exponent):
  67. transceiver._spi.xfer.return_value = [15, mdmcfg4]
  68. assert transceiver._get_symbol_rate_exponent() == exponent
  69. transceiver._spi.xfer.assert_called_once_with([0x10 | 0x80, 0])
  70. @pytest.mark.parametrize(
  71. ("mdmcfg4_before", "mdmcfg4_after", "exponent"),
  72. (
  73. (0b10001100, 0b10001100, 12),
  74. (0b10001100, 0b10001111, 15),
  75. (0b10001100, 0b10000000, 0),
  76. (0b10001100, 0b10001010, 0b1010),
  77. (0b01111100, 0b01111100, 12),
  78. (0b01111100, 0b01111111, 15),
  79. (0b01111100, 0b01110000, 0),
  80. (0b01110001, 0b01111100, 12),
  81. ),
  82. )
  83. def test__set_symbol_rate_exponent(
  84. transceiver, mdmcfg4_before, mdmcfg4_after, exponent
  85. ):
  86. transceiver._spi.xfer.return_value = [0x0F, 0x0F]
  87. with unittest.mock.patch.object(
  88. transceiver, "_read_single_byte", return_value=mdmcfg4_before
  89. ):
  90. transceiver._set_symbol_rate_exponent(exponent)
  91. transceiver._spi.xfer.assert_called_once_with([0x10 | 0x40, mdmcfg4_after])