add template based GRC block generation script

This commit is contained in:
Dimitri Stolnikov 2012-04-26 19:07:27 +02:00
parent c954d4a410
commit afac8e8c42
3 changed files with 243 additions and 2 deletions

View File

@ -17,8 +17,35 @@
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#install(FILES
# osmosdr_source_c.xml
## osmosdr_sink_c.xml
# DESTINATION share/gnuradio/grc/blocks
#)
########################################################################
# Rules for generating the source and sink xml wrappers
########################################################################
include(GrPython)
macro(GEN_BLOCK_XML _generator _xml_block)
set(generator ${CMAKE_CURRENT_SOURCE_DIR}/${_generator})
set(xml_block ${CMAKE_CURRENT_BINARY_DIR}/${_xml_block})
list(APPEND xml_blocks ${xml_block})
add_custom_command(
DEPENDS ${generator} OUTPUT ${xml_block}
COMMAND ${PYTHON_EXECUTABLE} ${generator} ${xml_block}
)
endmacro(GEN_BLOCK_XML)
GEN_BLOCK_XML(gen_osmosdr_blocks.py osmosdr_source_c.xml)
#GEN_BLOCK_XML(gen_osmosdr_blocks.py osmosdr_sink_c.xml)
add_custom_target(osmosdr_grc_xml_blocks ALL DEPENDS ${xml_blocks})
install(FILES
osmosdr_source_c.xml
# osmosdr_sink_c.xml
${xml_blocks}
osmosdr_block_tree.xml
# DESTINATION ${GRC_BLOCKS_DIR}
DESTINATION share/gnuradio/grc/blocks
)

201
grc/gen_osmosdr_blocks.py Normal file
View File

@ -0,0 +1,201 @@
"""
Copyright 2012 Free Software Foundation, Inc.
This file is part of GNU Radio
GNU Radio Companion is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
GNU Radio Companion is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
"""
MAIN_TMPL = """\
<?xml version="1.0"?>
<block>
<name>OsmoSDR $sourk.title()</name>
<key>osmosdr_$(sourk)_c</key>
<throttle>1</throttle>
<import>import osmosdr</import>
<make>osmosdr.$(sourk)_c( args='nchan=' + str(\$nchan) + ' ' + \$args )
self.\$(id).set_sample_rate(\$sample_rate)
#for $n in range($max_nchan)
\#if \$nchan() > $n
self.\$(id).set_center_freq(\$freq$(n), $n)
self.\$(id).set_freq_corr(\$corr$(n), $n)
self.\$(id).set_gain(\$gain$(n), $n)
\#if \$ant$(n)()
self.\$(id).set_antenna(\$ant$(n), $n)
\#end if
\#end if
#end for
</make>
<callback>set_sample_rate(\$sample_rate)</callback>
#for $n in range($max_nchan)
<callback>set_center_freq(\$freq$(n), $n)</callback>
<callback>set_freq_corr(\$corr$(n), $n)</callback>
<callback>set_gain(\$gain$(n), $n)</callback>
<callback>set_antenna(\$ant$(n), $n)</callback>
#end for
<param>
<name>$(dir.title())put Type</name>
<key>type</key>
<type>enum</type>
<option>
<name>Complex float32</name>
<key>fc32</key>
<opt>type:fc32</opt>
</option>
</param>
<param>
<name>Device Arguments</name>
<key>args</key>
<value></value>
<type>string</type>
<hide>
\#if \$args()
none
\#else
part
\#end if
</hide>
</param>
<param>
<name>Num Channels</name>
<key>nchan</key>
<value>1</value>
<type>int</type>
#for $n in range(1, $max_nchan+1)
<option>
<name>$(n)</name>
<key>$n</key>
</option>
#end for
</param>
<param>
<name>Sample Rate (sps)</name>
<key>sample_rate</key>
<value>samp_rate</value>
<type>real</type>
</param>
$params
<check>$max_nchan >= \$nchan</check>
<check>\$nchan > 0</check>
<$sourk>
<name>$dir</name>
<type>\$type.type</type>
<nports>\$nchan</nports>
</$sourk>
<doc>
The OsmoSDR $sourk.title() block:
Output Type:
This parameter controls the data type of the stream in gnuradio.
Device Arguments:
The device argument is a delimited string used to locate devices on your system.
Use the device id or name (if applicable) to specify a specific device or list
of devices. If left blank, the first device found will be used.
Examples:
fcd=0 fcd=1 fcd=2 ...
rtl=0 rtl=1 rtl=2 ...
uhd=0|name,mcr=52e6,chans=2 ...
osmo=0|name,mcr=64e6,chans=5,port=/dev/ttyUSB0 ...
Num Channels:
Selects the total number of channels in this multi-device configuration.
Sample Rate:
The sample rate is the number of samples per second output by this block.
Frequency:
The center frequency is the overall frequency of the RF chain.
Freq. Corr.:
The frequency correction factor in parts per million (ppm). Leave 0 if unknown.
Gain:
Overall gain of the device's signal path.
Antenna:
For devices with only one antenna, this may be left blank.
Otherwise, the user should specify one of the possible antenna choices.
See the OsmoSDR manual for more detailed documentation:
http://sdr.osmocom.org/trac/
</doc>
</block>
"""
PARAMS_TMPL = """
<param>
<name>Ch$(n): Frequency (Hz)</name>
<key>freq$(n)</key>
<value>0</value>
<type>real</type>
<hide>\#if \$nchan() > $n then 'none' else 'all'#</hide>
</param>
<param>
<name>Ch$(n): Freq. Corr. (ppm)</name>
<key>corr$(n)</key>
<value>0</value>
<type>real</type>
<hide>\#if \$nchan() > $n then 'none' else 'all'#</hide>
</param>
<param>
<name>Ch$(n): Gain (dB)</name>
<key>gain$(n)</key>
<value>0</value>
<type>real</type>
<hide>\#if \$nchan() > $n then 'none' else 'all'#</hide>
</param>
<param>
<name>Ch$(n): Antenna</name>
<key>ant$(n)</key>
<value></value>
<type>string</type>
<hide>
\#if not \$nchan() > $n
all
\#elif \$ant$(n)()
none
\#else
part
\#end if
</hide>
</param>
"""
def parse_tmpl(_tmpl, **kwargs):
from Cheetah import Template
return str(Template.Template(_tmpl, kwargs))
max_num_channels = 5
if __name__ == '__main__':
import sys
for file in sys.argv[1:]:
if file.endswith ('source_c.xml'):
sourk = 'source'
dir = 'out'
elif file.endswith ('sink_c.xml'):
sourk = 'sink'
dir = 'in'
else: raise Exception, 'is %s a source or sink?'%file
params = ''.join([parse_tmpl(PARAMS_TMPL, n=n) for n in range(max_num_channels)])
open(file, 'w').write(parse_tmpl(MAIN_TMPL,
max_nchan=max_num_channels,
params=params,
sourk=sourk,
dir=dir,
))

View File

@ -0,0 +1,13 @@
<?xml version="1.0"?>
<!--
###################################################
##Block Tree for OsmoSDR blocks.
###################################################
-->
<cat>
<name></name> <!-- Blank for Root Name -->
<cat>
<name>OsmoSDR</name>
<block>osmosdr_source_c</block>
</cat>
</cat>