Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.32 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 <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

29
class tag_source_demo : public gr::sync_block
30
{
31
public:
32

33
    tag_source_demo(
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
};