1 /* -*- c++ -*- */
2 /*
3  * Copyright 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 "ctrlport_probe2_i_impl.h"
28 #include <gnuradio/io_signature.h>
29 
30 namespace gr {
31 namespace blocks {
32 
make(const std::string & id,const std::string & desc,int len,unsigned int disp_mask)33 ctrlport_probe2_i::sptr ctrlport_probe2_i::make(const std::string& id,
34                                                 const std::string& desc,
35                                                 int len,
36                                                 unsigned int disp_mask)
37 {
38     return gnuradio::get_initial_sptr(
39         new ctrlport_probe2_i_impl(id, desc, len, disp_mask));
40 }
41 
ctrlport_probe2_i_impl(const std::string & id,const std::string & desc,int len,unsigned int disp_mask)42 ctrlport_probe2_i_impl::ctrlport_probe2_i_impl(const std::string& id,
43                                                const std::string& desc,
44                                                int len,
45                                                unsigned int disp_mask)
46     : sync_block(
47           "probe2_i", io_signature::make(1, 1, sizeof(int)), io_signature::make(0, 0, 0)),
48       d_id(id),
49       d_desc(desc),
50       d_len(len),
51       d_disp_mask(disp_mask)
52 {
53     set_length(len);
54 }
55 
~ctrlport_probe2_i_impl()56 ctrlport_probe2_i_impl::~ctrlport_probe2_i_impl() {}
57 
forecast(int noutput_items,gr_vector_int & ninput_items_required)58 void ctrlport_probe2_i_impl::forecast(int noutput_items,
59                                       gr_vector_int& ninput_items_required)
60 {
61     // make sure all inputs have noutput_items available
62     unsigned ninputs = ninput_items_required.size();
63     for (unsigned i = 0; i < ninputs; i++)
64         ninput_items_required[i] = d_len;
65 }
66 
get()67 std::vector<int> ctrlport_probe2_i_impl::get() { return buffered_get.get(); }
68 
set_length(int len)69 void ctrlport_probe2_i_impl::set_length(int len)
70 {
71     gr::thread::scoped_lock guard(d_setlock);
72 
73     if (len > 8191) {
74         GR_LOG_WARN(d_logger,
75                     boost::format("probe2_i: length %1% exceeds maximum"
76                                   " buffer size of 8191") %
77                         len);
78         len = 8191;
79     }
80 
81     d_len = len;
82     d_buffer.resize(d_len);
83     d_index = 0;
84 }
85 
length() const86 int ctrlport_probe2_i_impl::length() const { return (int)d_len; }
87 
work(int noutput_items,gr_vector_const_void_star & input_items,gr_vector_void_star & output_items)88 int ctrlport_probe2_i_impl::work(int noutput_items,
89                                  gr_vector_const_void_star& input_items,
90                                  gr_vector_void_star& output_items)
91 {
92     const int* in = (const int*)input_items[0];
93 
94     gr::thread::scoped_lock guard(d_setlock);
95 
96     // copy samples to get buffer if we need samples
97     if (d_index < d_len) {
98         // copy smaller of remaining buffer space and num inputs to work()
99         int num_copy = std::min((int)(d_len - d_index), noutput_items);
100 
101         memcpy(&d_buffer[d_index], in, num_copy * sizeof(int));
102         d_index += num_copy;
103     }
104 
105     // notify the waiting get() if we fill up the buffer
106     if (d_index == d_len) {
107         buffered_get.offer_data(d_buffer);
108         d_index = 0;
109     }
110 
111     return noutput_items;
112 }
113 
setup_rpc()114 void ctrlport_probe2_i_impl::setup_rpc()
115 {
116 #ifdef GR_CTRLPORT
117     int len = static_cast<int>(d_len);
118     d_rpc_vars.emplace_back(
119         new rpcbasic_register_get<ctrlport_probe2_i, std::vector<int>>(
120             alias(),
121             d_id.c_str(),
122             &ctrlport_probe2_i::get,
123             pmt::mp(-32768),
124             pmt::mp(32767),
125             pmt::mp(0),
126             "volts",
127             d_desc.c_str(),
128             RPC_PRIVLVL_MIN,
129             d_disp_mask));
130 
131     d_rpc_vars.emplace_back(
132         new rpcbasic_register_get<ctrlport_probe2_i, int>(alias(),
133                                                           "length",
134                                                           &ctrlport_probe2_i::length,
135                                                           pmt::mp(1),
136                                                           pmt::mp(10 * len),
137                                                           pmt::mp(len),
138                                                           "samples",
139                                                           "get vector length",
140                                                           RPC_PRIVLVL_MIN,
141                                                           DISPNULL));
142 
143     d_rpc_vars.emplace_back(
144         new rpcbasic_register_set<ctrlport_probe2_i, int>(alias(),
145                                                           "length",
146                                                           &ctrlport_probe2_i::set_length,
147                                                           pmt::mp(1),
148                                                           pmt::mp(10 * len),
149                                                           pmt::mp(len),
150                                                           "samples",
151                                                           "set vector length",
152                                                           RPC_PRIVLVL_MIN,
153                                                           DISPNULL));
154 #endif /* GR_CTRLPORT */
155 }
156 
157 } /* namespace blocks */
158 } /* namespace gr */
159