summaryrefslogtreecommitdiff
path: root/gr-blocks/python
diff options
context:
space:
mode:
authorghostop14 <ghostop14@gmail.com>2020-02-13 09:26:07 -0500
committerMichael Dickens <michael.dickens@ettus.com>2020-02-15 18:50:33 -0500
commitf6a346d8b59050b2da520c80f7fab9d85018e350 (patch)
treecd09945ad44a76838a2e1b058d4658acc94264fb /gr-blocks/python
parentd9750796171a2b98e78b8a1e9b55ff8c8a414ad0 (diff)
gr-blocks Add Msg to Var and Var to Msg Conversion Blocks
These 3 new blocks provide the missing glue to move between messages and variables in the same flowgraph. There are 3 variants here: 1. Monitor a variable and produce a user-specified message (pair) when the variable changes. Useful bridging standard GUI controls to message-based blocks. 2. When an inbound message (pair) is received, update a specified variable. 3. When an inbound message (dict) is received, extract a single dictionary entry and produce a message pair (useful if you have a multi-value dictionary but you just want to pull off a single attribute such as frequency or gain without modifying the upstream block. This can be paired with (2) if necessary to move from a dictionary item to a variable.
Diffstat (limited to 'gr-blocks/python')
-rw-r--r--gr-blocks/python/blocks/CMakeLists.txt3
-rw-r--r--gr-blocks/python/blocks/__init__.py3
-rw-r--r--gr-blocks/python/blocks/msg_meta_to_pair.py67
-rw-r--r--gr-blocks/python/blocks/msg_pair_to_var.py36
-rw-r--r--gr-blocks/python/blocks/var_to_msg.py38
5 files changed, 147 insertions, 0 deletions
diff --git a/gr-blocks/python/blocks/CMakeLists.txt b/gr-blocks/python/blocks/CMakeLists.txt
index 2f1dc85234..59ebde6b6e 100644
--- a/gr-blocks/python/blocks/CMakeLists.txt
+++ b/gr-blocks/python/blocks/CMakeLists.txt
@@ -13,6 +13,9 @@ GR_PYTHON_INSTALL(
__init__.py
parse_file_metadata.py
stream_to_vector_decimator.py
+ msg_pair_to_var.py
+ msg_meta_to_pair.py
+ var_to_msg.py
DESTINATION ${GR_PYTHON_DIR}/gnuradio/blocks
)
diff --git a/gr-blocks/python/blocks/__init__.py b/gr-blocks/python/blocks/__init__.py
index 3815a26b3a..c4eab53ce6 100644
--- a/gr-blocks/python/blocks/__init__.py
+++ b/gr-blocks/python/blocks/__init__.py
@@ -24,6 +24,9 @@ except ImportError:
from .blocks_swig import *
from .stream_to_vector_decimator import *
+from .msg_meta_to_pair import meta_to_pair
+from .msg_pair_to_var import msg_pair_to_var
+from .var_to_msg import var_to_msg_pair
#alias old add_vXX and multiply_vXX
add_vcc = add_cc
diff --git a/gr-blocks/python/blocks/msg_meta_to_pair.py b/gr-blocks/python/blocks/msg_meta_to_pair.py
new file mode 100644
index 0000000000..1c6dde2f61
--- /dev/null
+++ b/gr-blocks/python/blocks/msg_meta_to_pair.py
@@ -0,0 +1,67 @@
+#!/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 meta_to_pair(gr.sync_block):
+ """
+ This block converts a metadata dictionary item to a pmt pair that is
+ compatible with other blocks expecting a pair in. You can specify
+ which item in the incoming metadata to output as a pair and what
+ the pair name is.
+ """
+ def __init__(self, incomingKeyName, outgoingPairName):
+ gr.sync_block.__init__(self, name="meta_to_pair", in_sig=None, out_sig=None)
+
+ self.incomingKeyName = str(incomingKeyName)
+ self.outgoingPairName = str(outgoingPairName)
+
+ self.message_port_register_in(pmt.intern("inmeta"))
+ self.set_msg_handler(pmt.intern("inmeta"), self.msg_handler)
+ self.message_port_register_out(pmt.intern("outpair"))
+
+ def msg_handler(self, msg):
+ if not pmt.is_pair(msg):
+ gr.log.warn("Incoming message is not a pair. Only pairs are supported. "
+ "No message generated.")
+ return
+
+ meta = pmt.to_python(pmt.car(msg))
+
+ if not type(meta) is dict:
+ gr.log.warn("Incoming message does not contain a dictionary. "
+ "No message generated.")
+ return
+
+ if not self.incomingKeyName in meta:
+ gr.log.warn("Incoming message dictionary does not contain key %s. "
+ "No message generated." % self.incomingKeyName)
+ return
+
+ incomingVal = meta[self.incomingKeyName]
+
+ new_pair = None
+
+ try:
+ new_pair = pmt.cons(pmt.intern(self.outgoingPairName), pmt.to_pmt(incomingVal))
+ except Exception as e:
+ gr.log.error("Cannot construct new message: %s" % str(e))
+ return
+
+ try:
+ self.message_port_pub(pmt.intern("outpair"), new_pair)
+ except Exception as e:
+ gr.log.error("Cannot send message: %s" % str(e))
+ gr.log.error("Incoming dictionary (%s):" % str(type(meta)))
+ gr.log.error(str(meta))
+
+ def stop(self):
+ return True
diff --git a/gr-blocks/python/blocks/msg_pair_to_var.py b/gr-blocks/python/blocks/msg_pair_to_var.py
new file mode 100644
index 0000000000..17cd7fcc38
--- /dev/null
+++ b/gr-blocks/python/blocks/msg_pair_to_var.py
@@ -0,0 +1,36 @@
+#!/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 msg_pair_to_var(gr.sync_block):
+ """
+ This block will take an input message pair and allow you to set a gnuradio variable.
+ """
+ def __init__(self, callback):
+ gr.sync_block.__init__(self, name="msg_pair_to_var", in_sig=None, out_sig=None)
+
+ self.callback = callback
+
+ self.message_port_register_in(pmt.intern("inpair"))
+ self.set_msg_handler(pmt.intern("inpair"), self.msg_handler)
+
+ def msg_handler(self, msg):
+ try:
+ new_val = pmt.to_python(pmt.cdr(msg))
+
+ self.callback(new_val)
+
+ except Exception as e:
+ gr.log.error("Error with message conversion: %s" % str(e))
+
+ def stop(self):
+ return True
diff --git a/gr-blocks/python/blocks/var_to_msg.py b/gr-blocks/python/blocks/var_to_msg.py
new file mode 100644
index 0000000000..77a947ed0c
--- /dev/null
+++ b/gr-blocks/python/blocks/var_to_msg.py
@@ -0,0 +1,38 @@
+#!/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 will monitor a variable, and when it changes, generate a message.
+ """
+ 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):
+ if type(value) == float:
+ p = pmt.from_float(value)
+ elif type(value) == int:
+ p = pmt.from_long(value)
+ elif type(value) == bool:
+ p = pmt.from_bool(value)
+ else:
+ p = pmt.intern(value)
+
+ self.message_port_pub(pmt.intern("msgout"), pmt.cons(pmt.intern(self.pairname), p))
+
+ def stop(self):
+ return True