Imported Upstream version 3.2.2
[debian/gnuradio] / grc / grc_gnuradio / blks2 / variable_sink.py
1 #
2 # Copyright 2009 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 import threading
24 import numpy
25
26 class _variable_sink_base(gr.hier_block2, threading.Thread):
27         """
28         The thread polls the message queue for values and writes to a callback.
29         """
30
31         def __init__(self, vlen, decim, callback):
32                 self._vlen = vlen
33                 self._callback = callback
34                 self._item_size = self._size*self._vlen
35                 #init hier block
36                 gr.hier_block2.__init__(
37                         self, 'variable_sink',
38                         gr.io_signature(1, 1, self._item_size),
39                         gr.io_signature(0, 0, 0),
40                 )
41                 #create blocks
42                 self._decimator = gr.keep_one_in_n(self._item_size, decim)
43                 self._msgq = gr.msg_queue(2)
44                 message_sink = gr.message_sink(self._item_size, self._msgq, False)
45                 #connect
46                 self.connect(self, self._decimator, message_sink)
47                 #setup thread
48                 threading.Thread.__init__(self)
49                 self.setDaemon(True)
50                 self.start() 
51
52         def set_decim(self, decim): self._decimator.set_n(decim)
53
54         def run(self):
55                 while True: #truncate to item size, convert to array, callback
56                         msg = self._msgq.delete_head().to_string()[-self._item_size:]
57                         arr = map(self._cast, numpy.fromstring(msg, self._numpy))
58                         self._callback(self._vlen > 1 and arr or arr[0])
59
60 class variable_sink_b(_variable_sink_base): _numpy, _size, _cast = numpy.int8, gr.sizeof_char, int
61 class variable_sink_s(_variable_sink_base): _numpy, _size, _cast = numpy.int16, gr.sizeof_short, int
62 class variable_sink_i(_variable_sink_base): _numpy, _size, _cast = numpy.int32, gr.sizeof_int, int
63 class variable_sink_f(_variable_sink_base): _numpy, _size, _cast = numpy.float32, gr.sizeof_float, float
64 class variable_sink_c(_variable_sink_base): _numpy, _size, _cast = numpy.complex64, gr.sizeof_gr_complex, complex