]> sigrok.org Git - pulseview.git/blob - pv/view/analogsignal.cpp
3c3e5d0571a36a896c91441fe9ef27321bd83cbe
[pulseview.git] / pv / view / analogsignal.cpp
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
45 using std::max;
46 using std::make_pair;
47 using std::min;
48 using std::shared_ptr;
49 using std::deque;
50
51 namespace pv {
52 namespace views {
53 namespace TraceView {
54
55 const 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
62 const QColor AnalogSignal::GridMajorColor = QColor(0, 0, 0, 40*256/100);
63 const QColor AnalogSignal::GridMinorColor = QColor(0, 0, 0, 20*256/100);
64
65 const QColor AnalogSignal::SamplingPointColour(0x77, 0x77, 0x77);
66
67 const float AnalogSignal::EnvelopeThreshold = 256.0f;
68
69 const int AnalogSignal::MaximumVDivs = 10;
70 const int AnalogSignal::MinScaleIndex = -6;
71 const int AnalogSignal::MaxScaleIndex = 7;
72
73 const int AnalogSignal::InfoTextMarginRight = 20;
74 const int AnalogSignal::InfoTextMarginBottom = 5;
75
76 AnalogSignal::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
98 shared_ptr<pv::data::SignalData> AnalogSignal::data() const
99 {
100         return base_->analog_data();
101 }
102
103 void 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
111 void 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
128 std::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
135 int 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
143 void 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
153 void AnalogSignal::scale_handle_drag_release()
154 {
155         scale_index_drag_offset_ = scale_index_;
156         update_scale();
157 }
158
159 void 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
167 void 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
210 void 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
231 void 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
276 void 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         if (show_sampling_points) {
313                 p.setPen(SamplingPointColour);
314                 p.drawRects(sampling_points, points_count);
315         }
316
317         delete[] points;
318         delete[] sampling_points;
319 }
320
321 void AnalogSignal::paint_envelope(QPainter &p,
322         const shared_ptr<pv::data::AnalogSegment> &segment,
323         int y, int left, const int64_t start, const int64_t end,
324         const double pixels_offset, const double samples_per_pixel)
325 {
326         using pv::data::AnalogSegment;
327
328         AnalogSegment::EnvelopeSection e;
329         segment->get_envelope_section(e, start, end, samples_per_pixel);
330
331         if (e.length < 2)
332                 return;
333
334         p.setPen(QPen(Qt::NoPen));
335         p.setBrush(base_->colour());
336
337         QRectF *const rects = new QRectF[e.length];
338         QRectF *rect = rects;
339
340         for (uint64_t sample = 0; sample < e.length-1; sample++) {
341                 const float x = ((e.scale * sample + e.start) /
342                         samples_per_pixel - pixels_offset) + left;
343                 const AnalogSegment::EnvelopeSample *const s =
344                         e.samples + sample;
345
346                 // We overlap this sample with the next so that vertical
347                 // gaps do not appear during steep rising or falling edges
348                 const float b = y - max(s->max, (s+1)->min) * scale_;
349                 const float t = y - min(s->min, (s+1)->max) * scale_;
350
351                 float h = b - t;
352                 if (h >= 0.0f && h <= 1.0f)
353                         h = 1.0f;
354                 if (h <= 0.0f && h >= -1.0f)
355                         h = -1.0f;
356
357                 *rect++ = QRectF(x, t, 1.0f, h);
358         }
359
360         p.drawRects(rects, e.length);
361
362         delete[] rects;
363         delete[] e.samples;
364 }
365
366 float AnalogSignal::get_resolution(int scale_index)
367 {
368         const float seq[] = {1.0f, 2.0f, 5.0f};
369
370         const int offset = std::numeric_limits<int>::max() / (2 * countof(seq));
371         const std::div_t d = std::div(
372                 (int)(scale_index + countof(seq) * offset),
373                 countof(seq));
374
375         return powf(10.0f, d.quot - offset) * seq[d.rem];
376 }
377
378 void AnalogSignal::update_scale()
379 {
380         resolution_ = get_resolution(scale_index_);
381         scale_ = div_height_ / resolution_;
382 }
383
384 void AnalogSignal::perform_autoranging(bool force_update)
385 {
386         const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
387                 base_->analog_data()->analog_segments();
388
389         if (segments.empty())
390                 return;
391
392         static double prev_min = 0, prev_max = 0;
393         double min = 0, max = 0;
394
395         for (shared_ptr<pv::data::AnalogSegment> segment : segments) {
396                 std::pair<double, double> mm = segment->get_min_max();
397                 min = std::min(min, mm.first);
398                 max = std::max(max, mm.second);
399         }
400
401         if ((min == prev_min) && (max == prev_max) && !force_update)
402                 return;
403
404         prev_min = min;
405         prev_max = max;
406
407         // Use all divs for the positive range if there are no negative values
408         if ((min == 0) && (neg_vdivs_ > 0)) {
409                 pos_vdivs_ += neg_vdivs_;
410                 neg_vdivs_ = 0;
411         }
412
413         // Split up the divs if there are negative values but no negative divs
414         if ((min < 0) && (neg_vdivs_ == 0)) {
415                 neg_vdivs_ = pos_vdivs_ / 2;
416                 pos_vdivs_ -= neg_vdivs_;
417         }
418
419         double min_value_per_div;
420         if ((pos_vdivs_ > 0) && (neg_vdivs_ >  0))
421                 min_value_per_div = std::max(max / pos_vdivs_, -min / neg_vdivs_);
422         else if (pos_vdivs_ > 0)
423                 min_value_per_div = max / pos_vdivs_;
424         else
425                 min_value_per_div = -min / neg_vdivs_;
426
427         // Find first scale value that is bigger than the value we need
428         for (int i = MinScaleIndex; i < MaxScaleIndex; i++)
429                 if (get_resolution(i) > min_value_per_div) {
430                         scale_index_ = i;
431                         break;
432                 }
433
434         update_scale();
435 }
436
437 void AnalogSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
438 {
439         // Add the standard options
440         Signal::populate_popup_form(parent, form);
441
442         QFormLayout *const layout = new QFormLayout;
443
444         // Add the number of vdivs
445         QSpinBox *pvdiv_sb = new QSpinBox(parent);
446         pvdiv_sb->setRange(0, MaximumVDivs);
447         pvdiv_sb->setValue(pos_vdivs_);
448         connect(pvdiv_sb, SIGNAL(valueChanged(int)),
449                 this, SLOT(on_pos_vdivs_changed(int)));
450         layout->addRow(tr("Number of pos vertical divs"), pvdiv_sb);
451
452         QSpinBox *nvdiv_sb = new QSpinBox(parent);
453         nvdiv_sb->setRange(0, MaximumVDivs);
454         nvdiv_sb->setValue(neg_vdivs_);
455         connect(nvdiv_sb, SIGNAL(valueChanged(int)),
456                 this, SLOT(on_neg_vdivs_changed(int)));
457         layout->addRow(tr("Number of neg vertical divs"), nvdiv_sb);
458
459         // Add the vertical resolution
460         resolution_cb_ = new QComboBox(parent);
461
462         for (int i = MinScaleIndex; i < MaxScaleIndex; i++) {
463                 const QString label = QString("%1").arg(get_resolution(i));
464                 resolution_cb_->insertItem(0, label, QVariant(i));
465         }
466
467         const int cur_idx = resolution_cb_->findData(QVariant(scale_index_));
468         resolution_cb_->setCurrentIndex(cur_idx);
469
470         connect(resolution_cb_, SIGNAL(currentIndexChanged(int)),
471                 this, SLOT(on_resolution_changed(int)));
472
473         QGridLayout *const vdiv_layout = new QGridLayout;
474         QLabel *const vdiv_unit = new QLabel(tr("V/div"));
475         vdiv_layout->addWidget(resolution_cb_, 0, 0);
476         vdiv_layout->addWidget(vdiv_unit, 0, 1);
477
478         layout->addRow(tr("Vertical resolution"), vdiv_layout);
479
480         // Add the autoranging checkbox
481         QCheckBox* autoranging_cb = new QCheckBox();
482         autoranging_cb->setCheckState(autoranging_ ? Qt::Checked : Qt::Unchecked);
483
484         connect(autoranging_cb, SIGNAL(stateChanged(int)),
485                 this, SLOT(on_autoranging_changed(int)));
486
487         layout->addRow(tr("Autoranging"), autoranging_cb);
488
489         form->addRow(layout);
490 }
491
492 void AnalogSignal::on_samples_added()
493 {
494         perform_autoranging();
495
496         if (owner_) {
497                 // Call order is important, otherwise the lazy event handler won't work
498                 owner_->extents_changed(false, true);
499                 owner_->row_item_appearance_changed(false, true);
500         }
501 }
502
503 void AnalogSignal::on_pos_vdivs_changed(int vdivs)
504 {
505         pos_vdivs_ = vdivs;
506
507         if (autoranging_)
508                 perform_autoranging(true);
509
510         if (owner_) {
511                 // Call order is important, otherwise the lazy event handler won't work
512                 owner_->extents_changed(false, true);
513                 owner_->row_item_appearance_changed(false, true);
514         }
515 }
516
517 void AnalogSignal::on_neg_vdivs_changed(int vdivs)
518 {
519         neg_vdivs_ = vdivs;
520
521         if (autoranging_)
522                 perform_autoranging(true);
523
524         if (owner_) {
525                 // Call order is important, otherwise the lazy event handler won't work
526                 owner_->extents_changed(false, true);
527                 owner_->row_item_appearance_changed(false, true);
528         }
529 }
530
531 void AnalogSignal::on_resolution_changed(int index)
532 {
533         scale_index_ = resolution_cb_->itemData(index).toInt();
534         update_scale();
535
536         if (owner_)
537                 owner_->row_item_appearance_changed(false, true);
538 }
539
540 void AnalogSignal::on_autoranging_changed(int state)
541 {
542         autoranging_ = (state == Qt::Checked);
543
544         if (autoranging_)
545                 perform_autoranging(true);
546
547         if (owner_) {
548                 // Call order is important, otherwise the lazy event handler won't work
549                 owner_->extents_changed(false, true);
550                 owner_->row_item_appearance_changed(false, true);
551         }
552 }
553
554 } // namespace TraceView
555 } // namespace views
556 } // namespace pv