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