]>
Commit | Line | Data |
---|---|---|
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> | |
a8fd2759 | 26 | #include <vector> |
aba1dd16 | 27 | |
37b9fed4 | 28 | #include <QApplication> |
73e377fe | 29 | #include <QCheckBox> |
368a37c2 | 30 | #include <QComboBox> |
4cffac16 | 31 | #include <QFormLayout> |
368a37c2 SA |
32 | #include <QGridLayout> |
33 | #include <QLabel> | |
03cc651d | 34 | #include <QString> |
37b9fed4 | 35 | |
2acdb232 | 36 | #include "analogsignal.hpp" |
1573bf16 SA |
37 | #include "logicsignal.hpp" |
38 | #include "view.hpp" | |
39 | ||
2acdb232 | 40 | #include "pv/data/analog.hpp" |
f3d66e52 | 41 | #include "pv/data/analogsegment.hpp" |
a8fd2759 SA |
42 | #include "pv/data/logic.hpp" |
43 | #include "pv/data/logicsegment.hpp" | |
bf0edd2b | 44 | #include "pv/data/signalbase.hpp" |
8de1e1b2 | 45 | #include "pv/globalsettings.hpp" |
aba1dd16 | 46 | |
fe3a1c21 | 47 | #include <libsigrokcxx/libsigrokcxx.hpp> |
e8d00928 | 48 | |
6f925ba9 UH |
49 | using std::deque; |
50 | using std::div; | |
51 | using std::div_t; | |
819f4c25 | 52 | using std::max; |
a5d93c27 | 53 | using std::make_pair; |
819f4c25 | 54 | using std::min; |
6f925ba9 UH |
55 | using std::numeric_limits; |
56 | using std::pair; | |
f9abf97e | 57 | using std::shared_ptr; |
a8fd2759 | 58 | using std::vector; |
aba1dd16 JH |
59 | |
60 | namespace pv { | |
f4e57597 | 61 | namespace views { |
1573bf16 | 62 | namespace trace { |
aba1dd16 | 63 | |
568b90d4 JH |
64 | const QColor AnalogSignal::SignalColours[4] = { |
65 | QColor(0xC4, 0xA0, 0x00), // Yellow | |
66 | QColor(0x87, 0x20, 0x7A), // Magenta | |
67 | QColor(0x20, 0x4A, 0x87), // Blue | |
68 | QColor(0x4E, 0x9A, 0x06) // Green | |
69 | }; | |
70 | ||
c063290a UH |
71 | const QColor AnalogSignal::GridMajorColor = QColor(0, 0, 0, 40 * 256 / 100); |
72 | const QColor AnalogSignal::GridMinorColor = QColor(0, 0, 0, 20 * 256 / 100); | |
37b9fed4 | 73 | |
8de1e1b2 UH |
74 | const QColor AnalogSignal::SamplingPointColour(0x77, 0x77, 0x77); |
75 | ||
fdfd5b3e SA |
76 | const int64_t AnalogSignal::TracePaintBlockSize = 1024 * 1024; // 4 MiB (due to float) |
77 | const float AnalogSignal::EnvelopeThreshold = 64.0f; | |
7c68ddae | 78 | |
4cffac16 | 79 | const int AnalogSignal::MaximumVDivs = 10; |
368a37c2 SA |
80 | const int AnalogSignal::MinScaleIndex = -6; |
81 | const int AnalogSignal::MaxScaleIndex = 7; | |
4cffac16 | 82 | |
03cc651d SA |
83 | const int AnalogSignal::InfoTextMarginRight = 20; |
84 | const int AnalogSignal::InfoTextMarginBottom = 5; | |
85 | ||
b86aa8f4 | 86 | AnalogSignal::AnalogSignal( |
2b81ae46 | 87 | pv::Session &session, |
cbd2a2de | 88 | shared_ptr<data::SignalBase> base) : |
73a25a6e | 89 | Signal(session, base), |
834a4f1b | 90 | scale_index_(4), // 20 per div |
37b9fed4 SA |
91 | scale_index_drag_offset_(0), |
92 | div_height_(3 * QFontMetrics(QApplication::font()).height()), | |
459db2c5 SA |
93 | pos_vdivs_(1), |
94 | neg_vdivs_(1), | |
73e377fe | 95 | resolution_(0), |
a970015f SA |
96 | conversion_type_(data::SignalBase::NoConversion), |
97 | display_type_(DisplayBoth), | |
1f1d55ce | 98 | autoranging_(true) |
aba1dd16 | 99 | { |
85715407 SA |
100 | pv::data::Analog* analog_data = |
101 | dynamic_cast<pv::data::Analog*>(data().get()); | |
102 | ||
103 | connect(analog_data, SIGNAL(samples_added(QObject*, uint64_t, uint64_t)), | |
104 | this, SLOT(on_samples_added())); | |
105 | ||
73a25a6e | 106 | base_->set_colour(SignalColours[base_->index() % countof(SignalColours)]); |
834a4f1b | 107 | update_scale(); |
aba1dd16 JH |
108 | } |
109 | ||
3009b5b3 JH |
110 | shared_ptr<pv::data::SignalData> AnalogSignal::data() const |
111 | { | |
cbd2a2de | 112 | return base_->analog_data(); |
9a0cd293 JH |
113 | } |
114 | ||
3a21afa6 SA |
115 | void AnalogSignal::save_settings(QSettings &settings) const |
116 | { | |
459db2c5 SA |
117 | settings.setValue("pos_vdivs", pos_vdivs_); |
118 | settings.setValue("neg_vdivs", neg_vdivs_); | |
3a21afa6 | 119 | settings.setValue("scale_index", scale_index_); |
a970015f SA |
120 | settings.setValue("conversion_type", conversion_type_); |
121 | settings.setValue("display_type", display_type_); | |
73e377fe | 122 | settings.setValue("autoranging", autoranging_); |
3a21afa6 SA |
123 | } |
124 | ||
125 | void AnalogSignal::restore_settings(QSettings &settings) | |
126 | { | |
459db2c5 SA |
127 | if (settings.contains("pos_vdivs")) |
128 | pos_vdivs_ = settings.value("pos_vdivs").toInt(); | |
129 | ||
130 | if (settings.contains("neg_vdivs")) | |
131 | neg_vdivs_ = settings.value("neg_vdivs").toInt(); | |
3a21afa6 SA |
132 | |
133 | if (settings.contains("scale_index")) { | |
134 | scale_index_ = settings.value("scale_index").toInt(); | |
135 | update_scale(); | |
136 | } | |
73e377fe | 137 | |
a8fd2759 | 138 | if (settings.contains("conversion_type")) { |
a970015f | 139 | conversion_type_ = (data::SignalBase::ConversionType)(settings.value("conversion_type").toInt()); |
a8fd2759 SA |
140 | update_conversion_type(); |
141 | } | |
a970015f SA |
142 | |
143 | if (settings.contains("display_type")) | |
144 | display_type_ = (DisplayType)(settings.value("display_type").toInt()); | |
145 | ||
73e377fe SA |
146 | if (settings.contains("autoranging")) |
147 | autoranging_ = settings.value("autoranging").toBool(); | |
3a21afa6 SA |
148 | } |
149 | ||
6f925ba9 | 150 | pair<int, int> AnalogSignal::v_extents() const |
a5d93c27 | 151 | { |
459db2c5 SA |
152 | const int ph = pos_vdivs_ * div_height_; |
153 | const int nh = neg_vdivs_ * div_height_; | |
154 | return make_pair(-ph, nh); | |
a5d93c27 JH |
155 | } |
156 | ||
214470fc JH |
157 | int AnalogSignal::scale_handle_offset() const |
158 | { | |
459db2c5 | 159 | const int h = (pos_vdivs_ + neg_vdivs_) * div_height_; |
37b9fed4 | 160 | |
c063290a | 161 | return ((scale_index_drag_offset_ - scale_index_) * h / 4) - h / 2; |
214470fc JH |
162 | } |
163 | ||
164 | void AnalogSignal::scale_handle_dragged(int offset) | |
165 | { | |
459db2c5 | 166 | const int h = (pos_vdivs_ + neg_vdivs_) * div_height_; |
37b9fed4 | 167 | |
c063290a | 168 | scale_index_ = scale_index_drag_offset_ - (offset + h / 2) / (h / 4); |
834a4f1b SA |
169 | |
170 | update_scale(); | |
8a2fafcc JH |
171 | } |
172 | ||
173 | void AnalogSignal::scale_handle_drag_release() | |
174 | { | |
175 | scale_index_drag_offset_ = scale_index_; | |
834a4f1b | 176 | update_scale(); |
214470fc JH |
177 | } |
178 | ||
60938e04 | 179 | void AnalogSignal::paint_back(QPainter &p, ViewItemPaintParams &pp) |
fe08b6e8 | 180 | { |
73a25a6e | 181 | if (base_->enabled()) { |
99af6802 | 182 | Trace::paint_back(p, pp); |
97904bf7 | 183 | paint_axis(p, pp, get_visual_y()); |
99af6802 | 184 | } |
fe08b6e8 JH |
185 | } |
186 | ||
60938e04 | 187 | void AnalogSignal::paint_mid(QPainter &p, ViewItemPaintParams &pp) |
aba1dd16 | 188 | { |
cbd2a2de | 189 | assert(base_->analog_data()); |
8dbbc7f0 | 190 | assert(owner_); |
a8acb46e | 191 | |
be9e7b4b | 192 | const int y = get_visual_y(); |
01fd3263 | 193 | |
73a25a6e | 194 | if (!base_->enabled()) |
cec48d16 JH |
195 | return; |
196 | ||
a8fd2759 SA |
197 | if ((display_type_ == DisplayAnalog) || (display_type_ == DisplayBoth)) { |
198 | paint_grid(p, y, pp.left(), pp.right()); | |
199 | ||
200 | const deque< shared_ptr<pv::data::AnalogSegment> > &segments = | |
201 | base_->analog_data()->analog_segments(); | |
202 | if (segments.empty()) | |
203 | return; | |
204 | ||
205 | const shared_ptr<pv::data::AnalogSegment> &segment = | |
206 | segments.front(); | |
207 | ||
208 | const double pixels_offset = pp.pixels_offset(); | |
209 | const double samplerate = max(1.0, segment->samplerate()); | |
210 | const pv::util::Timestamp& start_time = segment->start_time(); | |
211 | const int64_t last_sample = segment->get_sample_count() - 1; | |
212 | const double samples_per_pixel = samplerate * pp.scale(); | |
213 | const pv::util::Timestamp start = samplerate * (pp.offset() - start_time); | |
214 | const pv::util::Timestamp end = start + samples_per_pixel * pp.width(); | |
215 | ||
216 | const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(), | |
217 | (int64_t)0), last_sample); | |
218 | const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(), | |
219 | (int64_t)0), last_sample); | |
220 | ||
221 | if (samples_per_pixel < EnvelopeThreshold) | |
222 | paint_trace(p, segment, y, pp.left(), | |
223 | start_sample, end_sample, | |
224 | pixels_offset, samples_per_pixel); | |
225 | else | |
226 | paint_envelope(p, segment, y, pp.left(), | |
227 | start_sample, end_sample, | |
228 | pixels_offset, samples_per_pixel); | |
229 | } | |
a8acb46e | 230 | |
a8fd2759 SA |
231 | if ((display_type_ == DisplayConverted) || (display_type_ == DisplayBoth)) { |
232 | if (((conversion_type_ == data::SignalBase::A2LConversionByTreshold) || | |
233 | (conversion_type_ == data::SignalBase::A2LConversionBySchmittTrigger))) { | |
a8acb46e | 234 | |
a8fd2759 SA |
235 | paint_logic_mid(p, pp); |
236 | } | |
237 | } | |
7c68ddae JH |
238 | } |
239 | ||
60938e04 | 240 | void AnalogSignal::paint_fore(QPainter &p, ViewItemPaintParams &pp) |
03cc651d SA |
241 | { |
242 | if (!enabled()) | |
243 | return; | |
244 | ||
b8c4a95b SA |
245 | if ((display_type_ == DisplayAnalog) || (display_type_ == DisplayBoth)) { |
246 | const int y = get_visual_y(); | |
03cc651d | 247 | |
b8c4a95b SA |
248 | // Show the info section on the right side of the trace |
249 | const QString infotext = QString("%1 V/div").arg(resolution_); | |
03cc651d | 250 | |
b8c4a95b SA |
251 | p.setPen(base_->colour()); |
252 | p.setFont(QApplication::font()); | |
03cc651d | 253 | |
b8c4a95b SA |
254 | const QRectF bounding_rect = QRectF(pp.left(), |
255 | y + v_extents().first, | |
256 | pp.width() - InfoTextMarginRight, | |
257 | v_extents().second - v_extents().first - InfoTextMarginBottom); | |
03cc651d | 258 | |
b8c4a95b SA |
259 | p.drawText(bounding_rect, Qt::AlignRight | Qt::AlignBottom, infotext); |
260 | } | |
03cc651d SA |
261 | } |
262 | ||
37b9fed4 SA |
263 | void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right) |
264 | { | |
46a0cadc SA |
265 | p.setRenderHint(QPainter::Antialiasing, false); |
266 | ||
8ad61f40 UH |
267 | GlobalSettings settings; |
268 | const bool show_analog_minor_grid = | |
269 | settings.value(GlobalSettings::Key_View_ShowAnalogMinorGrid).toBool(); | |
270 | ||
459db2c5 SA |
271 | if (pos_vdivs_ > 0) { |
272 | p.setPen(QPen(GridMajorColor, 1, Qt::DashLine)); | |
273 | for (int i = 1; i <= pos_vdivs_; i++) { | |
274 | const float dy = i * div_height_; | |
275 | p.drawLine(QLineF(left, y - dy, right, y - dy)); | |
276 | } | |
8ad61f40 | 277 | } |
459db2c5 | 278 | |
8ad61f40 | 279 | if ((pos_vdivs_ > 0) && show_analog_minor_grid) { |
459db2c5 SA |
280 | p.setPen(QPen(GridMinorColor, 1, Qt::DashLine)); |
281 | for (int i = 0; i < pos_vdivs_; i++) { | |
282 | const float dy = i * div_height_; | |
283 | const float dy25 = dy + (0.25 * div_height_); | |
284 | const float dy50 = dy + (0.50 * div_height_); | |
285 | const float dy75 = dy + (0.75 * div_height_); | |
286 | p.drawLine(QLineF(left, y - dy25, right, y - dy25)); | |
287 | p.drawLine(QLineF(left, y - dy50, right, y - dy50)); | |
288 | p.drawLine(QLineF(left, y - dy75, right, y - dy75)); | |
289 | } | |
37b9fed4 SA |
290 | } |
291 | ||
459db2c5 SA |
292 | if (neg_vdivs_ > 0) { |
293 | p.setPen(QPen(GridMajorColor, 1, Qt::DashLine)); | |
294 | for (int i = 1; i <= neg_vdivs_; i++) { | |
295 | const float dy = i * div_height_; | |
296 | p.drawLine(QLineF(left, y + dy, right, y + dy)); | |
297 | } | |
8ad61f40 | 298 | } |
459db2c5 | 299 | |
8ad61f40 | 300 | if ((pos_vdivs_ > 0) && show_analog_minor_grid) { |
459db2c5 SA |
301 | p.setPen(QPen(GridMinorColor, 1, Qt::DashLine)); |
302 | for (int i = 0; i < neg_vdivs_; i++) { | |
303 | const float dy = i * div_height_; | |
304 | const float dy25 = dy + (0.25 * div_height_); | |
305 | const float dy50 = dy + (0.50 * div_height_); | |
306 | const float dy75 = dy + (0.75 * div_height_); | |
307 | p.drawLine(QLineF(left, y + dy25, right, y + dy25)); | |
308 | p.drawLine(QLineF(left, y + dy50, right, y + dy50)); | |
309 | p.drawLine(QLineF(left, y + dy75, right, y + dy75)); | |
310 | } | |
37b9fed4 | 311 | } |
46a0cadc SA |
312 | |
313 | p.setRenderHint(QPainter::Antialiasing, true); | |
37b9fed4 SA |
314 | } |
315 | ||
7c68ddae | 316 | void AnalogSignal::paint_trace(QPainter &p, |
f3d66e52 | 317 | const shared_ptr<pv::data::AnalogSegment> &segment, |
7c68ddae JH |
318 | int y, int left, const int64_t start, const int64_t end, |
319 | const double pixels_offset, const double samples_per_pixel) | |
320 | { | |
fdfd5b3e SA |
321 | if (end <= start) |
322 | return; | |
323 | ||
324 | // Calculate and paint the sampling points if enabled and useful | |
325 | GlobalSettings settings; | |
326 | const bool show_sampling_points = | |
327 | settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool() && | |
328 | (samples_per_pixel < 0.25); | |
329 | ||
73a25a6e | 330 | p.setPen(base_->colour()); |
7c68ddae | 331 | |
76ce6c7a UH |
332 | const int64_t points_count = end - start; |
333 | ||
334 | QPointF *points = new QPointF[points_count]; | |
a8acb46e JH |
335 | QPointF *point = points; |
336 | ||
fdfd5b3e SA |
337 | QRectF *sampling_points = nullptr; |
338 | if (show_sampling_points) | |
339 | sampling_points = new QRectF[points_count]; | |
8de1e1b2 UH |
340 | QRectF *sampling_point = sampling_points; |
341 | ||
fdfd5b3e SA |
342 | int64_t sample_count = min(points_count, TracePaintBlockSize); |
343 | int64_t block_sample = 0; | |
344 | const float *sample_block = segment->get_samples(start, start + sample_count); | |
26a883ed | 345 | |
8de1e1b2 | 346 | const int w = 2; |
fdfd5b3e SA |
347 | for (int64_t sample = start; sample != end; sample++, block_sample++) { |
348 | ||
349 | if (block_sample == TracePaintBlockSize) { | |
350 | block_sample = 0; | |
351 | delete[] sample_block; | |
352 | sample_count = min(points_count - sample, TracePaintBlockSize); | |
353 | sample_block = segment->get_samples(sample, sample + sample_count); | |
354 | } | |
355 | ||
a8acb46e | 356 | const float x = (sample / samples_per_pixel - |
2658961b | 357 | pixels_offset) + left; |
26a883ed | 358 | |
fdfd5b3e | 359 | *point++ = QPointF(x, y - sample_block[block_sample] * scale_); |
8de1e1b2 | 360 | |
fdfd5b3e SA |
361 | if (show_sampling_points) |
362 | *sampling_point++ = | |
363 | QRectF(x - (w / 2), y - sample_block[block_sample] * scale_ - (w / 2), w, w); | |
a8acb46e | 364 | } |
fdfd5b3e | 365 | delete[] sample_block; |
a8acb46e | 366 | |
76ce6c7a | 367 | p.drawPolyline(points, points_count); |
a8acb46e | 368 | |
fdfd5b3e | 369 | if (show_sampling_points) { |
8de1e1b2 UH |
370 | p.setPen(SamplingPointColour); |
371 | p.drawRects(sampling_points, points_count); | |
fdfd5b3e | 372 | delete[] sampling_points; |
8de1e1b2 UH |
373 | } |
374 | ||
a8acb46e | 375 | delete[] points; |
aba1dd16 JH |
376 | } |
377 | ||
7c68ddae | 378 | void AnalogSignal::paint_envelope(QPainter &p, |
f3d66e52 | 379 | const shared_ptr<pv::data::AnalogSegment> &segment, |
7c68ddae JH |
380 | int y, int left, const int64_t start, const int64_t end, |
381 | const double pixels_offset, const double samples_per_pixel) | |
382 | { | |
f3d66e52 | 383 | using pv::data::AnalogSegment; |
7c68ddae | 384 | |
f3d66e52 JH |
385 | AnalogSegment::EnvelopeSection e; |
386 | segment->get_envelope_section(e, start, end, samples_per_pixel); | |
7c68ddae JH |
387 | |
388 | if (e.length < 2) | |
389 | return; | |
390 | ||
819f4c25 | 391 | p.setPen(QPen(Qt::NoPen)); |
73a25a6e | 392 | p.setBrush(base_->colour()); |
7c68ddae JH |
393 | |
394 | QRectF *const rects = new QRectF[e.length]; | |
395 | QRectF *rect = rects; | |
396 | ||
c063290a | 397 | for (uint64_t sample = 0; sample < e.length - 1; sample++) { |
7c68ddae JH |
398 | const float x = ((e.scale * sample + e.start) / |
399 | samples_per_pixel - pixels_offset) + left; | |
f3d66e52 | 400 | const AnalogSegment::EnvelopeSample *const s = |
7c68ddae JH |
401 | e.samples + sample; |
402 | ||
403 | // We overlap this sample with the next so that vertical | |
404 | // gaps do not appear during steep rising or falling edges | |
c063290a UH |
405 | const float b = y - max(s->max, (s + 1)->min) * scale_; |
406 | const float t = y - min(s->min, (s + 1)->max) * scale_; | |
7c68ddae JH |
407 | |
408 | float h = b - t; | |
f3290553 | 409 | if (h >= 0.0f && h <= 1.0f) |
7c68ddae | 410 | h = 1.0f; |
f3290553 | 411 | if (h <= 0.0f && h >= -1.0f) |
7c68ddae JH |
412 | h = -1.0f; |
413 | ||
414 | *rect++ = QRectF(x, t, 1.0f, h); | |
415 | } | |
416 | ||
417 | p.drawRects(rects, e.length); | |
418 | ||
419 | delete[] rects; | |
420 | delete[] e.samples; | |
421 | } | |
422 | ||
60938e04 | 423 | void AnalogSignal::paint_logic_mid(QPainter &p, ViewItemPaintParams &pp) |
a8fd2759 SA |
424 | { |
425 | QLineF *line; | |
426 | ||
427 | vector< pair<int64_t, bool> > edges; | |
428 | ||
429 | assert(base_); | |
430 | ||
431 | const int y = get_visual_y(); | |
432 | ||
6c0df7c4 | 433 | if (!base_->enabled() || !base_->logic_data()) |
a8fd2759 SA |
434 | return; |
435 | ||
436 | const int signal_margin = | |
437 | QFontMetrics(QApplication::font()).height() / 2; | |
438 | ||
439 | const int ph = min(pos_vdivs_, 1) * div_height_; | |
440 | const int nh = min(neg_vdivs_, 1) * div_height_; | |
441 | const float high_offset = y - ph + signal_margin + 0.5f; | |
442 | const float low_offset = y + nh - signal_margin - 0.5f; | |
443 | ||
444 | const deque< shared_ptr<pv::data::LogicSegment> > &segments = | |
445 | base_->logic_data()->logic_segments(); | |
6c0df7c4 | 446 | |
a8fd2759 SA |
447 | if (segments.empty()) |
448 | return; | |
449 | ||
450 | const shared_ptr<pv::data::LogicSegment> &segment = | |
451 | segments.front(); | |
452 | ||
453 | double samplerate = segment->samplerate(); | |
454 | ||
455 | // Show sample rate as 1Hz when it is unknown | |
456 | if (samplerate == 0.0) | |
457 | samplerate = 1.0; | |
458 | ||
459 | const double pixels_offset = pp.pixels_offset(); | |
460 | const pv::util::Timestamp& start_time = segment->start_time(); | |
461 | const int64_t last_sample = segment->get_sample_count() - 1; | |
462 | const double samples_per_pixel = samplerate * pp.scale(); | |
124be547 | 463 | const double pixels_per_sample = 1 / samples_per_pixel; |
a8fd2759 SA |
464 | const pv::util::Timestamp start = samplerate * (pp.offset() - start_time); |
465 | const pv::util::Timestamp end = start + samples_per_pixel * pp.width(); | |
466 | ||
467 | const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(), | |
468 | (int64_t)0), last_sample); | |
469 | const uint64_t end_sample = min(max(ceil(end).convert_to<int64_t>(), | |
470 | (int64_t)0), last_sample); | |
471 | ||
472 | segment->get_subsampled_edges(edges, start_sample, end_sample, | |
473 | samples_per_pixel / LogicSignal::Oversampling, 0); | |
474 | assert(edges.size() >= 2); | |
475 | ||
124be547 SA |
476 | // Check whether we need to paint the sampling points |
477 | GlobalSettings settings; | |
478 | const bool show_sampling_points = | |
479 | settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool() && | |
480 | (samples_per_pixel < 0.25); | |
481 | ||
482 | vector<QRectF> sampling_points; | |
483 | float sampling_point_x = 0.0f; | |
484 | int64_t sampling_point_sample = start_sample; | |
485 | const int w = 2; | |
486 | ||
487 | if (show_sampling_points) { | |
488 | sampling_points.reserve(end_sample - start_sample + 1); | |
489 | sampling_point_x = (edges.cbegin()->first / samples_per_pixel - pixels_offset) + pp.left(); | |
490 | } | |
491 | ||
a8fd2759 SA |
492 | // Paint the edges |
493 | const unsigned int edge_count = edges.size() - 2; | |
494 | QLineF *const edge_lines = new QLineF[edge_count]; | |
495 | line = edge_lines; | |
496 | ||
497 | for (auto i = edges.cbegin() + 1; i != edges.cend() - 1; i++) { | |
498 | const float x = ((*i).first / samples_per_pixel - | |
499 | pixels_offset) + pp.left(); | |
500 | *line++ = QLineF(x, high_offset, x, low_offset); | |
124be547 SA |
501 | |
502 | if (show_sampling_points) | |
503 | while (sampling_point_sample < (*i).first) { | |
504 | const float y = (*i).second ? low_offset : high_offset; | |
505 | sampling_points.emplace_back( | |
506 | QRectF(sampling_point_x - (w / 2), y - (w / 2), w, w)); | |
507 | sampling_point_sample++; | |
508 | sampling_point_x += pixels_per_sample; | |
509 | }; | |
a8fd2759 SA |
510 | } |
511 | ||
124be547 SA |
512 | // Calculate the sample points from the last edge to the end of the trace |
513 | if (show_sampling_points) | |
514 | while ((uint64_t)sampling_point_sample <= end_sample) { | |
515 | // Signal changed after the last edge, so the level is inverted | |
516 | const float y = (edges.cend() - 1)->second ? high_offset : low_offset; | |
517 | sampling_points.emplace_back( | |
518 | QRectF(sampling_point_x - (w / 2), y - (w / 2), w, w)); | |
519 | sampling_point_sample++; | |
520 | sampling_point_x += pixels_per_sample; | |
521 | }; | |
522 | ||
a8fd2759 SA |
523 | p.setPen(LogicSignal::EdgeColour); |
524 | p.drawLines(edge_lines, edge_count); | |
525 | delete[] edge_lines; | |
526 | ||
527 | // Paint the caps | |
528 | const unsigned int max_cap_line_count = edges.size(); | |
529 | QLineF *const cap_lines = new QLineF[max_cap_line_count]; | |
530 | ||
531 | p.setPen(LogicSignal::HighColour); | |
532 | paint_logic_caps(p, cap_lines, edges, true, samples_per_pixel, | |
533 | pixels_offset, pp.left(), high_offset); | |
534 | p.setPen(LogicSignal::LowColour); | |
535 | paint_logic_caps(p, cap_lines, edges, false, samples_per_pixel, | |
536 | pixels_offset, pp.left(), low_offset); | |
537 | ||
538 | delete[] cap_lines; | |
539 | ||
a8fd2759 | 540 | // Paint the sampling points |
124be547 SA |
541 | if (show_sampling_points) { |
542 | p.setPen(SamplingPointColour); | |
543 | p.drawRects(sampling_points.data(), sampling_points.size()); | |
a8fd2759 | 544 | } |
a8fd2759 SA |
545 | } |
546 | ||
547 | void AnalogSignal::paint_logic_caps(QPainter &p, QLineF *const lines, | |
548 | vector< pair<int64_t, bool> > &edges, bool level, | |
549 | double samples_per_pixel, double pixels_offset, float x_offset, | |
550 | float y_offset) | |
551 | { | |
552 | QLineF *line = lines; | |
553 | ||
554 | for (auto i = edges.begin(); i != (edges.end() - 1); i++) | |
555 | if ((*i).second == level) { | |
556 | *line++ = QLineF( | |
557 | ((*i).first / samples_per_pixel - | |
558 | pixels_offset) + x_offset, y_offset, | |
559 | ((*(i+1)).first / samples_per_pixel - | |
560 | pixels_offset) + x_offset, y_offset); | |
561 | } | |
562 | ||
563 | p.drawLines(lines, line - lines); | |
564 | } | |
565 | ||
368a37c2 | 566 | float AnalogSignal::get_resolution(int scale_index) |
8a2fafcc JH |
567 | { |
568 | const float seq[] = {1.0f, 2.0f, 5.0f}; | |
834a4f1b | 569 | |
6f925ba9 UH |
570 | const int offset = numeric_limits<int>::max() / (2 * countof(seq)); |
571 | const div_t d = div((int)(scale_index + countof(seq) * offset), | |
8a2fafcc | 572 | countof(seq)); |
834a4f1b | 573 | |
368a37c2 SA |
574 | return powf(10.0f, d.quot - offset) * seq[d.rem]; |
575 | } | |
576 | ||
577 | void AnalogSignal::update_scale() | |
578 | { | |
579 | resolution_ = get_resolution(scale_index_); | |
834a4f1b | 580 | scale_ = div_height_ / resolution_; |
8a2fafcc JH |
581 | } |
582 | ||
a8fd2759 SA |
583 | void AnalogSignal::update_conversion_type() |
584 | { | |
585 | base_->set_conversion_type(conversion_type_); | |
586 | ||
587 | if (owner_) | |
588 | owner_->row_item_appearance_changed(false, true); | |
589 | } | |
590 | ||
430b94aa | 591 | void AnalogSignal::perform_autoranging(bool keep_divs, bool force_update) |
73e377fe SA |
592 | { |
593 | const deque< shared_ptr<pv::data::AnalogSegment> > &segments = | |
594 | base_->analog_data()->analog_segments(); | |
595 | ||
596 | if (segments.empty()) | |
597 | return; | |
598 | ||
599 | static double prev_min = 0, prev_max = 0; | |
600 | double min = 0, max = 0; | |
601 | ||
602 | for (shared_ptr<pv::data::AnalogSegment> segment : segments) { | |
6f925ba9 | 603 | pair<double, double> mm = segment->get_min_max(); |
73e377fe SA |
604 | min = std::min(min, mm.first); |
605 | max = std::max(max, mm.second); | |
606 | } | |
607 | ||
608 | if ((min == prev_min) && (max == prev_max) && !force_update) | |
609 | return; | |
610 | ||
611 | prev_min = min; | |
612 | prev_max = max; | |
613 | ||
430b94aa SA |
614 | // If we're allowed to alter the div assignment... |
615 | if (!keep_divs) { | |
616 | // Use all divs for the positive range if there are no negative values | |
617 | if ((min == 0) && (neg_vdivs_ > 0)) { | |
618 | pos_vdivs_ += neg_vdivs_; | |
619 | neg_vdivs_ = 0; | |
620 | } | |
73e377fe | 621 | |
430b94aa SA |
622 | // Split up the divs if there are negative values but no negative divs |
623 | if ((min < 0) && (neg_vdivs_ == 0)) { | |
624 | neg_vdivs_ = pos_vdivs_ / 2; | |
625 | pos_vdivs_ -= neg_vdivs_; | |
626 | } | |
681b6d5a SA |
627 | } |
628 | ||
430b94aa SA |
629 | // If there is still no positive div when we need it, add one |
630 | // (this can happen when pos_vdivs==neg_vdivs==0) | |
17c7f31c | 631 | if ((max > 0) && (pos_vdivs_ == 0)) { |
430b94aa | 632 | pos_vdivs_ = 1; |
17c7f31c SA |
633 | owner_->extents_changed(false, true); |
634 | } | |
430b94aa SA |
635 | |
636 | // If there is still no negative div when we need it, add one | |
637 | // (this can happen when pos_vdivs was 0 or 1 when trying to split) | |
17c7f31c | 638 | if ((min < 0) && (neg_vdivs_ == 0)) { |
430b94aa | 639 | neg_vdivs_ = 1; |
17c7f31c SA |
640 | owner_->extents_changed(false, true); |
641 | } | |
430b94aa | 642 | |
73e377fe SA |
643 | double min_value_per_div; |
644 | if ((pos_vdivs_ > 0) && (neg_vdivs_ > 0)) | |
645 | min_value_per_div = std::max(max / pos_vdivs_, -min / neg_vdivs_); | |
646 | else if (pos_vdivs_ > 0) | |
647 | min_value_per_div = max / pos_vdivs_; | |
648 | else | |
649 | min_value_per_div = -min / neg_vdivs_; | |
650 | ||
651 | // Find first scale value that is bigger than the value we need | |
652 | for (int i = MinScaleIndex; i < MaxScaleIndex; i++) | |
653 | if (get_resolution(i) > min_value_per_div) { | |
654 | scale_index_ = i; | |
655 | break; | |
656 | } | |
657 | ||
658 | update_scale(); | |
659 | } | |
660 | ||
4cffac16 SA |
661 | void AnalogSignal::populate_popup_form(QWidget *parent, QFormLayout *form) |
662 | { | |
663 | // Add the standard options | |
664 | Signal::populate_popup_form(parent, form); | |
665 | ||
368a37c2 SA |
666 | QFormLayout *const layout = new QFormLayout; |
667 | ||
668 | // Add the number of vdivs | |
430b94aa SA |
669 | pvdiv_sb_ = new QSpinBox(parent); |
670 | pvdiv_sb_->setRange(0, MaximumVDivs); | |
671 | pvdiv_sb_->setValue(pos_vdivs_); | |
672 | connect(pvdiv_sb_, SIGNAL(valueChanged(int)), | |
459db2c5 | 673 | this, SLOT(on_pos_vdivs_changed(int))); |
430b94aa | 674 | layout->addRow(tr("Number of pos vertical divs"), pvdiv_sb_); |
459db2c5 | 675 | |
430b94aa SA |
676 | nvdiv_sb_ = new QSpinBox(parent); |
677 | nvdiv_sb_->setRange(0, MaximumVDivs); | |
678 | nvdiv_sb_->setValue(neg_vdivs_); | |
679 | connect(nvdiv_sb_, SIGNAL(valueChanged(int)), | |
459db2c5 | 680 | this, SLOT(on_neg_vdivs_changed(int))); |
430b94aa | 681 | layout->addRow(tr("Number of neg vertical divs"), nvdiv_sb_); |
368a37c2 SA |
682 | |
683 | // Add the vertical resolution | |
684 | resolution_cb_ = new QComboBox(parent); | |
685 | ||
686 | for (int i = MinScaleIndex; i < MaxScaleIndex; i++) { | |
687 | const QString label = QString("%1").arg(get_resolution(i)); | |
688 | resolution_cb_->insertItem(0, label, QVariant(i)); | |
689 | } | |
690 | ||
a970015f | 691 | int cur_idx = resolution_cb_->findData(QVariant(scale_index_)); |
368a37c2 SA |
692 | resolution_cb_->setCurrentIndex(cur_idx); |
693 | ||
694 | connect(resolution_cb_, SIGNAL(currentIndexChanged(int)), | |
695 | this, SLOT(on_resolution_changed(int))); | |
696 | ||
697 | QGridLayout *const vdiv_layout = new QGridLayout; | |
698 | QLabel *const vdiv_unit = new QLabel(tr("V/div")); | |
699 | vdiv_layout->addWidget(resolution_cb_, 0, 0); | |
700 | vdiv_layout->addWidget(vdiv_unit, 0, 1); | |
701 | ||
702 | layout->addRow(tr("Vertical resolution"), vdiv_layout); | |
703 | ||
73e377fe SA |
704 | // Add the autoranging checkbox |
705 | QCheckBox* autoranging_cb = new QCheckBox(); | |
706 | autoranging_cb->setCheckState(autoranging_ ? Qt::Checked : Qt::Unchecked); | |
707 | ||
708 | connect(autoranging_cb, SIGNAL(stateChanged(int)), | |
709 | this, SLOT(on_autoranging_changed(int))); | |
710 | ||
711 | layout->addRow(tr("Autoranging"), autoranging_cb); | |
712 | ||
a970015f SA |
713 | // Add the conversion type dropdown |
714 | conversion_cb_ = new QComboBox(); | |
715 | ||
716 | conversion_cb_->addItem("none", data::SignalBase::NoConversion); | |
717 | conversion_cb_->addItem("to logic via threshold", data::SignalBase::A2LConversionByTreshold); | |
718 | conversion_cb_->addItem("to logic via schmitt-trigger", data::SignalBase::A2LConversionBySchmittTrigger); | |
719 | ||
720 | cur_idx = conversion_cb_->findData(QVariant(conversion_type_)); | |
721 | conversion_cb_->setCurrentIndex(cur_idx); | |
722 | ||
33d5aa61 | 723 | // layout->addRow(tr("Conversion"), conversion_cb_); |
a970015f SA |
724 | |
725 | connect(conversion_cb_, SIGNAL(currentIndexChanged(int)), | |
726 | this, SLOT(on_conversion_changed(int))); | |
727 | ||
728 | // Add the display type dropdown | |
729 | display_type_cb_ = new QComboBox(); | |
730 | ||
731 | display_type_cb_->addItem(tr("Analog"), DisplayAnalog); | |
732 | display_type_cb_->addItem(tr("Converted"), DisplayConverted); | |
733 | display_type_cb_->addItem(tr("Both"), DisplayBoth); | |
734 | ||
735 | cur_idx = display_type_cb_->findData(QVariant(display_type_)); | |
736 | display_type_cb_->setCurrentIndex(cur_idx); | |
737 | ||
33d5aa61 | 738 | // layout->addRow(tr("Traces to show:"), display_type_cb_); |
a970015f | 739 | |
b8c4a95b SA |
740 | connect(display_type_cb_, SIGNAL(currentIndexChanged(int)), |
741 | this, SLOT(on_display_type_changed(int))); | |
742 | ||
368a37c2 | 743 | form->addRow(layout); |
4cffac16 SA |
744 | } |
745 | ||
85715407 SA |
746 | void AnalogSignal::on_samples_added() |
747 | { | |
430b94aa | 748 | perform_autoranging(false, false); |
85715407 SA |
749 | } |
750 | ||
459db2c5 SA |
751 | void AnalogSignal::on_pos_vdivs_changed(int vdivs) |
752 | { | |
430b94aa SA |
753 | if (vdivs == pos_vdivs_) |
754 | return; | |
755 | ||
459db2c5 SA |
756 | pos_vdivs_ = vdivs; |
757 | ||
80067e49 SA |
758 | // There has to be at least one div, positive or negative |
759 | if ((neg_vdivs_ == 0) && (pos_vdivs_ == 0)) { | |
760 | pos_vdivs_ = 1; | |
761 | if (pvdiv_sb_) | |
762 | pvdiv_sb_->setValue(pos_vdivs_); | |
763 | } | |
764 | ||
430b94aa SA |
765 | if (autoranging_) { |
766 | perform_autoranging(true, true); | |
767 | ||
768 | // It could be that a positive or negative div was added, so update | |
769 | if (pvdiv_sb_) { | |
770 | pvdiv_sb_->setValue(pos_vdivs_); | |
771 | nvdiv_sb_->setValue(neg_vdivs_); | |
772 | } | |
773 | } | |
73e377fe | 774 | |
459db2c5 SA |
775 | if (owner_) { |
776 | // Call order is important, otherwise the lazy event handler won't work | |
777 | owner_->extents_changed(false, true); | |
778 | owner_->row_item_appearance_changed(false, true); | |
779 | } | |
780 | } | |
781 | ||
782 | void AnalogSignal::on_neg_vdivs_changed(int vdivs) | |
4cffac16 | 783 | { |
430b94aa SA |
784 | if (vdivs == neg_vdivs_) |
785 | return; | |
786 | ||
459db2c5 | 787 | neg_vdivs_ = vdivs; |
4cffac16 | 788 | |
80067e49 SA |
789 | // There has to be at least one div, positive or negative |
790 | if ((neg_vdivs_ == 0) && (pos_vdivs_ == 0)) { | |
791 | pos_vdivs_ = 1; | |
792 | if (pvdiv_sb_) | |
793 | pvdiv_sb_->setValue(pos_vdivs_); | |
794 | } | |
795 | ||
430b94aa SA |
796 | if (autoranging_) { |
797 | perform_autoranging(true, true); | |
798 | ||
799 | // It could be that a positive or negative div was added, so update | |
800 | if (pvdiv_sb_) { | |
801 | pvdiv_sb_->setValue(pos_vdivs_); | |
802 | nvdiv_sb_->setValue(neg_vdivs_); | |
803 | } | |
804 | } | |
73e377fe | 805 | |
75d0779e SA |
806 | if (owner_) { |
807 | // Call order is important, otherwise the lazy event handler won't work | |
4cffac16 | 808 | owner_->extents_changed(false, true); |
75d0779e SA |
809 | owner_->row_item_appearance_changed(false, true); |
810 | } | |
4cffac16 SA |
811 | } |
812 | ||
368a37c2 SA |
813 | void AnalogSignal::on_resolution_changed(int index) |
814 | { | |
815 | scale_index_ = resolution_cb_->itemData(index).toInt(); | |
816 | update_scale(); | |
817 | ||
818 | if (owner_) | |
819 | owner_->row_item_appearance_changed(false, true); | |
820 | } | |
4cffac16 | 821 | |
73e377fe SA |
822 | void AnalogSignal::on_autoranging_changed(int state) |
823 | { | |
824 | autoranging_ = (state == Qt::Checked); | |
825 | ||
826 | if (autoranging_) | |
430b94aa | 827 | perform_autoranging(false, true); |
73e377fe | 828 | |
85715407 SA |
829 | if (owner_) { |
830 | // Call order is important, otherwise the lazy event handler won't work | |
831 | owner_->extents_changed(false, true); | |
73e377fe | 832 | owner_->row_item_appearance_changed(false, true); |
85715407 | 833 | } |
73e377fe SA |
834 | } |
835 | ||
a970015f SA |
836 | void AnalogSignal::on_conversion_changed(int index) |
837 | { | |
a8fd2759 SA |
838 | data::SignalBase::ConversionType old_conv_type = conversion_type_; |
839 | ||
a970015f | 840 | conversion_type_ = (data::SignalBase::ConversionType)(conversion_cb_->itemData(index).toInt()); |
a8fd2759 SA |
841 | |
842 | if (conversion_type_ != old_conv_type) { | |
843 | base_->set_conversion_type(conversion_type_); | |
844 | update_conversion_type(); | |
845 | } | |
a970015f SA |
846 | } |
847 | ||
b8c4a95b SA |
848 | void AnalogSignal::on_display_type_changed(int index) |
849 | { | |
850 | display_type_ = (DisplayType)(display_type_cb_->itemData(index).toInt()); | |
851 | ||
852 | if (owner_) | |
853 | owner_->row_item_appearance_changed(false, true); | |
854 | } | |
855 | ||
1573bf16 | 856 | } // namespace trace |
f4e57597 | 857 | } // namespace views |
aba1dd16 | 858 | } // namespace pv |