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