test_0x22_frend0.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. ("frend0", "setting_index"),
  22. [
  23. (0b01000, 0),
  24. (0b01001, 1),
  25. (0b01111, 7),
  26. (0b10000, 0),
  27. (0b10001, 1),
  28. (0b10111, 7),
  29. (0b10101, 5),
  30. ],
  31. )
  32. def test__get_power_amplifier_setting_index(transceiver, frend0, setting_index):
  33. transceiver._spi.xfer.return_value = [15, frend0]
  34. assert transceiver._get_power_amplifier_setting_index() == setting_index
  35. transceiver._spi.xfer.assert_called_once_with([0x22 | 0x80, 0])
  36. @pytest.mark.parametrize(
  37. ("frend0_before", "frend0_after", "setting_index"),
  38. [
  39. (0b01000, 0b01000, 0),
  40. (0b01000, 0b01001, 1),
  41. (0b01000, 0b01111, 7),
  42. (0b10000, 0b10000, 0),
  43. (0b10000, 0b10001, 1),
  44. (0b10000, 0b10111, 7),
  45. ],
  46. )
  47. def test__set_power_amplifier_setting_index(
  48. transceiver, frend0_before, frend0_after, setting_index
  49. ):
  50. transceiver._spi.xfer.return_value = [15, 15]
  51. with unittest.mock.patch.object(
  52. transceiver, "_read_single_byte", return_value=frend0_before
  53. ):
  54. transceiver._set_power_amplifier_setting_index(setting_index)
  55. transceiver._spi.xfer.assert_called_once_with([0x22 | 0x40, frend0_after])
  56. @pytest.mark.parametrize("setting_index", (-1, 8, 21))
  57. def test__set_power_amplifier_setting_index_invalid(transceiver, setting_index):
  58. with pytest.raises(Exception):
  59. transceiver._set_power_amplifier_setting_index(setting_index)
  60. transceiver._spi.xfer.assert_not_called()