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