]> sigrok.org Git - pulseview.git/blob - pv/view/analogsignal.cpp
7e5bcd11facdb6d1a81368c27fd866da1afc6ff9
[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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <extdef.h>
22
23 #include <cassert>
24 #include <cmath>
25 #include <cstdlib>
26 #include <limits>
27
28 #include <QApplication>
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/view/view.hpp"
40
41 #include <libsigrokcxx/libsigrokcxx.hpp>
42
43 using std::max;
44 using std::make_pair;
45 using std::min;
46 using std::shared_ptr;
47 using std::deque;
48
49 using sigrok::Channel;
50
51 namespace pv {
52 namespace view {
53
54 const 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
61 const QColor AnalogSignal::GridMajorColor = QColor(0xB0, 0xB0, 0xB0);
62 const QColor AnalogSignal::GridMinorColor = QColor(0xD0, 0xD0, 0xD0);
63
64 const float AnalogSignal::EnvelopeThreshold = 256.0f;
65
66 const int AnalogSignal::MaximumVDivs = 10;
67 const int AnalogSignal::MinScaleIndex = -6;
68 const int AnalogSignal::MaxScaleIndex = 7;
69
70 const int AnalogSignal::InfoTextMarginRight = 20;
71 const int AnalogSignal::InfoTextMarginBottom = 5;
72
73 AnalogSignal::AnalogSignal(
74         pv::Session &session,
75         shared_ptr<Channel> channel,
76         shared_ptr<data::Analog> data) :
77         Signal(session, channel),
78         data_(data),
79         scale_index_(4), // 20 per div
80         scale_index_drag_offset_(0),
81         div_height_(3 * QFontMetrics(QApplication::font()).height()),
82         vdivs_(1),
83         resolution_(0)
84 {
85         set_colour(SignalColours[channel_->index() % countof(SignalColours)]);
86         update_scale();
87 }
88
89 shared_ptr<pv::data::SignalData> AnalogSignal::data() const
90 {
91         return data_;
92 }
93
94 shared_ptr<pv::data::Analog> AnalogSignal::analog_data() const
95 {
96         return data_;
97 }
98
99 std::pair<int, int> AnalogSignal::v_extents() const
100 {
101         const int h = vdivs_ * div_height_;
102         return make_pair(-h, h);
103 }
104
105 int AnalogSignal::scale_handle_offset() const
106 {
107         const int h = vdivs_ * div_height_;
108
109         return ((scale_index_drag_offset_ - scale_index_) *
110                 h / 4) - h / 2;
111 }
112
113 void AnalogSignal::scale_handle_dragged(int offset)
114 {
115         const int h = vdivs_ * div_height_;
116
117         scale_index_ = scale_index_drag_offset_ -
118                 (offset + h / 2) / (h / 4);
119
120         update_scale();
121 }
122
123 void AnalogSignal::scale_handle_drag_release()
124 {
125         scale_index_drag_offset_ = scale_index_;
126         update_scale();
127 }
128
129 void AnalogSignal::paint_back(QPainter &p, const ViewItemPaintParams &pp)
130 {
131         if (channel_->enabled()) {
132                 Trace::paint_back(p, pp);
133                 paint_axis(p, pp, get_visual_y());
134         }
135 }
136
137 void AnalogSignal::paint_mid(QPainter &p, const ViewItemPaintParams &pp)
138 {
139         assert(data_);
140         assert(owner_);
141
142         const int y = get_visual_y();
143
144         if (!channel_->enabled())
145                 return;
146
147         paint_grid(p, y, pp.left(), pp.right());
148
149         const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
150                 data_->analog_segments();
151         if (segments.empty())
152                 return;
153
154         const shared_ptr<pv::data::AnalogSegment> &segment =
155                 segments.front();
156
157         const double pixels_offset = pp.pixels_offset();
158         const double samplerate = max(1.0, segment->samplerate());
159         const pv::util::Timestamp& start_time = segment->start_time();
160         const int64_t last_sample = segment->get_sample_count() - 1;
161         const double samples_per_pixel = samplerate * pp.scale();
162         const pv::util::Timestamp start = samplerate * (pp.offset() - start_time);
163         const pv::util::Timestamp end = start + samples_per_pixel * pp.width();
164
165         const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(),
166                 (int64_t)0), last_sample);
167         const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(),
168                 (int64_t)0), last_sample);
169
170         if (samples_per_pixel < EnvelopeThreshold)
171                 paint_trace(p, segment, y, pp.left(),
172                         start_sample, end_sample,
173                         pixels_offset, samples_per_pixel);
174         else
175                 paint_envelope(p, segment, y, pp.left(),
176                         start_sample, end_sample,
177                         pixels_offset, samples_per_pixel);
178 }
179
180 void AnalogSignal::paint_fore(QPainter &p, const ViewItemPaintParams &pp)
181 {
182         if (!enabled())
183                 return;
184
185         const int y = get_visual_y();
186
187         // Show the info section on the right side of the trace
188         const QString infotext = QString("%1 V/div").arg(resolution_);
189
190         p.setPen(colour_);
191         p.setFont(QApplication::font());
192
193         const QRectF bounding_rect = QRectF(pp.left(),
194                         y + v_extents().first,
195                         pp.width() - InfoTextMarginRight,
196                         v_extents().second - v_extents().first - InfoTextMarginBottom);
197
198         p.drawText(bounding_rect, Qt::AlignRight | Qt::AlignBottom, infotext);
199 }
200
201 void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right)
202 {
203         p.setPen(QPen(GridMajorColor, 0.5, Qt::DashLine));
204         for (int i = 1; i <= vdivs_; i++) {
205                 const int dy = i * div_height_;
206                 p.drawLine(QLineF(left, y - dy, right, y - dy));
207                 p.drawLine(QLineF(left, y + dy, right, y + dy));
208         }
209
210         p.setPen(QPen(GridMinorColor, 0.5, Qt::DashLine));
211         for (int i = 0; i < vdivs_; i++) {
212                 const int dy = i * div_height_;
213                 const float dy25 = dy + (0.25 * div_height_);
214                 const float dy50 = dy + (0.50 * div_height_);
215                 const float dy75 = dy + (0.75 * div_height_);
216                 p.drawLine(QLineF(left, y - dy25, right, y - dy25));
217                 p.drawLine(QLineF(left, y + dy25, right, y + dy25));
218                 p.drawLine(QLineF(left, y - dy50, right, y - dy50));
219                 p.drawLine(QLineF(left, y + dy50, right, y + dy50));
220                 p.drawLine(QLineF(left, y - dy75, right, y - dy75));
221                 p.drawLine(QLineF(left, y + dy75, right, y + dy75));
222         }
223 }
224
225 void AnalogSignal::paint_trace(QPainter &p,
226         const shared_ptr<pv::data::AnalogSegment> &segment,
227         int y, int left, const int64_t start, const int64_t end,
228         const double pixels_offset, const double samples_per_pixel)
229 {
230         const int64_t sample_count = end - start;
231
232         const float *const samples = segment->get_samples(start, end);
233         assert(samples);
234
235         p.setPen(colour_);
236
237         QPointF *points = new QPointF[sample_count];
238         QPointF *point = points;
239
240         for (int64_t sample = start; sample != end; sample++) {
241                 const float x = (sample / samples_per_pixel -
242                         pixels_offset) + left;
243                 *point++ = QPointF(x,
244                         y - samples[sample - start] * scale_);
245         }
246
247         p.drawPolyline(points, point - points);
248
249         delete[] samples;
250         delete[] points;
251 }
252
253 void AnalogSignal::paint_envelope(QPainter &p,
254         const shared_ptr<pv::data::AnalogSegment> &segment,
255         int y, int left, const int64_t start, const int64_t end,
256         const double pixels_offset, const double samples_per_pixel)
257 {
258         using pv::data::AnalogSegment;
259
260         AnalogSegment::EnvelopeSection e;
261         segment->get_envelope_section(e, start, end, samples_per_pixel);
262
263         if (e.length < 2)
264                 return;
265
266         p.setPen(QPen(Qt::NoPen));
267         p.setBrush(colour_);
268
269         QRectF *const rects = new QRectF[e.length];
270         QRectF *rect = rects;
271
272         for (uint64_t sample = 0; sample < e.length-1; sample++) {
273                 const float x = ((e.scale * sample + e.start) /
274                         samples_per_pixel - pixels_offset) + left;
275                 const AnalogSegment::EnvelopeSample *const s =
276                         e.samples + sample;
277
278                 // We overlap this sample with the next so that vertical
279                 // gaps do not appear during steep rising or falling edges
280                 const float b = y - max(s->max, (s+1)->min) * scale_;
281                 const float t = y - min(s->min, (s+1)->max) * scale_;
282
283                 float h = b - t;
284                 if (h >= 0.0f && h <= 1.0f)
285                         h = 1.0f;
286                 if (h <= 0.0f && h >= -1.0f)
287                         h = -1.0f;
288
289                 *rect++ = QRectF(x, t, 1.0f, h);
290         }
291
292         p.drawRects(rects, e.length);
293
294         delete[] rects;
295         delete[] e.samples;
296 }
297
298 float AnalogSignal::get_resolution(int scale_index)
299 {
300         const float seq[] = {1.0f, 2.0f, 5.0f};
301
302         const int offset = std::numeric_limits<int>::max() / (2 * countof(seq));
303         const std::div_t d = std::div(
304                 (int)(scale_index + countof(seq) * offset),
305                 countof(seq));
306
307         return powf(10.0f, d.quot - offset) * seq[d.rem];
308 }
309
310 void AnalogSignal::update_scale()
311 {
312         resolution_ = get_resolution(scale_index_);
313         scale_ = div_height_ / resolution_;
314 }
315
316 void AnalogSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
317 {
318         // Add the standard options
319         Signal::populate_popup_form(parent, form);
320
321         QFormLayout *const layout = new QFormLayout;
322
323         // Add the number of vdivs
324         QSpinBox *vdiv_sb = new QSpinBox(parent);
325         vdiv_sb->setRange(1, MaximumVDivs);
326         vdiv_sb->setValue(vdivs_);
327         connect(vdiv_sb, SIGNAL(valueChanged(int)),
328                 this, SLOT(on_vdivs_changed(int)));
329         layout->addRow(tr("Number of vertical divs"), vdiv_sb);
330
331         // Add the vertical resolution
332         resolution_cb_ = new QComboBox(parent);
333
334         for (int i = MinScaleIndex; i < MaxScaleIndex; i++) {
335                 const QString label = QString("%1").arg(get_resolution(i));
336                 resolution_cb_->insertItem(0, label, QVariant(i));
337         }
338
339         const int cur_idx = resolution_cb_->findData(QVariant(scale_index_));
340         resolution_cb_->setCurrentIndex(cur_idx);
341
342         connect(resolution_cb_, SIGNAL(currentIndexChanged(int)),
343                 this, SLOT(on_resolution_changed(int)));
344
345         QGridLayout *const vdiv_layout = new QGridLayout;
346         QLabel *const vdiv_unit = new QLabel(tr("V/div"));
347         vdiv_layout->addWidget(resolution_cb_, 0, 0);
348         vdiv_layout->addWidget(vdiv_unit, 0, 1);
349
350         layout->addRow(tr("Vertical resolution"), vdiv_layout);
351
352         form->addRow(layout);
353 }
354
355 void AnalogSignal::on_vdivs_changed(int vdivs)
356 {
357         vdivs_ = vdivs;
358
359         if (owner_)
360                 owner_->extents_changed(false, true);
361 }
362
363 void AnalogSignal::on_resolution_changed(int index)
364 {
365         scale_index_ = resolution_cb_->itemData(index).toInt();
366         update_scale();
367
368         if (owner_)
369                 owner_->row_item_appearance_changed(false, true);
370 }
371
372 } // namespace view
373 } // namespace pv