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