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_s_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 disp_mask)33 ctrlport_probe2_s::sptr ctrlport_probe2_s::make(const std::string& id,
34 const std::string& desc,
35 int len,
36 unsigned disp_mask)
37 {
38 return gnuradio::get_initial_sptr(
39 new ctrlport_probe2_s_impl(id, desc, len, disp_mask));
40 }
41
ctrlport_probe2_s_impl(const std::string & id,const std::string & desc,int len,unsigned disp_mask)42 ctrlport_probe2_s_impl::ctrlport_probe2_s_impl(const std::string& id,
43 const std::string& desc,
44 int len,
45 unsigned disp_mask)
46 : sync_block("probe2_s",
47 io_signature::make(1, 1, sizeof(short)),
48 io_signature::make(0, 0, 0)),
49 d_id(id),
50 d_desc(desc),
51 d_len(len),
52 d_disp_mask(disp_mask)
53 {
54 set_length(len);
55 }
56
~ctrlport_probe2_s_impl()57 ctrlport_probe2_s_impl::~ctrlport_probe2_s_impl() {}
58
forecast(int noutput_items,gr_vector_int & ninput_items_required)59 void ctrlport_probe2_s_impl::forecast(int noutput_items,
60 gr_vector_int& ninput_items_required)
61 {
62 // make sure all inputs have noutput_items available
63 unsigned ninputs = ninput_items_required.size();
64 for (unsigned i = 0; i < ninputs; i++)
65 ninput_items_required[i] = d_len;
66 }
67
get()68 std::vector<short> ctrlport_probe2_s_impl::get() { return buffered_get.get(); }
69
set_length(int len)70 void ctrlport_probe2_s_impl::set_length(int len)
71 {
72 gr::thread::scoped_lock guard(d_setlock);
73
74 if (len > 8191) {
75 GR_LOG_WARN(d_logger,
76 boost::format("probe2_s: length %1% exceeds maximum"
77 " buffer size of 8191") %
78 len);
79 len = 8191;
80 }
81
82 d_len = len;
83 d_buffer.resize(d_len);
84 d_index = 0;
85 }
86
length() const87 int ctrlport_probe2_s_impl::length() const { return (int)d_len; }
88
work(int noutput_items,gr_vector_const_void_star & input_items,gr_vector_void_star & output_items)89 int ctrlport_probe2_s_impl::work(int noutput_items,
90 gr_vector_const_void_star& input_items,
91 gr_vector_void_star& output_items)
92 {
93 const short* in = (const short*)input_items[0];
94
95 gr::thread::scoped_lock guard(d_setlock);
96
97 // copy samples to get buffer if we need samples
98 if (d_index < d_len) {
99 // copy smaller of remaining buffer space and num inputs to work()
100 int num_copy = std::min((int)(d_len - d_index), noutput_items);
101
102 memcpy(&d_buffer[d_index], in, num_copy * sizeof(short));
103 d_index += num_copy;
104 }
105
106 // notify the waiting get() if we fill up the buffer
107 if (d_index == d_len) {
108 buffered_get.offer_data(d_buffer);
109 }
110
111 return noutput_items;
112 }
113
setup_rpc()114 void ctrlport_probe2_s_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_s, std::vector<short>>(
120 alias(),
121 d_id.c_str(),
122 &ctrlport_probe2_s::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_s, int>(alias(),
133 "length",
134 &ctrlport_probe2_s::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_s, int>(alias(),
145 "length",
146 &ctrlport_probe2_s::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