Imported Upstream version 3.2.2
[debian/gnuradio] / grc / grc_gnuradio / blks2 / selector.py
1 #
2 # Copyright 2008 Free Software Foundation, Inc.
3 #
4 # This file is part of GNU Radio
5 #
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)
9 # any later version.
10 #
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.
15 #
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.
20 #
21
22 from gnuradio import gr
23
24 class selector(gr.hier_block2):
25         """A hier2 block with N inputs and M outputs, where data is only forwarded through input n to output m."""
26         def __init__(self, item_size, num_inputs, num_outputs, input_index, output_index):
27                 """
28                 Selector constructor.
29                 @param item_size the size of the gr data stream in bytes
30                 @param num_inputs the number of inputs (integer)
31                 @param num_outputs the number of outputs (integer)
32                 @param input_index the index for the source data
33                 @param output_index the index for the destination data
34                 """
35                 gr.hier_block2.__init__(
36                         self, 'selector',
37                         gr.io_signature(num_inputs, num_inputs, item_size),
38                         gr.io_signature(num_outputs, num_outputs, item_size),
39                 )
40                 #terminator blocks for unused inputs and outputs
41                 self.input_terminators = [gr.null_sink(item_size) for i in range(num_inputs)]
42                 self.output_terminators = [gr.head(item_size, 0) for i in range(num_outputs)]
43                 self.copy = gr.kludge_copy(item_size)
44                 #connections
45                 for i in range(num_inputs): self.connect((self, i), self.input_terminators[i])
46                 for i in range(num_outputs): self.connect(gr.null_source(item_size), self.output_terminators[i], (self, i))
47                 self.item_size = item_size
48                 self.input_index = input_index
49                 self.output_index = output_index
50                 self.num_inputs = num_inputs
51                 self.num_outputs = num_outputs
52                 self._connect_current()
53
54         def _indexes_valid(self):
55                 """
56                 Are the input and output indexes within range of the number of inputs and outputs?
57                 @return true if input index and output index are in range
58                 """
59                 return self.input_index in range(self.num_inputs) and self.output_index in range(self.num_outputs)
60
61         def _connect_current(self):
62                 """If the input and output indexes are valid:
63                 disconnect the blocks at the input and output index from their terminators,
64                 and connect them to one another. Then connect the terminators to one another."""
65                 if self._indexes_valid():
66                         self.disconnect((self, self.input_index), self.input_terminators[self.input_index])
67                         self.disconnect(self.output_terminators[self.output_index], (self, self.output_index))
68                         self.connect((self, self.input_index), self.copy)
69                         self.connect(self.copy, (self, self.output_index))
70                         self.connect(self.output_terminators[self.output_index], self.input_terminators[self.input_index])
71
72         def _disconnect_current(self):
73                 """If the input and output indexes are valid:
74                 disconnect the blocks at the input and output index from one another,
75                 and the terminators at the input and output index from one another.
76                 Reconnect the blocks to the terminators."""
77                 if self._indexes_valid():
78                         self.disconnect((self, self.input_index), self.copy)
79                         self.disconnect(self.copy, (self, self.output_index))
80                         self.disconnect(self.output_terminators[self.output_index], self.input_terminators[self.input_index])
81                         self.connect((self, self.input_index), self.input_terminators[self.input_index])
82                         self.connect(self.output_terminators[self.output_index], (self, self.output_index))
83
84         def set_input_index(self, input_index):
85                 """
86                 Change the block to the new input index if the index changed.
87                 @param input_index the new input index
88                 """
89                 if self.input_index != input_index:
90                         self.lock()
91                         self._disconnect_current()
92                         self.input_index = input_index
93                         self._connect_current()
94                         self.unlock()
95
96         def set_output_index(self, output_index):
97                 """
98                 Change the block to the new output index if the index changed.
99                 @param output_index the new output index
100                 """
101                 if self.output_index != output_index:
102                         self.lock()
103                         self._disconnect_current()
104                         self.output_index = output_index
105                         self._connect_current()
106                         self.unlock()
107
108 class valve(selector):
109         """Wrapper for selector with 1 input and 1 output."""
110
111         def __init__(self, item_size, open):
112                 """
113                 Constructor for valve.
114                 @param item_size the size of the gr data stream in bytes
115                 @param open true if initial valve state is open
116                 """
117                 if open: output_index = -1
118                 else: output_index = 0
119                 selector.__init__(self, item_size, 1, 1, 0, output_index)
120
121         def set_open(self, open):
122                 """
123                 Callback to set open state.
124                 @param open true to set valve state to open
125                 """
126                 if open: output_index = -1
127                 else: output_index = 0
128                 self.set_output_index(output_index)