diff options
Diffstat (limited to 'grc/src')
-rw-r--r-- | grc/src/grc_gnuradio/blks2/Makefile.am | 5 | ||||
-rw-r--r-- | grc/src/grc_gnuradio/blks2/__init__.py | 1 | ||||
-rw-r--r-- | grc/src/grc_gnuradio/blks2/variable_sink.py | 64 |
3 files changed, 68 insertions, 2 deletions
diff --git a/grc/src/grc_gnuradio/blks2/Makefile.am b/grc/src/grc_gnuradio/blks2/Makefile.am index 396fc5f9d..7db1d5c8c 100644 --- a/grc/src/grc_gnuradio/blks2/Makefile.am +++ b/grc/src/grc_gnuradio/blks2/Makefile.am @@ -1,5 +1,5 @@ # -# Copyright 2008 Free Software Foundation, Inc. +# Copyright 2008, 2009 Free Software Foundation, Inc. # # This file is part of GNU Radio # @@ -28,4 +28,5 @@ ourpython_PYTHON = \ error_rate.py \ packet.py \ probe.py \ - selector.py + selector.py \ + variable_sink.py diff --git a/grc/src/grc_gnuradio/blks2/__init__.py b/grc/src/grc_gnuradio/blks2/__init__.py index 185230ab0..a28498a33 100644 --- a/grc/src/grc_gnuradio/blks2/__init__.py +++ b/grc/src/grc_gnuradio/blks2/__init__.py @@ -24,3 +24,4 @@ from packet import options, packet_encoder, packet_decoder, \ packet_demod_b, packet_demod_s, packet_demod_i, packet_demod_f, packet_demod_c from error_rate import error_rate from probe import probe_function, probe_avg_mag_sqrd_c, probe_avg_mag_sqrd_f, probe_density_b, probe_mpsk_snr_c +from variable_sink import variable_sink_b, variable_sink_s, variable_sink_i, variable_sink_f, variable_sink_c diff --git a/grc/src/grc_gnuradio/blks2/variable_sink.py b/grc/src/grc_gnuradio/blks2/variable_sink.py new file mode 100644 index 000000000..cad3b8b04 --- /dev/null +++ b/grc/src/grc_gnuradio/blks2/variable_sink.py @@ -0,0 +1,64 @@ +# +# Copyright 2009 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. +# + +from gnuradio import gr +import threading +import numpy + +class _variable_sink_base(gr.hier_block2, threading.Thread): + """ + The thread polls the message queue for values and writes to a callback. + """ + + def __init__(self, vlen, decim, callback): + self._vlen = vlen + self._callback = callback + self._item_size = self._size*self._vlen + #init hier block + gr.hier_block2.__init__( + self, 'variable_sink', + gr.io_signature(1, 1, self._item_size), + gr.io_signature(0, 0, 0), + ) + #create blocks + self._decimator = gr.keep_one_in_n(self._item_size, decim) + self._msgq = gr.msg_queue(2) + message_sink = gr.message_sink(self._item_size, self._msgq, False) + #connect + self.connect(self, self._decimator, message_sink) + #setup thread + threading.Thread.__init__(self) + self.setDaemon(True) + self.start() + + def set_decim(self, decim): self._decimator.set_n(decim) + + def run(self): + while True: #truncate to item size, convert to array, callback + msg = self._msgq.delete_head().to_string()[-self._item_size:] + arr = map(self._cast, numpy.fromstring(msg, self._numpy)) + self._callback(self._vlen > 1 and arr or arr[0]) + +class variable_sink_b(_variable_sink_base): _numpy, _size, _cast = numpy.int8, gr.sizeof_char, int +class variable_sink_s(_variable_sink_base): _numpy, _size, _cast = numpy.int16, gr.sizeof_short, int +class variable_sink_i(_variable_sink_base): _numpy, _size, _cast = numpy.int32, gr.sizeof_int, int +class variable_sink_f(_variable_sink_base): _numpy, _size, _cast = numpy.float32, gr.sizeof_float, float +class variable_sink_c(_variable_sink_base): _numpy, _size, _cast = numpy.complex64, gr.sizeof_gr_complex, complex |