summaryrefslogtreecommitdiff
path: root/gr-blocks/python/blocks/var_to_msg.py
blob: 0a317830cf549e0b0947cbc7a1f6341734845f8f (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
#!/usr/bin/env python
#
# Copyright 2020 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
#

from gnuradio import gr
import pmt


class var_to_msg_pair(gr.sync_block):
    """
    This block has a callback that will emit a message pair with the updated variable
    value when called. This is useful for monitoring a GRC variable and emitting a message
    when it is changed.
    """

    def __init__(self, pairname):
        gr.sync_block.__init__(
            self, name="var_to_msg_pair", in_sig=None, out_sig=None)

        self.pairname = pairname

        self.message_port_register_out(pmt.intern("msgout"))

    def variable_changed(self, value):
        try:
            if type(value) == float:
                p = pmt.from_double(value)
            elif type(value) == int:
                p = pmt.from_long(value)
            elif type(value) == bool:
                p = pmt.from_bool(value)
            elif type(value) == str:
                p = pmt.intern(value)
            else:
                p = pmt.to_pmt(value)

            self.message_port_pub(pmt.intern("msgout"),
                                  pmt.cons(pmt.intern(self.pairname), p))

        except Exception as e:
            gr.log.error("Unable to convert " + repr(value) +
                         " to PDU, no message will be emitted (reason: %s)" % repr(e))

    def stop(self):
        return True