2 # Copyright 2009 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 import gr
26 class _variable_sink_base(gr.hier_block2, threading.Thread):
28 The thread polls the message queue for values and writes to a callback.
31 def __init__(self, vlen, decim, callback):
33 self._callback = callback
34 self._item_size = self._size*self._vlen
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),
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)
46 self.connect(self, self._decimator, message_sink)
48 threading.Thread.__init__(self)
52 def set_decim(self, decim): self._decimator.set_n(decim)
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])
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