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