test_0x12_mdmcfg2.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. from cc1101.options import SyncMode
  20. # pylint: disable=protected-access
  21. @pytest.mark.parametrize(
  22. ("mdmcfg2", "sync_mode"),
  23. [
  24. (0b00000000, SyncMode.NO_PREAMBLE_AND_SYNC_WORD),
  25. (0b00000001, SyncMode.TRANSMIT_16_MATCH_15_BITS),
  26. (0b00000010, SyncMode.TRANSMIT_16_MATCH_16_BITS),
  27. (0b00000011, SyncMode.TRANSMIT_32_MATCH_30_BITS),
  28. (0b00000110, SyncMode.TRANSMIT_16_MATCH_16_BITS),
  29. (0b00000111, SyncMode.TRANSMIT_32_MATCH_30_BITS),
  30. (0b00001100, SyncMode.NO_PREAMBLE_AND_SYNC_WORD),
  31. (0b01101011, SyncMode.TRANSMIT_32_MATCH_30_BITS),
  32. (0b01101111, SyncMode.TRANSMIT_32_MATCH_30_BITS),
  33. ],
  34. )
  35. def test_get_sync_mode(transceiver, mdmcfg2, sync_mode):
  36. transceiver._spi.xfer.return_value = [15, mdmcfg2]
  37. assert transceiver.get_sync_mode() == sync_mode
  38. transceiver._spi.xfer.assert_called_once_with([0x12 | 0x80, 0])
  39. @pytest.mark.parametrize(
  40. ("mdmcfg2_before", "mdmcfg2_after", "sync_mode", "threshold_enabled"),
  41. [
  42. (0b00000010, 0b00000000, SyncMode.NO_PREAMBLE_AND_SYNC_WORD, None),
  43. (0b00000010, 0b00000001, SyncMode.TRANSMIT_16_MATCH_15_BITS, None),
  44. (0b00000010, 0b00000010, SyncMode.TRANSMIT_16_MATCH_16_BITS, None),
  45. (0b00000010, 0b00000011, SyncMode.TRANSMIT_32_MATCH_30_BITS, None),
  46. (0b01101110, 0b01101111, SyncMode.TRANSMIT_32_MATCH_30_BITS, None),
  47. (0b00000010, 0b00000110, SyncMode.TRANSMIT_16_MATCH_16_BITS, True),
  48. (0b00000010, 0b00000111, SyncMode.TRANSMIT_32_MATCH_30_BITS, True),
  49. (0b01101110, 0b01101111, SyncMode.TRANSMIT_32_MATCH_30_BITS, True),
  50. (0b00000010, 0b00000010, SyncMode.TRANSMIT_16_MATCH_16_BITS, False),
  51. (0b00000010, 0b00000011, SyncMode.TRANSMIT_32_MATCH_30_BITS, False),
  52. (0b01101110, 0b01101011, SyncMode.TRANSMIT_32_MATCH_30_BITS, False),
  53. ],
  54. )
  55. def test_set_sync_mode(
  56. transceiver, mdmcfg2_before, mdmcfg2_after, sync_mode, threshold_enabled
  57. ):
  58. transceiver._spi.xfer.return_value = [15, 15]
  59. with unittest.mock.patch.object(
  60. transceiver, "_read_single_byte", return_value=mdmcfg2_before
  61. ):
  62. transceiver.set_sync_mode(
  63. sync_mode, _carrier_sense_threshold_enabled=threshold_enabled
  64. )
  65. transceiver._spi.xfer.assert_called_once_with([0x12 | 0x40, mdmcfg2_after])