root / grc / python / flow_graph.tmpl @ fedf4fbc
History | View | Annotate | Download (7.6 kB)
| 1 | #!/usr/bin/env python |
|---|---|
| 2 | ######################################################## |
| 3 | ##Cheetah template - gnuradio_python |
| 4 | ## |
| 5 | ##@param imports the import statements |
| 6 | ##@param flow_graph the flow_graph |
| 7 | ##@param variables the variable blocks |
| 8 | ##@param parameters the paramater blocks |
| 9 | ##@param blocks the signal blocks |
| 10 | ##@param connections the connections |
| 11 | ##@param messages the msg type connections |
| 12 | ##@param generate_options the type of flow graph |
| 13 | ##@param var_id2cbs variable id map to callback strings |
| 14 | ######################################################## |
| 15 | #def indent($code) |
| 16 | #set $code = '\n\t\t'.join(str($code).splitlines()) |
| 17 | $code#slurp |
| 18 | #end def |
| 19 | #import time |
| 20 | #set $DIVIDER = '#'*50 |
| 21 | $DIVIDER |
| 22 | # Gnuradio Python Flow Graph |
| 23 | # Title: $title |
| 24 | #if $flow_graph.get_option('author')
|
| 25 | # Author: $flow_graph.get_option('author')
|
| 26 | #end if |
| 27 | #if $flow_graph.get_option('description')
|
| 28 | # Description: $flow_graph.get_option('description')
|
| 29 | #end if |
| 30 | # Generated: $time.ctime() |
| 31 | $DIVIDER |
| 32 | |
| 33 | ######################################################## |
| 34 | ##Create Imports |
| 35 | ######################################################## |
| 36 | #for $imp in $imports |
| 37 | $imp |
| 38 | #end for |
| 39 | |
| 40 | ######################################################## |
| 41 | ##Create Class |
| 42 | ## Write the class declaration for a top or hier block. |
| 43 | ## The parameter names are the arguments to __init__. |
| 44 | ## Determine the absolute icon path (wx gui only). |
| 45 | ## Setup the IO signature (hier block only). |
| 46 | ######################################################## |
| 47 | #set $class_name = $flow_graph.get_option('id')
|
| 48 | #set $param_str = ', '.join(['self'] + ['%s=%s'%(param.get_id(), param.get_make()) for param in $parameters]) |
| 49 | #if $generate_options == 'wx_gui' |
| 50 | #import gtk |
| 51 | #set $icon = gtk.IconTheme().lookup_icon('gnuradio-grc', 32, 0)
|
| 52 | class $(class_name)(grc_wxgui.top_block_gui): |
| 53 | |
| 54 | def __init__($param_str): |
| 55 | grc_wxgui.top_block_gui.__init__(self, title="$title") |
| 56 | #if $icon |
| 57 | _icon_path = "$icon.get_filename()" |
| 58 | self.SetIcon(wx.Icon(_icon_path, wx.BITMAP_TYPE_ANY)) |
| 59 | #end if |
| 60 | #elif $generate_options == 'qt_gui' |
| 61 | class $(class_name)(gr.top_block, Qt.QWidget): |
| 62 | |
| 63 | def __init__($param_str): |
| 64 | gr.top_block.__init__(self, "$title") |
| 65 | Qt.QWidget.__init__(self) |
| 66 | self.top_layout = Qt.QBoxLayout(Qt.QBoxLayout.TopToBottom, self) |
| 67 | self.top_grid_layout = Qt.QGridLayout() |
| 68 | self.top_layout.addLayout(self.top_grid_layout) |
| 69 | #elif $generate_options == 'no_gui' |
| 70 | class $(class_name)(gr.top_block): |
| 71 | |
| 72 | def __init__($param_str): |
| 73 | gr.top_block.__init__(self, "$title") |
| 74 | #elif $generate_options == 'hb' |
| 75 | #set $in_sigs = $flow_graph.get_input_signaturev() |
| 76 | #set $out_sigs = $flow_graph.get_output_signaturev() |
| 77 | class $(class_name)(gr.hier_block2): |
| 78 | #def make_io_sig($io_sigs) |
| 79 | #set $size_strs = ['%s*%s'%(io_sig['size'], io_sig['vlen']) for io_sig in $io_sigs] |
| 80 | #if len($io_sigs) == 0 |
| 81 | gr.io_signature(0, 0, 0)#slurp |
| 82 | #elif len($io_sigs) == 1 |
| 83 | gr.io_signature(1, 1, $size_strs[0])#slurp |
| 84 | #else |
| 85 | gr.io_signaturev($(len($io_sigs)), $(len($io_sigs)), [$(', '.join($size_strs))])#slurp
|
| 86 | #end if |
| 87 | #end def |
| 88 | |
| 89 | def __init__($param_str): |
| 90 | gr.hier_block2.__init__( |
| 91 | self, "$title", |
| 92 | $make_io_sig($in_sigs), |
| 93 | $make_io_sig($out_sigs), |
| 94 | ) |
| 95 | #end if |
| 96 | ######################################################## |
| 97 | ##Create Parameters |
| 98 | ## Set the parameter to a property of self. |
| 99 | ######################################################## |
| 100 | #if $parameters |
| 101 | |
| 102 | $DIVIDER |
| 103 | # Parameters |
| 104 | $DIVIDER |
| 105 | #end if |
| 106 | #for $param in $parameters |
| 107 | $indent($param.get_var_make()) |
| 108 | #end for |
| 109 | ######################################################## |
| 110 | ##Create Variables |
| 111 | ######################################################## |
| 112 | #if $variables |
| 113 | |
| 114 | $DIVIDER |
| 115 | # Variables |
| 116 | $DIVIDER |
| 117 | #end if |
| 118 | #for $var in $variables |
| 119 | $indent($var.get_var_make()) |
| 120 | #end for |
| 121 | ######################################################## |
| 122 | ##Create Message Queues |
| 123 | ######################################################## |
| 124 | #if $messages |
| 125 | |
| 126 | $DIVIDER |
| 127 | # Message Queues |
| 128 | $DIVIDER |
| 129 | #end if |
| 130 | #for $msg in $messages |
| 131 | $(msg.get_source().get_parent().get_id())_msgq_out = $(msg.get_sink().get_parent().get_id())_msgq_in = gr.msg_queue(2) |
| 132 | #end for |
| 133 | ######################################################## |
| 134 | ##Create Blocks |
| 135 | ######################################################## |
| 136 | #if $blocks |
| 137 | |
| 138 | $DIVIDER |
| 139 | # Blocks |
| 140 | $DIVIDER |
| 141 | #end if |
| 142 | #for $blk in filter(lambda b: b.get_make(), $blocks) |
| 143 | #if $blk in $variables |
| 144 | $indent($blk.get_make()) |
| 145 | #else |
| 146 | self.$blk.get_id() = $indent($blk.get_make()) |
| 147 | #end if |
| 148 | #end for |
| 149 | ######################################################## |
| 150 | ##Create Connections |
| 151 | ## The port name should be the id of the parent block. |
| 152 | ## However, port names for IO pads should be self. |
| 153 | ######################################################## |
| 154 | #def make_port_name($port) |
| 155 | #if $port.get_parent().get_key().startswith('pad_')
|
| 156 | self#slurp |
| 157 | #else |
| 158 | self.$port.get_parent().get_id()#slurp |
| 159 | #end if |
| 160 | #end def |
| 161 | #if $connections |
| 162 | |
| 163 | $DIVIDER |
| 164 | # Connections |
| 165 | $DIVIDER |
| 166 | #end if |
| 167 | #for $con in $connections |
| 168 | #set $source = $con.get_source() |
| 169 | #set $sink = $con.get_sink() |
| 170 | ##resolve virtual sources to the actual sources |
| 171 | #if $source.get_parent().is_virtual_source() |
| 172 | #set $source = $source.resolve_virtual_source() |
| 173 | #end if |
| 174 | ##do not generate connections with virtual sinks |
| 175 | #if not $sink.get_parent().is_virtual_sink() |
| 176 | self.connect(($make_port_name($source), $source.get_key()), ($make_port_name($sink), $sink.get_key())) |
| 177 | #end if |
| 178 | #end for |
| 179 | |
| 180 | ######################################################## |
| 181 | ##Create Callbacks |
| 182 | ## Write a set method for this variable that calls the callbacks |
| 183 | ######################################################## |
| 184 | #for $var in $parameters + $variables |
| 185 | #set $id = $var.get_id() |
| 186 | def get_$(id)(self): |
| 187 | return self.$id |
| 188 | |
| 189 | def set_$(id)(self, $id): |
| 190 | self.$id = $id |
| 191 | #for $callback in $var_id2cbs[$id] |
| 192 | $indent($callback) |
| 193 | #end for |
| 194 | |
| 195 | #end for |
| 196 | ######################################################## |
| 197 | ##Create Main |
| 198 | ## For top block code, generate a main routine. |
| 199 | ## Instantiate the top block and run as gui or cli. |
| 200 | ######################################################## |
| 201 | #def make_default($type, $param) |
| 202 | #if $type == 'eng_float' |
| 203 | eng_notation.num_to_str($param.get_make())#slurp |
| 204 | #else |
| 205 | $param.get_make()#slurp |
| 206 | #end if |
| 207 | #end def |
| 208 | #def make_short_id($param) |
| 209 | #set $short_id = $param.get_param('short_id').get_evaluated()
|
| 210 | #if $short_id |
| 211 | #set $short_id = '-' + $short_id |
| 212 | #end if |
| 213 | $short_id#slurp |
| 214 | #end def |
| 215 | #if $generate_options != 'hb' |
| 216 | if __name__ == '__main__': |
| 217 | parser = OptionParser(option_class=eng_option, usage="%prog: [options]") |
| 218 | #set $params_eq_list = list() |
| 219 | #for $param in $parameters |
| 220 | #set $type = $param.get_param('type').get_value()
|
| 221 | #if $type |
| 222 | #silent $params_eq_list.append('%s=options.%s'%($param.get_id(), $param.get_id()))
|
| 223 | parser.add_option("$make_short_id($param)", "--$param.get_id().replace('_', '-')", dest="$param.get_id()", type="$type", default=$make_default($type, $param),
|
| 224 | help="Set $($param.get_param('label').get_evaluated() or $param.get_id()) [default=%default]")
|
| 225 | #end if |
| 226 | #end for |
| 227 | (options, args) = parser.parse_args() |
| 228 | #if $flow_graph.get_option('realtime_scheduling')
|
| 229 | if gr.enable_realtime_scheduling() != gr.RT_OK: |
| 230 | print "Error: failed to enable realtime scheduling." |
| 231 | #end if |
| 232 | #if $generate_options == 'wx_gui' |
| 233 | tb = $(class_name)($(', '.join($params_eq_list)))
|
| 234 | tb.Run($flow_graph.get_option('run'))
|
| 235 | #elif $generate_options == 'qt_gui' |
| 236 | qapp = Qt.QApplication(sys.argv) |
| 237 | tb = $(class_name)($(', '.join($params_eq_list)))
|
| 238 | #if $flow_graph.get_option('run')
|
| 239 | tb.start() |
| 240 | #end if |
| 241 | tb.show() |
| 242 | qapp.exec_() |
| 243 | #elif $generate_options == 'no_gui' |
| 244 | #set $run_options = $flow_graph.get_option('run_options')
|
| 245 | #if $run_options == 'prompt' |
| 246 | tb = $(class_name)($(', '.join($params_eq_list)))
|
| 247 | tb.start() |
| 248 | raw_input('Press Enter to quit: ')
|
| 249 | tb.stop() |
| 250 | #elif $run_options == 'run' |
| 251 | tb.run() |
| 252 | #end if |
| 253 | #end if |
| 254 | #end if |
| 255 |