Statistics
| Branch: | Tag: | Revision:

root / gr-uhd / examples / c++ / tag_source_demo.h @ master

History | View | Annotate | Download (4.3 kB)

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 <gr_sync_block.h>
23
#include <gr_io_signature.h>
24
#include <boost/foreach.hpp>
25
#include <boost/format.hpp>
26
#include <iostream>
27
#include <complex>
28
29
class tag_source_demo : public gr_sync_block{
30
public:
31
32
    tag_source_demo(
33
        const uint64_t start_secs,
34
        const double start_fracs,
35
        const double samp_rate,
36
        const double idle_duration,
37
        const double burst_duration
38
    ):
39
        gr_sync_block(
40
            "uhd tag source demo",
41
            gr_make_io_signature(0, 0, 0),
42
            gr_make_io_signature(1, 1, sizeof(std::complex<float>))
43
        ),
44
        _time_secs(start_secs),
45
        _time_fracs(start_fracs),
46
        _samp_rate(samp_rate),
47
        _samps_per_burst(samp_rate*burst_duration),
48
        _cycle_duration(idle_duration + burst_duration),
49
        _samps_left_in_burst(1), //immediate EOB
50
        _do_new_burst(false)
51
    {
52
        //NOP
53
    }
54
55
    void make_time_tag(const uint64_t tag_count){;
56
        const pmt::pmt_t key = pmt::pmt_string_to_symbol("tx_time");
57
        const pmt::pmt_t value = pmt::pmt_make_tuple(
58
            pmt::pmt_from_uint64(_time_secs),
59
            pmt::pmt_from_double(_time_fracs)
60
        );
61
        const pmt::pmt_t srcid = pmt::pmt_string_to_symbol(this->name());
62
        this->add_item_tag(0/*chan0*/, tag_count, key, value, srcid);
63
    }
64
65
    void make_sob_tag(const uint64_t tag_count){
66
        const pmt::pmt_t key = pmt::pmt_string_to_symbol("tx_sob");
67
        const pmt::pmt_t value = pmt::PMT_T;
68
        const pmt::pmt_t srcid = pmt::pmt_string_to_symbol(this->name());
69
        this->add_item_tag(0/*chan0*/, tag_count, key, value, srcid);
70
    }
71
72
    void make_eob_tag(const uint64_t tag_count){;
73
        const pmt::pmt_t key = pmt::pmt_string_to_symbol("tx_eob");
74
        const pmt::pmt_t value = pmt::PMT_T;
75
        const pmt::pmt_t srcid = pmt::pmt_string_to_symbol(this->name());
76
        this->add_item_tag(0/*chan0*/, tag_count, key, value, srcid);
77
    }
78
79
    int work(
80
        int noutput_items,
81
        gr_vector_const_void_star &input_items,
82
        gr_vector_void_star &output_items
83
    ){
84
        //load the output with a constant
85
        std::complex<float> *output = reinterpret_cast<std::complex<float> *>(output_items[0]);
86
        for (size_t i = 0; i < size_t(noutput_items); i++){
87
            output[i] = std::complex<float>(0.7, 0.7);
88
        }
89
90
        //Handle the start of burst condition.
91
        //Tag a start of burst and timestamp.
92
        //Increment the time for the next burst.
93
        if (_do_new_burst){
94
            _do_new_burst = false;
95
            _samps_left_in_burst = _samps_per_burst;
96
97
            this->make_sob_tag(this->nitems_written(0));
98
            this->make_time_tag(this->nitems_written(0));
99
100
            _time_fracs += _cycle_duration;
101
            double intpart; //normalize
102
            _time_fracs = std::modf(_time_fracs, &intpart);
103
            _time_secs += uint64_t(intpart);
104
        }
105
106
        //Handle the end of burst condition.
107
        //Tag an end of burst and return early.
108
        //the next work call will be a start of burst.
109
        if (_samps_left_in_burst < size_t(noutput_items)){
110
            this->make_eob_tag(this->nitems_written(0) + _samps_left_in_burst - 1);
111
            _do_new_burst = true;
112
            noutput_items = _samps_left_in_burst;
113
        }
114
115
        _samps_left_in_burst -= noutput_items;
116
        return noutput_items;
117
    }
118
119
private:
120
    uint64_t _time_secs;
121
    double _time_fracs;
122
    const double _samp_rate;
123
    const uint64_t _samps_per_burst;
124
    const double _cycle_duration;
125
    uint64_t _samps_left_in_burst;
126
    bool _do_new_burst;
127
128
};