Skip to content

Drivers of your own

A new radio, or a stream from some other source, needs no change to pysdrlib. Register a Device subclass in your program, or ship it as a package that declares an entry point. Every program that has the package installed then sees it: find(), open(), pysdrlib server.

In a script

@pysdrlib.register("noise_source")
class NoiseSource(pysdrlib.Device):
    """White noise at any frequency: the smallest receive-only Device"""
    NAME = "Noise source"
    CAPS = Capabilities(driver="", freq=[Range(0, 6e9)], default_freq=100e6, rates=[Range(1e3, 10e6)],
                        default_rate=1e6, gains={"rx": {"LEVEL": GainStage(1, 0, 40, 20)}, "tx": {}})

    def _open(self, level=0.1):          # pysdrlib.open("noise_source", level=...) arguments arrive here
        self.level = level
        self._rng = np.random.default_rng()

    def _close(self):
        pass

    def _set_rx_freq(self, freq, channel=0):
        pass                             # a radio would tune here; these hooks get values already checked against CAPS
    def _set_rx_sample_rate(self, Fs, channel=0):
        pass
    def _set_rx_level_gain(self, gain, channel=0):   # one per stage: set_rx_level_gain is made from CAPS
        self.gain = gain

    def _start_rx(self):
        pass
    def _stop_rx(self):
        pass
    def _get_samples(self):              # whatever has "arrived": read() builds exact counts on it
        n = 4096
        x = self._rng.standard_normal(n) + 1j * self._rng.standard_normal(n)
        return (self.level * 10 ** ((self.gain - 40) / 20) * x).astype(np.complex64)
print(testing.check_driver(NoiseSource))      # what's missing or wrong, before a radio is involved
sdr = pysdrlib.open("noise_source", level=0.5)
sdr.initialize(cf=433.92e6, Fs=1e6)
sdr.start_rx()
x = sdr.read(10_000)
sdr.close()

A Device in Python provides the samples itself (_get_samples), and pysdrlib builds read, stream, gaps, gain scaling and range checks on it. A radio with a C/C++ library is a NativeDevice over a compiled extension, which streams without the GIL: see Adding a driver.

As a package

python -m pysdrlib.scaffold my_radio --out-of-tree      # writes pysdrlib-my-radio/: Device, pyproject.toml, a test

The package declares an entry point:

[project.entry-points."pysdrlib.drivers"]
my_radio = "my_package:MyRadio"

Once it is installed (pip install -e .), pysdrlib.open("my_radio", host="10.0.0.5") works in any program; the keywords go to the class's _open. The entry point is imported only when the driver is first used, and one that fails to import is skipped by find. PYSDRLIB_NO_PLUGINS=1 ignores entry points.

Network radios

pysdrlib.find(addresses=["192.168.10.2"]) also looks at network addresses, for the drivers whose find takes addresses. Plain find() probes no network.

Checking it

pysdrlib.testing.check_driver(MyRadio) and check_package("my_radio") list what is missing or wrong before a radio is involved: see pysdrlib.testing.