1 /* -*- c++ -*- */
2 /*
3  * Copyright 2010,2013 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include "burst_tagger_impl.h"
28 #include <gnuradio/io_signature.h>
29 #include <string.h>
30 
31 namespace gr {
32 namespace blocks {
33 
make(size_t itemsize)34 burst_tagger::sptr burst_tagger::make(size_t itemsize)
35 {
36     return gnuradio::get_initial_sptr(new burst_tagger_impl(itemsize));
37 }
38 
burst_tagger_impl(size_t itemsize)39 burst_tagger_impl::burst_tagger_impl(size_t itemsize)
40     : sync_block("burst_tagger",
41                  io_signature::make2(2, 2, itemsize, sizeof(short)),
42                  io_signature::make(1, 1, itemsize)),
43       d_itemsize(itemsize),
44       d_state(false)
45 {
46     std::stringstream str;
47     str << name() << unique_id();
48 
49     d_true_key = pmt::string_to_symbol("burst");
50     d_true_value = pmt::PMT_T;
51 
52     d_false_key = pmt::string_to_symbol("burst");
53     d_false_value = pmt::PMT_F;
54 
55     d_id = pmt::string_to_symbol(str.str());
56 }
57 
~burst_tagger_impl()58 burst_tagger_impl::~burst_tagger_impl() {}
59 
set_true_tag(const std::string & key,bool value)60 void burst_tagger_impl::set_true_tag(const std::string& key, bool value)
61 {
62     d_true_key = pmt::string_to_symbol(key);
63     if (value == true) {
64         d_true_value = pmt::PMT_T;
65     } else {
66         d_true_value = pmt::PMT_F;
67     }
68 }
69 
set_false_tag(const std::string & key,bool value)70 void burst_tagger_impl::set_false_tag(const std::string& key, bool value)
71 {
72     d_false_key = pmt::string_to_symbol(key);
73     if (value == true) {
74         d_false_value = pmt::PMT_T;
75     } else {
76         d_false_value = pmt::PMT_F;
77     }
78 }
79 
work(int noutput_items,gr_vector_const_void_star & input_items,gr_vector_void_star & output_items)80 int burst_tagger_impl::work(int noutput_items,
81                             gr_vector_const_void_star& input_items,
82                             gr_vector_void_star& output_items)
83 {
84     const char* signal = (const char*)input_items[0];
85     const short* trigger = (const short*)input_items[1];
86     char* out = (char*)output_items[0];
87 
88     memcpy(out, signal, noutput_items * d_itemsize);
89 
90     for (int i = 0; i < noutput_items; i++) {
91         if (trigger[i] > 0) {
92             if (d_state == false) {
93                 d_state = true;
94                 add_item_tag(0, nitems_written(0) + i, d_true_key, d_true_value, d_id);
95             }
96         } else {
97             if (d_state == true) {
98                 d_state = false;
99                 add_item_tag(0, nitems_written(0) + i, d_false_key, d_false_value, d_id);
100             }
101         }
102     }
103     return noutput_items;
104 }
105 
106 } /* namespace blocks */
107 } /* namespace gr */
108