test_0x12_mdmcfg2.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. from cc1101.options import ModulationFormat, SyncMode
  20. # pylint: disable=protected-access
  21. @pytest.mark.parametrize(
  22. ("mdmcfg2", "mod_format"),
  23. (
  24. (0b00000010, ModulationFormat.FSK2),
  25. (0b10001110, ModulationFormat.FSK2),
  26. (0b10011110, ModulationFormat.GFSK),
  27. (0b10111110, ModulationFormat.ASK_OOK),
  28. (0b11001110, ModulationFormat.FSK4),
  29. (0b11001001, ModulationFormat.FSK4),
  30. (0b11111001, ModulationFormat.MSK),
  31. ),
  32. )
  33. def test_get_modulation_format(transceiver, mdmcfg2, mod_format):
  34. transceiver._spi.xfer.return_value = [15, mdmcfg2]
  35. assert transceiver.get_modulation_format() == mod_format
  36. transceiver._spi.xfer.assert_called_once_with([0x12 | 0x80, 0])
  37. @pytest.mark.parametrize(
  38. ("mdmcfg2_before", "mdmcfg2_after", "mod_format"),
  39. [
  40. (0b00000010, 0b00000010, ModulationFormat.FSK2),
  41. (0b11111111, 0b10001111, ModulationFormat.FSK2),
  42. (0b00000010, 0b00010010, ModulationFormat.GFSK),
  43. (0b00000010, 0b00110010, ModulationFormat.ASK_OOK),
  44. (0b00000010, 0b01000010, ModulationFormat.FSK4),
  45. (0b01110101, 0b01000101, ModulationFormat.FSK4),
  46. (0b11111111, 0b11001111, ModulationFormat.FSK4),
  47. (0b00000010, 0b01110010, ModulationFormat.MSK),
  48. (0b11111111, 0b11111111, ModulationFormat.MSK),
  49. ],
  50. )
  51. def test__set_modulation_format(transceiver, mdmcfg2_before, mdmcfg2_after, mod_format):
  52. transceiver._spi.xfer.return_value = [15, 15]
  53. with unittest.mock.patch.object(
  54. transceiver, "_read_single_byte", return_value=mdmcfg2_before
  55. ):
  56. transceiver._set_modulation_format(mod_format)
  57. transceiver._spi.xfer.assert_called_once_with([0x12 | 0x40, mdmcfg2_after])
  58. @pytest.mark.parametrize(
  59. ("mdmcfg2_before", "mdmcfg2_after"),
  60. [
  61. (0b00000010, 0b00001010),
  62. (0b00001010, 0b00001010),
  63. (0b11110111, 0b11111111),
  64. (0b11111111, 0b11111111),
  65. ],
  66. )
  67. def test_enable_manchester_code(transceiver, mdmcfg2_before, mdmcfg2_after):
  68. transceiver._spi.xfer.return_value = [15, 15]
  69. with unittest.mock.patch.object(
  70. transceiver, "_read_single_byte", return_value=mdmcfg2_before
  71. ):
  72. transceiver.enable_manchester_code()
  73. transceiver._spi.xfer.assert_called_once_with([0x12 | 0x40, mdmcfg2_after])
  74. @pytest.mark.parametrize(
  75. ("mdmcfg2", "sync_mode"),
  76. [
  77. (0b00000000, SyncMode.NO_PREAMBLE_AND_SYNC_WORD),
  78. (0b00000001, SyncMode.TRANSMIT_16_MATCH_15_BITS),
  79. (0b00000010, SyncMode.TRANSMIT_16_MATCH_16_BITS),
  80. (0b00000011, SyncMode.TRANSMIT_32_MATCH_30_BITS),
  81. (0b00000110, SyncMode.TRANSMIT_16_MATCH_16_BITS),
  82. (0b00000111, SyncMode.TRANSMIT_32_MATCH_30_BITS),
  83. (0b00001100, SyncMode.NO_PREAMBLE_AND_SYNC_WORD),
  84. (0b01101011, SyncMode.TRANSMIT_32_MATCH_30_BITS),
  85. (0b01101111, SyncMode.TRANSMIT_32_MATCH_30_BITS),
  86. ],
  87. )
  88. def test_get_sync_mode(transceiver, mdmcfg2, sync_mode):
  89. transceiver._spi.xfer.return_value = [15, mdmcfg2]
  90. assert transceiver.get_sync_mode() == sync_mode
  91. transceiver._spi.xfer.assert_called_once_with([0x12 | 0x80, 0])
  92. @pytest.mark.parametrize(
  93. ("mdmcfg2_before", "mdmcfg2_after", "sync_mode", "threshold_enabled"),
  94. [
  95. (0b00000010, 0b00000000, SyncMode.NO_PREAMBLE_AND_SYNC_WORD, None),
  96. (0b00000010, 0b00000001, SyncMode.TRANSMIT_16_MATCH_15_BITS, None),
  97. (0b00000010, 0b00000010, SyncMode.TRANSMIT_16_MATCH_16_BITS, None),
  98. (0b00000010, 0b00000011, SyncMode.TRANSMIT_32_MATCH_30_BITS, None),
  99. (0b01101110, 0b01101111, SyncMode.TRANSMIT_32_MATCH_30_BITS, None),
  100. (0b00000010, 0b00000110, SyncMode.TRANSMIT_16_MATCH_16_BITS, True),
  101. (0b00000010, 0b00000111, SyncMode.TRANSMIT_32_MATCH_30_BITS, True),
  102. (0b01101110, 0b01101111, SyncMode.TRANSMIT_32_MATCH_30_BITS, True),
  103. (0b00000010, 0b00000010, SyncMode.TRANSMIT_16_MATCH_16_BITS, False),
  104. (0b00000010, 0b00000011, SyncMode.TRANSMIT_32_MATCH_30_BITS, False),
  105. (0b01101110, 0b01101011, SyncMode.TRANSMIT_32_MATCH_30_BITS, False),
  106. ],
  107. )
  108. def test_set_sync_mode(
  109. transceiver, mdmcfg2_before, mdmcfg2_after, sync_mode, threshold_enabled
  110. ):
  111. transceiver._spi.xfer.return_value = [15, 15]
  112. with unittest.mock.patch.object(
  113. transceiver, "_read_single_byte", return_value=mdmcfg2_before
  114. ):
  115. transceiver.set_sync_mode(
  116. sync_mode, _carrier_sense_threshold_enabled=threshold_enabled
  117. )
  118. transceiver._spi.xfer.assert_called_once_with([0x12 | 0x40, mdmcfg2_after])