2 # Copyright 2005 Free Software Foundation, Inc.
4 # This file is part of GNU Radio
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING. If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
22 from gnuradio_swig_python import io_signature
25 class hier_block_base(object):
27 Abstract base class for hierarchical signal processing blocks.
29 def __init__(self, fg):
31 @param fg: The flow graph that contains this hierarchical block.
32 @type fg: gr.flow_graph
34 self.fg = weakref.proxy(fg)
36 def input_signature(self):
38 @return input signature of hierarchical block.
39 @rtype gr.io_signature
43 def output_signature(self):
45 @return output signature of hierarchical block.
46 @rtype gr.io_signature
50 def resolve_input_port(self, port_number):
52 @param port_number: which input port number to resolve to an endpoint.
53 @type port_number: int
54 @return: sequence of endpoints
55 @rtype: sequence of endpoint
57 Note that an input port can resolve to more than one endpoint.
61 def resolve_output_port(self, port_number):
63 @param port_number: which output port number to resolve to an endpoint.
64 @type port_number: int
68 Output ports resolve to a single endpoint.
73 class hier_block(hier_block_base):
75 Simple concrete class for building hierarchical blocks.
77 This class assumes that there is at most a single block at the
78 head of the chain and a single block at the end of the chain.
79 Either head or tail may be None indicating a sink or source respectively.
81 If you needs something more elaborate than this, derive a new class from
84 def __init__(self, fg, head_block, tail_block):
86 @param fg: The flow graph that contains this hierarchical block.
88 @param head_block: the first block in the signal processing chain.
89 @type head_block: None or subclass of gr.block or gr.hier_block_base
90 @param tail_block: the last block in the signal processing chain.
91 @type tail_block: None or subclass of gr.block or gr.hier_block_base
93 hier_block_base.__init__(self, fg)
94 # FIXME add type checks here for easier debugging of misuse
95 self.head = head_block
96 self.tail = tail_block
98 def input_signature(self):
100 return self.head.input_signature()
102 return io_signature(0,0,0)
104 def output_signature(self):
106 return self.tail.output_signature()
108 return io_signature(0,0,0)
110 def resolve_input_port(self, port_number):
111 return ((self.head, port_number),)
113 def resolve_output_port(self, port_number):
114 return (self.tail, port_number)
117 class compose(hier_block):
119 Compose one or more blocks (primitive or hierarchical) into a new hierarchical block.
121 def __init__(self, fg, *blocks):
123 @param fg: flow graph
124 @type fg: gr.flow_graph
125 @param *blocks: list of blocks
126 @type *blocks: list of blocks
129 raise ValueError, ("compose requires at least one block; none provided.")
132 hier_block.__init__(self, fg, blocks[0], blocks[-1])