root / gr-uhd / examples / tag_source_demo.h @ 330cddf3
History | View | Annotate | Download (4.3 kB)
| 1 | 3c70ea18 | Josh Blum | /*
|
|---|---|---|---|
| 2 | 3c70ea18 | Josh Blum | * Copyright 2011 Free Software Foundation, Inc. |
| 3 | 3c70ea18 | Josh Blum | * |
| 4 | 3c70ea18 | Josh Blum | * This file is part of GNU Radio |
| 5 | 3c70ea18 | Josh Blum | * |
| 6 | 3c70ea18 | Josh Blum | * GNU Radio is free software; you can redistribute it and/or modify |
| 7 | 3c70ea18 | Josh Blum | * it under the terms of the GNU General Public License as published by |
| 8 | 3c70ea18 | Josh Blum | * the Free Software Foundation; either version 3, or (at your option) |
| 9 | 3c70ea18 | Josh Blum | * any later version. |
| 10 | 3c70ea18 | Josh Blum | * |
| 11 | 3c70ea18 | Josh Blum | * GNU Radio is distributed in the hope that it will be useful, |
| 12 | 3c70ea18 | Josh Blum | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | 3c70ea18 | Josh Blum | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | 3c70ea18 | Josh Blum | * GNU General Public License for more details. |
| 15 | 3c70ea18 | Josh Blum | * |
| 16 | 3c70ea18 | Josh Blum | * You should have received a copy of the GNU General Public License |
| 17 | 3c70ea18 | Josh Blum | * along with GNU Radio; see the file COPYING. If not, write to |
| 18 | 3c70ea18 | Josh Blum | * the Free Software Foundation, Inc., 51 Franklin Street, |
| 19 | 3c70ea18 | Josh Blum | * Boston, MA 02110-1301, USA. |
| 20 | 3c70ea18 | Josh Blum | */ |
| 21 | 3c70ea18 | Josh Blum | |
| 22 | 3c70ea18 | Josh Blum | #include <gr_sync_block.h> |
| 23 | 3c70ea18 | Josh Blum | #include <gr_io_signature.h> |
| 24 | 3c70ea18 | Josh Blum | #include <boost/foreach.hpp> |
| 25 | 3c70ea18 | Josh Blum | #include <boost/format.hpp> |
| 26 | 3c70ea18 | Josh Blum | #include <iostream> |
| 27 | 3c70ea18 | Josh Blum | #include <complex> |
| 28 | 3c70ea18 | Josh Blum | |
| 29 | 3c70ea18 | Josh Blum | class tag_source_demo : public gr_sync_block{
|
| 30 | 3c70ea18 | Josh Blum | public:
|
| 31 | 3c70ea18 | Josh Blum | |
| 32 | 3c70ea18 | Josh Blum | tag_source_demo( |
| 33 | 3c70ea18 | Josh Blum | const uint64_t start_secs,
|
| 34 | 3c70ea18 | Josh Blum | const double start_fracs, |
| 35 | 3c70ea18 | Josh Blum | const double samp_rate, |
| 36 | 3c70ea18 | Josh Blum | const double idle_duration, |
| 37 | 3c70ea18 | Josh Blum | const double burst_duration |
| 38 | 3c70ea18 | Josh Blum | ): |
| 39 | 3c70ea18 | Josh Blum | gr_sync_block( |
| 40 | 3c70ea18 | Josh Blum | "uhd tag source demo",
|
| 41 | 3c70ea18 | Josh Blum | gr_make_io_signature(0, 0, 0), |
| 42 | 3c70ea18 | Josh Blum | gr_make_io_signature(1, 1, sizeof(std::complex<float>)) |
| 43 | 3c70ea18 | Josh Blum | ), |
| 44 | 3c70ea18 | Josh Blum | _time_secs(start_secs), |
| 45 | 3c70ea18 | Josh Blum | _time_fracs(start_fracs), |
| 46 | 3c70ea18 | Josh Blum | _samp_rate(samp_rate), |
| 47 | 3c70ea18 | Josh Blum | _samps_per_burst(samp_rate*burst_duration), |
| 48 | 3c70ea18 | Josh Blum | _cycle_duration(idle_duration + burst_duration), |
| 49 | 3c70ea18 | Josh Blum | _samps_left_in_burst(1), //immediate EOB |
| 50 | 3c70ea18 | Josh Blum | _do_new_burst(false)
|
| 51 | 3c70ea18 | Josh Blum | {
|
| 52 | 3c70ea18 | Josh Blum | //NOP
|
| 53 | 3c70ea18 | Josh Blum | } |
| 54 | 3c70ea18 | Josh Blum | |
| 55 | 3c70ea18 | Josh Blum | void make_time_tag(const uint64_t tag_count){; |
| 56 | 3c70ea18 | Josh Blum | const pmt::pmt_t key = pmt::pmt_string_to_symbol("tx_time"); |
| 57 | 3c70ea18 | Josh Blum | const pmt::pmt_t value = pmt::pmt_make_tuple(
|
| 58 | 3c70ea18 | Josh Blum | pmt::pmt_from_uint64(_time_secs), |
| 59 | 3c70ea18 | Josh Blum | pmt::pmt_from_double(_time_fracs) |
| 60 | 3c70ea18 | Josh Blum | ); |
| 61 | 3c70ea18 | Josh Blum | const pmt::pmt_t srcid = pmt::pmt_string_to_symbol(this->name());
|
| 62 | 3c70ea18 | Josh Blum | this->add_item_tag(0/*chan0*/, tag_count, key, value, srcid); |
| 63 | 3c70ea18 | Josh Blum | } |
| 64 | 3c70ea18 | Josh Blum | |
| 65 | 3c70ea18 | Josh Blum | void make_sob_tag(const uint64_t tag_count){ |
| 66 | 3c70ea18 | Josh Blum | const pmt::pmt_t key = pmt::pmt_string_to_symbol("tx_sob"); |
| 67 | 3c70ea18 | Josh Blum | const pmt::pmt_t value = pmt::PMT_T;
|
| 68 | 3c70ea18 | Josh Blum | const pmt::pmt_t srcid = pmt::pmt_string_to_symbol(this->name());
|
| 69 | 3c70ea18 | Josh Blum | this->add_item_tag(0/*chan0*/, tag_count, key, value, srcid); |
| 70 | 3c70ea18 | Josh Blum | } |
| 71 | 3c70ea18 | Josh Blum | |
| 72 | 3c70ea18 | Josh Blum | void make_eob_tag(const uint64_t tag_count){; |
| 73 | 3c70ea18 | Josh Blum | const pmt::pmt_t key = pmt::pmt_string_to_symbol("tx_eob"); |
| 74 | 3c70ea18 | Josh Blum | const pmt::pmt_t value = pmt::PMT_T;
|
| 75 | 3c70ea18 | Josh Blum | const pmt::pmt_t srcid = pmt::pmt_string_to_symbol(this->name());
|
| 76 | 3c70ea18 | Josh Blum | this->add_item_tag(0/*chan0*/, tag_count, key, value, srcid); |
| 77 | 3c70ea18 | Josh Blum | } |
| 78 | 3c70ea18 | Josh Blum | |
| 79 | 3c70ea18 | Josh Blum | int work(
|
| 80 | 3c70ea18 | Josh Blum | int noutput_items,
|
| 81 | 3c70ea18 | Josh Blum | gr_vector_const_void_star &input_items, |
| 82 | 3c70ea18 | Josh Blum | gr_vector_void_star &output_items |
| 83 | 3c70ea18 | Josh Blum | ){
|
| 84 | 3c70ea18 | Josh Blum | //load the output with a constant
|
| 85 | 3c70ea18 | Josh Blum | std::complex<float> *output = reinterpret_cast<std::complex<float> *>(output_items[0]); |
| 86 | 3c70ea18 | Josh Blum | for (size_t i = 0; i < size_t(noutput_items); i++){ |
| 87 | 3c70ea18 | Josh Blum | output[i] = std::complex<float>(0.7, 0.7); |
| 88 | 3c70ea18 | Josh Blum | } |
| 89 | 3c70ea18 | Josh Blum | |
| 90 | 3c70ea18 | Josh Blum | //Handle the start of burst condition.
|
| 91 | 3c70ea18 | Josh Blum | //Tag a start of burst and timestamp.
|
| 92 | 3c70ea18 | Josh Blum | //Increment the time for the next burst.
|
| 93 | 3c70ea18 | Josh Blum | if (_do_new_burst){
|
| 94 | 3c70ea18 | Josh Blum | _do_new_burst = false;
|
| 95 | 3c70ea18 | Josh Blum | _samps_left_in_burst = _samps_per_burst; |
| 96 | 3c70ea18 | Josh Blum | |
| 97 | 3c70ea18 | Josh Blum | this->make_sob_tag(this->nitems_written(0));
|
| 98 | 3c70ea18 | Josh Blum | this->make_time_tag(this->nitems_written(0));
|
| 99 | 3c70ea18 | Josh Blum | |
| 100 | 3c70ea18 | Josh Blum | _time_fracs += _cycle_duration; |
| 101 | 3c70ea18 | Josh Blum | double intpart; //normalize |
| 102 | 3c70ea18 | Josh Blum | _time_fracs = std::modf(_time_fracs, &intpart); |
| 103 | 3c70ea18 | Josh Blum | _time_secs += uint64_t(intpart); |
| 104 | 3c70ea18 | Josh Blum | } |
| 105 | 3c70ea18 | Josh Blum | |
| 106 | 3c70ea18 | Josh Blum | //Handle the end of burst condition.
|
| 107 | 3c70ea18 | Josh Blum | //Tag an end of burst and return early.
|
| 108 | 3c70ea18 | Josh Blum | //the next work call will be a start of burst.
|
| 109 | 3c70ea18 | Josh Blum | if (_samps_left_in_burst < size_t(noutput_items)){
|
| 110 | 3c70ea18 | Josh Blum | this->make_eob_tag(this->nitems_written(0) + _samps_left_in_burst - 1); |
| 111 | 3c70ea18 | Josh Blum | _do_new_burst = true;
|
| 112 | 3c70ea18 | Josh Blum | noutput_items = _samps_left_in_burst; |
| 113 | 3c70ea18 | Josh Blum | } |
| 114 | 3c70ea18 | Josh Blum | |
| 115 | 3c70ea18 | Josh Blum | _samps_left_in_burst -= noutput_items; |
| 116 | 3c70ea18 | Josh Blum | return noutput_items;
|
| 117 | 3c70ea18 | Josh Blum | } |
| 118 | 3c70ea18 | Josh Blum | |
| 119 | 3c70ea18 | Josh Blum | private:
|
| 120 | 3c70ea18 | Josh Blum | uint64_t _time_secs; |
| 121 | 3c70ea18 | Josh Blum | double _time_fracs;
|
| 122 | 3c70ea18 | Josh Blum | const double _samp_rate; |
| 123 | 3c70ea18 | Josh Blum | const uint64_t _samps_per_burst;
|
| 124 | 3c70ea18 | Josh Blum | const double _cycle_duration; |
| 125 | 3c70ea18 | Josh Blum | uint64_t _samps_left_in_burst; |
| 126 | 435dc713 | Josh Blum | bool _do_new_burst;
|
| 127 | 3c70ea18 | Josh Blum | |
| 128 | 3c70ea18 | Josh Blum | }; |