aboutsummaryrefslogtreecommitdiffstats
path: root/tools/gr-usrptest/apps/uhd_rf_test/uhd_source_gen.py
blob: ad10619f79b3576e58a3a7445a9907fe9d4270ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
Source Generator Plugins for RF Test Scripts
"""

import importlib
from inspect import isclass
from builtins import input, object
from six import itervalues


###############################################################################
# Source Generator Plugins
###############################################################################
class SourceGenerator(object):
    """
    Parent class for source generators.
    """
    def __init__(self, log, **_):
        self.log = log

    def tune(self, freq, power_lvl_dbm):
        """
        Set the TX frequency. This function can block until the Tx frequency is
        stable, so if LOs need to settle or whatnot, it's OK for this function
        to take a while to return. After it's returned, the assumption is that
        we can start RXing.
        """
        raise NotImplementedError()

    def tear_down(self):
        """Do all necessary clean-up for the source generator"""
        pass


class ManualSourceGenerator(SourceGenerator):
    """
    Not really a source generator, but a command-line interaction with the user
    to manually set the TX source.
    """
    def tune(self, freq, power_lvl_dbm):
        """
        Ask the user to set the TX frequency. Once the user continues, the
        assumption is that we can start RXing.
        """
        input("Please tune the signal generator to {:.3f} MHz and {:.1f} dBm, "
              "then press Enter".format(freq / 1e6, power_lvl_dbm))


DEFAULT_SOURCE_GENERATORS = {
    'default': ManualSourceGenerator,
}
DEFAULT_SOURCE_GENERATOR = 'default'


def get_source_generator(log=None, src_gen_id=None, **kwargs):
    """
    Factory function to get a source generator
    """
    src_gen_id = src_gen_id or DEFAULT_SOURCE_GENERATOR
    if src_gen_id in DEFAULT_SOURCE_GENERATORS:
        return DEFAULT_SOURCE_GENERATORS.get(src_gen_id)(log, **kwargs)
    try:
        module = importlib.import_module(src_gen_id)
    except ImportError:
        raise RuntimeError("Could not find source generator plugin `{}'!".format(
            src_gen_id))
    src_gens = [
        x for x in itervalues(module.__dict__)
        if isclass(x) and issubclass(x, SourceGenerator) and x != SourceGenerator
    ]
    if not src_gens:
        raise RuntimeError(
            "Could not find any source generator classes in module `{}'!"
            .format(src_gen_id))
    if len(src_gens) > 1:
        raise RuntimeError(
            "Ambiguous source generator plugin `{}'! Too many generator classes: {}"
            .format(src_gen_id, src_gens))
    return src_gens[0](log, **kwargs)