123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- import unittest.mock
- import warnings
- import pytest
- import cc1101
- _FREQUENCY_CONTROL_WORD_HERTZ_PARAMS = [
- ([0x10, 0xA7, 0x62], 433000000),
- ([0x10, 0xAB, 0x85], 433420000),
- ([0x10, 0xB1, 0x3B], 434000000),
- ([0x21, 0x62, 0x76], 868000000),
- ]
- @pytest.mark.parametrize(
- ("control_word", "hertz"), _FREQUENCY_CONTROL_WORD_HERTZ_PARAMS
- )
- def test__frequency_control_word_to_hertz(control_word, hertz):
- assert cc1101.CC1101._frequency_control_word_to_hertz(
- control_word
- ) == pytest.approx(hertz, abs=200)
- @pytest.mark.parametrize(
- ("control_word", "hertz"), _FREQUENCY_CONTROL_WORD_HERTZ_PARAMS
- )
- def test__hertz_to_frequency_control_word(control_word, hertz):
- assert cc1101.CC1101._hertz_to_frequency_control_word(hertz) == control_word
- _FILTER_BANDWIDTH_MANTISSA_EXPONENT_REAL_PARAMS = [
-
-
- (0, 2, 203e3),
-
- (0, 0, 812e3),
- (0, 1, 406e3),
- (0, 2, 203e3),
- (1, 0, 650e3),
- (1, 1, 325e3),
- (3, 0, 464e3),
- (3, 1, 232e3),
- (3, 2, 116e3),
- (3, 3, 58e3),
- ]
- @pytest.mark.parametrize(
- ("mantissa", "exponent", "real"), _FILTER_BANDWIDTH_MANTISSA_EXPONENT_REAL_PARAMS
- )
- def test__filter_bandwidth_floating_point_to_real(mantissa, exponent, real):
- assert cc1101.CC1101._filter_bandwidth_floating_point_to_real(
- mantissa=mantissa, exponent=exponent
- ) == pytest.approx(real, rel=1e-3)
- _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS = [
-
-
- (34, 12, 115051),
- (34, 12 + 1, 115051 * 2),
- (34, 12 - 1, 115051 / 2),
- ]
- @pytest.mark.parametrize(
- ("mantissa", "exponent", "real"), _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS
- )
- def test__symbol_rate_floating_point_to_real(mantissa, exponent, real):
- assert cc1101.CC1101._symbol_rate_floating_point_to_real(
- mantissa=mantissa, exponent=exponent
- ) == pytest.approx(real, rel=1e-5)
- @pytest.mark.parametrize(
- ("mantissa", "exponent", "real"), _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS
- )
- def test__symbol_rate_real_to_floating_point(mantissa, exponent, real):
- assert cc1101.CC1101._symbol_rate_real_to_floating_point(real) == (
- mantissa,
- exponent,
- )
- @pytest.mark.parametrize(
- ("freq_hz", "warn"),
- (
- (100e6, True),
- (281.6e6, True),
- (281.65e6, False),
- (281.7e6, False),
- (433.92e6, False),
- ),
- )
- def test_set_base_frequency_hertz_low_warning(transceiver, freq_hz, warn):
- with unittest.mock.patch.object(
- transceiver, "_set_base_frequency_control_word"
- ) as set_control_word_mock:
- with warnings.catch_warnings(record=True) as caught_warnings:
- transceiver.set_base_frequency_hertz(freq_hz)
- assert set_control_word_mock.call_count == 1
- if warn:
- assert len(caught_warnings) == 1
- assert (
- str(caught_warnings[0].message)
- == "CC1101 is unable to transmit at frequencies below 281.7 MHz"
- )
- else:
- assert not caught_warnings
|