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