test_0x13_mdmcfg1.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. ("mdmcfg1", "length"),
  22. [
  23. (0b00000010, 2),
  24. (0b00010010, 3),
  25. (0b00100010, 4),
  26. (0b00110010, 6),
  27. (0b01000010, 8),
  28. (0b01010010, 12),
  29. (0b01100010, 16),
  30. (0b01110010, 24),
  31. ],
  32. )
  33. def test_get_preamble_length_bytes(transceiver, mdmcfg1, length):
  34. transceiver._spi.xfer.return_value = [0, mdmcfg1]
  35. assert transceiver.get_preamble_length_bytes() == length
  36. transceiver._spi.xfer.assert_called_once_with([0x13 | 0x80, 0])
  37. @pytest.mark.parametrize(
  38. ("mdmcfg1_before", "mdmcfg1_after", "length"),
  39. [
  40. (0b00000010, 0b00000010, 2),
  41. (0b00000010, 0b00010010, 3),
  42. (0b00000010, 0b00100010, 4),
  43. (0b00000010, 0b00110010, 6),
  44. (0b00000010, 0b01000010, 8),
  45. (0b00000010, 0b01010010, 12),
  46. (0b00000010, 0b01100010, 16),
  47. (0b00000010, 0b01110010, 24),
  48. (0b01010010, 0b01100010, 16),
  49. (0b01110010, 0b00000010, 2),
  50. (0b01110010, 0b01000010, 8),
  51. (0b11011010, 0b11101010, 16),
  52. (0b11110111, 0b11000111, 8),
  53. (0b11111110, 0b10001110, 2),
  54. ],
  55. )
  56. def test_set_preamble_length_bytes(transceiver, mdmcfg1_before, mdmcfg1_after, length):
  57. transceiver._spi.xfer.return_value = [15, 15]
  58. with unittest.mock.patch.object(
  59. transceiver, "_read_single_byte", return_value=mdmcfg1_before
  60. ):
  61. transceiver.set_preamble_length_bytes(length)
  62. transceiver._spi.xfer.assert_called_once_with([0x13 | 0x40, mdmcfg1_after])
  63. @pytest.mark.parametrize(
  64. "length", [-21, 0, 1, 5, 7, 9, 10, 11, 13, 14, 15, 17, 20, 23, 25, 32, 42]
  65. )
  66. def test_set_preamble_length_bytes_invalid(transceiver, length):
  67. with pytest.raises(ValueError, match=r"\bpreamble length\b"):
  68. transceiver.set_preamble_length_bytes(length)