GNU Radio 3.4.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 <gr_tag_info.h>
00025 #include <boost/foreach.hpp>
00026 #include <boost/format.hpp>
00027 #include <iostream>
00028 #include <complex>
00029 
00030 class tag_source_demo : public gr_sync_block{
00031 public:
00032 
00033     tag_source_demo(
00034         const uint64_t start_secs,
00035         const double start_fracs,
00036         const double samp_rate,
00037         const double idle_duration,
00038         const double burst_duration
00039     ):
00040         gr_sync_block(
00041             "uhd tag source demo",
00042             gr_make_io_signature(0, 0, 0),
00043             gr_make_io_signature(1, 1, sizeof(std::complex<float>))
00044         ),
00045         _time_secs(start_secs),
00046         _time_fracs(start_fracs),
00047         _samp_rate(samp_rate),
00048         _samps_per_burst(samp_rate*burst_duration),
00049         _cycle_duration(idle_duration + burst_duration),
00050         _samps_left_in_burst(1), //immediate EOB
00051         _do_new_burst(false)
00052     {
00053         //NOP
00054     }
00055 
00056     void make_time_tag(const uint64_t tag_count){;
00057         const pmt::pmt_t key = pmt::pmt_string_to_symbol("tx_time");
00058         const pmt::pmt_t value = pmt::pmt_make_tuple(
00059             pmt::pmt_from_uint64(_time_secs),
00060             pmt::pmt_from_double(_time_fracs)
00061         );
00062         const pmt::pmt_t srcid = pmt::pmt_string_to_symbol(this->name());
00063         this->add_item_tag(0/*chan0*/, tag_count, key, value, srcid);
00064     }
00065 
00066     void make_sob_tag(const uint64_t tag_count){
00067         const pmt::pmt_t key = pmt::pmt_string_to_symbol("tx_sob");
00068         const pmt::pmt_t value = pmt::PMT_T;
00069         const pmt::pmt_t srcid = pmt::pmt_string_to_symbol(this->name());
00070         this->add_item_tag(0/*chan0*/, tag_count, key, value, srcid);
00071     }
00072 
00073     void make_eob_tag(const uint64_t tag_count){;
00074         const pmt::pmt_t key = pmt::pmt_string_to_symbol("tx_eob");
00075         const pmt::pmt_t value = pmt::PMT_T;
00076         const pmt::pmt_t srcid = pmt::pmt_string_to_symbol(this->name());
00077         this->add_item_tag(0/*chan0*/, tag_count, key, value, srcid);
00078     }
00079 
00080     int work(
00081         int noutput_items,
00082         gr_vector_const_void_star &input_items,
00083         gr_vector_void_star &output_items
00084     ){
00085         //load the output with a constant
00086         std::complex<float> *output = reinterpret_cast<std::complex<float> *>(output_items[0]);
00087         for (size_t i = 0; i < size_t(noutput_items); i++){
00088             output[i] = std::complex<float>(0.7, 0.7);
00089         }
00090 
00091         //Handle the start of burst condition.
00092         //Tag a start of burst and timestamp.
00093         //Increment the time for the next burst.
00094         if (_do_new_burst){
00095             _do_new_burst = false;
00096             _samps_left_in_burst = _samps_per_burst;
00097 
00098             this->make_sob_tag(this->nitems_written(0));
00099             this->make_time_tag(this->nitems_written(0));
00100 
00101             _time_fracs += _cycle_duration;
00102             double intpart; //normalize
00103             _time_fracs = std::modf(_time_fracs, &intpart);
00104             _time_secs += uint64_t(intpart);
00105         }
00106 
00107         //Handle the end of burst condition.
00108         //Tag an end of burst and return early.
00109         //the next work call will be a start of burst.
00110         if (_samps_left_in_burst < size_t(noutput_items)){
00111             this->make_eob_tag(this->nitems_written(0) + _samps_left_in_burst - 1);
00112             _do_new_burst = true;
00113             noutput_items = _samps_left_in_burst;
00114         }
00115 
00116         _samps_left_in_burst -= noutput_items;
00117         return noutput_items;
00118     }
00119 
00120 private:
00121     uint64_t _time_secs;
00122     double _time_fracs;
00123     const double _samp_rate;
00124     const uint64_t _samps_per_burst;
00125     const double _cycle_duration;
00126     bool _do_new_burst;
00127     uint64_t _samps_left_in_burst;
00128 
00129 };