1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
% if not generate_options.startswith('hb'):
#!/usr/bin/env python2
% endif
# -*- coding: utf-8 -*-
<%def name="indent(code)">${ '\n '.join(str(code).splitlines()) }</%def>
"""
GNU Radio Python Flow Graph
Title: ${title}
% if flow_graph.get_option('author'):
Author: ${flow_graph.get_option('author')}
% endif
% if flow_graph.get_option('description'):
Description: ${flow_graph.get_option('description')}
% endif
Generated: ${ generated_time }
"""
% if generate_options == 'qt_gui':
from distutils.version import StrictVersion
if __name__ == '__main__':
import ctypes
import sys
if sys.platform.startswith('linux'):
try:
x11 = ctypes.cdll.LoadLibrary('libX11.so')
x11.XInitThreads()
except:
print "Warning: failed to XInitThreads()"
% endif
########################################################
##Create Imports
########################################################
% for imp in imports:
##${imp.replace(" # grc-generated hier_block", "")}
${imp}
% endfor
########################################################
##Create Class
## Write the class declaration for a top or hier block.
## The parameter names are the arguments to __init__.
## Setup the IO signature (hier block only).
########################################################
<%
class_name = flow_graph.get_option('id')
param_str = ', '.join(['self'] + ['%s=%s'%(param.name, param.templates.render('make')) for param in parameters])
%>\
% if generate_options == 'qt_gui':
from gnuradio import qtgui
class ${class_name}(gr.top_block, Qt.QWidget):
def __init__(${param_str}):
gr.top_block.__init__(self, "${title}")
Qt.QWidget.__init__(self)
self.setWindowTitle("${title}")
qtgui.util.check_set_qss()
try:
self.setWindowIcon(Qt.QIcon.fromTheme('gnuradio-grc'))
except:
pass
self.top_scroll_layout = Qt.QVBoxLayout()
self.setLayout(self.top_scroll_layout)
self.top_scroll = Qt.QScrollArea()
self.top_scroll.setFrameStyle(Qt.QFrame.NoFrame)
self.top_scroll_layout.addWidget(self.top_scroll)
self.top_scroll.setWidgetResizable(True)
self.top_widget = Qt.QWidget()
self.top_scroll.setWidget(self.top_widget)
self.top_layout = Qt.QVBoxLayout(self.top_widget)
self.top_grid_layout = Qt.QGridLayout()
self.top_layout.addLayout(self.top_grid_layout)
self.settings = Qt.QSettings("GNU Radio", "${class_name}")
try:
if StrictVersion(Qt.qVersion()) < StrictVersion("5.0.0"):
self.restoreGeometry(self.settings.value("geometry").toByteArray())
else:
self.restoreGeometry(self.settings.value("geometry"))
except:
pass
% elif generate_options == 'no_gui':
class ${class_name}(gr.top_block):
def __init__(${param_str}):
gr.top_block.__init__(self, "${title}")
% elif generate_options.startswith('hb'):
<% in_sigs = flow_graph.get_hier_block_stream_io('in') %>
<% out_sigs = flow_graph.get_hier_block_stream_io('out') %>
% if generate_options == 'hb_qt_gui':
class ${class_name}(gr.hier_block2, Qt.QWidget):
% else:
class ${class_name}(gr.hier_block2):
% endif
<%def name="make_io_sig(io_sigs)">
<% size_strs = ['%s*%s'%(io_sig['size'], io_sig['vlen']) for io_sig in io_sigs] %>
% if len(io_sigs) == 0:
gr.io_signature(0, 0, 0)\
#elif len(${io_sigs}) == 1
gr.io_signature(1, 1, ${size_strs[0]})
% else:
gr.io_signaturev(${len(io_sigs)}, ${len(io_sigs)}, [${', '.join(ize_strs)}])
% endif
</%def>
def __init__(${param_str}):
gr.hier_block2.__init__(
self, "${ title }",
${make_io_sig(in_sigs)},
${make_io_sig(out_sigs)},
)
% for pad in flow_graph.get_hier_block_message_io('in'):
self.message_port_register_hier_in("${ pad['label'] }")
% endfor
% for pad in flow_graph.get_hier_block_message_io('out'):
self.message_port_register_hier_out("${ pad['label'] }")
% endfor
% if generate_options == 'hb_qt_gui':
Qt.QWidget.__init__(self)
self.top_layout = Qt.QVBoxLayout()
self.top_grid_layout = Qt.QGridLayout()
self.top_layout.addLayout(self.top_grid_layout)
self.setLayout(self.top_layout)
% endif
% endif
% if flow_graph.get_option('thread_safe_setters'):
self._lock = threading.RLock()
% endif
########################################################
##Create Parameters
## Set the parameter to a property of self.
########################################################
% if parameters:
${'##################################################'}
# Parameters
${'##################################################'}
% endif
% for param in parameters:
${indent(param.get_var_make())}
% endfor
########################################################
##Create Variables
########################################################
% if variables:
${'##################################################'}
# Variables
${'##################################################'}
% endif
% for var in variables:
${indent(var.templates.render('var_make'))}
% endfor
% if blocks:
${'##################################################'}
# Blocks
${'##################################################'}
% endif
% for blk, blk_make in blocks:
${ indent(blk_make.strip('\n')) }
## % if 'alias' in blk.params and blk.params['alias'].get_evaluated():
## (self.${blk.name}).set_block_alias("${blk.params['alias'].get_evaluated()}")
## % endif
## % if 'affinity' in blk.params and blk.params['affinity'].get_evaluated():
## (self.${blk.name}).set_processor_affinity(${blk.params['affinity'].get_evaluated()})
## % endif
## % if len(blk.sources) > 0 and 'minoutbuf' in blk.params and int(blk.params['minoutbuf'].get_evaluated()) > 0:
## (self.${blk.name}).set_min_output_buffer(${blk.params['minoutbuf'].get_evaluated()})
## % endif
## % if len(blk.sources) > 0 and 'maxoutbuf' in blk.params and int(blk.params['maxoutbuf'].get_evaluated()) > 0:
## (self.${blk.name}).set_max_output_buffer(${blk.params['maxoutbuf'].get_evaluated()})
## % endif
% endfor
% if connections:
${'##################################################'}
# Connections
${'##################################################'}
% for connection in connections:
${ connection.rstrip() }
% endfor
% endif
########################################################
## QT sink close method reimplementation
########################################################
% if generate_options == 'qt_gui':
def closeEvent(self, event):
self.settings = Qt.QSettings("GNU Radio", "${class_name}")
self.settings.setValue("geometry", self.saveGeometry())
event.accept()
% if flow_graph.get_option('qt_qss_theme'):
def setStyleSheetFromFile(self, filename):
try:
if not os.path.exists(filename):
filename = os.path.join(
gr.prefix(), "share", "gnuradio", "themes", filename)
with open(filename) as ss:
self.setStyleSheet(ss.read())
except Exception as e:
print >> sys.stderr, e
% endif
% endif
##
##
##
## Create Callbacks
## Write a set method for this variable that calls the callbacks
########################################################
% for var in parameters + variables:
def get_${ var.name }(self):
return self.${ var.name }
def set_${ var.name }(self, ${ var.name }):
% if flow_graph.get_option('thread_safe_setters'):
with self._lock:
self.${ var.name } = ${ var.name }
% for callback in callbacks[var.name]:
${ indent(callback) }
% endfor
% else:
self.${ var.name } = ${ var.name }
% for callback in callbacks[var.name]:
${ indent(callback) }
% endfor
% endif
% endfor
########################################################
##Create Main
## For top block code, generate a main routine.
## Instantiate the top block and run as gui or cli.
########################################################
<%def name="make_default(type_, param)">
% if type_ == 'eng_float':
eng_notation.num_to_str(${param.templates.render('make')})
% else:
${param.templates.render('make')}
% endif
</%def>\
% if not generate_options.startswith('hb'):
<% params_eq_list = list() %>
% if parameters:
<% arg_parser_args = '' %>\
def argument_parser():
% if flow_graph.get_option('description'):
<%
arg_parser_args = 'description=description'
%>description = ${repr(flow_graph.get_option('description'))}
% endif
parser = ArgumentParser(${arg_parser_args})
% for param in parameters:
<%
switches = ['"--{}"'.format(param.name.replace('_', '-'))]
short_id = param.params['short_id'].get_value()
if short_id:
switches.insert(0, '"-{}"'.format(short_id))
type_ = param.params['type'].get_value()
if type_:
params_eq_list.append('%s=options.%s' % (param.name, param.name))
default = param.templates.render('make')
if type_ == 'eng_float':
default = eng_notation.num_to_str(default)
# FIXME:
if type_ == 'string':
type_ = 'str'
%>\
% if type_:
parser.add_argument(
${ ', '.join(switches) }, dest="${param.name}", type=${type_}, default=${ default },
help="Set ${param.params['label'].get_evaluated() or param.name} [default=%(default)r]")
% endif
% endfor
return parser
% endif
def main(top_block_cls=${class_name}, options=None):
% if parameters:
if options is None:
options = argument_parser().parse_args()
% endif
% if flow_graph.get_option('realtime_scheduling'):
if gr.enable_realtime_scheduling() != gr.RT_OK:
print "Error: failed to enable real-time scheduling."
% endif
% if generate_options == 'qt_gui':
if StrictVersion("4.5.0") <= StrictVersion(Qt.qVersion()) < StrictVersion("5.0.0"):
style = gr.prefs().get_string('qtgui', 'style', 'raster')
Qt.QApplication.setGraphicsSystem(style)
qapp = Qt.QApplication(sys.argv)
tb = top_block_cls(${ ', '.join(params_eq_list) })
% if flow_graph.get_option('run'):
tb.start(${flow_graph.get_option('max_nouts') or ''})
% endif
% if flow_graph.get_option('qt_qss_theme'):
tb.setStyleSheetFromFile(${ flow_graph.get_option('qt_qss_theme') })
% endif
tb.show()
def quitting():
tb.stop()
tb.wait()
qapp.aboutToQuit.connect(quitting)
% for m in monitors:
if 'en' in m.params:
if m.params['en'].get_value():
(tb.${m.name}).start()
else:
sys.stderr.write("Monitor '{0}' does not have an enable ('en') parameter.".format("tb.${m.name}"))
% endfor
qapp.exec_()
% elif generate_options == 'no_gui':
tb = top_block_cls(${ ', '.join(params_eq_list) })
% if flow_graph.get_option('run_options') == 'prompt':
tb.start(${ flow_graph.get_option('max_nouts') or '' })
% for m in monitors:
(tb.${m.name}).start()
% endfor
try:
raw_input('Press Enter to quit: ')
except EOFError:
pass
tb.stop()
% elif flow_graph.get_option('run_options') == 'run':
tb.start(${flow_graph.get_option('max_nouts') or ''})
% endif
% for m in monitors:
(tb.${m.name}).start()
% endfor
tb.wait()
% endif
if __name__ == '__main__':
main()
% endif
|