test_0x08_pktctrl0.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 PacketLengthMode
  20. # pylint: disable=protected-access
  21. @pytest.mark.parametrize(
  22. ("pktctrl0_before", "pktctrl0_after"),
  23. (
  24. # unchanged
  25. (0b00000000, 0b00000000),
  26. (0b00010000, 0b00010000),
  27. (0b00010001, 0b00010001),
  28. (0b01000000, 0b01000000),
  29. (0b01000010, 0b01000010),
  30. (0b01110000, 0b01110000),
  31. (0b01110010, 0b01110010),
  32. # disabled
  33. (0b00010100, 0b00010000),
  34. (0b01000100, 0b01000000),
  35. (0b01000110, 0b01000010),
  36. (0b01110110, 0b01110010),
  37. ),
  38. )
  39. def test_disable_checksum(transceiver, pktctrl0_before, pktctrl0_after):
  40. xfer_mock = transceiver._spi.xfer
  41. xfer_mock.return_value = [15, 15]
  42. with unittest.mock.patch.object(
  43. transceiver, "_read_single_byte", return_value=pktctrl0_before
  44. ):
  45. transceiver.disable_checksum()
  46. xfer_mock.assert_called_once_with([0x08 | 0x40, pktctrl0_after])
  47. @pytest.mark.parametrize(
  48. ("pktctrl0", "expected_mode"),
  49. (
  50. (0b00000000, PacketLengthMode.FIXED),
  51. (0b00000001, PacketLengthMode.VARIABLE),
  52. (0b01000100, PacketLengthMode.FIXED),
  53. (0b01000101, PacketLengthMode.VARIABLE),
  54. ),
  55. )
  56. def test_get_packet_length_mode(transceiver, pktctrl0, expected_mode):
  57. xfer_mock = transceiver._spi.xfer
  58. xfer_mock.return_value = [0, pktctrl0]
  59. assert transceiver.get_packet_length_mode() == expected_mode
  60. xfer_mock.assert_called_once_with([0x08 | 0x80, 0])
  61. @pytest.mark.parametrize(
  62. ("pktctrl0_before", "pktctrl0_after", "mode"),
  63. (
  64. (0b00000000, 0b00000000, PacketLengthMode.FIXED),
  65. (0b00000001, 0b00000000, PacketLengthMode.FIXED),
  66. (0b00000001, 0b00000001, PacketLengthMode.VARIABLE),
  67. (0b00000010, 0b00000000, PacketLengthMode.FIXED),
  68. (0b00000010, 0b00000001, PacketLengthMode.VARIABLE),
  69. (0b01000100, 0b01000100, PacketLengthMode.FIXED),
  70. (0b01000100, 0b01000101, PacketLengthMode.VARIABLE),
  71. (0b01000101, 0b01000100, PacketLengthMode.FIXED),
  72. (0b01000101, 0b01000101, PacketLengthMode.VARIABLE),
  73. ),
  74. )
  75. def test_set_packet_length_mode(transceiver, pktctrl0_before, pktctrl0_after, mode):
  76. xfer_mock = transceiver._spi.xfer
  77. xfer_mock.return_value = [15, 15]
  78. with unittest.mock.patch.object(
  79. transceiver, "_read_single_byte", return_value=pktctrl0_before
  80. ):
  81. transceiver.set_packet_length_mode(mode)
  82. xfer_mock.assert_called_once_with([0x08 | 0x40, pktctrl0_after])