GNU Radio 3.7.1 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 <gnuradio/sync_block.h>
00023 #include <gnuradio/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 {
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         sync_block(
00041             "uhd tag source demo",
00042             gr::io_signature::make(0, 0, 0),
00043             gr::io_signature::make(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     {
00058         const pmt::pmt_t key = pmt::string_to_symbol("tx_time");
00059         const pmt::pmt_t value = pmt::make_tuple(
00060             pmt::from_uint64(_time_secs),
00061             pmt::from_double(_time_fracs)
00062         );
00063         const pmt::pmt_t srcid = pmt::string_to_symbol(this->name());
00064         this->add_item_tag(0/*chan0*/, tag_count, key, value, srcid);
00065     }
00066 
00067     void make_sob_tag(const uint64_t tag_count)
00068     {
00069         const pmt::pmt_t key = pmt::string_to_symbol("tx_sob");
00070         const pmt::pmt_t value = pmt::PMT_T;
00071         const pmt::pmt_t srcid = pmt::string_to_symbol(this->name());
00072         this->add_item_tag(0/*chan0*/, tag_count, key, value, srcid);
00073     }
00074 
00075     void make_eob_tag(const uint64_t tag_count)
00076     {
00077         const pmt::pmt_t key = pmt::string_to_symbol("tx_eob");
00078         const pmt::pmt_t value = pmt::PMT_T;
00079         const pmt::pmt_t srcid = pmt::string_to_symbol(this->name());
00080         this->add_item_tag(0/*chan0*/, tag_count, key, value, srcid);
00081     }
00082 
00083     int work(
00084         int noutput_items,
00085         gr_vector_const_void_star &input_items,
00086         gr_vector_void_star &output_items)
00087     {
00088         //load the output with a constant
00089         std::complex<float> *output = reinterpret_cast<std::complex<float> *>(output_items[0]);
00090         for (size_t i = 0; i < size_t(noutput_items); i++){
00091             output[i] = std::complex<float>(0.7, 0.7);
00092         }
00093 
00094         //Handle the start of burst condition.
00095         //Tag a start of burst and timestamp.
00096         //Increment the time for the next burst.
00097         if (_do_new_burst){
00098             _do_new_burst = false;
00099             _samps_left_in_burst = _samps_per_burst;
00100 
00101             this->make_sob_tag(this->nitems_written(0));
00102             this->make_time_tag(this->nitems_written(0));
00103 
00104             _time_fracs += _cycle_duration;
00105             double intpart; //normalize
00106             _time_fracs = std::modf(_time_fracs, &intpart);
00107             _time_secs += uint64_t(intpart);
00108         }
00109 
00110         //Handle the end of burst condition.
00111         //Tag an end of burst and return early.
00112         //the next work call will be a start of burst.
00113         if (_samps_left_in_burst < size_t(noutput_items)){
00114             this->make_eob_tag(this->nitems_written(0) + _samps_left_in_burst - 1);
00115             _do_new_burst = true;
00116             noutput_items = _samps_left_in_burst;
00117         }
00118 
00119         _samps_left_in_burst -= noutput_items;
00120         return noutput_items;
00121     }
00122 
00123 private:
00124     uint64_t _time_secs;
00125     double _time_fracs;
00126     const double _samp_rate;
00127     const uint64_t _samps_per_burst;
00128     const double _cycle_duration;
00129     uint64_t _samps_left_in_burst;
00130     bool _do_new_burst;
00131 
00132 };