1
0

test_0x12_mdmcfg2.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 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", "sync_mode"),
  60. [
  61. (0b00000000, SyncMode.NO_PREAMBLE_AND_SYNC_WORD),
  62. (0b00000001, SyncMode.TRANSMIT_16_MATCH_15_BITS),
  63. (0b00000010, SyncMode.TRANSMIT_16_MATCH_16_BITS),
  64. (0b00000011, SyncMode.TRANSMIT_32_MATCH_30_BITS),
  65. (0b00000110, SyncMode.TRANSMIT_16_MATCH_16_BITS),
  66. (0b00000111, SyncMode.TRANSMIT_32_MATCH_30_BITS),
  67. (0b00001100, SyncMode.NO_PREAMBLE_AND_SYNC_WORD),
  68. (0b01101011, SyncMode.TRANSMIT_32_MATCH_30_BITS),
  69. (0b01101111, SyncMode.TRANSMIT_32_MATCH_30_BITS),
  70. ],
  71. )
  72. def test_get_sync_mode(transceiver, mdmcfg2, sync_mode):
  73. transceiver._spi.xfer.return_value = [15, mdmcfg2]
  74. assert transceiver.get_sync_mode() == sync_mode
  75. transceiver._spi.xfer.assert_called_once_with([0x12 | 0x80, 0])
  76. @pytest.mark.parametrize(
  77. ("mdmcfg2_before", "mdmcfg2_after", "sync_mode", "threshold_enabled"),
  78. [
  79. (0b00000010, 0b00000000, SyncMode.NO_PREAMBLE_AND_SYNC_WORD, None),
  80. (0b00000010, 0b00000001, SyncMode.TRANSMIT_16_MATCH_15_BITS, None),
  81. (0b00000010, 0b00000010, SyncMode.TRANSMIT_16_MATCH_16_BITS, None),
  82. (0b00000010, 0b00000011, SyncMode.TRANSMIT_32_MATCH_30_BITS, None),
  83. (0b01101110, 0b01101111, SyncMode.TRANSMIT_32_MATCH_30_BITS, None),
  84. (0b00000010, 0b00000110, SyncMode.TRANSMIT_16_MATCH_16_BITS, True),
  85. (0b00000010, 0b00000111, SyncMode.TRANSMIT_32_MATCH_30_BITS, True),
  86. (0b01101110, 0b01101111, SyncMode.TRANSMIT_32_MATCH_30_BITS, True),
  87. (0b00000010, 0b00000010, SyncMode.TRANSMIT_16_MATCH_16_BITS, False),
  88. (0b00000010, 0b00000011, SyncMode.TRANSMIT_32_MATCH_30_BITS, False),
  89. (0b01101110, 0b01101011, SyncMode.TRANSMIT_32_MATCH_30_BITS, False),
  90. ],
  91. )
  92. def test_set_sync_mode(
  93. transceiver, mdmcfg2_before, mdmcfg2_after, sync_mode, threshold_enabled
  94. ):
  95. transceiver._spi.xfer.return_value = [15, 15]
  96. with unittest.mock.patch.object(
  97. transceiver, "_read_single_byte", return_value=mdmcfg2_before
  98. ):
  99. transceiver.set_sync_mode(
  100. sync_mode, _carrier_sense_threshold_enabled=threshold_enabled
  101. )
  102. transceiver._spi.xfer.assert_called_once_with([0x12 | 0x40, mdmcfg2_after])