test_0x12_mdmcfg2.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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", "sync_mode"),
  39. [
  40. (0b00000000, SyncMode.NO_PREAMBLE_AND_SYNC_WORD),
  41. (0b00000001, SyncMode.TRANSMIT_16_MATCH_15_BITS),
  42. (0b00000010, SyncMode.TRANSMIT_16_MATCH_16_BITS),
  43. (0b00000011, SyncMode.TRANSMIT_32_MATCH_30_BITS),
  44. (0b00000110, SyncMode.TRANSMIT_16_MATCH_16_BITS),
  45. (0b00000111, SyncMode.TRANSMIT_32_MATCH_30_BITS),
  46. (0b00001100, SyncMode.NO_PREAMBLE_AND_SYNC_WORD),
  47. (0b01101011, SyncMode.TRANSMIT_32_MATCH_30_BITS),
  48. (0b01101111, SyncMode.TRANSMIT_32_MATCH_30_BITS),
  49. ],
  50. )
  51. def test_get_sync_mode(transceiver, mdmcfg2, sync_mode):
  52. transceiver._spi.xfer.return_value = [15, mdmcfg2]
  53. assert transceiver.get_sync_mode() == sync_mode
  54. transceiver._spi.xfer.assert_called_once_with([0x12 | 0x80, 0])
  55. @pytest.mark.parametrize(
  56. ("mdmcfg2_before", "mdmcfg2_after", "sync_mode", "threshold_enabled"),
  57. [
  58. (0b00000010, 0b00000000, SyncMode.NO_PREAMBLE_AND_SYNC_WORD, None),
  59. (0b00000010, 0b00000001, SyncMode.TRANSMIT_16_MATCH_15_BITS, None),
  60. (0b00000010, 0b00000010, SyncMode.TRANSMIT_16_MATCH_16_BITS, None),
  61. (0b00000010, 0b00000011, SyncMode.TRANSMIT_32_MATCH_30_BITS, None),
  62. (0b01101110, 0b01101111, SyncMode.TRANSMIT_32_MATCH_30_BITS, None),
  63. (0b00000010, 0b00000110, SyncMode.TRANSMIT_16_MATCH_16_BITS, True),
  64. (0b00000010, 0b00000111, SyncMode.TRANSMIT_32_MATCH_30_BITS, True),
  65. (0b01101110, 0b01101111, SyncMode.TRANSMIT_32_MATCH_30_BITS, True),
  66. (0b00000010, 0b00000010, SyncMode.TRANSMIT_16_MATCH_16_BITS, False),
  67. (0b00000010, 0b00000011, SyncMode.TRANSMIT_32_MATCH_30_BITS, False),
  68. (0b01101110, 0b01101011, SyncMode.TRANSMIT_32_MATCH_30_BITS, False),
  69. ],
  70. )
  71. def test_set_sync_mode(
  72. transceiver, mdmcfg2_before, mdmcfg2_after, sync_mode, threshold_enabled
  73. ):
  74. transceiver._spi.xfer.return_value = [15, 15]
  75. with unittest.mock.patch.object(
  76. transceiver, "_read_single_byte", return_value=mdmcfg2_before
  77. ):
  78. transceiver.set_sync_mode(
  79. sync_mode, _carrier_sense_threshold_enabled=threshold_enabled
  80. )
  81. transceiver._spi.xfer.assert_called_once_with([0x12 | 0x40, mdmcfg2_after])