From 30ea41c48215af4c50049aefb4ff0e59f14decfe Mon Sep 17 00:00:00 2001
From: Josh Blum <josh@joshknows.com>
Date: Tue, 17 Apr 2012 22:14:05 -0700
Subject: this is squashed python blocks support

---
 .../src/python/gnuradio/gr/CMakeLists.txt          |   3 +
 gnuradio-core/src/python/gnuradio/gr/__init__.py   |   3 +-
 gnuradio-core/src/python/gnuradio/gr/gateway.py    | 215 +++++++++++++++++++
 .../src/python/gnuradio/gr/qa_block_gateway.py     | 235 +++++++++++++++++++++
 4 files changed, 455 insertions(+), 1 deletion(-)
 create mode 100644 gnuradio-core/src/python/gnuradio/gr/gateway.py
 create mode 100644 gnuradio-core/src/python/gnuradio/gr/qa_block_gateway.py

(limited to 'gnuradio-core/src/python')

diff --git a/gnuradio-core/src/python/gnuradio/gr/CMakeLists.txt b/gnuradio-core/src/python/gnuradio/gr/CMakeLists.txt
index 3e75ead031..62f3d7e46d 100644
--- a/gnuradio-core/src/python/gnuradio/gr/CMakeLists.txt
+++ b/gnuradio-core/src/python/gnuradio/gr/CMakeLists.txt
@@ -23,6 +23,7 @@ include(GrPython)
 GR_PYTHON_INSTALL(FILES
     __init__.py
     exceptions.py
+    gateway.py
     gr_threading.py
     gr_threading_23.py
     gr_threading_24.py
@@ -43,6 +44,8 @@ file(GLOB py_qa_test_files "qa_*.py")
 foreach(py_qa_test_file ${py_qa_test_files})
     get_filename_component(py_qa_test_name ${py_qa_test_file} NAME_WE)
     set(GR_TEST_PYTHON_DIRS
+        ${CMAKE_SOURCE_DIR}/gruel/src/python
+        ${CMAKE_BINARY_DIR}/gruel/src/swig
         ${CMAKE_BINARY_DIR}/gnuradio-core/src/python
         ${CMAKE_BINARY_DIR}/gnuradio-core/src/lib/swig
     )
diff --git a/gnuradio-core/src/python/gnuradio/gr/__init__.py b/gnuradio-core/src/python/gnuradio/gr/__init__.py
index 602d1119fb..5b9a6a32c7 100644
--- a/gnuradio-core/src/python/gnuradio/gr/__init__.py
+++ b/gnuradio-core/src/python/gnuradio/gr/__init__.py
@@ -1,5 +1,5 @@
 #
-# Copyright 2003,2004,2006,2008,2009,2010 Free Software Foundation, Inc.
+# Copyright 2003-2012 Free Software Foundation, Inc.
 #
 # This file is part of GNU Radio
 #
@@ -44,6 +44,7 @@ from gnuradio_core import *
 from exceptions import *
 from hier_block2 import *
 from top_block import *
+from gateway import basic_block, sync_block, decim_block, interp_block
 
 if _RTLD_GLOBAL != 0:
     sys.setdlopenflags(_dlopenflags)             # Restore original flags
diff --git a/gnuradio-core/src/python/gnuradio/gr/gateway.py b/gnuradio-core/src/python/gnuradio/gr/gateway.py
new file mode 100644
index 0000000000..244b8b5925
--- /dev/null
+++ b/gnuradio-core/src/python/gnuradio/gr/gateway.py
@@ -0,0 +1,215 @@
+#
+# Copyright 2011-2012 Free Software Foundation, Inc.
+#
+# This file is part of GNU Radio
+#
+# GNU Radio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# GNU Radio is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Radio; see the file COPYING.  If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+#
+
+import gnuradio_core as gr_core
+from gnuradio_core import io_signature, io_signaturev
+from gnuradio_core import gr_block_gw_message_type
+from gnuradio_core import block_gateway
+import numpy
+
+########################################################################
+# Magic to turn pointers into numpy arrays
+# http://docs.scipy.org/doc/numpy/reference/arrays.interface.html
+########################################################################
+def pointer_to_ndarray(addr, dtype, nitems):
+    class array_like:
+        __array_interface__ = {
+            'data' : (int(addr), False),
+            'typestr' : dtype.base.str,
+            'descr' : dtype.base.descr,
+            'shape' : (nitems,) + dtype.shape,
+            'strides' : None,
+            'version' : 3
+        }
+    return numpy.asarray(array_like()).view(dtype.base)
+
+########################################################################
+# Handler that does callbacks from C++
+########################################################################
+class gateway_handler(gr_core.feval_ll):
+
+    #dont put a constructor, it wont work
+
+    def init(self, callback):
+        self._callback = callback
+
+    def eval(self, arg):
+        try: self._callback()
+        except Exception as ex:
+            print("handler caught exception: %s"%ex)
+            import traceback; traceback.print_exc()
+            raise ex
+        return 0
+
+########################################################################
+# The guts that make this into a gr block
+########################################################################
+class gateway_block(object):
+
+    def __init__(self, name, in_sig, out_sig, work_type, factor):
+
+        #ensure that the sigs are iterable dtypes
+        def sig_to_dtype_sig(sig):
+            if sig is None: sig = ()
+            return map(numpy.dtype, sig)
+        self.__in_sig = sig_to_dtype_sig(in_sig)
+        self.__out_sig = sig_to_dtype_sig(out_sig)
+
+        #cache the ranges to iterate when dispatching work
+        self.__in_indexes = range(len(self.__in_sig))
+        self.__out_indexes = range(len(self.__out_sig))
+
+        #convert the signatures into gr.io_signatures
+        def sig_to_gr_io_sigv(sig):
+            if not len(sig): return io_signature(0, 0, 0)
+            return io_signaturev(len(sig), len(sig), [s.itemsize for s in sig])
+        gr_in_sig = sig_to_gr_io_sigv(self.__in_sig)
+        gr_out_sig = sig_to_gr_io_sigv(self.__out_sig)
+
+        #create internal gateway block
+        self.__handler = gateway_handler()
+        self.__handler.init(self.__gr_block_handle)
+        self.__gateway = block_gateway(
+            self.__handler, name, gr_in_sig, gr_out_sig, work_type, factor)
+        self.__message = self.__gateway.gr_block_message()
+
+        #register gr_block functions
+        prefix = 'gr_block__'
+        for attr in [x for x in dir(self.__gateway) if x.startswith(prefix)]:
+            setattr(self, attr.replace(prefix, ''), getattr(self.__gateway, attr))
+        self.pop_msg_queue = lambda: gr_core.gr_block_gw_pop_msg_queue_safe(self.__gateway)
+
+    def to_basic_block(self):
+        """
+        Makes this block connectable by hier/top block python
+        """
+        return self.__gateway.to_basic_block()
+
+    def __gr_block_handle(self):
+        """
+        Dispatch tasks according to the action type specified in the message.
+        """
+        if self.__message.action == gr_block_gw_message_type.ACTION_GENERAL_WORK:
+            self.__message.general_work_args_return_value = self.general_work(
+
+                input_items=[pointer_to_ndarray(
+                    self.__message.general_work_args_input_items[i],
+                    self.__in_sig[i],
+                    self.__message.general_work_args_ninput_items[i]
+                ) for i in self.__in_indexes],
+
+                output_items=[pointer_to_ndarray(
+                    self.__message.general_work_args_output_items[i],
+                    self.__out_sig[i],
+                    self.__message.general_work_args_noutput_items
+                ) for i in self.__out_indexes],
+            )
+
+        elif self.__message.action == gr_block_gw_message_type.ACTION_WORK:
+            self.__message.work_args_return_value = self.work(
+
+                input_items=[pointer_to_ndarray(
+                    self.__message.work_args_input_items[i],
+                    self.__in_sig[i],
+                    self.__message.work_args_ninput_items
+                ) for i in self.__in_indexes],
+
+                output_items=[pointer_to_ndarray(
+                    self.__message.work_args_output_items[i],
+                    self.__out_sig[i],
+                    self.__message.work_args_noutput_items
+                ) for i in self.__out_indexes],
+            )
+
+        elif self.__message.action == gr_block_gw_message_type.ACTION_FORECAST:
+            self.forecast(
+                noutput_items=self.__message.forecast_args_noutput_items,
+                ninput_items_required=self.__message.forecast_args_ninput_items_required,
+            )
+
+        elif self.__message.action == gr_block_gw_message_type.ACTION_START:
+            self.__message.start_args_return_value = self.start()
+
+        elif self.__message.action == gr_block_gw_message_type.ACTION_STOP:
+            self.__message.stop_args_return_value = self.stop()
+
+    def forecast(self, noutput_items, ninput_items_required):
+        """
+        forecast is only called from a general block
+        this is the default implementation
+        """
+        for ninput_item in ninput_items_required:
+            ninput_item = noutput_items + self.history() - 1;
+        return
+
+    def general_work(self, *args, **kwargs):
+        """general work to be overloaded in a derived class"""
+        raise NotImplementedError("general work not implemented")
+
+    def work(self, *args, **kwargs):
+        """work to be overloaded in a derived class"""
+        raise NotImplementedError("work not implemented")
+
+    def start(self): return True
+    def stop(self): return True
+
+########################################################################
+# Wrappers for the user to inherit from
+########################################################################
+class basic_block(gateway_block):
+    def __init__(self, name, in_sig, out_sig):
+        gateway_block.__init__(self,
+            name=name,
+            in_sig=in_sig,
+            out_sig=out_sig,
+            work_type=gr_core.GR_BLOCK_GW_WORK_GENERAL,
+            factor=1, #not relevant factor
+        )
+
+class sync_block(gateway_block):
+    def __init__(self, name, in_sig, out_sig):
+        gateway_block.__init__(self,
+            name=name,
+            in_sig=in_sig,
+            out_sig=out_sig,
+            work_type=gr_core.GR_BLOCK_GW_WORK_SYNC,
+            factor=1,
+        )
+
+class decim_block(gateway_block):
+    def __init__(self, name, in_sig, out_sig, decim):
+        gateway_block.__init__(self,
+            name=name,
+            in_sig=in_sig,
+            out_sig=out_sig,
+            work_type=gr_core.GR_BLOCK_GW_WORK_DECIM,
+            factor=decim,
+        )
+
+class interp_block(gateway_block):
+    def __init__(self, name, in_sig, out_sig, interp):
+        gateway_block.__init__(self,
+            name=name,
+            in_sig=in_sig,
+            out_sig=out_sig,
+            work_type=gr_core.GR_BLOCK_GW_WORK_INTERP,
+            factor=interp,
+        )
diff --git a/gnuradio-core/src/python/gnuradio/gr/qa_block_gateway.py b/gnuradio-core/src/python/gnuradio/gr/qa_block_gateway.py
new file mode 100644
index 0000000000..b2199cdf8f
--- /dev/null
+++ b/gnuradio-core/src/python/gnuradio/gr/qa_block_gateway.py
@@ -0,0 +1,235 @@
+#
+# Copyright 2011-2012 Free Software Foundation, Inc.
+#
+# This file is part of GNU Radio
+#
+# GNU Radio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# GNU Radio is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Radio; see the file COPYING.  If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+#
+
+from gnuradio import gr, gr_unittest
+import pmt #from gruel import pmt
+import numpy
+
+class add_2_f32_1_f32(gr.sync_block):
+    def __init__(self):
+        gr.sync_block.__init__(
+            self,
+            name = "add 2 f32",
+            in_sig = [numpy.float32, numpy.float32],
+            out_sig = [numpy.float32],
+        )
+
+    def work(self, input_items, output_items):
+        output_items[0][:] = input_items[0] + input_items[1]
+        return len(output_items[0])
+
+class add_2_fc32_1_fc32(gr.sync_block):
+    def __init__(self):
+        gr.sync_block.__init__(
+            self,
+            name = "add 2 fc32",
+            in_sig = [numpy.complex64, numpy.complex64],
+            out_sig = [numpy.complex64],
+        )
+
+    def work(self, input_items, output_items):
+        output_items[0][:] = input_items[0] + input_items[1]
+        return len(output_items[0])
+
+class convolve(gr.sync_block):
+    """
+    A demonstration using block history to properly perform a convolution.
+    """
+    def __init__(self):
+        gr.sync_block.__init__(
+            self,
+            name = "convolve",
+            in_sig = [numpy.float32],
+            out_sig = [numpy.float32]
+        )
+        self._taps = [1, 0, 0, 0]
+        self.set_history(len(self._taps))
+
+    def work(self, input_items, output_items):
+        output_items[0][:] = numpy.convolve(input_items[0], self._taps, mode='valid')
+        return len(output_items[0])
+
+class decim2x(gr.decim_block):
+    def __init__(self):
+        gr.decim_block.__init__(
+            self,
+            name = "decim2x",
+            in_sig = [numpy.float32],
+            out_sig = [numpy.float32],
+            decim = 2
+        )
+
+    def work(self, input_items, output_items):
+        output_items[0][:] = input_items[0][::2]
+        return len(output_items[0])
+
+class interp2x(gr.interp_block):
+    def __init__(self):
+        gr.interp_block.__init__(
+            self,
+            name = "interp2x",
+            in_sig = [numpy.float32],
+            out_sig = [numpy.float32],
+            interp = 2
+        )
+
+    def work(self, input_items, output_items):
+        output_items[0][1::2] = input_items[0]
+        output_items[0][::2] = input_items[0]
+        return len(output_items[0])
+
+class tag_source(gr.sync_block):
+    def __init__(self):
+        gr.sync_block.__init__(
+            self,
+            name = "tag source",
+            in_sig = None,
+            out_sig = [numpy.float32],
+        )
+
+    def work(self, input_items, output_items):
+        num_output_items = len(output_items[0])
+
+        #put code here to fill the output items...
+
+        #make a new tag on the middle element every time work is called
+        count = self.nitems_written(0) + num_output_items/2
+        key = pmt.pmt_string_to_symbol("example_key")
+        value = pmt.pmt_string_to_symbol("example_value")
+        self.add_item_tag(0, count, key, value)
+
+        return num_output_items
+
+class tag_sink(gr.sync_block):
+    def __init__(self):
+        gr.sync_block.__init__(
+            self,
+            name = "tag sink",
+            in_sig = [numpy.float32],
+            out_sig = None,
+        )
+        self.key = None
+
+    def work(self, input_items, output_items):
+        num_input_items = len(input_items[0])
+
+        #put code here to process the input items...
+
+        #print all the tags received in this work call
+        nread = self.nitems_read(0)
+        tags = self.get_tags_in_range(0, nread, nread+num_input_items)
+        for tag in tags:
+            print tag.offset
+            print pmt.pmt_symbol_to_string(tag.key)
+            print pmt.pmt_symbol_to_string(tag.value)
+            self.key = pmt.pmt_symbol_to_string(tag.key)
+
+        return num_input_items
+
+class fc32_to_f32_2(gr.sync_block):
+    def __init__(self):
+        gr.sync_block.__init__(
+            self,
+            name = "fc32_to_f32_2",
+            in_sig = [numpy.complex64],
+            out_sig = [(numpy.float32, 2)],
+        )
+
+    def work(self, input_items, output_items):
+        output_items[0][::,0] = numpy.real(input_items[0])
+        output_items[0][::,1] = numpy.imag(input_items[0])
+        return len(output_items[0])
+
+class test_block_gateway(gr_unittest.TestCase):
+
+    def test_add_f32(self):
+        tb = gr.top_block()
+        src0 = gr.vector_source_f([1, 3, 5, 7, 9], False)
+        src1 = gr.vector_source_f([0, 2, 4, 6, 8], False)
+        adder = add_2_f32_1_f32()
+        sink = gr.vector_sink_f()
+        tb.connect((src0, 0), (adder, 0))
+        tb.connect((src1, 0), (adder, 1))
+        tb.connect(adder, sink)
+        tb.run()
+        self.assertItemsEqual(sink.data(), (1, 5, 9, 13, 17))
+
+    def test_add_fc32(self):
+        tb = gr.top_block()
+        src0 = gr.vector_source_c([1, 3j, 5, 7j, 9], False)
+        src1 = gr.vector_source_c([0, 2j, 4, 6j, 8], False)
+        adder = add_2_fc32_1_fc32()
+        sink = gr.vector_sink_c()
+        tb.connect((src0, 0), (adder, 0))
+        tb.connect((src1, 0), (adder, 1))
+        tb.connect(adder, sink)
+        tb.run()
+        self.assertItemsEqual(sink.data(), (1, 5j, 9, 13j, 17))
+
+    def test_convolve(self):
+        tb = gr.top_block()
+        src = gr.vector_source_f([1, 2, 3, 4, 5, 6, 7, 8], False)
+        cv = convolve()
+        sink = gr.vector_sink_f()
+        tb.connect(src, cv, sink)
+        tb.run()
+        self.assertItemsEqual(sink.data(), (1, 2, 3, 4, 5, 6, 7, 8))
+
+    def test_decim2x(self):
+        tb = gr.top_block()
+        src = gr.vector_source_f([1, 2, 3, 4, 5, 6, 7, 8], False)
+        d2x = decim2x()
+        sink = gr.vector_sink_f()
+        tb.connect(src, d2x, sink)
+        tb.run()
+        self.assertItemsEqual(sink.data(), (1, 3, 5, 7))
+
+    def test_interp2x(self):
+        tb = gr.top_block()
+        src = gr.vector_source_f([1, 3, 5, 7, 9], False)
+        i2x = interp2x()
+        sink = gr.vector_sink_f()
+        tb.connect(src, i2x, sink)
+        tb.run()
+        self.assertItemsEqual(sink.data(), (1, 1, 3, 3, 5, 5, 7, 7, 9, 9))
+
+    def test_tags(self):
+        src = tag_source()
+        sink = tag_sink()
+        head = gr.head(gr.sizeof_float, 50000) #should be enough items to get a tag through
+        tb = gr.top_block()
+        tb.connect(src, head, sink)
+        tb.run()
+        self.assertEqual(sink.key, "example_key")
+
+    def test_fc32_to_f32_2(self):
+        tb = gr.top_block()
+        src = gr.vector_source_c([1+2j, 3+4j, 5+6j, 7+8j, 9+10j], False)
+        convert = fc32_to_f32_2()
+        v2s = gr.vector_to_stream(gr.sizeof_float, 2)
+        sink = gr.vector_sink_f()
+        tb.connect(src, convert, v2s, sink)
+        tb.run()
+        self.assertItemsEqual(sink.data(), (1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
+
+if __name__ == '__main__':
+    gr_unittest.run(test_block_gateway, "test_block_gateway.xml")
+
-- 
cgit v1.2.3