]>
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> | |
aba1dd16 | 26 | |
37b9fed4 | 27 | #include <QApplication> |
368a37c2 | 28 | #include <QComboBox> |
4cffac16 | 29 | #include <QFormLayout> |
368a37c2 SA |
30 | #include <QGridLayout> |
31 | #include <QLabel> | |
4cffac16 | 32 | #include <QSpinBox> |
03cc651d | 33 | #include <QString> |
37b9fed4 | 34 | |
2acdb232 JH |
35 | #include "analogsignal.hpp" |
36 | #include "pv/data/analog.hpp" | |
f3d66e52 | 37 | #include "pv/data/analogsegment.hpp" |
bf0edd2b | 38 | #include "pv/data/signalbase.hpp" |
2acdb232 | 39 | #include "pv/view/view.hpp" |
aba1dd16 | 40 | |
fe3a1c21 | 41 | #include <libsigrokcxx/libsigrokcxx.hpp> |
e8d00928 | 42 | |
819f4c25 | 43 | using std::max; |
a5d93c27 | 44 | using std::make_pair; |
819f4c25 | 45 | using std::min; |
f9abf97e | 46 | using std::shared_ptr; |
819f4c25 | 47 | using std::deque; |
aba1dd16 JH |
48 | |
49 | namespace pv { | |
f4e57597 SA |
50 | namespace views { |
51 | namespace TraceView { | |
aba1dd16 | 52 | |
568b90d4 JH |
53 | const QColor AnalogSignal::SignalColours[4] = { |
54 | QColor(0xC4, 0xA0, 0x00), // Yellow | |
55 | QColor(0x87, 0x20, 0x7A), // Magenta | |
56 | QColor(0x20, 0x4A, 0x87), // Blue | |
57 | QColor(0x4E, 0x9A, 0x06) // Green | |
58 | }; | |
59 | ||
46a0cadc SA |
60 | const QColor AnalogSignal::GridMajorColor = QColor(0, 0, 0, 40*256/100); |
61 | const QColor AnalogSignal::GridMinorColor = QColor(0, 0, 0, 20*256/100); | |
37b9fed4 | 62 | |
7c68ddae JH |
63 | const float AnalogSignal::EnvelopeThreshold = 256.0f; |
64 | ||
4cffac16 | 65 | const int AnalogSignal::MaximumVDivs = 10; |
368a37c2 SA |
66 | const int AnalogSignal::MinScaleIndex = -6; |
67 | const int AnalogSignal::MaxScaleIndex = 7; | |
4cffac16 | 68 | |
03cc651d SA |
69 | const int AnalogSignal::InfoTextMarginRight = 20; |
70 | const int AnalogSignal::InfoTextMarginBottom = 5; | |
71 | ||
b86aa8f4 | 72 | AnalogSignal::AnalogSignal( |
2b81ae46 | 73 | pv::Session &session, |
cbd2a2de | 74 | shared_ptr<data::SignalBase> base) : |
73a25a6e | 75 | Signal(session, base), |
834a4f1b | 76 | scale_index_(4), // 20 per div |
37b9fed4 SA |
77 | scale_index_drag_offset_(0), |
78 | div_height_(3 * QFontMetrics(QApplication::font()).height()), | |
834a4f1b SA |
79 | vdivs_(1), |
80 | resolution_(0) | |
aba1dd16 | 81 | { |
73a25a6e | 82 | base_->set_colour(SignalColours[base_->index() % countof(SignalColours)]); |
834a4f1b | 83 | update_scale(); |
aba1dd16 JH |
84 | } |
85 | ||
3009b5b3 JH |
86 | shared_ptr<pv::data::SignalData> AnalogSignal::data() const |
87 | { | |
cbd2a2de | 88 | return base_->analog_data(); |
9a0cd293 JH |
89 | } |
90 | ||
3a21afa6 SA |
91 | void AnalogSignal::save_settings(QSettings &settings) const |
92 | { | |
93 | settings.setValue("vdivs", vdivs_); | |
94 | settings.setValue("scale_index", scale_index_); | |
95 | } | |
96 | ||
97 | void AnalogSignal::restore_settings(QSettings &settings) | |
98 | { | |
99 | if (settings.contains("vdivs")) | |
100 | vdivs_ = settings.value("vdivs").toInt(); | |
101 | ||
102 | if (settings.contains("scale_index")) { | |
103 | scale_index_ = settings.value("scale_index").toInt(); | |
104 | update_scale(); | |
105 | } | |
106 | } | |
107 | ||
a5d93c27 JH |
108 | std::pair<int, int> AnalogSignal::v_extents() const |
109 | { | |
37b9fed4 | 110 | const int h = vdivs_ * div_height_; |
8a2fafcc | 111 | return make_pair(-h, h); |
a5d93c27 JH |
112 | } |
113 | ||
214470fc JH |
114 | int AnalogSignal::scale_handle_offset() const |
115 | { | |
37b9fed4 SA |
116 | const int h = vdivs_ * div_height_; |
117 | ||
8a2fafcc | 118 | return ((scale_index_drag_offset_ - scale_index_) * |
37b9fed4 | 119 | h / 4) - h / 2; |
214470fc JH |
120 | } |
121 | ||
122 | void AnalogSignal::scale_handle_dragged(int offset) | |
123 | { | |
37b9fed4 SA |
124 | const int h = vdivs_ * div_height_; |
125 | ||
8a2fafcc | 126 | scale_index_ = scale_index_drag_offset_ - |
37b9fed4 | 127 | (offset + h / 2) / (h / 4); |
834a4f1b SA |
128 | |
129 | update_scale(); | |
8a2fafcc JH |
130 | } |
131 | ||
132 | void AnalogSignal::scale_handle_drag_release() | |
133 | { | |
134 | scale_index_drag_offset_ = scale_index_; | |
834a4f1b | 135 | update_scale(); |
214470fc JH |
136 | } |
137 | ||
5b5fa4da | 138 | void AnalogSignal::paint_back(QPainter &p, const ViewItemPaintParams &pp) |
fe08b6e8 | 139 | { |
73a25a6e | 140 | if (base_->enabled()) { |
99af6802 | 141 | Trace::paint_back(p, pp); |
97904bf7 | 142 | paint_axis(p, pp, get_visual_y()); |
99af6802 | 143 | } |
fe08b6e8 JH |
144 | } |
145 | ||
5b5fa4da | 146 | void AnalogSignal::paint_mid(QPainter &p, const ViewItemPaintParams &pp) |
aba1dd16 | 147 | { |
cbd2a2de | 148 | assert(base_->analog_data()); |
8dbbc7f0 | 149 | assert(owner_); |
a8acb46e | 150 | |
be9e7b4b | 151 | const int y = get_visual_y(); |
01fd3263 | 152 | |
73a25a6e | 153 | if (!base_->enabled()) |
cec48d16 JH |
154 | return; |
155 | ||
37b9fed4 SA |
156 | paint_grid(p, y, pp.left(), pp.right()); |
157 | ||
f3d66e52 | 158 | const deque< shared_ptr<pv::data::AnalogSegment> > &segments = |
cbd2a2de | 159 | base_->analog_data()->analog_segments(); |
f3d66e52 | 160 | if (segments.empty()) |
a8acb46e JH |
161 | return; |
162 | ||
f3d66e52 JH |
163 | const shared_ptr<pv::data::AnalogSegment> &segment = |
164 | segments.front(); | |
a8acb46e | 165 | |
4c8a6a6d | 166 | const double pixels_offset = pp.pixels_offset(); |
69e33a1b | 167 | const double samplerate = max(1.0, segment->samplerate()); |
60d9b99a | 168 | const pv::util::Timestamp& start_time = segment->start_time(); |
f3d66e52 | 169 | const int64_t last_sample = segment->get_sample_count() - 1; |
4c8a6a6d | 170 | const double samples_per_pixel = samplerate * pp.scale(); |
60d9b99a JS |
171 | const pv::util::Timestamp start = samplerate * (pp.offset() - start_time); |
172 | const pv::util::Timestamp end = start + samples_per_pixel * pp.width(); | |
a8acb46e | 173 | |
60d9b99a | 174 | const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(), |
a8acb46e | 175 | (int64_t)0), last_sample); |
60d9b99a | 176 | const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(), |
a8acb46e JH |
177 | (int64_t)0), last_sample); |
178 | ||
7c68ddae | 179 | if (samples_per_pixel < EnvelopeThreshold) |
f3d66e52 | 180 | paint_trace(p, segment, y, pp.left(), |
7c68ddae JH |
181 | start_sample, end_sample, |
182 | pixels_offset, samples_per_pixel); | |
183 | else | |
f3d66e52 | 184 | paint_envelope(p, segment, y, pp.left(), |
7c68ddae JH |
185 | start_sample, end_sample, |
186 | pixels_offset, samples_per_pixel); | |
187 | } | |
188 | ||
03cc651d SA |
189 | void AnalogSignal::paint_fore(QPainter &p, const ViewItemPaintParams &pp) |
190 | { | |
191 | if (!enabled()) | |
192 | return; | |
193 | ||
194 | const int y = get_visual_y(); | |
195 | ||
196 | // Show the info section on the right side of the trace | |
197 | const QString infotext = QString("%1 V/div").arg(resolution_); | |
198 | ||
73a25a6e | 199 | p.setPen(base_->colour()); |
03cc651d SA |
200 | p.setFont(QApplication::font()); |
201 | ||
202 | const QRectF bounding_rect = QRectF(pp.left(), | |
203 | y + v_extents().first, | |
204 | pp.width() - InfoTextMarginRight, | |
205 | v_extents().second - v_extents().first - InfoTextMarginBottom); | |
206 | ||
207 | p.drawText(bounding_rect, Qt::AlignRight | Qt::AlignBottom, infotext); | |
208 | } | |
209 | ||
37b9fed4 SA |
210 | void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right) |
211 | { | |
46a0cadc SA |
212 | p.setRenderHint(QPainter::Antialiasing, false); |
213 | ||
214 | p.setPen(QPen(GridMajorColor, 1, Qt::DashLine)); | |
37b9fed4 | 215 | for (int i = 1; i <= vdivs_; i++) { |
46a0cadc | 216 | const float dy = i * div_height_; |
37b9fed4 SA |
217 | p.drawLine(QLineF(left, y - dy, right, y - dy)); |
218 | p.drawLine(QLineF(left, y + dy, right, y + dy)); | |
219 | } | |
220 | ||
46a0cadc | 221 | p.setPen(QPen(GridMinorColor, 1, Qt::DashLine)); |
37b9fed4 | 222 | for (int i = 0; i < vdivs_; i++) { |
46a0cadc | 223 | const float dy = i * div_height_; |
37b9fed4 SA |
224 | const float dy25 = dy + (0.25 * div_height_); |
225 | const float dy50 = dy + (0.50 * div_height_); | |
226 | const float dy75 = dy + (0.75 * div_height_); | |
227 | p.drawLine(QLineF(left, y - dy25, right, y - dy25)); | |
228 | p.drawLine(QLineF(left, y + dy25, right, y + dy25)); | |
229 | p.drawLine(QLineF(left, y - dy50, right, y - dy50)); | |
230 | p.drawLine(QLineF(left, y + dy50, right, y + dy50)); | |
231 | p.drawLine(QLineF(left, y - dy75, right, y - dy75)); | |
232 | p.drawLine(QLineF(left, y + dy75, right, y + dy75)); | |
233 | } | |
46a0cadc SA |
234 | |
235 | p.setRenderHint(QPainter::Antialiasing, true); | |
37b9fed4 SA |
236 | } |
237 | ||
7c68ddae | 238 | void AnalogSignal::paint_trace(QPainter &p, |
f3d66e52 | 239 | const shared_ptr<pv::data::AnalogSegment> &segment, |
7c68ddae JH |
240 | int y, int left, const int64_t start, const int64_t end, |
241 | const double pixels_offset, const double samples_per_pixel) | |
242 | { | |
73a25a6e | 243 | p.setPen(base_->colour()); |
7c68ddae | 244 | |
26a883ed | 245 | QPointF *points = new QPointF[end - start]; |
a8acb46e JH |
246 | QPointF *point = points; |
247 | ||
26a883ed SA |
248 | pv::data::SegmentAnalogDataIterator* it = |
249 | segment->begin_sample_iteration(start); | |
250 | ||
7c68ddae | 251 | for (int64_t sample = start; sample != end; sample++) { |
a8acb46e | 252 | const float x = (sample / samples_per_pixel - |
2658961b | 253 | pixels_offset) + left; |
26a883ed SA |
254 | |
255 | *point++ = QPointF(x, y - *((float*)it->value) * scale_); | |
256 | segment->continue_sample_iteration(it, 1); | |
a8acb46e | 257 | } |
26a883ed | 258 | segment->end_sample_iteration(it); |
a8acb46e | 259 | |
306d43a7 | 260 | p.drawPolyline(points, point - points); |
a8acb46e JH |
261 | |
262 | delete[] points; | |
aba1dd16 JH |
263 | } |
264 | ||
7c68ddae | 265 | void AnalogSignal::paint_envelope(QPainter &p, |
f3d66e52 | 266 | const shared_ptr<pv::data::AnalogSegment> &segment, |
7c68ddae JH |
267 | int y, int left, const int64_t start, const int64_t end, |
268 | const double pixels_offset, const double samples_per_pixel) | |
269 | { | |
f3d66e52 | 270 | using pv::data::AnalogSegment; |
7c68ddae | 271 | |
f3d66e52 JH |
272 | AnalogSegment::EnvelopeSection e; |
273 | segment->get_envelope_section(e, start, end, samples_per_pixel); | |
7c68ddae JH |
274 | |
275 | if (e.length < 2) | |
276 | return; | |
277 | ||
819f4c25 | 278 | p.setPen(QPen(Qt::NoPen)); |
73a25a6e | 279 | p.setBrush(base_->colour()); |
7c68ddae JH |
280 | |
281 | QRectF *const rects = new QRectF[e.length]; | |
282 | QRectF *rect = rects; | |
283 | ||
f3290553 | 284 | for (uint64_t sample = 0; sample < e.length-1; sample++) { |
7c68ddae JH |
285 | const float x = ((e.scale * sample + e.start) / |
286 | samples_per_pixel - pixels_offset) + left; | |
f3d66e52 | 287 | const AnalogSegment::EnvelopeSample *const s = |
7c68ddae JH |
288 | e.samples + sample; |
289 | ||
290 | // We overlap this sample with the next so that vertical | |
291 | // gaps do not appear during steep rising or falling edges | |
834a4f1b SA |
292 | const float b = y - max(s->max, (s+1)->min) * scale_; |
293 | const float t = y - min(s->min, (s+1)->max) * scale_; | |
7c68ddae JH |
294 | |
295 | float h = b - t; | |
f3290553 | 296 | if (h >= 0.0f && h <= 1.0f) |
7c68ddae | 297 | h = 1.0f; |
f3290553 | 298 | if (h <= 0.0f && h >= -1.0f) |
7c68ddae JH |
299 | h = -1.0f; |
300 | ||
301 | *rect++ = QRectF(x, t, 1.0f, h); | |
302 | } | |
303 | ||
304 | p.drawRects(rects, e.length); | |
305 | ||
306 | delete[] rects; | |
307 | delete[] e.samples; | |
308 | } | |
309 | ||
368a37c2 | 310 | float AnalogSignal::get_resolution(int scale_index) |
8a2fafcc JH |
311 | { |
312 | const float seq[] = {1.0f, 2.0f, 5.0f}; | |
834a4f1b | 313 | |
8a2fafcc JH |
314 | const int offset = std::numeric_limits<int>::max() / (2 * countof(seq)); |
315 | const std::div_t d = std::div( | |
368a37c2 | 316 | (int)(scale_index + countof(seq) * offset), |
8a2fafcc | 317 | countof(seq)); |
834a4f1b | 318 | |
368a37c2 SA |
319 | return powf(10.0f, d.quot - offset) * seq[d.rem]; |
320 | } | |
321 | ||
322 | void AnalogSignal::update_scale() | |
323 | { | |
324 | resolution_ = get_resolution(scale_index_); | |
834a4f1b | 325 | scale_ = div_height_ / resolution_; |
8a2fafcc JH |
326 | } |
327 | ||
4cffac16 SA |
328 | void AnalogSignal::populate_popup_form(QWidget *parent, QFormLayout *form) |
329 | { | |
330 | // Add the standard options | |
331 | Signal::populate_popup_form(parent, form); | |
332 | ||
368a37c2 SA |
333 | QFormLayout *const layout = new QFormLayout; |
334 | ||
335 | // Add the number of vdivs | |
4cffac16 SA |
336 | QSpinBox *vdiv_sb = new QSpinBox(parent); |
337 | vdiv_sb->setRange(1, MaximumVDivs); | |
338 | vdiv_sb->setValue(vdivs_); | |
339 | connect(vdiv_sb, SIGNAL(valueChanged(int)), | |
340 | this, SLOT(on_vdivs_changed(int))); | |
368a37c2 SA |
341 | layout->addRow(tr("Number of vertical divs"), vdiv_sb); |
342 | ||
343 | // Add the vertical resolution | |
344 | resolution_cb_ = new QComboBox(parent); | |
345 | ||
346 | for (int i = MinScaleIndex; i < MaxScaleIndex; i++) { | |
347 | const QString label = QString("%1").arg(get_resolution(i)); | |
348 | resolution_cb_->insertItem(0, label, QVariant(i)); | |
349 | } | |
350 | ||
351 | const int cur_idx = resolution_cb_->findData(QVariant(scale_index_)); | |
352 | resolution_cb_->setCurrentIndex(cur_idx); | |
353 | ||
354 | connect(resolution_cb_, SIGNAL(currentIndexChanged(int)), | |
355 | this, SLOT(on_resolution_changed(int))); | |
356 | ||
357 | QGridLayout *const vdiv_layout = new QGridLayout; | |
358 | QLabel *const vdiv_unit = new QLabel(tr("V/div")); | |
359 | vdiv_layout->addWidget(resolution_cb_, 0, 0); | |
360 | vdiv_layout->addWidget(vdiv_unit, 0, 1); | |
361 | ||
362 | layout->addRow(tr("Vertical resolution"), vdiv_layout); | |
363 | ||
364 | form->addRow(layout); | |
4cffac16 SA |
365 | } |
366 | ||
367 | void AnalogSignal::on_vdivs_changed(int vdivs) | |
368 | { | |
369 | vdivs_ = vdivs; | |
370 | ||
75d0779e SA |
371 | if (owner_) { |
372 | // Call order is important, otherwise the lazy event handler won't work | |
4cffac16 | 373 | owner_->extents_changed(false, true); |
75d0779e SA |
374 | owner_->row_item_appearance_changed(false, true); |
375 | } | |
4cffac16 SA |
376 | } |
377 | ||
368a37c2 SA |
378 | void AnalogSignal::on_resolution_changed(int index) |
379 | { | |
380 | scale_index_ = resolution_cb_->itemData(index).toInt(); | |
381 | update_scale(); | |
382 | ||
383 | if (owner_) | |
384 | owner_->row_item_appearance_changed(false, true); | |
385 | } | |
4cffac16 | 386 | |
f4e57597 SA |
387 | } // namespace TraceView |
388 | } // namespace views | |
aba1dd16 | 389 | } // namespace pv |