[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Commit-gnuradio] [gnuradio] 18/28: basic: added stream selector block
From: |
git |
Subject: |
[Commit-gnuradio] [gnuradio] 18/28: basic: added stream selector block |
Date: |
Mon, 15 Aug 2016 00:47:06 +0000 (UTC) |
This is an automated email from the git hooks/post-receive script.
nwest pushed a commit to annotated tag gr_basic_work
in repository gnuradio.
commit 84efab3bf6842ad36469839b765af347cf4fdb20
Author: Josh Blum <address@hidden>
Date: Sun Nov 13 15:22:00 2011 -0800
basic: added stream selector block
---
gnuradio-core/src/lib/runtime/gr_hier_block2.h | 1 +
gnuradio-core/src/lib/swig/gr_swig_block_magic.i | 6 -
gr-basic/grc/basic_block_tree.xml | 1 +
gr-basic/grc/basic_stream_selector.xml | 56 ++++++
gr-basic/include/CMakeLists.txt | 1 +
gr-basic/include/gr_basic_stream_selector.h | 51 +++++
gr-basic/lib/CMakeLists.txt | 1 +
gr-basic/lib/gr_basic_stream_selector.cc | 218 +++++++++++++++++++++
.../swig/{basic_swig.i => basic_stream_selector.i} | 15 +-
gr-basic/swig/basic_swig.i | 4 +
10 files changed, 341 insertions(+), 13 deletions(-)
diff --git a/gnuradio-core/src/lib/runtime/gr_hier_block2.h
b/gnuradio-core/src/lib/runtime/gr_hier_block2.h
index 8687b7d..9652f6b 100644
--- a/gnuradio-core/src/lib/runtime/gr_hier_block2.h
+++ b/gnuradio-core/src/lib/runtime/gr_hier_block2.h
@@ -55,6 +55,7 @@ private:
gr_hier_block2_detail *d_detail;
protected:
+ gr_hier_block2 (void){} //allows pure virtual interface sub-classes
gr_hier_block2(const std::string &name,
gr_io_signature_sptr input_signature,
gr_io_signature_sptr output_signature);
diff --git a/gnuradio-core/src/lib/swig/gr_swig_block_magic.i
b/gnuradio-core/src/lib/swig/gr_swig_block_magic.i
index 7eae9c1..5929386 100644
--- a/gnuradio-core/src/lib/swig/gr_swig_block_magic.i
+++ b/gnuradio-core/src/lib/swig/gr_swig_block_magic.i
@@ -37,12 +37,6 @@ BASE_NAME = FULL_NAME.make
%enddef
#endif
-//ignore base classes commonly inherited by gr blocks
-%ignore gr_sync_block;
-%ignore gr_sync_decimator;
-%ignore gr_sync_interpolator;
-%ignore gr_hier_block2;
-
//----------------------------------------------------------------------
//-- GR_SWIG_BLOCK_MAGIC
//----------------------------------------------------------------------
diff --git a/gr-basic/grc/basic_block_tree.xml
b/gr-basic/grc/basic_block_tree.xml
index 1e87971..6833b7d 100644
--- a/gr-basic/grc/basic_block_tree.xml
+++ b/gr-basic/grc/basic_block_tree.xml
@@ -31,6 +31,7 @@
<cat>
<name>Basic</name>
<block>basic_sig_source</block>
+ <block>basic_stream_selector</block>
<block>basic_add</block>
<block>basic_add_const</block>
<block>basic_subtract</block>
diff --git a/gr-basic/grc/basic_stream_selector.xml
b/gr-basic/grc/basic_stream_selector.xml
new file mode 100644
index 0000000..df42d56
--- /dev/null
+++ b/gr-basic/grc/basic_stream_selector.xml
@@ -0,0 +1,56 @@
+<?xml version="1.0"?>
+<!--
+###################################################
+## Basic stream selector/stream multi-mux
+###################################################
+ -->
+<block>
+ <name>Stream Selector</name>
+ <key>basic_stream_selector</key>
+ <import>from gnuradio import gr</import>
+ <import>from gnuradio import basic</import>
+ <make>basic.stream_selector(
+ gr.io_signature($num_inputs, $num_inputs, $item_size),
+ gr.io_signature($num_outputs, $num_outputs, $item_size),
+)
+self.$(id).set_paths($paths)
+</make>
+ <callback>self.$(id).set_paths($paths)</callback>
+ <param>
+ <name>Item Size</name>
+ <key>item_size</key>
+ <value>1</value>
+ <type>int</type>
+ </param>
+ <param>
+ <name>Num Inputs</name>
+ <key>num_inputs</key>
+ <value>2</value>
+ <type>int</type>
+ </param>
+ <param>
+ <name>Num Outputs</name>
+ <key>num_outputs</key>
+ <value>2</value>
+ <type>int</type>
+ </param>
+ <param>
+ <name>Paths</name>
+ <key>paths</key>
+ <value>0, 1</value>
+ <type>int_vector</type>
+ </param>
+ <check>len($paths) == $num_inputs</check>
+ <sink>
+ <name>in</name>
+ <type>byte</type>
+ <vlen>$item_size</vlen>
+ <nports>$num_inputs</nports>
+ </sink>
+ <source>
+ <name>out</name>
+ <type>byte</type>
+ <vlen>$item_size</vlen>
+ <nports>$num_outputs</nports>
+ </source>
+</block>
diff --git a/gr-basic/include/CMakeLists.txt b/gr-basic/include/CMakeLists.txt
index 2304d83..0a60365 100644
--- a/gr-basic/include/CMakeLists.txt
+++ b/gr-basic/include/CMakeLists.txt
@@ -29,6 +29,7 @@ install(FILES
gr_basic_multiply_const.h
gr_basic_op_types.h
gr_basic_sig_source.h
+ gr_basic_stream_selector.h
gr_basic_subtract.h
DESTINATION ${GR_INCLUDE_DIR}/gnuradio
COMPONENT "basic_devel"
diff --git a/gr-basic/include/gr_basic_stream_selector.h
b/gr-basic/include/gr_basic_stream_selector.h
new file mode 100644
index 0000000..356fa0e
--- /dev/null
+++ b/gr-basic/include/gr_basic_stream_selector.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2011 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.
+ */
+#ifndef INCLUDED_GR_BASIC_STREAM_SELECTOR_H
+#define INCLUDED_GR_BASIC_STREAM_SELECTOR_H
+
+#include <gr_basic_api.h>
+#include <gr_hier_block2.h>
+
+/*!
+ * The sector blocks allows streams to be dynamically routed at runtime.
+ */
+class GR_BASIC_API gr_basic_stream_selector : virtual public gr_hier_block2{
+public:
+ typedef boost::shared_ptr<gr_basic_stream_selector> sptr;
+
+ /*!
+ * Make a new stream selector block
+ * \param in_sig signature to describe inputs
+ * \param out_sig signature to describe outputs
+ */
+ static sptr make(gr_io_signature_sptr in_sig, gr_io_signature_sptr
out_sig);
+
+ /*!
+ * Set the path for samples for each input ports.
+ * Element i of paths specifies the output port index for input port i.
+ * The value of the element may also be: -1 to block or -2 to consume.
+ * \param paths a list of stream destinations for each port
+ */
+ virtual void set_paths(const std::vector<int> &paths) = 0;
+
+};
+
+#endif /* INCLUDED_GR_BASIC_STREAM_SELECTOR_H */
diff --git a/gr-basic/lib/CMakeLists.txt b/gr-basic/lib/CMakeLists.txt
index 53e6c79..c46891a 100644
--- a/gr-basic/lib/CMakeLists.txt
+++ b/gr-basic/lib/CMakeLists.txt
@@ -39,6 +39,7 @@ list(APPEND gr_basic_sources
gr_basic_multiply.cc
gr_basic_multiply_const.cc
gr_basic_sig_source.cc
+ gr_basic_stream_selector.cc
gr_basic_subtract.cc
)
diff --git a/gr-basic/lib/gr_basic_stream_selector.cc
b/gr-basic/lib/gr_basic_stream_selector.cc
new file mode 100644
index 0000000..2543b81
--- /dev/null
+++ b/gr-basic/lib/gr_basic_stream_selector.cc
@@ -0,0 +1,218 @@
+/*
+ * Copyright 2011 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.
+ */
+
+#include <gr_basic_stream_selector.h>
+#include <gruel/thread.h>
+#include <gr_sync_block.h>
+#include <gr_io_signature.h>
+#include <stdexcept>
+#include <cstring> //memcpy
+#include <iostream>
+
+/***********************************************************************
+ * A single selector output
+ **********************************************************************/
+class gr_basic_stream_selector_output : public gr_sync_block{
+public:
+ gr_basic_stream_selector_output(const size_t item_size):
+ gr_sync_block(
+ "stream selector output",
+ gr_make_io_signature (0, 0, 0),
+ gr_make_io_signature (1, 1, item_size)
+ ),
+ _item_size(item_size),
+ _has_data(false)
+ {
+ //NOP
+ }
+
+ size_t post_output(const void *mem, const size_t len){
+ //guarantees that only one caller enters at a time
+ gruel::scoped_lock caller_lock(_caller_mutex);
+
+ gruel::scoped_lock lock(_mutex);
+ _mem = mem;
+ _len = len;
+ _has_data = true;
+ _cond.notify_one();
+ while (_has_data){
+ _cond.wait(lock);
+ }
+ return _len;
+ }
+
+ int work(
+ int noutput_items,
+ gr_vector_const_void_star &,
+ gr_vector_void_star &output_items
+ ){
+ gruel::scoped_lock lock(_mutex);
+ while (!_has_data){
+ _cond.wait(lock);
+ }
+ _len = std::min(_len, noutput_items*_item_size);
+ std::memcpy(output_items[0], _mem, _len);
+ _has_data = false;
+ _cond.notify_one();
+ return _len/_item_size;
+ }
+
+private:
+ const size_t _item_size;
+ gruel::mutex _mutex;
+ gruel::mutex _caller_mutex;
+ gruel::condition_variable _cond;
+ const void *_mem;
+ size_t _len;
+ bool _has_data;
+};
+
+/***********************************************************************
+ * A single selector input
+ **********************************************************************/
+class gr_basic_stream_selector_input : public gr_sync_block{
+public:
+ gr_basic_stream_selector_input(const size_t item_size):
+ gr_sync_block(
+ "stream selector input",
+ gr_make_io_signature (1, 1, item_size),
+ gr_make_io_signature (0, 0, 0)
+ ),
+ _item_size(item_size)
+ {
+ this->set_output(NULL);
+ }
+
+ void set_output(gr_basic_stream_selector_output *output, bool block =
true){
+ gruel::scoped_lock lock(_mutex);
+ _output = output;
+ _block = block;
+ lock.unlock();
+ _blocker.notify_one();
+ }
+
+ int work(
+ int noutput_items,
+ gr_vector_const_void_star &input_items,
+ gr_vector_void_star &
+ ){
+ gruel::scoped_lock lock(_mutex);
+ while (true){
+ if (_output != NULL){
+ return _output->post_output(input_items[0],
noutput_items*_item_size)/_item_size;
+ }
+ if (_block){
+ _blocker.wait(lock);
+ continue;
+ }
+ else{ //consume all
+ return noutput_items;
+ }
+ }
+ }
+
+private:
+ const size_t _item_size;
+ gruel::mutex _mutex;
+ gruel::condition_variable _blocker;
+ gr_basic_stream_selector_output *_output;
+ bool _block;
+};
+
+/***********************************************************************
+ * The selector implementation glue
+ **********************************************************************/
+class gr_basic_stream_selector_impl : public gr_basic_stream_selector{
+public:
+ gr_basic_stream_selector_impl(
+ gr_io_signature_sptr in_sig,
+ gr_io_signature_sptr out_sig
+ ):
+ gr_hier_block2(
+ "stream selector impl",
+ in_sig, out_sig
+ )
+ {
+ //sanity check IO signatures
+ if (in_sig->min_streams() != in_sig->max_streams() ||
in_sig->min_streams() < 1){
+ throw std::invalid_argument("stream selector input signature
invalid");
+ }
+ if (out_sig->min_streams() != out_sig->max_streams() ||
out_sig->min_streams() < 1){
+ throw std::invalid_argument("stream selector output signature
invalid");
+ }
+
+ //create and connect input blocks
+ for (size_t i = 0; i < size_t(in_sig->min_streams()); i++){
+
_inputs.push_back(boost::shared_ptr<gr_basic_stream_selector_input>(
+ new
gr_basic_stream_selector_input(in_sig->sizeof_stream_item(i))
+ ));
+ this->connect(this->self(), i, _inputs.back(), 0);
+ }
+
+ //create and connect output blocks
+ for (size_t i = 0; i < size_t(out_sig->min_streams()); i++){
+
_outputs.push_back(boost::shared_ptr<gr_basic_stream_selector_output>(
+ new
gr_basic_stream_selector_output(out_sig->sizeof_stream_item(i))
+ ));
+ this->connect(_outputs.back(), 0, this->self(), i);
+ }
+ }
+
+ void set_paths(const std::vector<int> &paths){
+ if (paths.size() != _inputs.size()){
+ throw std::invalid_argument("stream selector set paths wrong
length");
+ }
+
+ //set all inputs to block before we apply the new paths
+ for (size_t i = 0; i < paths.size(); i++){
+ _inputs.at(i)->set_output(NULL, true/*block*/);
+ }
+
+ //apply the new path to each input
+ for (size_t i = 0; i < paths.size(); i++){
+ if (paths[i] == -1){
+ _inputs.at(i)->set_output(NULL, true/*block*/);
+ }
+ else if (paths[i] == -2){
+ _inputs.at(i)->set_output(NULL, false/*consume*/);
+ }
+ else if (size_t(paths[i]) >= _outputs.size()){
+ throw std::invalid_argument("stream selector output index
invalid");
+ }
+ else{
+ _inputs.at(i)->set_output(_outputs.at(paths[i]).get());
+ }
+ }
+ }
+
+private:
+ std::vector<boost::shared_ptr<gr_basic_stream_selector_input> > _inputs;
+ std::vector<boost::shared_ptr<gr_basic_stream_selector_output> > _outputs;
+};
+
+/***********************************************************************
+ * Factory function
+ **********************************************************************/
+gr_basic_stream_selector::sptr gr_basic_stream_selector::make(
+ gr_io_signature_sptr in_sig, gr_io_signature_sptr out_sig
+){
+ return sptr(new gr_basic_stream_selector_impl(in_sig, out_sig));
+}
diff --git a/gr-basic/swig/basic_swig.i b/gr-basic/swig/basic_stream_selector.i
similarity index 87%
copy from gr-basic/swig/basic_swig.i
copy to gr-basic/swig/basic_stream_selector.i
index 6e94a22..757de9a 100644
--- a/gr-basic/swig/basic_swig.i
+++ b/gr-basic/swig/basic_stream_selector.i
@@ -19,15 +19,16 @@
* Boston, MA 02110-1301, USA.
*/
-#define GR_BASIC_API
-
////////////////////////////////////////////////////////////////////////
-// standard includes
+// block headers
////////////////////////////////////////////////////////////////////////
-%include <gnuradio.i>
+%{
+#include <gr_basic_stream_selector.h>
+%}
+
+%include <gr_basic_stream_selector.h>
////////////////////////////////////////////////////////////////////////
-// block includes
+// block magic
////////////////////////////////////////////////////////////////////////
-%include <basic_ops.i>
-%include <basic_sig_source.i>
+GR_SWIG_BLOCK_MAGIC2(gr_basic,stream_selector)
diff --git a/gr-basic/swig/basic_swig.i b/gr-basic/swig/basic_swig.i
index 6e94a22..ff71849 100644
--- a/gr-basic/swig/basic_swig.i
+++ b/gr-basic/swig/basic_swig.i
@@ -21,6 +21,9 @@
#define GR_BASIC_API
+%ignore gr_sync_block;
+%ignore gr_hier_block2;
+
////////////////////////////////////////////////////////////////////////
// standard includes
////////////////////////////////////////////////////////////////////////
@@ -31,3 +34,4 @@
////////////////////////////////////////////////////////////////////////
%include <basic_ops.i>
%include <basic_sig_source.i>
+%include <basic_stream_selector.i>
- [Commit-gnuradio] [gnuradio] 04/28: basic: added other basic operators, (continued)
- [Commit-gnuradio] [gnuradio] 04/28: basic: added other basic operators, git, 2016/08/14
- [Commit-gnuradio] [gnuradio] 01/28: basic: attempt at new component, partial adder implementation, git, 2016/08/14
- [Commit-gnuradio] [gnuradio] 27/28: basic: added dynamic delay block, git, 2016/08/14
- [Commit-gnuradio] [gnuradio] 13/28: Volk: whoops, git, 2016/08/14
- [Commit-gnuradio] [gnuradio] 17/28: core: squashed in-place block work, git, 2016/08/14
- [Commit-gnuradio] [gnuradio] 07/28: basic add/mult const, needs test, git, 2016/08/14
- [Commit-gnuradio] [gnuradio] 15/28: basic: added super fast signal source, git, 2016/08/14
- [Commit-gnuradio] [gnuradio] 26/28: basic: use set_output_alignment in the basic math blocks, git, 2016/08/14
- [Commit-gnuradio] [gnuradio] 19/28: core: make in-place buffering enabled per port, git, 2016/08/14
- [Commit-gnuradio] [gnuradio] 22/28: filter: added 32f decimating fir + tweaks, git, 2016/08/14
- [Commit-gnuradio] [gnuradio] 18/28: basic: added stream selector block,
git <=
- [Commit-gnuradio] [gnuradio] 05/28: basic: whoops, wrong operator, git, 2016/08/14
- [Commit-gnuradio] [gnuradio] 12/28: Volk: 32f_s32f_multiply_32f, git, 2016/08/14
- [Commit-gnuradio] [gnuradio] 03/28: basic: added int16 data types and filled in float32, git, 2016/08/14
- [Commit-gnuradio] [gnuradio] 08/28: basic: working add/mult const blocks, block magic2 for static make, git, 2016/08/14
- [Commit-gnuradio] [gnuradio] 20/28: core: enable inplace on some core math blocks, git, 2016/08/14
- [Commit-gnuradio] [gnuradio] 28/28: just a bunch of tweaks, git, 2016/08/14
- [Commit-gnuradio] [gnuradio] 21/28: filter: initial commit of gr filter, made fir decim block, git, 2016/08/14