added include <cstdio> statements in several files to make it compatible with g+...
[debian/gnuradio] / grc / data / platforms / 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 controls the variables with gui controls
9 ##@param parameters the paramater blocks
10 ##@param blocks the signal blocks
11 ##@param connections the connections
12 ##@param generate_options the type of flow graph
13 ##@param var_id2expr variable id map to expression
14 ##@param var_id2deps variable id map to direct dependencies
15 ##@param var_id2cbs variable id map to callback strings
16 ########################################################
17 #import time
18 #set $DIVIDER = '#'*50
19 $DIVIDER
20 # Gnuradio Python Flow Graph
21 # Title: $flow_graph.get_option('title')
22 # Author: $flow_graph.get_option('author')
23 # Description: $flow_graph.get_option('description')
24 # Generated: $time.ctime()
25 $DIVIDER
26
27 ########################################################
28 ##Create Imports
29 ########################################################
30 #for $imp in $imports
31 $imp
32 #end for
33
34 ########################################################
35 ##Create Class
36 ##      Write the class declaration for a top or hier block.
37 ##      The parameter names are the arguments to __init__.
38 ##      Determine the absolute icon path (wx gui only).
39 ##      Setup the IO signature (hier block only).
40 ########################################################
41 #set $class_name = $flow_graph.get_option('id')
42 #if $generate_options == 'wx_gui'
43         #import gtk
44         #set $icon = gtk.IconTheme().lookup_icon('gnuradio-grc', 32, 0)
45 class $(class_name)(grc_wxgui.top_block_gui):
46
47         def __init__(self, options):
48                 grc_wxgui.top_block_gui.__init__(
49                         self,
50                         title="$flow_graph.get_parent().get_name() - Executing: $flow_graph.get_option('title')",
51         #if $icon
52                         icon="$icon.get_filename()",
53         #end if
54                 )
55 #elif $generate_options == 'no_gui'
56 class $(class_name)(gr.top_block):
57
58         def __init__(self, options):
59                 gr.top_block.__init__(self, "$flow_graph.get_option('title')")
60 #elif $generate_options == 'hb'
61         #set $in_sig = $flow_graph.get_input_signature()
62         #set $out_sig = $flow_graph.get_output_signature()
63         #set $param_str = ', '.join(['self'] + ['%s=%s'%(param.get_id(), param.get_make()) for param in $parameters])
64 class $(class_name)(gr.hier_block2):
65
66         def __init__($param_str):
67                 gr.hier_block2.__init__(
68                         self,
69                         "$flow_graph.get_option('title')",
70                         gr.io_signature($in_sig.nports, $in_sig.nports, $in_sig.size*$in_sig.vlen),
71                         gr.io_signature($out_sig.nports, $out_sig.nports, $out_sig.size*$out_sig.vlen),
72                 )
73 #end if
74 ########################################################
75 ##Create Parameters
76 ##      Set the parameter to a property of self.
77 ########################################################
78 #if $parameters
79
80                 $DIVIDER
81                 # Parameters
82                 $DIVIDER
83 #end if
84 #for $param in $parameters
85         #if $generate_options != 'hb'
86                 self.$param.get_id() = $param.get_id() = options.$param.get_id()
87         #else
88                 self.$param.get_id() = $param.get_id()
89         #end if
90 #end for
91 ########################################################
92 ##Create Variables
93 ##      Set the variable to a property of self.
94 ##      Write the first line of the variable make.
95 ########################################################
96 #if $variables
97
98                 $DIVIDER
99                 # Variables
100                 $DIVIDER
101 #end if
102 #for $var in $variables
103         #set $code = $var.get_make().splitlines()[0]
104                 self.$var.get_id() = $var.get_id() = $code
105 #end for
106 ########################################################
107 ##Create Controls
108 ##      Write the variable make (excluding first line).
109 ##      Indent each line with 2 tabs.
110 ########################################################
111 #if $controls
112
113                 $DIVIDER
114                 # Controls
115                 $DIVIDER
116 #end if
117 #for $ctrl in $controls
118         #set $code = '\n\t\t'.join($ctrl.get_make().splitlines()[1:])
119                 $code
120 #end for
121 ########################################################
122 ##Create Blocks
123 ##      Write the block make, and indent with 2 tabs.
124 ########################################################
125 #if $blocks
126
127                 $DIVIDER
128                 # Blocks
129                 $DIVIDER
130 #end if
131 #for $blk in filter(lambda b: b.get_make(), $blocks)
132         #set $code = '\n\t\t'.join($blk.get_make().splitlines())
133                 self.$blk.get_id() = $code
134 #end for
135 ########################################################
136 ##Create Connections
137 ##      The port name should be the id of the parent block.
138 ##      However, port names for IO pads should be self.
139 ########################################################
140 #if $connections
141
142                 $DIVIDER
143                 # Connections
144                 $DIVIDER
145 #end if
146 #for $con in $connections
147         #set $source = $con.get_source()
148         #set $sink = $con.get_sink()
149         #if $source.get_parent().get_key() == 'pad_source'
150                 #set $source_name = 'self'
151         #else
152                 #set $source_name = 'self.' + $source.get_parent().get_id()
153         #end if
154         #if $sink.get_parent().get_key() == 'pad_sink'
155                 #set $sink_name = 'self'
156         #else
157                 #set $sink_name = 'self.' + $sink.get_parent().get_id()
158         #end if
159                 self.connect(($source_name, $source.get_key()), ($sink_name, $sink.get_key()))
160 #end for
161
162 ########################################################
163 ##Create Callbacks
164 ##      Write a set method for this variable that calls the callbacks
165 ##      and sets the direct variable dependencies.
166 ########################################################
167 #for $var in $parameters + $variables
168         #set $id = $var.get_id()
169         def set_$(id)(self, $id):
170                 self.$id = $id
171         #for $dep in $var_id2deps[$id]
172                 self.set_$(dep)($var_id2expr[$dep])
173         #end for
174         #for $callback in $var_id2cbs[$id]
175                 self.$callback
176         #end for
177
178 #end for
179 ########################################################
180 ##Create Main
181 ##      For top block code, generate a main routine.
182 ##      Instantiate the top block and run as gui or cli.
183 ########################################################
184 #if $generate_options != 'hb'
185 if __name__ == '__main__':
186         parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
187         #for $param in $parameters
188                 #set $type = $param.get_param('type').get_value()
189                 #if $type
190         parser.add_option("--$param.get_id()", dest="$param.get_id()", type="$type", default=$param.get_make())
191                 #end if
192         #end for
193         (options, args) = parser.parse_args()
194         #if $flow_graph.get_option('realtime_scheduling')
195         if gr.enable_realtime_scheduling() != gr.RT_OK:
196                 print "Error: failed to enable realtime scheduling."
197         #end if
198         tb = $(class_name)(options)
199         #if $generate_options == 'wx_gui'
200         tb.Run()
201         #elif $generate_options == 'no_gui'
202         tb.start()
203         raw_input('Press Enter to quit: ')
204         tb.stop()
205         #end if
206 #end if
207