92 lines
2.3 KiB
Python
92 lines
2.3 KiB
Python
|
import subprocess
|
||
|
import numpy as np # type: ignore
|
||
|
import pytest # type: ignore
|
||
|
|
||
|
from ria_toolkit_oss.datatypes.recording import Recording
|
||
|
from ria_toolkit_oss.sdr.hackrf import HackRF
|
||
|
|
||
|
|
||
|
SAMPLE_RATE = int(1e6)
|
||
|
CENTER_FREQUENCY = int(3440e6)
|
||
|
CHANNEL = 0
|
||
|
ABS_GAIN = 10
|
||
|
REL_GAIN = -37
|
||
|
t = np.linspace(0, 1, int(1e6 * 1), endpoint=False)
|
||
|
angular_frequency = 2 * np.pi * 1
|
||
|
SINE_WAVE = 10 * np.exp(1j * angular_frequency * t)
|
||
|
|
||
|
|
||
|
def radio_connected() -> bool:
|
||
|
try:
|
||
|
# Example: check if a specific USB device is present
|
||
|
result = subprocess.run(
|
||
|
["lsusb"],
|
||
|
capture_output=True,
|
||
|
text=True,
|
||
|
check=True
|
||
|
)
|
||
|
return "hackrf" in result.stdout.lower()
|
||
|
except Exception:
|
||
|
return False
|
||
|
|
||
|
|
||
|
@pytest.mark.skipif(not radio_connected(), reason="Required radio not connected")
|
||
|
def test_hackrf_relative_mode():
|
||
|
try:
|
||
|
radio = HackRF()
|
||
|
radio.init_tx(
|
||
|
sample_rate=SAMPLE_RATE,
|
||
|
center_frequency=CENTER_FREQUENCY,
|
||
|
channel=CHANNEL,
|
||
|
gain=REL_GAIN,
|
||
|
gain_mode='relative'
|
||
|
)
|
||
|
assert int(radio.radio.txvga_gain) == ABS_GAIN
|
||
|
finally:
|
||
|
radio.close()
|
||
|
|
||
|
|
||
|
@pytest.mark.skipif(not radio_connected(), reason="Required radio not connected")
|
||
|
def test_hackrf_rx():
|
||
|
try:
|
||
|
rx_radio = HackRF()
|
||
|
try:
|
||
|
rx_radio.init_rx(
|
||
|
sample_rate=SAMPLE_RATE,
|
||
|
center_frequency=CENTER_FREQUENCY,
|
||
|
channel=CHANNEL,
|
||
|
gain=ABS_GAIN,
|
||
|
gain_mode='absolute'
|
||
|
)
|
||
|
except NotImplementedError:
|
||
|
assert True
|
||
|
finally:
|
||
|
rx_radio.close()
|
||
|
|
||
|
|
||
|
@pytest.mark.skipif(not radio_connected(), reason="Required radio not connected")
|
||
|
def test_hackrf_tx():
|
||
|
try:
|
||
|
tx_radio = HackRF()
|
||
|
tx_radio.init_tx(
|
||
|
sample_rate=SAMPLE_RATE,
|
||
|
center_frequency=CENTER_FREQUENCY,
|
||
|
channel=CHANNEL,
|
||
|
gain=ABS_GAIN,
|
||
|
)
|
||
|
|
||
|
max_val = np.max(np.abs(SINE_WAVE))
|
||
|
data = SINE_WAVE / (max_val * 1.01)
|
||
|
recording = Recording(
|
||
|
data=SINE_WAVE,
|
||
|
metadata={'data': 'sine_wave'}
|
||
|
)
|
||
|
|
||
|
tx_radio.tx_recording(
|
||
|
recording=recording,
|
||
|
num_samples=SAMPLE_RATE
|
||
|
)
|
||
|
assert True
|
||
|
finally:
|
||
|
tx_radio.close()
|