1 # Copyright 2008 Free Software Foundation, Inc.
3 # This file is part of GNU Radio
5 # GNU Radio is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3, or (at your option)
10 # GNU Radio is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with GNU Radio; see the file COPYING. If not, write to
17 # the Free Software Foundation, Inc., 51 Franklin Street,
18 # Boston, MA 02110-1301, USA.
21 from gnuradio import gr
22 import gnuradio.gr.gr_threading as _threading
25 #######################################################################################
27 #######################################################################################
28 class queue_sink_thread(_threading.Thread):
30 Read samples from the queue sink and execute the callback.
33 def __init__(self, queue_sink, callback):
35 Queue sink thread contructor.
36 @param queue_sink the queue to pop messages from
37 @param callback the function of one argument
39 self._queue_sink = queue_sink
40 self._callback = callback
41 _threading.Thread.__init__(self)
43 self.keep_running = True
47 while self.keep_running:
48 self._callback(self._queue_sink.pop())
50 #######################################################################################
52 #######################################################################################
53 class _queue_sink_base(gr.hier_block2):
55 Queue sink base, a queue sink for any size queue.
56 Easy read access to a gnuradio data stream from python.
57 Call pop to read a sample from a gnuradio data stream.
58 Samples are cast as python data types, complex, float, or int.
61 def __init__(self, vlen=1):
63 Queue sink base contructor.
64 @param vlen the vector length
68 gr.hier_block2.__init__(
71 gr.io_signature(1, 1, self._item_size*self._vlen), # Input signature
72 gr.io_signature(0, 0, 0) # Output signature
75 self._msgq = gr.msg_queue(1)
76 message_sink = gr.message_sink(self._item_size*self._vlen, self._msgq, False) #False -> blocking
78 self.connect(self, message_sink)
83 Pop a new sample off the front of the queue.
86 while len(self.arr) < self._item_size*self._vlen:
87 msg = self._msgq.delete_head()
88 self.arr = self.arr + msg.to_string()
89 sample = self.arr[:self._item_size*self._vlen]
90 self.arr = self.arr[self._item_size*self._vlen:]
91 sample = map(self._cast, numpy.fromstring(sample, self._numpy))
92 if self._vlen == 1: return sample[0]
95 class queue_sink_c(_queue_sink_base):
96 _item_size = gr.sizeof_gr_complex
97 _numpy = numpy.complex64
98 def _cast(self, arg): return complex(arg.real, arg.imag)
100 class queue_sink_f(_queue_sink_base):
101 _item_size = gr.sizeof_float
102 _numpy = numpy.float32
105 class queue_sink_i(_queue_sink_base):
106 _item_size = gr.sizeof_int
110 class queue_sink_s(_queue_sink_base):
111 _item_size = gr.sizeof_short
115 class queue_sink_b(_queue_sink_base):
116 _item_size = gr.sizeof_char
120 #######################################################################################
122 #######################################################################################
123 class _queue_source_base(gr.hier_block2):
125 Queue source base, a queue source for any size queue.
126 Easy write access to a gnuradio data stream from python.
127 Call push to to write a sample into the gnuradio data stream.
130 def __init__(self, vlen=1):
132 Queue source base contructor.
133 @param vlen the vector length
137 gr.hier_block2.__init__(
140 gr.io_signature(0, 0, 0), # Input signature
141 gr.io_signature(1, 1, self._item_size*self._vlen) # Output signature
143 #create message source
144 message_source = gr.message_source(self._item_size*self._vlen, 1)
145 self._msgq = message_source.msgq()
147 self.connect(message_source, self)
149 def push(self, item):
151 Push an item into the back of the queue.
154 if self._vlen == 1: item = [item]
155 arr = numpy.array(item, self._numpy)
156 msg = gr.message_from_string(arr.tostring(), 0, self._item_size, self._vlen)
157 self._msgq.insert_tail(msg)
159 class queue_source_c(_queue_source_base):
160 _item_size = gr.sizeof_gr_complex
161 _numpy = numpy.complex64
163 class queue_source_f(_queue_source_base):
164 _item_size = gr.sizeof_float
165 _numpy = numpy.float32
167 class queue_source_i(_queue_source_base):
168 _item_size = gr.sizeof_int
171 class queue_source_s(_queue_source_base):
172 _item_size = gr.sizeof_short
175 class queue_source_b(_queue_source_base):
176 _item_size = gr.sizeof_char