From cf1c3ab117ee76cac3c574331c42173e0444aedd Mon Sep 17 00:00:00 2001 From: Dimitri Stolnikov Date: Sat, 5 May 2012 20:43:34 +0200 Subject: [PATCH] pick first device if no device arguments were specified --- apps/osmosdr_source.py | 10 ++--- lib/fcd/fcd_source.cc | 72 +++++++++++++++++++++--------------- lib/osmosdr_source_c_impl.cc | 56 +++++++++++++++++++++++++--- lib/rtl/rtl_source_c.cc | 10 +++++ lib/rtl/rtl_source_c.h | 2 + lib/uhd/uhd_source_c.cc | 13 ++++++- 6 files changed, 119 insertions(+), 44 deletions(-) diff --git a/apps/osmosdr_source.py b/apps/osmosdr_source.py index 832d645..952f7f9 100755 --- a/apps/osmosdr_source.py +++ b/apps/osmosdr_source.py @@ -20,14 +20,12 @@ class osmosdr_source_c(grc_wxgui.top_block_gui): def __init__(self): grc_wxgui.top_block_gui.__init__(self, title="OsmoSDR Source") - #self.src = osmosdr.source_c() - self.src = osmosdr.source_c("rtl=0") - #self.src = osmosdr.source_c("fcd=0") - #self.src = osmosdr.source_c("uhd=0") + self.src = osmosdr.source_c() self.src.set_sample_rate(1024000) - self.src.set_center_freq(394.5e6) - self.src.set_gain(10) + self.src.set_center_freq(394.4e6) + self.src.set_gain_mode(0) + self.src.set_gain(9) ################################################## # Variables diff --git a/lib/fcd/fcd_source.cc b/lib/fcd/fcd_source.cc index 8ed7103..c77e33a 100644 --- a/lib/fcd/fcd_source.cc +++ b/lib/fcd/fcd_source.cc @@ -23,6 +23,7 @@ #include #include +#include #include @@ -37,36 +38,7 @@ fcd_source_sptr make_fcd_source(const std::string &args) return gnuradio::get_initial_sptr(new fcd_source(args)); } -fcd_source::fcd_source(const std::string &args) : - gr_hier_block2("fcd_source", - gr_make_io_signature (0, 0, 0), - gr_make_io_signature (1, 1, sizeof (gr_complex))) -{ - std::string dev_name; - unsigned int dev_index = 0; - - dict_t dict = params_to_dict(args); - - if (dict.count("fcd")) - dev_index = boost::lexical_cast< unsigned int >( dict["fcd"] ); - - std::vector< std::string > devices = fcd_source::get_devices(); - - if ( devices.size() ) - dev_name = devices[dev_index]; - else - throw std::runtime_error("No FunCube Dongle found."); - - _src = fcd_make_source_c( dev_name ); - - connect( _src, 0, self(), 0 ); -} - -fcd_source::~fcd_source() -{ -} - -std::vector< std::string > fcd_source::get_devices() +static std::vector< std::string > _get_devices() { std::vector< std::string > devices; @@ -96,6 +68,46 @@ std::vector< std::string > fcd_source::get_devices() return devices; } +fcd_source::fcd_source(const std::string &args) : + gr_hier_block2("fcd_source", + gr_make_io_signature (0, 0, 0), + gr_make_io_signature (1, 1, sizeof (gr_complex))) +{ + std::string dev_name; + unsigned int dev_index = 0; + + dict_t dict = params_to_dict(args); + + if (dict.count("fcd")) + dev_index = boost::lexical_cast< unsigned int >( dict["fcd"] ); + + std::vector< std::string > devices = _get_devices(); + + if ( devices.size() ) + dev_name = devices[dev_index]; + else + throw std::runtime_error("No FunCube Dongle found."); + + _src = fcd_make_source_c( dev_name ); + + connect( _src, 0, self(), 0 ); +} + +fcd_source::~fcd_source() +{ +} + +std::vector< std::string > fcd_source::get_devices() +{ + int id = 0; + std::vector< std::string > devices; + + BOOST_FOREACH( std::string dev, _get_devices() ) + devices.push_back( "fcd=" + boost::lexical_cast< std::string >( id++ ) ); + + return devices; +} + gr_basic_block_sptr fcd_source::self() { return gr_hier_block2::self(); diff --git a/lib/osmosdr_source_c_impl.cc b/lib/osmosdr_source_c_impl.cc index 6552861..5c32bab 100644 --- a/lib/osmosdr_source_c_impl.cc +++ b/lib/osmosdr_source_c_impl.cc @@ -71,14 +71,58 @@ osmosdr_source_c_impl::osmosdr_source_c_impl (const std::string &args) args_to_io_signature(args)) { size_t channel = 0; + bool device_specified = false; - BOOST_FOREACH(std::string arg, args_to_vector(args)) { + std::vector< std::string > arg_list = args_to_vector(args); + + BOOST_FOREACH(std::string arg, arg_list) { dict_t dict = params_to_dict(arg); -// std::cout << std::endl; + if ( dict.count("osmosdr") | + dict.count("fcd") | + dict.count("file") | + dict.count("rtl") | + dict.count("uhd") ) + { + device_specified = true; + } + } + + std::vector< std::string > dev_list; +#ifdef ENABLE_OSMOSDR + // TODO: implement +#endif +#ifdef ENABLE_FCD + BOOST_FOREACH( std::string dev, fcd_source::get_devices() ) + dev_list.push_back( dev ); +#endif +#ifdef ENABLE_RTL + BOOST_FOREACH( std::string dev, rtl_source_c::get_devices() ) + dev_list.push_back( dev ); +#endif +#ifdef ENABLE_UHD + BOOST_FOREACH( std::string dev, uhd_source_c::get_devices() ) + dev_list.push_back( dev ); +#endif +// std::cerr << std::endl; +// BOOST_FOREACH( std::string dev, dev_list ) +// std::cerr << "'" << dev << "'" << std::endl; + + if (!device_specified) { + if ( dev_list.size() ) + arg_list.push_back( dev_list.front() ); + else + throw std::runtime_error("No supported devices found to pick from."); + } + + BOOST_FOREACH(std::string arg, arg_list) { + + dict_t dict = params_to_dict(arg); + +// std::cerr << std::endl; // BOOST_FOREACH( dict_t::value_type &entry, dict ) -// std::cout << "'" << entry.first << "' = '" << entry.second << "'" << std::endl; +// std::cerr << "'" << entry.first << "' = '" << entry.second << "'" << std::endl; #ifdef ENABLE_OSMOSDR if ( dict.count("osmosdr") ) { @@ -143,12 +187,12 @@ size_t osmosdr_source_c_impl::get_num_channels() osmosdr::meta_range_t osmosdr_source_c_impl::get_sample_rates() { - return _devs[0]->get_sample_rates(); + return _devs[0]->get_sample_rates(); // assume same devices used in the group } double osmosdr_source_c_impl::set_sample_rate(double rate) { - double sample_rate = _sample_rate; + double sample_rate = 0; if (_sample_rate != rate) { BOOST_FOREACH( osmosdr_src_iface *dev, _devs ) @@ -162,7 +206,7 @@ double osmosdr_source_c_impl::set_sample_rate(double rate) double osmosdr_source_c_impl::get_sample_rate() { - return _devs[0]->get_sample_rate(); + return _devs[0]->get_sample_rate(); // assume same devices used in the group } osmosdr::freq_range_t osmosdr_source_c_impl::get_freq_range( size_t chan ) diff --git a/lib/rtl/rtl_source_c.cc b/lib/rtl/rtl_source_c.cc index f6c298c..f60d2cf 100644 --- a/lib/rtl/rtl_source_c.cc +++ b/lib/rtl/rtl_source_c.cc @@ -280,6 +280,16 @@ int rtl_source_c::work( int noutput_items, return noutput_items; } +std::vector rtl_source_c::get_devices() +{ + std::vector result; + + for (unsigned int i = 0; i < rtlsdr_get_device_count(); i++) + result.push_back( "rtl=" + boost::lexical_cast< std::string >( i ) ); + + return result; +} + size_t rtl_source_c::get_num_channels() { return 1; diff --git a/lib/rtl/rtl_source_c.h b/lib/rtl/rtl_source_c.h index a957f9d..b2591f1 100644 --- a/lib/rtl/rtl_source_c.h +++ b/lib/rtl/rtl_source_c.h @@ -83,6 +83,8 @@ public: gr_vector_const_void_star &input_items, gr_vector_void_star &output_items ); + static std::vector< std::string > get_devices(); + size_t get_num_channels( void ); osmosdr::meta_range_t get_sample_rates( void ); diff --git a/lib/uhd/uhd_source_c.cc b/lib/uhd/uhd_source_c.cc index 5c60f21..d2fda61 100644 --- a/lib/uhd/uhd_source_c.cc +++ b/lib/uhd/uhd_source_c.cc @@ -42,13 +42,22 @@ uhd_source_c::uhd_source_c(const std::string &args) : args_to_io_signature(args)) { size_t num_channels = 1; + std::string arguments = args; dict_t dict = params_to_dict(args); if (dict.count("nchan")) num_channels = boost::lexical_cast< unsigned int >( dict["nchan"] ); - _src = uhd_make_usrp_source( args, + arguments.clear(); // rebuild argument string without uhd= prefix + BOOST_FOREACH( dict_t::value_type &entry, dict ) { + if ( "uhd" == entry.first ) + arguments += entry.second; + else + arguments += entry.first + "=" + entry.second + ","; + } + + _src = uhd_make_usrp_source( arguments, uhd::io_type_t::COMPLEX_FLOAT32, num_channels ); @@ -69,7 +78,7 @@ std::vector< std::string > uhd_source_c::get_devices() uhd::device_addr_t hint; BOOST_FOREACH(uhd::device_addr_t device, uhd::device::find(hint)) - devices += device.to_string(); + devices += "uhd=" + device.to_string(); return devices; }