test_spi.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 cc1101
  19. import cc1101.addresses
  20. import cc1101.options
  21. # pylint: disable=protected-access
  22. def test__read_status_register(transceiver):
  23. transceiver._spi.xfer.return_value = [0, 20]
  24. transceiver._read_status_register(cc1101.addresses.StatusRegisterAddress.VERSION)
  25. transceiver._spi.xfer.assert_called_once_with([0x31 | 0xC0, 0])
  26. def test__command_strobe(transceiver):
  27. transceiver._spi.xfer.return_value = [15]
  28. transceiver._command_strobe(cc1101.addresses.StrobeAddress.STX)
  29. transceiver._spi.xfer.assert_called_once_with([0x35 | 0x00])
  30. def test__reset(transceiver):
  31. transceiver._spi.xfer.return_value = [15]
  32. transceiver._reset()
  33. transceiver._spi.xfer.assert_called_once_with([0x30 | 0x00])
  34. def test___enter__(transceiver):
  35. with unittest.mock.patch.object(
  36. transceiver, "_read_status_register"
  37. ) as read_status_register_mock, unittest.mock.patch.object(
  38. transceiver, "_reset"
  39. ) as reset_mock, unittest.mock.patch.object(
  40. transceiver, "_set_modulation_format"
  41. ) as set_modulation_format_mock, unittest.mock.patch.object(
  42. transceiver, "_set_power_amplifier_setting_index"
  43. ) as set_pa_setting_mock, unittest.mock.patch.object(
  44. transceiver, "_disable_data_whitening"
  45. ) as disable_whitening_mock, unittest.mock.patch.object(
  46. transceiver, "_write_burst"
  47. ) as write_burst_mock, unittest.mock.patch.object(
  48. transceiver,
  49. "get_main_radio_control_state_machine_state",
  50. return_value=cc1101.MainRadioControlStateMachineState.IDLE,
  51. ):
  52. read_status_register_mock.side_effect = lambda r: {
  53. cc1101.addresses.StatusRegisterAddress.PARTNUM: 0,
  54. cc1101.addresses.StatusRegisterAddress.VERSION: 0x14,
  55. }[r]
  56. with transceiver as transceiver_context:
  57. assert transceiver == transceiver_context
  58. transceiver._spi.open.assert_called_once_with(0, 0)
  59. assert transceiver._spi.max_speed_hz == 55700
  60. reset_mock.assert_called_once_with()
  61. set_modulation_format_mock.assert_called_once_with(
  62. cc1101.options.ModulationFormat.ASK_OOK
  63. )
  64. set_pa_setting_mock.assert_called_once_with(1)
  65. disable_whitening_mock.assert_called_once_with()
  66. write_burst_mock.assert_called_once_with(0x18, [0b010100])