GNU Radio 3.7.3 C++ API
tag_source_demo.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Free Software Foundation, Inc.
3  *
4  * This file is part of GNU Radio
5  *
6  * GNU Radio is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3, or (at your option)
9  * any later version.
10  *
11  * GNU Radio is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GNU Radio; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #include <gnuradio/sync_block.h>
23 #include <gnuradio/io_signature.h>
24 #include <boost/foreach.hpp>
25 #include <boost/format.hpp>
26 #include <iostream>
27 #include <complex>
28 
30 {
31 public:
32 
34  const uint64_t start_secs,
35  const double start_fracs,
36  const double samp_rate,
37  const double idle_duration,
38  const double burst_duration
39  ):
40  sync_block(
41  "uhd tag source demo",
42  gr::io_signature::make(0, 0, 0),
43  gr::io_signature::make(1, 1, sizeof(std::complex<float>))
44  ),
45  _time_secs(start_secs),
46  _time_fracs(start_fracs),
47  _samp_rate(samp_rate),
48  _samps_per_burst(samp_rate*burst_duration),
49  _cycle_duration(idle_duration + burst_duration),
50  _samps_left_in_burst(1), //immediate EOB
51  _do_new_burst(false)
52  {
53  //NOP
54  }
55 
56  void make_time_tag(const uint64_t tag_count)
57  {
58  const pmt::pmt_t key = pmt::string_to_symbol("tx_time");
59  const pmt::pmt_t value = pmt::make_tuple(
60  pmt::from_uint64(_time_secs),
61  pmt::from_double(_time_fracs)
62  );
63  const pmt::pmt_t srcid = pmt::string_to_symbol(this->name());
64  this->add_item_tag(0/*chan0*/, tag_count, key, value, srcid);
65  }
66 
67  void make_sob_tag(const uint64_t tag_count)
68  {
69  const pmt::pmt_t key = pmt::string_to_symbol("tx_sob");
70  const pmt::pmt_t value = pmt::PMT_T;
71  const pmt::pmt_t srcid = pmt::string_to_symbol(this->name());
72  this->add_item_tag(0/*chan0*/, tag_count, key, value, srcid);
73  }
74 
75  void make_eob_tag(const uint64_t tag_count)
76  {
77  const pmt::pmt_t key = pmt::string_to_symbol("tx_eob");
78  const pmt::pmt_t value = pmt::PMT_T;
79  const pmt::pmt_t srcid = pmt::string_to_symbol(this->name());
80  this->add_item_tag(0/*chan0*/, tag_count, key, value, srcid);
81  }
82 
83  int work(
84  int noutput_items,
85  gr_vector_const_void_star &input_items,
86  gr_vector_void_star &output_items)
87  {
88  //load the output with a constant
89  std::complex<float> *output = reinterpret_cast<std::complex<float> *>(output_items[0]);
90  for (size_t i = 0; i < size_t(noutput_items); i++){
91  output[i] = std::complex<float>(0.7, 0.7);
92  }
93 
94  //Handle the start of burst condition.
95  //Tag a start of burst and timestamp.
96  //Increment the time for the next burst.
97  if (_do_new_burst){
98  _do_new_burst = false;
99  _samps_left_in_burst = _samps_per_burst;
100 
101  this->make_sob_tag(this->nitems_written(0));
102  this->make_time_tag(this->nitems_written(0));
103 
104  _time_fracs += _cycle_duration;
105  double intpart; //normalize
106  _time_fracs = std::modf(_time_fracs, &intpart);
107  _time_secs += uint64_t(intpart);
108  }
109 
110  //Handle the end of burst condition.
111  //Tag an end of burst and return early.
112  //the next work call will be a start of burst.
113  if (_samps_left_in_burst < size_t(noutput_items)){
114  this->make_eob_tag(this->nitems_written(0) + _samps_left_in_burst - 1);
115  _do_new_burst = true;
116  noutput_items = _samps_left_in_burst;
117  }
118 
119  _samps_left_in_burst -= noutput_items;
120  return noutput_items;
121  }
122 
123 private:
124  uint64_t _time_secs;
125  double _time_fracs;
126  const double _samp_rate;
127  const uint64_t _samps_per_burst;
128  const double _cycle_duration;
129  uint64_t _samps_left_in_burst;
130  bool _do_new_burst;
131 
132 };
void add_item_tag(unsigned int which_output, uint64_t abs_offset, const pmt::pmt_t &key, const pmt::pmt_t &value, const pmt::pmt_t &srcid=pmt::PMT_F)
Adds a new tag onto the given output buffer.
Definition: block.h:605
int work(int noutput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items)
just like gr::block::general_work, only this arranges to call consume_each for you ...
Definition: tag_source_demo.h:83
PMT_API const pmt_t PMT_T
std::string name() const
Definition: basic_block.h:147
std::vector< const void * > gr_vector_const_void_star
Definition: gnuradio-runtime/include/gnuradio/types.h:38
tag_source_demo(const uint64_t start_secs, const double start_fracs, const double samp_rate, const double idle_duration, const double burst_duration)
Definition: tag_source_demo.h:33
PMT_API pmt_t string_to_symbol(const std::string &s)
Return the symbol whose name is s.
void make_time_tag(const uint64_t tag_count)
Definition: tag_source_demo.h:56
std::vector< void * > gr_vector_void_star
Definition: gnuradio-runtime/include/gnuradio/types.h:37
PMT_API pmt_t from_uint64(uint64_t x)
Return the pmt value that represents the uint64 x.
uint64_t nitems_written(unsigned int which_output)
Return the number of items written on output stream which_output.
unsigned __int64 uint64_t
Definition: stdint.h:90
synchronous 1:1 input to output with historyOverride work to provide the signal processing implementa...
Definition: sync_block.h:37
#define false
Definition: stdbool.h:33
PMT_API pmt_t make_tuple()
sync_block(void)
Definition: sync_block.h:40
Definition: tag_source_demo.h:29
PMT_API pmt_t from_double(double x)
Return the pmt value that represents double x.
void make_eob_tag(const uint64_t tag_count)
Definition: tag_source_demo.h:75
boost::intrusive_ptr< pmt_base > pmt_t
typedef for shared pointer (transparent reference counting). See http://www.boost.org/libs/smart_ptr/smart_ptr.htm
Definition: pmt.h:56
void make_sob_tag(const uint64_t tag_count)
Definition: tag_source_demo.h:67