]> sigrok.org Git - pulseview.git/blame_incremental - pv/data/signalbase.cpp
Make get_raw_samples() use provided mem instead of allocating
[pulseview.git] / pv / data / signalbase.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 * Copyright (C) 2016 Soeren Apel <soeren@apelpie.net>
6 *
7 * This program 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 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "analog.hpp"
22#include "analogsegment.hpp"
23#include "decode/row.hpp"
24#include "logic.hpp"
25#include "logicsegment.hpp"
26#include "signalbase.hpp"
27#include "signaldata.hpp"
28
29#include <pv/binding/decoder.hpp>
30#include <pv/session.hpp>
31
32using std::dynamic_pointer_cast;
33using std::make_shared;
34using std::shared_ptr;
35using std::tie;
36using std::unique_lock;
37
38namespace pv {
39namespace data {
40
41const int SignalBase::ColourBGAlpha = 8 * 256 / 100;
42const uint64_t SignalBase::ConversionBlockSize = 4096;
43
44SignalBase::SignalBase(shared_ptr<sigrok::Channel> channel, ChannelType channel_type) :
45 channel_(channel),
46 channel_type_(channel_type),
47 conversion_type_(NoConversion)
48{
49 if (channel_)
50 internal_name_ = QString::fromStdString(channel_->name());
51}
52
53SignalBase::~SignalBase()
54{
55 stop_conversion();
56}
57
58shared_ptr<sigrok::Channel> SignalBase::channel() const
59{
60 return channel_;
61}
62
63QString SignalBase::name() const
64{
65 return (channel_) ? QString::fromStdString(channel_->name()) : name_;
66}
67
68QString SignalBase::internal_name() const
69{
70 return internal_name_;
71}
72
73void SignalBase::set_name(QString name)
74{
75 if (channel_)
76 channel_->set_name(name.toUtf8().constData());
77
78 name_ = name;
79
80 name_changed(name);
81}
82
83bool SignalBase::enabled() const
84{
85 return (channel_) ? channel_->enabled() : true;
86}
87
88void SignalBase::set_enabled(bool value)
89{
90 if (channel_) {
91 channel_->set_enabled(value);
92 enabled_changed(value);
93 }
94}
95
96SignalBase::ChannelType SignalBase::type() const
97{
98 return channel_type_;
99}
100
101unsigned int SignalBase::index() const
102{
103 return (channel_) ? channel_->index() : 0;
104}
105
106unsigned int SignalBase::logic_bit_index() const
107{
108 if (channel_type_ == LogicChannel)
109 return channel_->index();
110 else
111 return 0;
112}
113
114QColor SignalBase::colour() const
115{
116 return colour_;
117}
118
119void SignalBase::set_colour(QColor colour)
120{
121 colour_ = colour;
122
123 bgcolour_ = colour;
124 bgcolour_.setAlpha(ColourBGAlpha);
125
126 colour_changed(colour);
127}
128
129QColor SignalBase::bgcolour() const
130{
131 return bgcolour_;
132}
133
134void SignalBase::set_data(shared_ptr<pv::data::SignalData> data)
135{
136 if (data_) {
137 disconnect(data.get(), SIGNAL(samples_cleared()),
138 this, SLOT(on_samples_cleared()));
139 disconnect(data.get(), SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
140 this, SLOT(on_samples_added(QObject*, uint64_t, uint64_t)));
141 }
142
143 data_ = data;
144
145 if (data_) {
146 connect(data.get(), SIGNAL(samples_cleared()),
147 this, SLOT(on_samples_cleared()));
148 connect(data.get(), SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
149 this, SLOT(on_samples_added(QObject*, uint64_t, uint64_t)));
150 }
151}
152
153shared_ptr<data::Analog> SignalBase::analog_data() const
154{
155 shared_ptr<Analog> result = nullptr;
156
157 if (channel_type_ == AnalogChannel)
158 result = dynamic_pointer_cast<Analog>(data_);
159
160 return result;
161}
162
163shared_ptr<data::Logic> SignalBase::logic_data() const
164{
165 shared_ptr<Logic> result = nullptr;
166
167 if (channel_type_ == LogicChannel)
168 result = dynamic_pointer_cast<Logic>(data_);
169
170 if (((conversion_type_ == A2LConversionByTreshold) ||
171 (conversion_type_ == A2LConversionBySchmittTrigger)))
172 result = dynamic_pointer_cast<Logic>(converted_data_);
173
174 return result;
175}
176
177void SignalBase::set_conversion_type(ConversionType t)
178{
179 if (conversion_type_ != NoConversion) {
180 stop_conversion();
181
182 // Discard converted data
183 converted_data_.reset();
184 samples_cleared();
185 }
186
187 conversion_type_ = t;
188
189 // Re-create an empty container
190 // so that the signal is recognized as providing logic data
191 // and thus can be assigned to a decoder
192 if (conversion_is_a2l())
193 if (!converted_data_)
194 converted_data_ = make_shared<Logic>(1); // Contains only one channel
195
196 start_conversion();
197
198 conversion_type_changed(t);
199}
200
201#ifdef ENABLE_DECODE
202bool SignalBase::is_decode_signal() const
203{
204 return (channel_type_ == DecodeChannel);
205}
206#endif
207
208void SignalBase::save_settings(QSettings &settings) const
209{
210 settings.setValue("name", name());
211 settings.setValue("enabled", enabled());
212 settings.setValue("colour", colour());
213 settings.setValue("conversion_type", (int)conversion_type_);
214}
215
216void SignalBase::restore_settings(QSettings &settings)
217{
218 set_name(settings.value("name").toString());
219 set_enabled(settings.value("enabled").toBool());
220 set_colour(settings.value("colour").value<QColor>());
221 set_conversion_type((ConversionType)settings.value("conversion_type").toInt());
222}
223
224uint8_t SignalBase::convert_a2l_threshold(float threshold, float value)
225{
226 return (value >= threshold) ? 1 : 0;
227}
228
229uint8_t SignalBase::convert_a2l_schmitt_trigger(float lo_thr, float hi_thr,
230 float value, uint8_t &state)
231{
232 if (value < lo_thr)
233 state = 0;
234 else if (value > hi_thr)
235 state = 1;
236
237 return state;
238}
239
240bool SignalBase::conversion_is_a2l() const
241{
242 return ((channel_type_ == AnalogChannel) &&
243 ((conversion_type_ == A2LConversionByTreshold) ||
244 (conversion_type_ == A2LConversionBySchmittTrigger)));
245}
246
247void SignalBase::conversion_thread_proc(QObject* segment)
248{
249 // TODO Support for multiple segments is missing
250
251 uint64_t start_sample, end_sample;
252 start_sample = end_sample = 0;
253
254 do {
255 if (conversion_is_a2l()) {
256
257 AnalogSegment *asegment = qobject_cast<AnalogSegment*>(segment);
258
259 const shared_ptr<Logic> logic_data = dynamic_pointer_cast<Logic>(converted_data_);
260
261 // Create the initial logic data segment if needed
262 if (logic_data->segments().size() == 0) {
263 shared_ptr<LogicSegment> lsegment =
264 make_shared<LogicSegment>(*logic_data.get(), 1, asegment->samplerate());
265 logic_data->push_segment(lsegment);
266 }
267
268 LogicSegment *lsegment = dynamic_cast<LogicSegment*>(logic_data->segments().front().get());
269
270 start_sample = lsegment->get_sample_count();
271 end_sample = asegment->get_sample_count();
272
273 if (end_sample > start_sample) {
274 float min_v, max_v;
275 tie(min_v, max_v) = asegment->get_min_max();
276
277 float* asamples = new float[ConversionBlockSize];
278 vector<uint8_t> lsamples;
279 lsamples.reserve(ConversionBlockSize);
280
281 uint64_t i = start_sample;
282
283 if (conversion_type_ == A2LConversionByTreshold) {
284 const float threshold = (min_v + max_v) * 0.5; // middle between min and max
285
286 // Convert as many sample blocks as we can
287 while ((end_sample - i) > ConversionBlockSize) {
288 asegment->get_samples(i, i + ConversionBlockSize, asamples);
289 for (uint32_t j = 0; j < ConversionBlockSize; j++)
290 lsamples.push_back(convert_a2l_threshold(threshold, asamples[j]));
291 lsegment->append_payload(lsamples.data(), lsamples.size());
292 samples_added(lsegment, i, i + ConversionBlockSize);
293 i += ConversionBlockSize;
294 lsamples.clear();
295 }
296
297 // Convert remaining samples
298 asegment->get_samples(i, end_sample, asamples);
299 for (uint32_t j = 0; j < (end_sample - i); j++)
300 lsamples.push_back(convert_a2l_threshold(threshold, asamples[j]));
301 lsegment->append_payload(lsamples.data(), lsamples.size());
302 samples_added(lsegment, i, end_sample);
303 }
304
305 if (conversion_type_ == A2LConversionBySchmittTrigger) {
306 const float amplitude = max_v - min_v;
307 const float lo_thr = min_v + (amplitude * 0.1); // 10% above min
308 const float hi_thr = max_v - (amplitude * 0.1); // 10% below max
309 uint8_t state = 0; // TODO Use value of logic sample n-1 instead of 0
310
311 // Convert as many sample blocks as we can
312 while ((end_sample - i) > ConversionBlockSize) {
313 asegment->get_samples(i, i + ConversionBlockSize, asamples);
314 for (uint32_t j = 0; j < ConversionBlockSize; j++)
315 lsamples.push_back(convert_a2l_schmitt_trigger(lo_thr, hi_thr, asamples[j], state));
316 lsegment->append_payload(lsamples.data(), lsamples.size());
317 samples_added(lsegment, i, i + ConversionBlockSize);
318 i += ConversionBlockSize;
319 lsamples.clear();
320 }
321
322 // Convert remaining samples
323 asegment->get_samples(i, end_sample, asamples);
324 for (uint32_t j = 0; j < (end_sample - i); j++)
325 lsamples.push_back(convert_a2l_schmitt_trigger(lo_thr, hi_thr, asamples[j], state));
326 lsegment->append_payload(lsamples.data(), lsamples.size());
327 samples_added(lsegment, i, end_sample);
328 }
329
330 // If acquisition is ongoing, start-/endsample may have changed
331 end_sample = asegment->get_sample_count();
332
333 delete[] asamples;
334 }
335 }
336
337 if (!conversion_interrupt_ && (start_sample == end_sample)) {
338 unique_lock<mutex> input_lock(conversion_input_mutex_);
339 conversion_input_cond_.wait(input_lock);
340 }
341 } while (!conversion_interrupt_);
342}
343
344void SignalBase::start_conversion()
345{
346 stop_conversion();
347
348 if (conversion_is_a2l()) {
349 shared_ptr<Analog> analog_data = dynamic_pointer_cast<Analog>(data_);
350
351 if (analog_data->analog_segments().size() > 0) {
352 // TODO Support for multiple segments is missing
353 AnalogSegment *asegment = analog_data->analog_segments().front().get();
354
355 conversion_interrupt_ = false;
356 conversion_thread_ = std::thread(
357 &SignalBase::conversion_thread_proc, this, asegment);
358 }
359 }
360}
361
362void SignalBase::stop_conversion()
363{
364 // Stop conversion so we can restart it from the beginning
365 conversion_interrupt_ = true;
366 conversion_input_cond_.notify_one();
367 if (conversion_thread_.joinable())
368 conversion_thread_.join();
369}
370
371void SignalBase::on_samples_cleared()
372{
373 if (converted_data_)
374 converted_data_->clear();
375
376 samples_cleared();
377}
378
379void SignalBase::on_samples_added(QObject* segment, uint64_t start_sample,
380 uint64_t end_sample)
381{
382 if (conversion_type_ != NoConversion) {
383 if (conversion_thread_.joinable()) {
384 // Notify the conversion thread since it's running
385 conversion_input_cond_.notify_one();
386 } else {
387 // Start the conversion thread
388 start_conversion();
389 }
390 }
391
392 samples_added(segment, start_sample, end_sample);
393}
394
395void SignalBase::on_capture_state_changed(int state)
396{
397 if (state == Session::Running) {
398 if (conversion_type_ != NoConversion) {
399 // Restart conversion
400 stop_conversion();
401 start_conversion();
402 }
403 }
404}
405
406} // namespace data
407} // namespace pv