GNU Radio 3.5.3.2 C++ API
tag_source_demo.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2011 Free Software Foundation, Inc.
00003  * 
00004  * This file is part of GNU Radio
00005  * 
00006  * GNU Radio is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 3, or (at your option)
00009  * any later version.
00010  * 
00011  * GNU Radio is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with GNU Radio; see the file COPYING.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street,
00019  * Boston, MA 02110-1301, USA.
00020  */
00021 
00022 #include <gr_sync_block.h>
00023 #include <gr_io_signature.h>
00024 #include <boost/foreach.hpp>
00025 #include <boost/format.hpp>
00026 #include <iostream>
00027 #include <complex>
00028 
00029 class tag_source_demo : public gr_sync_block{
00030 public:
00031 
00032     tag_source_demo(
00033         const uint64_t start_secs,
00034         const double start_fracs,
00035         const double samp_rate,
00036         const double idle_duration,
00037         const double burst_duration
00038     ):
00039         gr_sync_block(
00040             "uhd tag source demo",
00041             gr_make_io_signature(0, 0, 0),
00042             gr_make_io_signature(1, 1, sizeof(std::complex<float>))
00043         ),
00044         _time_secs(start_secs),
00045         _time_fracs(start_fracs),
00046         _samp_rate(samp_rate),
00047         _samps_per_burst(samp_rate*burst_duration),
00048         _cycle_duration(idle_duration + burst_duration),
00049         _samps_left_in_burst(1), //immediate EOB
00050         _do_new_burst(false)
00051     {
00052         //NOP
00053     }
00054 
00055     void make_time_tag(const uint64_t tag_count){;
00056         const pmt::pmt_t key = pmt::pmt_string_to_symbol("tx_time");
00057         const pmt::pmt_t value = pmt::pmt_make_tuple(
00058             pmt::pmt_from_uint64(_time_secs),
00059             pmt::pmt_from_double(_time_fracs)
00060         );
00061         const pmt::pmt_t srcid = pmt::pmt_string_to_symbol(this->name());
00062         this->add_item_tag(0/*chan0*/, tag_count, key, value, srcid);
00063     }
00064 
00065     void make_sob_tag(const uint64_t tag_count){
00066         const pmt::pmt_t key = pmt::pmt_string_to_symbol("tx_sob");
00067         const pmt::pmt_t value = pmt::PMT_T;
00068         const pmt::pmt_t srcid = pmt::pmt_string_to_symbol(this->name());
00069         this->add_item_tag(0/*chan0*/, tag_count, key, value, srcid);
00070     }
00071 
00072     void make_eob_tag(const uint64_t tag_count){;
00073         const pmt::pmt_t key = pmt::pmt_string_to_symbol("tx_eob");
00074         const pmt::pmt_t value = pmt::PMT_T;
00075         const pmt::pmt_t srcid = pmt::pmt_string_to_symbol(this->name());
00076         this->add_item_tag(0/*chan0*/, tag_count, key, value, srcid);
00077     }
00078 
00079     int work(
00080         int noutput_items,
00081         gr_vector_const_void_star &input_items,
00082         gr_vector_void_star &output_items
00083     ){
00084         //load the output with a constant
00085         std::complex<float> *output = reinterpret_cast<std::complex<float> *>(output_items[0]);
00086         for (size_t i = 0; i < size_t(noutput_items); i++){
00087             output[i] = std::complex<float>(0.7, 0.7);
00088         }
00089 
00090         //Handle the start of burst condition.
00091         //Tag a start of burst and timestamp.
00092         //Increment the time for the next burst.
00093         if (_do_new_burst){
00094             _do_new_burst = false;
00095             _samps_left_in_burst = _samps_per_burst;
00096 
00097             this->make_sob_tag(this->nitems_written(0));
00098             this->make_time_tag(this->nitems_written(0));
00099 
00100             _time_fracs += _cycle_duration;
00101             double intpart; //normalize
00102             _time_fracs = std::modf(_time_fracs, &intpart);
00103             _time_secs += uint64_t(intpart);
00104         }
00105 
00106         //Handle the end of burst condition.
00107         //Tag an end of burst and return early.
00108         //the next work call will be a start of burst.
00109         if (_samps_left_in_burst < size_t(noutput_items)){
00110             this->make_eob_tag(this->nitems_written(0) + _samps_left_in_burst - 1);
00111             _do_new_burst = true;
00112             noutput_items = _samps_left_in_burst;
00113         }
00114 
00115         _samps_left_in_burst -= noutput_items;
00116         return noutput_items;
00117     }
00118 
00119 private:
00120     uint64_t _time_secs;
00121     double _time_fracs;
00122     const double _samp_rate;
00123     const uint64_t _samps_per_burst;
00124     const double _cycle_duration;
00125     uint64_t _samps_left_in_burst;
00126     bool _do_new_burst;
00127 
00128 };