Statistics
| Branch: | Tag: | Revision:

root / grc / data / platforms / python / flow_graph.tmpl @ d01692fa

History | View | Annotate | Download (5.8 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 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: %s'%$flow_graph.get_option('title'))
22
$('# Author: %s'%$flow_graph.get_option('author'))
23
$('# Description: %s'%$flow_graph.get_option('description'))
24
$('# Generated: %s'%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
#set $param_str = ', '.join(['self'] + ['%s=%s'%(param.get_id(), param.get_make()) for param in $parameters])
43
#if $generate_options == 'wx_gui'
44
	#import gtk
45
	#set $icon = gtk.IconTheme().lookup_icon('gnuradio-grc', 32, 0)
46
class $(class_name)(grc_wxgui.top_block_gui):
47
48
	def __init__($param_str):
49
		grc_wxgui.top_block_gui.__init__(
50
			self,
51
			title="$flow_graph.get_parent().get_name() - Executing: $flow_graph.get_option('title')",
52
	#if $icon
53
			icon="$icon.get_filename()",
54
	#end if
55
		)
56
#elif $generate_options == 'no_gui'
57
class $(class_name)(gr.top_block):
58
59
	def __init__($param_str):
60
		gr.top_block.__init__(self, "$flow_graph.get_option('title')")
61
#elif $generate_options == 'hb'
62
	#set $in_sig = $flow_graph.get_input_signature()
63
	#set $out_sig = $flow_graph.get_output_signature()
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
		self.$param.get_id() = $param.get_id()
86
#end for
87
########################################################
88
##Create Variables
89
##	Set the variable to a property of self.
90
##	Write the first line of the variable make.
91
########################################################
92
#if $variables
93
94
		$DIVIDER
95
		# Variables
96
		$DIVIDER
97
#end if
98
#for $var in $variables
99
	#set $code = $var.get_make().splitlines()[0]
100
		self.$var.get_id() = $var.get_id() = $code
101
#end for
102
########################################################
103
##Create Controls
104
##	Write the variable make (excluding first line).
105
##	Indent each line with 2 tabs.
106
########################################################
107
#if $controls
108
109
		$DIVIDER
110
		# Controls
111
		$DIVIDER
112
#end if
113
#for $ctrl in $controls
114
	#set $code = '\n\t\t'.join($ctrl.get_make().splitlines()[1:])
115
		$code
116
#end for
117
########################################################
118
##Create Blocks
119
##	Write the block make, and indent with 2 tabs.
120
########################################################
121
#if $blocks
122
123
		$DIVIDER
124
		# Blocks
125
		$DIVIDER
126
#end if
127
#for $blk in filter(lambda b: b.get_make(), $blocks)
128
	#set $code = '\n\t\t'.join($blk.get_make().splitlines())
129
		$("self.%s = %s"%($blk.get_id(), $code))
130
#end for
131
########################################################
132
##Create Connections
133
##	The port name should be the id of the parent block.
134
##	However, port names for IO pads should be self.
135
########################################################
136
#if $connections
137
138
		$DIVIDER
139
		# Connections
140
		$DIVIDER
141
#end if
142
#for $con in $connections
143
	#set $source = $con.get_source()
144
	#set $sink = $con.get_sink()
145
	#if $source.get_parent().get_key() == 'pad_source'
146
		#set $source_name = 'self'
147
	#else
148
		#set $source_name = 'self.' + $source.get_parent().get_id()
149
	#end if
150
	#if $sink.get_parent().get_key() == 'pad_sink'
151
		#set $sink_name = 'self'
152
	#else
153
		#set $sink_name = 'self.' + $sink.get_parent().get_id()
154
	#end if
155
		$("self.connect((%s, %s), (%s, %s))"%(
156
				$source_name,
157
				$source.get_key(),
158
				$sink_name,
159
				$sink.get_key(),
160
			)
161
		)
162
#end for
163
164
########################################################
165
##Create Callbacks
166
##	Write a set method for this variable that calls the callbacks
167
##	and sets the direct variable dependencies.
168
########################################################
169
#for $var in $parameters + $variables
170
	#set $id = $var.get_id()
171
	def set_$(id)(self, $id):
172
		self.$id = $id
173
	#for $dep in $var_id2deps[$id]
174
		self.set_$(dep)($var_id2expr[$dep])
175
	#end for
176
	#for $callback in $var_id2cbs[$id]
177
		self.$callback
178
	#end for
179
180
#end for
181
########################################################
182
##Create Main
183
##	For top block code, generate a main routine.
184
##	Instantiate the top block and run as gui or cli.
185
########################################################
186
#if $generate_options != 'hb'
187
if __name__ == '__main__':
188
	tb = $(class_name)()
189
	#if $generate_options == 'wx_gui'
190
	tb.Run()
191
	#elif $generate_options == 'no_gui'
192
	tb.start()
193
	raw_input('Press Enter to quit: ')
194
	tb.stop()
195
	#end if
196
#end if
197