]> sigrok.org Git - pulseview.git/blame - pv/view/analogsignal.cpp
LogicSegment: Remove constructor requiring sigrok::Logic
[pulseview.git] / pv / view / analogsignal.cpp
CommitLineData
aba1dd16
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
6 * This program 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 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
aba1dd16
JH
18 */
19
20#include <extdef.h>
21
3b68d03d
JH
22#include <cassert>
23#include <cmath>
8a2fafcc
JH
24#include <cstdlib>
25#include <limits>
aba1dd16 26
37b9fed4 27#include <QApplication>
73e377fe 28#include <QCheckBox>
368a37c2 29#include <QComboBox>
4cffac16 30#include <QFormLayout>
368a37c2
SA
31#include <QGridLayout>
32#include <QLabel>
4cffac16 33#include <QSpinBox>
03cc651d 34#include <QString>
37b9fed4 35
2acdb232
JH
36#include "analogsignal.hpp"
37#include "pv/data/analog.hpp"
f3d66e52 38#include "pv/data/analogsegment.hpp"
bf0edd2b 39#include "pv/data/signalbase.hpp"
2acdb232 40#include "pv/view/view.hpp"
8de1e1b2 41#include "pv/globalsettings.hpp"
aba1dd16 42
fe3a1c21 43#include <libsigrokcxx/libsigrokcxx.hpp>
e8d00928 44
6f925ba9
UH
45using std::deque;
46using std::div;
47using std::div_t;
819f4c25 48using std::max;
a5d93c27 49using std::make_pair;
819f4c25 50using std::min;
6f925ba9
UH
51using std::numeric_limits;
52using std::pair;
f9abf97e 53using std::shared_ptr;
aba1dd16
JH
54
55namespace pv {
f4e57597
SA
56namespace views {
57namespace TraceView {
aba1dd16 58
568b90d4
JH
59const QColor AnalogSignal::SignalColours[4] = {
60 QColor(0xC4, 0xA0, 0x00), // Yellow
61 QColor(0x87, 0x20, 0x7A), // Magenta
62 QColor(0x20, 0x4A, 0x87), // Blue
63 QColor(0x4E, 0x9A, 0x06) // Green
64};
65
46a0cadc
SA
66const QColor AnalogSignal::GridMajorColor = QColor(0, 0, 0, 40*256/100);
67const QColor AnalogSignal::GridMinorColor = QColor(0, 0, 0, 20*256/100);
37b9fed4 68
8de1e1b2
UH
69const QColor AnalogSignal::SamplingPointColour(0x77, 0x77, 0x77);
70
7c68ddae
JH
71const float AnalogSignal::EnvelopeThreshold = 256.0f;
72
4cffac16 73const int AnalogSignal::MaximumVDivs = 10;
368a37c2
SA
74const int AnalogSignal::MinScaleIndex = -6;
75const int AnalogSignal::MaxScaleIndex = 7;
4cffac16 76
03cc651d
SA
77const int AnalogSignal::InfoTextMarginRight = 20;
78const int AnalogSignal::InfoTextMarginBottom = 5;
79
b86aa8f4 80AnalogSignal::AnalogSignal(
2b81ae46 81 pv::Session &session,
cbd2a2de 82 shared_ptr<data::SignalBase> base) :
73a25a6e 83 Signal(session, base),
834a4f1b 84 scale_index_(4), // 20 per div
37b9fed4
SA
85 scale_index_drag_offset_(0),
86 div_height_(3 * QFontMetrics(QApplication::font()).height()),
459db2c5
SA
87 pos_vdivs_(1),
88 neg_vdivs_(1),
73e377fe 89 resolution_(0),
1f1d55ce 90 autoranging_(true)
aba1dd16 91{
85715407
SA
92 pv::data::Analog* analog_data =
93 dynamic_cast<pv::data::Analog*>(data().get());
94
95 connect(analog_data, SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
96 this, SLOT(on_samples_added()));
97
73a25a6e 98 base_->set_colour(SignalColours[base_->index() % countof(SignalColours)]);
834a4f1b 99 update_scale();
aba1dd16
JH
100}
101
3009b5b3
JH
102shared_ptr<pv::data::SignalData> AnalogSignal::data() const
103{
cbd2a2de 104 return base_->analog_data();
9a0cd293
JH
105}
106
3a21afa6
SA
107void AnalogSignal::save_settings(QSettings &settings) const
108{
459db2c5
SA
109 settings.setValue("pos_vdivs", pos_vdivs_);
110 settings.setValue("neg_vdivs", neg_vdivs_);
3a21afa6 111 settings.setValue("scale_index", scale_index_);
73e377fe 112 settings.setValue("autoranging", autoranging_);
3a21afa6
SA
113}
114
115void AnalogSignal::restore_settings(QSettings &settings)
116{
459db2c5
SA
117 if (settings.contains("pos_vdivs"))
118 pos_vdivs_ = settings.value("pos_vdivs").toInt();
119
120 if (settings.contains("neg_vdivs"))
121 neg_vdivs_ = settings.value("neg_vdivs").toInt();
3a21afa6
SA
122
123 if (settings.contains("scale_index")) {
124 scale_index_ = settings.value("scale_index").toInt();
125 update_scale();
126 }
73e377fe
SA
127
128 if (settings.contains("autoranging"))
129 autoranging_ = settings.value("autoranging").toBool();
3a21afa6
SA
130}
131
6f925ba9 132pair<int, int> AnalogSignal::v_extents() const
a5d93c27 133{
459db2c5
SA
134 const int ph = pos_vdivs_ * div_height_;
135 const int nh = neg_vdivs_ * div_height_;
136 return make_pair(-ph, nh);
a5d93c27
JH
137}
138
214470fc
JH
139int AnalogSignal::scale_handle_offset() const
140{
459db2c5 141 const int h = (pos_vdivs_ + neg_vdivs_) * div_height_;
37b9fed4 142
8a2fafcc 143 return ((scale_index_drag_offset_ - scale_index_) *
37b9fed4 144 h / 4) - h / 2;
214470fc
JH
145}
146
147void AnalogSignal::scale_handle_dragged(int offset)
148{
459db2c5 149 const int h = (pos_vdivs_ + neg_vdivs_) * div_height_;
37b9fed4 150
8a2fafcc 151 scale_index_ = scale_index_drag_offset_ -
37b9fed4 152 (offset + h / 2) / (h / 4);
834a4f1b
SA
153
154 update_scale();
8a2fafcc
JH
155}
156
157void AnalogSignal::scale_handle_drag_release()
158{
159 scale_index_drag_offset_ = scale_index_;
834a4f1b 160 update_scale();
214470fc
JH
161}
162
5b5fa4da 163void AnalogSignal::paint_back(QPainter &p, const ViewItemPaintParams &pp)
fe08b6e8 164{
73a25a6e 165 if (base_->enabled()) {
99af6802 166 Trace::paint_back(p, pp);
97904bf7 167 paint_axis(p, pp, get_visual_y());
99af6802 168 }
fe08b6e8
JH
169}
170
5b5fa4da 171void AnalogSignal::paint_mid(QPainter &p, const ViewItemPaintParams &pp)
aba1dd16 172{
cbd2a2de 173 assert(base_->analog_data());
8dbbc7f0 174 assert(owner_);
a8acb46e 175
be9e7b4b 176 const int y = get_visual_y();
01fd3263 177
73a25a6e 178 if (!base_->enabled())
cec48d16
JH
179 return;
180
37b9fed4
SA
181 paint_grid(p, y, pp.left(), pp.right());
182
f3d66e52 183 const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
cbd2a2de 184 base_->analog_data()->analog_segments();
f3d66e52 185 if (segments.empty())
a8acb46e
JH
186 return;
187
f3d66e52
JH
188 const shared_ptr<pv::data::AnalogSegment> &segment =
189 segments.front();
a8acb46e 190
4c8a6a6d 191 const double pixels_offset = pp.pixels_offset();
69e33a1b 192 const double samplerate = max(1.0, segment->samplerate());
60d9b99a 193 const pv::util::Timestamp& start_time = segment->start_time();
f3d66e52 194 const int64_t last_sample = segment->get_sample_count() - 1;
4c8a6a6d 195 const double samples_per_pixel = samplerate * pp.scale();
60d9b99a
JS
196 const pv::util::Timestamp start = samplerate * (pp.offset() - start_time);
197 const pv::util::Timestamp end = start + samples_per_pixel * pp.width();
a8acb46e 198
60d9b99a 199 const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(),
a8acb46e 200 (int64_t)0), last_sample);
60d9b99a 201 const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(),
a8acb46e
JH
202 (int64_t)0), last_sample);
203
7c68ddae 204 if (samples_per_pixel < EnvelopeThreshold)
f3d66e52 205 paint_trace(p, segment, y, pp.left(),
7c68ddae
JH
206 start_sample, end_sample,
207 pixels_offset, samples_per_pixel);
208 else
f3d66e52 209 paint_envelope(p, segment, y, pp.left(),
7c68ddae
JH
210 start_sample, end_sample,
211 pixels_offset, samples_per_pixel);
212}
213
03cc651d
SA
214void AnalogSignal::paint_fore(QPainter &p, const ViewItemPaintParams &pp)
215{
216 if (!enabled())
217 return;
218
219 const int y = get_visual_y();
220
221 // Show the info section on the right side of the trace
222 const QString infotext = QString("%1 V/div").arg(resolution_);
223
73a25a6e 224 p.setPen(base_->colour());
03cc651d
SA
225 p.setFont(QApplication::font());
226
227 const QRectF bounding_rect = QRectF(pp.left(),
228 y + v_extents().first,
229 pp.width() - InfoTextMarginRight,
230 v_extents().second - v_extents().first - InfoTextMarginBottom);
231
232 p.drawText(bounding_rect, Qt::AlignRight | Qt::AlignBottom, infotext);
233}
234
37b9fed4
SA
235void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right)
236{
46a0cadc
SA
237 p.setRenderHint(QPainter::Antialiasing, false);
238
459db2c5
SA
239 if (pos_vdivs_ > 0) {
240 p.setPen(QPen(GridMajorColor, 1, Qt::DashLine));
241 for (int i = 1; i <= pos_vdivs_; i++) {
242 const float dy = i * div_height_;
243 p.drawLine(QLineF(left, y - dy, right, y - dy));
244 }
245
246 p.setPen(QPen(GridMinorColor, 1, Qt::DashLine));
247 for (int i = 0; i < pos_vdivs_; i++) {
248 const float dy = i * div_height_;
249 const float dy25 = dy + (0.25 * div_height_);
250 const float dy50 = dy + (0.50 * div_height_);
251 const float dy75 = dy + (0.75 * div_height_);
252 p.drawLine(QLineF(left, y - dy25, right, y - dy25));
253 p.drawLine(QLineF(left, y - dy50, right, y - dy50));
254 p.drawLine(QLineF(left, y - dy75, right, y - dy75));
255 }
37b9fed4
SA
256 }
257
459db2c5
SA
258 if (neg_vdivs_ > 0) {
259 p.setPen(QPen(GridMajorColor, 1, Qt::DashLine));
260 for (int i = 1; i <= neg_vdivs_; i++) {
261 const float dy = i * div_height_;
262 p.drawLine(QLineF(left, y + dy, right, y + dy));
263 }
264
265 p.setPen(QPen(GridMinorColor, 1, Qt::DashLine));
266 for (int i = 0; i < neg_vdivs_; i++) {
267 const float dy = i * div_height_;
268 const float dy25 = dy + (0.25 * div_height_);
269 const float dy50 = dy + (0.50 * div_height_);
270 const float dy75 = dy + (0.75 * div_height_);
271 p.drawLine(QLineF(left, y + dy25, right, y + dy25));
272 p.drawLine(QLineF(left, y + dy50, right, y + dy50));
273 p.drawLine(QLineF(left, y + dy75, right, y + dy75));
274 }
37b9fed4 275 }
46a0cadc
SA
276
277 p.setRenderHint(QPainter::Antialiasing, true);
37b9fed4
SA
278}
279
7c68ddae 280void AnalogSignal::paint_trace(QPainter &p,
f3d66e52 281 const shared_ptr<pv::data::AnalogSegment> &segment,
7c68ddae
JH
282 int y, int left, const int64_t start, const int64_t end,
283 const double pixels_offset, const double samples_per_pixel)
284{
73a25a6e 285 p.setPen(base_->colour());
7c68ddae 286
76ce6c7a
UH
287 const int64_t points_count = end - start;
288
289 QPointF *points = new QPointF[points_count];
a8acb46e
JH
290 QPointF *point = points;
291
8de1e1b2
UH
292 QRectF *const sampling_points = new QRectF[points_count];
293 QRectF *sampling_point = sampling_points;
294
26a883ed
SA
295 pv::data::SegmentAnalogDataIterator* it =
296 segment->begin_sample_iteration(start);
297
8de1e1b2 298 const int w = 2;
7c68ddae 299 for (int64_t sample = start; sample != end; sample++) {
a8acb46e 300 const float x = (sample / samples_per_pixel -
2658961b 301 pixels_offset) + left;
26a883ed
SA
302
303 *point++ = QPointF(x, y - *((float*)it->value) * scale_);
8de1e1b2
UH
304 *sampling_point++ = QRectF(x - (w / 2), y - *((float*)it->value) * scale_ - (w / 2), w, w);
305
26a883ed 306 segment->continue_sample_iteration(it, 1);
a8acb46e 307 }
26a883ed 308 segment->end_sample_iteration(it);
a8acb46e 309
76ce6c7a 310 p.drawPolyline(points, points_count);
a8acb46e 311
8de1e1b2
UH
312 // Paint the sampling points if enabled
313 GlobalSettings settings;
314 const bool show_sampling_points =
315 settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool();
00f6bae9
UH
316
317 if (show_sampling_points && (samples_per_pixel < 0.25)) {
8de1e1b2
UH
318 p.setPen(SamplingPointColour);
319 p.drawRects(sampling_points, points_count);
320 }
321
a8acb46e 322 delete[] points;
8de1e1b2 323 delete[] sampling_points;
aba1dd16
JH
324}
325
7c68ddae 326void AnalogSignal::paint_envelope(QPainter &p,
f3d66e52 327 const shared_ptr<pv::data::AnalogSegment> &segment,
7c68ddae
JH
328 int y, int left, const int64_t start, const int64_t end,
329 const double pixels_offset, const double samples_per_pixel)
330{
f3d66e52 331 using pv::data::AnalogSegment;
7c68ddae 332
f3d66e52
JH
333 AnalogSegment::EnvelopeSection e;
334 segment->get_envelope_section(e, start, end, samples_per_pixel);
7c68ddae
JH
335
336 if (e.length < 2)
337 return;
338
819f4c25 339 p.setPen(QPen(Qt::NoPen));
73a25a6e 340 p.setBrush(base_->colour());
7c68ddae
JH
341
342 QRectF *const rects = new QRectF[e.length];
343 QRectF *rect = rects;
344
f3290553 345 for (uint64_t sample = 0; sample < e.length-1; sample++) {
7c68ddae
JH
346 const float x = ((e.scale * sample + e.start) /
347 samples_per_pixel - pixels_offset) + left;
f3d66e52 348 const AnalogSegment::EnvelopeSample *const s =
7c68ddae
JH
349 e.samples + sample;
350
351 // We overlap this sample with the next so that vertical
352 // gaps do not appear during steep rising or falling edges
834a4f1b
SA
353 const float b = y - max(s->max, (s+1)->min) * scale_;
354 const float t = y - min(s->min, (s+1)->max) * scale_;
7c68ddae
JH
355
356 float h = b - t;
f3290553 357 if (h >= 0.0f && h <= 1.0f)
7c68ddae 358 h = 1.0f;
f3290553 359 if (h <= 0.0f && h >= -1.0f)
7c68ddae
JH
360 h = -1.0f;
361
362 *rect++ = QRectF(x, t, 1.0f, h);
363 }
364
365 p.drawRects(rects, e.length);
366
367 delete[] rects;
368 delete[] e.samples;
369}
370
368a37c2 371float AnalogSignal::get_resolution(int scale_index)
8a2fafcc
JH
372{
373 const float seq[] = {1.0f, 2.0f, 5.0f};
834a4f1b 374
6f925ba9
UH
375 const int offset = numeric_limits<int>::max() / (2 * countof(seq));
376 const div_t d = div((int)(scale_index + countof(seq) * offset),
8a2fafcc 377 countof(seq));
834a4f1b 378
368a37c2
SA
379 return powf(10.0f, d.quot - offset) * seq[d.rem];
380}
381
382void AnalogSignal::update_scale()
383{
384 resolution_ = get_resolution(scale_index_);
834a4f1b 385 scale_ = div_height_ / resolution_;
8a2fafcc
JH
386}
387
73e377fe
SA
388void AnalogSignal::perform_autoranging(bool force_update)
389{
390 const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
391 base_->analog_data()->analog_segments();
392
393 if (segments.empty())
394 return;
395
396 static double prev_min = 0, prev_max = 0;
397 double min = 0, max = 0;
398
399 for (shared_ptr<pv::data::AnalogSegment> segment : segments) {
6f925ba9 400 pair<double, double> mm = segment->get_min_max();
73e377fe
SA
401 min = std::min(min, mm.first);
402 max = std::max(max, mm.second);
403 }
404
405 if ((min == prev_min) && (max == prev_max) && !force_update)
406 return;
407
408 prev_min = min;
409 prev_max = max;
410
411 // Use all divs for the positive range if there are no negative values
412 if ((min == 0) && (neg_vdivs_ > 0)) {
413 pos_vdivs_ += neg_vdivs_;
414 neg_vdivs_ = 0;
415 }
416
681b6d5a
SA
417 // Split up the divs if there are negative values but no negative divs
418 if ((min < 0) && (neg_vdivs_ == 0)) {
419 neg_vdivs_ = pos_vdivs_ / 2;
420 pos_vdivs_ -= neg_vdivs_;
421 }
422
73e377fe
SA
423 double min_value_per_div;
424 if ((pos_vdivs_ > 0) && (neg_vdivs_ > 0))
425 min_value_per_div = std::max(max / pos_vdivs_, -min / neg_vdivs_);
426 else if (pos_vdivs_ > 0)
427 min_value_per_div = max / pos_vdivs_;
428 else
429 min_value_per_div = -min / neg_vdivs_;
430
431 // Find first scale value that is bigger than the value we need
432 for (int i = MinScaleIndex; i < MaxScaleIndex; i++)
433 if (get_resolution(i) > min_value_per_div) {
434 scale_index_ = i;
435 break;
436 }
437
438 update_scale();
439}
440
4cffac16
SA
441void AnalogSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
442{
443 // Add the standard options
444 Signal::populate_popup_form(parent, form);
445
368a37c2
SA
446 QFormLayout *const layout = new QFormLayout;
447
448 // Add the number of vdivs
459db2c5
SA
449 QSpinBox *pvdiv_sb = new QSpinBox(parent);
450 pvdiv_sb->setRange(0, MaximumVDivs);
451 pvdiv_sb->setValue(pos_vdivs_);
452 connect(pvdiv_sb, SIGNAL(valueChanged(int)),
453 this, SLOT(on_pos_vdivs_changed(int)));
454 layout->addRow(tr("Number of pos vertical divs"), pvdiv_sb);
455
456 QSpinBox *nvdiv_sb = new QSpinBox(parent);
457 nvdiv_sb->setRange(0, MaximumVDivs);
458 nvdiv_sb->setValue(neg_vdivs_);
459 connect(nvdiv_sb, SIGNAL(valueChanged(int)),
460 this, SLOT(on_neg_vdivs_changed(int)));
461 layout->addRow(tr("Number of neg vertical divs"), nvdiv_sb);
368a37c2
SA
462
463 // Add the vertical resolution
464 resolution_cb_ = new QComboBox(parent);
465
466 for (int i = MinScaleIndex; i < MaxScaleIndex; i++) {
467 const QString label = QString("%1").arg(get_resolution(i));
468 resolution_cb_->insertItem(0, label, QVariant(i));
469 }
470
471 const int cur_idx = resolution_cb_->findData(QVariant(scale_index_));
472 resolution_cb_->setCurrentIndex(cur_idx);
473
474 connect(resolution_cb_, SIGNAL(currentIndexChanged(int)),
475 this, SLOT(on_resolution_changed(int)));
476
477 QGridLayout *const vdiv_layout = new QGridLayout;
478 QLabel *const vdiv_unit = new QLabel(tr("V/div"));
479 vdiv_layout->addWidget(resolution_cb_, 0, 0);
480 vdiv_layout->addWidget(vdiv_unit, 0, 1);
481
482 layout->addRow(tr("Vertical resolution"), vdiv_layout);
483
73e377fe
SA
484 // Add the autoranging checkbox
485 QCheckBox* autoranging_cb = new QCheckBox();
486 autoranging_cb->setCheckState(autoranging_ ? Qt::Checked : Qt::Unchecked);
487
488 connect(autoranging_cb, SIGNAL(stateChanged(int)),
489 this, SLOT(on_autoranging_changed(int)));
490
491 layout->addRow(tr("Autoranging"), autoranging_cb);
492
368a37c2 493 form->addRow(layout);
4cffac16
SA
494}
495
85715407
SA
496void AnalogSignal::on_samples_added()
497{
498 perform_autoranging();
499
500 if (owner_) {
501 // Call order is important, otherwise the lazy event handler won't work
502 owner_->extents_changed(false, true);
503 owner_->row_item_appearance_changed(false, true);
504 }
505}
506
459db2c5
SA
507void AnalogSignal::on_pos_vdivs_changed(int vdivs)
508{
509 pos_vdivs_ = vdivs;
510
73e377fe
SA
511 if (autoranging_)
512 perform_autoranging(true);
513
459db2c5
SA
514 if (owner_) {
515 // Call order is important, otherwise the lazy event handler won't work
516 owner_->extents_changed(false, true);
517 owner_->row_item_appearance_changed(false, true);
518 }
519}
520
521void AnalogSignal::on_neg_vdivs_changed(int vdivs)
4cffac16 522{
459db2c5 523 neg_vdivs_ = vdivs;
4cffac16 524
73e377fe
SA
525 if (autoranging_)
526 perform_autoranging(true);
527
75d0779e
SA
528 if (owner_) {
529 // Call order is important, otherwise the lazy event handler won't work
4cffac16 530 owner_->extents_changed(false, true);
75d0779e
SA
531 owner_->row_item_appearance_changed(false, true);
532 }
4cffac16
SA
533}
534
368a37c2
SA
535void AnalogSignal::on_resolution_changed(int index)
536{
537 scale_index_ = resolution_cb_->itemData(index).toInt();
538 update_scale();
539
540 if (owner_)
541 owner_->row_item_appearance_changed(false, true);
542}
4cffac16 543
73e377fe
SA
544void AnalogSignal::on_autoranging_changed(int state)
545{
546 autoranging_ = (state == Qt::Checked);
547
548 if (autoranging_)
549 perform_autoranging(true);
550
85715407
SA
551 if (owner_) {
552 // Call order is important, otherwise the lazy event handler won't work
553 owner_->extents_changed(false, true);
73e377fe 554 owner_->row_item_appearance_changed(false, true);
85715407 555 }
73e377fe
SA
556}
557
f4e57597
SA
558} // namespace TraceView
559} // namespace views
aba1dd16 560} // namespace pv